Module: Timetastic
- Defined in:
- lib/timetastic.rb
Defined Under Namespace
Classes: Traveller
Constant Summary collapse
- Domains =
[ :hours, :weeks, :days, :months, :years ]
Class Attribute Summary collapse
-
.fixed_time ⇒ Object
see Timetastic#fixate() below.
Class Method Summary collapse
- .days_and_months_between(b, e) ⇒ Object
- .days_between(b, e) ⇒ Object
-
.days_in_month(year, month) ⇒ Object
Snippet that calculates the number of days in any given month taking into account leap years.
-
.fixate(y, m = 1, d = 1, h = 0, mi = 0, s = 0, &block) ⇒ Object
Fixes the relative time by which all of Timetastic operations are carried out for the duration of the given block.
- .last(delta = 1, relative_to = nil) ⇒ Object (also: past)
- .months_between(b, e) ⇒ Object
- .next(delta = 1, relative_to = nil) ⇒ Object (also: coming)
-
.now ⇒ Object
Returns the relative time by which operations are carried out.
- .this ⇒ Object
Class Attribute Details
.fixed_time ⇒ Object
see Timetastic#fixate() below
8 9 10 |
# File 'lib/timetastic.rb', line 8 def fixed_time @fixed_time end |
Class Method Details
.days_and_months_between(b, e) ⇒ Object
28 29 30 31 32 |
# File 'lib/timetastic.rb', line 28 def days_and_months_between(b, e) # WARN: BUGGY! is capped @ 12 months (does not account for year wrapping) t = Time.at(e.to_i - b.to_i) [ t.day, t.month ] end |
.days_between(b, e) ⇒ Object
24 25 26 |
# File 'lib/timetastic.rb', line 24 def days_between(b, e) (e.to_i - b.to_i) / 60 / 60 / 24 end |
.days_in_month(year, month) ⇒ Object
Snippet that calculates the number of days in any given month taking into account leap years.
Credit goes to this SO thread: bit.ly/4GjMor
65 66 67 |
# File 'lib/timetastic.rb', line 65 def days_in_month(year, month) (Date.new(year, 12, 31) << (12-month)).day end |
.fixate(y, m = 1, d = 1, h = 0, mi = 0, s = 0, &block) ⇒ Object
Fixes the relative time by which all of Timetastic operations are carried out for the duration of the given block.
Mainly used for tests and gem development.
An alternative way to ‘fix’ the time is to directly use the exposed attribute Timetastic#fixed_time, ie:
> Timetastic.fixed_time = Time.new(Time.now.year, 6, 1)
You can simply set the attribute to nil to reset the time back to Time.now
51 52 53 54 55 56 57 58 59 |
# File 'lib/timetastic.rb', line 51 def fixate(y, m = 1, d = 1, h = 0, mi = 0, s = 0, &block) if y.is_a?(Time) @fixed_time = y else @fixed_time = Time.new(y,m,d,h,mi,s) end block.call(@fixed_time) @fixed_time = nil end |
.last(delta = 1, relative_to = nil) ⇒ Object Also known as: past
10 11 12 |
# File 'lib/timetastic.rb', line 10 def last(delta = 1, relative_to = nil) Traveller.new(-1, delta, relative_to) end |
.months_between(b, e) ⇒ Object
34 35 36 |
# File 'lib/timetastic.rb', line 34 def months_between(b, e) (Time.at(e.to_i - b.to_i) / 2.62974e6).ceil end |
.next(delta = 1, relative_to = nil) ⇒ Object Also known as: coming
16 17 18 |
# File 'lib/timetastic.rb', line 16 def next(delta = 1, relative_to = nil) Traveller.new(1, delta, relative_to) end |
.now ⇒ Object
Returns the relative time by which operations are carried out
70 71 72 |
# File 'lib/timetastic.rb', line 70 def now() @fixed_time || Time.now end |