Class: Time
Overview
Some codes extracted from Rails, but modified codes and additional codes
Constant Summary collapse
- DATE_FORMATS =
{ :default => "%Y-%m-%d %H:%M:%S" }
- DAYS_INTO_WEEK =
{ :monday => 0, :tuesday => 1, :wednesday => 2, :thursday => 3, :friday => 4, :saturday => 5, :sunday => 6 }
Instance Method Summary collapse
- #advance(options) ⇒ Object
- #ago(seconds) ⇒ Object
- #beginning_of_business_week(start_day = :monday) ⇒ Object
- #beginning_of_day ⇒ Object (also: #midnight)
- #beginning_of_month ⇒ Object
- #beginning_of_week(start_day = :monday) ⇒ Object
- #beginning_of_year ⇒ Object
- #business_week(start_day = :monday) ⇒ Object
- #change(options) ⇒ Object
- #days_in_month ⇒ Object
- #days_to_week_start(start_day = :monday) ⇒ Object
- #end_of_business_week(start_day = :monday) ⇒ Object
- #end_of_day ⇒ Object
- #end_of_month ⇒ Object
- #end_of_week(start_day = :monday) ⇒ Object
- #end_of_year ⇒ Object
- #future? ⇒ Boolean
- #last_month ⇒ Object
- #last_week ⇒ Object
- #last_year ⇒ Object
- #leap_year? ⇒ Boolean
- #next_month ⇒ Object
- #next_week ⇒ Object
- #next_year ⇒ Object
- #past? ⇒ Boolean
- #since(seconds) ⇒ Object
- #to_date ⇒ Object
- #to_datetime ⇒ Object
- #to_formatted_s(format = :default) ⇒ Object
- #today? ⇒ Boolean
- #tomorrow ⇒ Object
- #weekend? ⇒ Boolean
- #weekends ⇒ Object
- #yesterday ⇒ Object
Instance Method Details
#advance(options) ⇒ Object
30 31 32 33 34 35 36 37 38 39 |
# File 'lib/small/time.rb', line 30 def advance() seconds = ([:years] || 0).years + ([:months] || 0).months + ([:weeks] || 0).weeks + ([:days] || 0).days + ([:hours] || 0).hours + ([:minutes] || 0).minutes + ([:seconds] || 0).seconds since(seconds) end |
#ago(seconds) ⇒ Object
26 27 28 |
# File 'lib/small/time.rb', line 26 def ago(seconds) since(-seconds) end |
#beginning_of_business_week(start_day = :monday) ⇒ Object
111 112 113 |
# File 'lib/small/time.rb', line 111 def beginning_of_business_week(start_day = :monday) business_week(start_day).first end |
#beginning_of_day ⇒ Object Also known as: midnight
119 120 121 |
# File 'lib/small/time.rb', line 119 def beginning_of_day change(:hour => 0) end |
#beginning_of_month ⇒ Object
129 130 131 |
# File 'lib/small/time.rb', line 129 def beginning_of_month change(:day => 1, :hour => 0) end |
#beginning_of_week(start_day = :monday) ⇒ Object
124 125 126 127 |
# File 'lib/small/time.rb', line 124 def beginning_of_week(start_day = :monday) days_to_start = days_to_week_start(start_day) (self - days_to_start.days).midnight end |
#beginning_of_year ⇒ Object
133 134 135 |
# File 'lib/small/time.rb', line 133 def beginning_of_year change(:month => 1, :day => 1, :hour => 0) end |
#business_week(start_day = :monday) ⇒ Object
105 106 107 108 109 |
# File 'lib/small/time.rb', line 105 def business_week(start_day = :monday) days = [] 5.times {|i| days << beginning_of_week(start_day).since(i.days) } days end |
#change(options) ⇒ Object
41 42 43 44 45 46 47 48 49 50 51 52 |
# File 'lib/small/time.rb', line 41 def change() ::Time.send( utc? ? :utc : :local, [:year] || year, [:month] || month, [:day] || day, [:hour] || hour, [:min] || ([:hour] ? 0 : min), [:sec] || (([:hour] || [:min]) ? 0 : sec), [:usec] || (([:hour] || [:min] || [:sec]) ? 0 : usec) ) end |
#days_in_month ⇒ Object
90 91 92 |
# File 'lib/small/time.rb', line 90 def days_in_month ::Date.civil(year, month, -1).day end |
#days_to_week_start(start_day = :monday) ⇒ Object
94 95 96 97 98 99 |
# File 'lib/small/time.rb', line 94 def days_to_week_start(start_day = :monday) start_day_number = DAYS_INTO_WEEK[start_day] current_day_number = wday != 0 ? wday - 1 : 6 days_span = current_day_number - start_day_number days_span >= 0 ? days_span : 7 + days_span end |
#end_of_business_week(start_day = :monday) ⇒ Object
115 116 117 |
# File 'lib/small/time.rb', line 115 def end_of_business_week(start_day = :monday) business_week(start_day).last end |
#end_of_day ⇒ Object
137 138 139 |
# File 'lib/small/time.rb', line 137 def end_of_day change(:hour => 23, :min => 59, :sec => 59, :usec => 999999.99) end |
#end_of_month ⇒ Object
146 147 148 |
# File 'lib/small/time.rb', line 146 def end_of_month change(:day => days_in_month, :hour => 23, :min => 59, :sec => 59, :usec => 999999.99) end |
#end_of_week(start_day = :monday) ⇒ Object
141 142 143 144 |
# File 'lib/small/time.rb', line 141 def end_of_week(start_day = :monday) days_to_end = 6 - days_to_week_start(start_day) (self + days_to_end.days).end_of_day end |
#end_of_year ⇒ Object
150 151 152 |
# File 'lib/small/time.rb', line 150 def end_of_year change(:month => 12, :day => 31, :hour => 23, :min => 59, :sec => 59, :usec => 999999.999) end |
#last_month ⇒ Object
174 175 176 |
# File 'lib/small/time.rb', line 174 def last_month advance(:months => -1) end |
#last_week ⇒ Object
182 183 184 |
# File 'lib/small/time.rb', line 182 def last_week advance(:weeks => -1) end |
#last_year ⇒ Object
166 167 168 |
# File 'lib/small/time.rb', line 166 def last_year advance(:years => -1) end |
#leap_year? ⇒ Boolean
70 71 72 |
# File 'lib/small/time.rb', line 70 def leap_year? to_date.leap? end |
#next_month ⇒ Object
170 171 172 |
# File 'lib/small/time.rb', line 170 def next_month advance(:months => 1) end |
#next_week ⇒ Object
178 179 180 |
# File 'lib/small/time.rb', line 178 def next_week advance(:weeks => 1) end |
#next_year ⇒ Object
162 163 164 |
# File 'lib/small/time.rb', line 162 def next_year advance(:years => 1) end |
#since(seconds) ⇒ Object
22 23 24 |
# File 'lib/small/time.rb', line 22 def since(seconds) self + seconds end |
#to_date ⇒ Object
54 55 56 |
# File 'lib/small/time.rb', line 54 def to_date ::Date.new(year, month, day) end |
#to_datetime ⇒ Object
58 59 60 |
# File 'lib/small/time.rb', line 58 def to_datetime ::DateTime.civil(year, month, day, hour, min, sec, Rational(utc_offset, 86400)) end |
#to_formatted_s(format = :default) ⇒ Object
62 63 64 65 66 67 68 |
# File 'lib/small/time.rb', line 62 def to_formatted_s(format = :default) if formatter = DATE_FORMATS[format] strftime(formatter) else self.to_s end end |
#today? ⇒ Boolean
78 79 80 |
# File 'lib/small/time.rb', line 78 def today? to_date == ::Time.now.to_date end |
#tomorrow ⇒ Object
154 155 156 |
# File 'lib/small/time.rb', line 154 def tomorrow advance(:days => 1) end |
#weekend? ⇒ Boolean
74 75 76 |
# File 'lib/small/time.rb', line 74 def weekend? weekends.include?(self) end |
#weekends ⇒ Object
101 102 103 |
# File 'lib/small/time.rb', line 101 def weekends [end_of_week.ago(1.day), end_of_week] end |
#yesterday ⇒ Object
158 159 160 |
# File 'lib/small/time.rb', line 158 def yesterday advance(:days => -1) end |