Class: Time
- Inherits:
-
Object
- Object
- Time
- Defined in:
- lib/time.rb
Instance Method Summary collapse
- #beginning_of(secs = 1.day) ⇒ Object
- #beginning_of_day ⇒ Object
- #beginning_of_hour ⇒ Object
-
#beginning_of_minute ⇒ Object
Time.at((self.to_i/1.hour).floor*1.hour+1.hour).
- #beginning_of_month ⇒ Object
- #beginning_of_period(secs = 5.minutes) ⇒ Object
- #beginning_of_week ⇒ Object
- #beginning_of_year ⇒ Object
- #end_of(secs = 1.day) ⇒ Object
- #end_of_day ⇒ Object
-
#end_of_hour ⇒ Object
Time.at((self.to_i/1.hour).floor*1.hour).
-
#end_of_minute ⇒ Object
Time.at((self.to_i/1.minute).floor*1.minute).
- #end_of_month ⇒ Object
-
#end_of_period(secs = 5.minutes) ⇒ Object
Time.at((self.to_i/seconds).floor*seconds).
- #end_of_week ⇒ Object
- #end_of_year ⇒ Object
- #upto(max, interval = 1.day, inclusive = true) ⇒ Object
Instance Method Details
#beginning_of(secs = 1.day) ⇒ Object
43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 |
# File 'lib/time.rb', line 43 def beginning_of(secs=1.day) case secs when 1.hour self.beginning_of_hour when 1.day self.beginning_of_day when 1.week self.beginning_of_week when 1.month self.beginning_of_month when 1.year self.beginning_of_year else if secs < 1.day self.beginning_of_period(secs) else return self end end end |
#beginning_of_day ⇒ Object
20 21 22 |
# File 'lib/time.rb', line 20 def beginning_of_day self-(self.hour).hours-(self.min).minutes-self.sec end |
#beginning_of_hour ⇒ Object
26 27 28 29 |
# File 'lib/time.rb', line 26 def beginning_of_hour self-(self.min).minutes-self.sec # Time.at((self.to_i/1.hour).floor*1.hour) end |
#beginning_of_minute ⇒ Object
Time.at((self.to_i/1.hour).floor*1.hour+1.hour)
34 35 36 37 |
# File 'lib/time.rb', line 34 def beginning_of_minute self-self.sec # Time.at((self.to_i/1.minute).floor*1.minute) end |
#beginning_of_month ⇒ Object
8 9 10 |
# File 'lib/time.rb', line 8 def beginning_of_month self-(self.mday-1).days-(self.hour).hours-(self.min).minutes-self.sec end |
#beginning_of_period(secs = 5.minutes) ⇒ Object
85 86 87 88 89 90 91 |
# File 'lib/time.rb', line 85 def beginning_of_period(secs=5.minutes) return self if secs >= 1.day/2 secs_today = self - self.beginning_of_day periods_today = (secs_today/secs).floor self.beginning_of_day + periods_today * secs # Time.at((self.to_i/seconds).floor*seconds) end |
#beginning_of_week ⇒ Object
14 15 16 |
# File 'lib/time.rb', line 14 def beginning_of_week self-(self.wday).days-(self.hour).hours-(self.min).minutes-self.sec end |
#beginning_of_year ⇒ Object
2 3 4 |
# File 'lib/time.rb', line 2 def beginning_of_year self-(self.yday-1).days-(self.hour).hours-(self.min).minutes-self.sec end |
#end_of(secs = 1.day) ⇒ Object
64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 |
# File 'lib/time.rb', line 64 def end_of(secs=1.day) case secs when 1.hour self.end_of_hour when 1.day self.end_of_day when 1.week self.end_of_week when 1.month self.end_of_month when 1.year self.end_of_year else if secs < 1.day self.end_of_period(secs) else return self end end end |
#end_of_day ⇒ Object
23 24 25 |
# File 'lib/time.rb', line 23 def end_of_day self.beginning_of_day + 24.hours - 1.second end |
#end_of_hour ⇒ Object
Time.at((self.to_i/1.hour).floor*1.hour)
30 31 32 33 |
# File 'lib/time.rb', line 30 def end_of_hour self.beginning_of_hour + 1.hour - 1.second # Time.at((self.to_i/1.hour).floor*1.hour+1.hour) end |
#end_of_minute ⇒ Object
Time.at((self.to_i/1.minute).floor*1.minute)
38 39 40 41 |
# File 'lib/time.rb', line 38 def end_of_minute self.beginning_of_minute + 1.minute - 1.second # Time.at((self.to_i/1.minute).floor*5.minute) end |
#end_of_month ⇒ Object
11 12 13 |
# File 'lib/time.rb', line 11 def end_of_month self.beginning_of_month + 1.month - 1.second end |
#end_of_period(secs = 5.minutes) ⇒ Object
Time.at((self.to_i/seconds).floor*seconds)
92 93 94 95 96 |
# File 'lib/time.rb', line 92 def end_of_period(secs=5.minutes) return self if secs >= 1.day/2 beginning_of(secs) + secs - 1.second # Time.at((self.to_i/seconds).floor*seconds+seconds) end |
#end_of_week ⇒ Object
17 18 19 |
# File 'lib/time.rb', line 17 def end_of_week self.beginning_of_week + 1.week - 1.second end |
#end_of_year ⇒ Object
5 6 7 |
# File 'lib/time.rb', line 5 def end_of_year self.beginning_of_year + 365.days - 1.second end |
#upto(max, interval = 1.day, inclusive = true) ⇒ Object
98 99 100 101 102 103 104 |
# File 'lib/time.rb', line 98 def upto(max, interval=1.day, inclusive=true) t = self while t < max || (inclusive && t <= max) yield t t += interval end end |