Class: Moments::Difference
- Inherits:
-
Object
- Object
- Moments::Difference
- Defined in:
- lib/moments/difference.rb
Overview
Calculates differences between two given Time instances.
Instance Method Summary collapse
- #future? ⇒ Boolean
- #in_days ⇒ Object
- #in_hours ⇒ Object
- #in_minutes ⇒ Object
- #in_months ⇒ Object
- #in_seconds ⇒ Object
- #in_years ⇒ Object
-
#initialize(from, to) ⇒ Difference
constructor
Parameters: from:: A instance of Time to:: A instance of Time.
- #past? ⇒ Boolean
- #same? ⇒ Boolean
- #to_hash ⇒ Object
Constructor Details
#initialize(from, to) ⇒ Difference
Parameters:
- from
-
A instance of Time
- to
-
A instance of Time
27 28 29 30 31 32 33 34 |
# File 'lib/moments/difference.rb', line 27 def initialize(from, to) @from = parse_argument from @to = parse_argument to @ordered_from, @ordered_to = [@from, @to].sort precise_difference end |
Instance Method Details
#future? ⇒ Boolean
40 41 42 |
# File 'lib/moments/difference.rb', line 40 def future? @from < @to end |
#in_days ⇒ Object
64 65 66 |
# File 'lib/moments/difference.rb', line 64 def in_days in_hours / 24 end |
#in_hours ⇒ Object
60 61 62 |
# File 'lib/moments/difference.rb', line 60 def in_hours in_minutes / 60 end |
#in_minutes ⇒ Object
56 57 58 |
# File 'lib/moments/difference.rb', line 56 def in_minutes in_seconds / 60 end |
#in_months ⇒ Object
68 69 70 71 72 73 |
# File 'lib/moments/difference.rb', line 68 def in_months months_diff = @ordered_to.month - @ordered_from.month months_diff -= 1 if months_diff.positive? && @ordered_to.mday < @ordered_from.mday (@ordered_to.year - @ordered_from.year) * 12 + months_diff end |
#in_seconds ⇒ Object
52 53 54 |
# File 'lib/moments/difference.rb', line 52 def in_seconds @ordered_to.to_i - @ordered_from.to_i end |
#in_years ⇒ Object
75 76 77 78 79 80 81 82 83 84 85 86 |
# File 'lib/moments/difference.rb', line 75 def in_years years_diff = @ordered_to.year - @ordered_from.year return years_diff unless years_diff.positive? return years_diff if @ordered_to.month > @ordered_from.month if (@ordered_to.month < @ordered_from.month) || (@ordered_to.mday < @ordered_from.mday) years_diff -= 1 end years_diff end |
#past? ⇒ Boolean
48 49 50 |
# File 'lib/moments/difference.rb', line 48 def past? @from > @to end |
#same? ⇒ Boolean
44 45 46 |
# File 'lib/moments/difference.rb', line 44 def same? @from == @to end |
#to_hash ⇒ Object
36 37 38 |
# File 'lib/moments/difference.rb', line 36 def to_hash @diff end |