Class: TimeCrisis::Time
- Defined in:
- lib/time_crisis/time.rb,
lib/time_crisis/to_json.rb,
lib/time_crisis/support/ext/time/zones.rb,
lib/time_crisis/support/ext/time/acts_like.rb,
lib/time_crisis/support/ext/time/conversions.rb,
lib/time_crisis/support/ext/time/calculations.rb,
lib/time_crisis/support/ext/time/marshal_with_utc_flag.rb,
lib/time_crisis/support/ext/time/publicize_conversion_methods.rb
Constant Summary collapse
- DATE_FORMATS =
{ :db => "%Y-%m-%d %H:%M:%S", :number => "%Y%m%d%H%M%S", :time => "%H:%M", :short => "%d %b %H:%M", :long => "%B %d, %Y %H:%M", :rfc822 => lambda { |time| time.strftime("%a, %d %b %Y %H:%M:%S #{time.formatted_offset(false)}") } }
- DAYS_INTO_WEEK =
{ :monday => 0, :tuesday => 1, :wednesday => 2, :thursday => 3, :friday => 4, :saturday => 5, :sunday => 6 }
Class Attribute Summary collapse
-
.zone_default ⇒ Object
Returns the value of attribute zone_default.
Class Method Summary collapse
- .===(other) ⇒ Object
- ._load(marshaled_time) ⇒ Object
- ._original_load ⇒ Object
- .current ⇒ Object
- .days_in_month(month, year = now.year) ⇒ Object
- .local_time(*args) ⇒ Object
- .time_with_datetime_fallback(utc_or_local, year, month = 1, day = 1, hour = 0, min = 0, sec = 0, usec = 0) ⇒ Object
- .use_zone(time_zone) ⇒ Object
- .utc_time(*args) ⇒ Object
- .zone ⇒ Object
- .zone=(time_zone) ⇒ Object
Instance Method Summary collapse
- #_original_dump ⇒ Object (also: #_dump)
- #acts_like_time? ⇒ Boolean
- #advance(options) ⇒ Object
- #ago(seconds) ⇒ Object
- #beginning_of_day ⇒ Object (also: #midnight, #at_midnight, #at_beginning_of_day)
- #beginning_of_month ⇒ Object (also: #at_beginning_of_month)
- #beginning_of_quarter ⇒ Object (also: #at_beginning_of_quarter)
- #beginning_of_week ⇒ Object (also: #monday, #at_beginning_of_week)
- #beginning_of_year ⇒ Object (also: #at_beginning_of_year)
- #change(options) ⇒ Object
- #compare_with_coercion(other) ⇒ Object (also: #<=>)
- #end_of_day ⇒ Object
- #end_of_month ⇒ Object (also: #at_end_of_month)
- #end_of_quarter ⇒ Object (also: #at_end_of_quarter)
- #end_of_week ⇒ Object (also: #at_end_of_week)
- #end_of_year ⇒ Object (also: #at_end_of_year)
- #formatted_offset(colon = true, alternate_utc_string = nil) ⇒ Object
- #future? ⇒ Boolean
- #in_time_zone(zone = ::TimeCrisis::Time.zone) ⇒ Object
- #last_month ⇒ Object
- #last_year ⇒ Object
- #minus_with_coercion(other) ⇒ Object (also: #-)
-
#minus_with_duration(other) ⇒ Object
:nodoc:.
- #months_ago(months) ⇒ Object
- #months_since(months) ⇒ Object
- #next_month ⇒ Object
- #next_week(day = :monday) ⇒ Object
- #next_year ⇒ Object
- #past? ⇒ Boolean
-
#plus_with_duration(other) ⇒ Object
(also: #+)
:nodoc:.
- #readable_inspect ⇒ Object (also: #inspect)
- #seconds_since_midnight ⇒ Object
- #since(seconds) ⇒ Object (also: #in)
- #to_date ⇒ Object
- #to_datetime ⇒ Object
- #to_formatted_s(format = :default) ⇒ Object (also: #to_s)
- #to_json(options = nil) ⇒ Object
- #to_tc_date ⇒ Object
- #to_tc_datetime ⇒ Object
- #to_tc_time ⇒ Object
- #to_time ⇒ Object
- #today? ⇒ Boolean
- #tomorrow ⇒ Object
- #years_ago(years) ⇒ Object
- #years_since(years) ⇒ Object
- #yesterday ⇒ Object
Class Attribute Details
.zone_default ⇒ Object
Returns the value of attribute zone_default.
6 7 8 |
# File 'lib/time_crisis/support/ext/time/zones.rb', line 6 def zone_default @zone_default end |
Class Method Details
.===(other) ⇒ Object
10 11 12 |
# File 'lib/time_crisis/support/ext/time/calculations.rb', line 10 def ===(other) other.is_a?(::Time) || other.is_a?(::TimeCrisis::Time) end |
._load(marshaled_time) ⇒ Object
6 7 8 9 10 |
# File 'lib/time_crisis/support/ext/time/marshal_with_utc_flag.rb', line 6 def _load(marshaled_time) time = _original_load(marshaled_time) utc = time.instance_variable_get('@marshal_with_utc_coercion') utc ? time.utc : time end |
._original_load ⇒ Object
5 |
# File 'lib/time_crisis/support/ext/time/marshal_with_utc_flag.rb', line 5 alias_method :_original_load, :_load |
.current ⇒ Object
23 24 25 |
# File 'lib/time_crisis/support/ext/time/zones.rb', line 23 def current ::TimeCrisis::Time.zone_default ? ::TimeCrisis::Time.zone.now : ::TimeCrisis::Time.now end |
.days_in_month(month, year = now.year) ⇒ Object
14 15 16 |
# File 'lib/time_crisis/support/ext/time/calculations.rb', line 14 def days_in_month(month, year = now.year) ::TimeCrisis::Date.civil(year, month, 1).days_in_month end |
.local_time(*args) ⇒ Object
29 30 31 |
# File 'lib/time_crisis/support/ext/time/calculations.rb', line 29 def local_time(*args) time_with_datetime_fallback(:local, *args) end |
.time_with_datetime_fallback(utc_or_local, year, month = 1, day = 1, hour = 0, min = 0, sec = 0, usec = 0) ⇒ Object
18 19 20 21 22 23 |
# File 'lib/time_crisis/support/ext/time/calculations.rb', line 18 def time_with_datetime_fallback(utc_or_local, year, month=1, day=1, hour=0, min=0, sec=0, usec=0) ::TimeCrisis::Time.send(utc_or_local, year, month, day, hour, min, sec, usec) rescue offset = utc_or_local.to_sym == :local ? ::TimeCrisis::DateTime.local_offset : 0 ::DateTime.civil(year, month, day, hour, min, sec, 0, offset) end |
.use_zone(time_zone) ⇒ Object
16 17 18 19 20 21 |
# File 'lib/time_crisis/support/ext/time/zones.rb', line 16 def use_zone(time_zone) old_zone, ::TimeCrisis::Time.zone = ::TimeCrisis::Time.zone, get_zone(time_zone) yield ensure ::TimeCrisis::Time.zone = old_zone end |
.utc_time(*args) ⇒ Object
25 26 27 |
# File 'lib/time_crisis/support/ext/time/calculations.rb', line 25 def utc_time(*args) time_with_datetime_fallback(:utc, *args) end |
.zone ⇒ Object
8 9 10 |
# File 'lib/time_crisis/support/ext/time/zones.rb', line 8 def zone Thread.current[:tc_time_zone] || zone_default end |
.zone=(time_zone) ⇒ Object
12 13 14 |
# File 'lib/time_crisis/support/ext/time/zones.rb', line 12 def zone=(time_zone) Thread.current[:tc_time_zone] = get_zone(time_zone) end |
Instance Method Details
#_original_dump ⇒ Object Also known as: _dump
13 |
# File 'lib/time_crisis/support/ext/time/marshal_with_utc_flag.rb', line 13 alias :_dump :_original_dump |
#acts_like_time? ⇒ Boolean
5 6 7 |
# File 'lib/time_crisis/support/ext/time/acts_like.rb', line 5 def acts_like_time? true end |
#advance(options) ⇒ Object
63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 |
# File 'lib/time_crisis/support/ext/time/calculations.rb', line 63 def advance() unless [:weeks].nil? [:weeks], partial_weeks = [:weeks].divmod(1) [:days] = ([:days] || 0) + 7 * partial_weeks end unless [:days].nil? [:days], partial_days = [:days].divmod(1) [:hours] = ([:hours] || 0) + 24 * partial_days end d = to_tc_date.advance() time_advanced_by_date = change(:year => d.year, :month => d.month, :day => d.day) seconds_to_advance = ([:seconds] || 0) + ([:minutes] || 0) * 60 + ([:hours] || 0) * 3600 seconds_to_advance == 0 ? time_advanced_by_date : time_advanced_by_date.since(seconds_to_advance) end |
#ago(seconds) ⇒ Object
80 81 82 |
# File 'lib/time_crisis/support/ext/time/calculations.rb', line 80 def ago(seconds) since(-seconds) end |
#beginning_of_day ⇒ Object Also known as: midnight, at_midnight, at_beginning_of_day
140 141 142 |
# File 'lib/time_crisis/support/ext/time/calculations.rb', line 140 def beginning_of_day change(:hour => 0, :min => 0, :sec => 0, :usec => 0) end |
#beginning_of_month ⇒ Object Also known as: at_beginning_of_month
151 152 153 |
# File 'lib/time_crisis/support/ext/time/calculations.rb', line 151 def beginning_of_month change(:day => 1,:hour => 0, :min => 0, :sec => 0, :usec => 0) end |
#beginning_of_quarter ⇒ Object Also known as: at_beginning_of_quarter
162 163 164 |
# File 'lib/time_crisis/support/ext/time/calculations.rb', line 162 def beginning_of_quarter beginning_of_month.change(:month => [10, 7, 4, 1].detect { |m| m <= month }) end |
#beginning_of_week ⇒ Object Also known as: monday, at_beginning_of_week
123 124 125 126 |
# File 'lib/time_crisis/support/ext/time/calculations.rb', line 123 def beginning_of_week days_to_monday = wday!=0 ? wday-1 : 6 (self - days_to_monday.days).midnight end |
#beginning_of_year ⇒ Object Also known as: at_beginning_of_year
172 173 174 |
# File 'lib/time_crisis/support/ext/time/calculations.rb', line 172 def beginning_of_year change(:month => 1, :day => 1, :hour => 0, :min => 0, :sec => 0, :usec => 0) end |
#change(options) ⇒ Object
50 51 52 53 54 55 56 57 58 59 60 61 |
# File 'lib/time_crisis/support/ext/time/calculations.rb', line 50 def change() ::TimeCrisis::Time.send( utc? ? :utc_time : :local_time, [:year] || year, [:month] || month, [:day] || day, [:hour] || hour, [:min] || ([:hour] ? 0 : min), [:sec] || (([:hour] || [:min]) ? 0 : sec), [:usec] || (([:hour] || [:min] || [:sec]) ? 0 : usec) ) end |
#compare_with_coercion(other) ⇒ Object Also known as: <=>
217 218 219 220 221 222 223 224 |
# File 'lib/time_crisis/support/ext/time/calculations.rb', line 217 def compare_with_coercion(other) other = other.comparable_time if other.respond_to?(:comparable_time) if other.acts_like?(:date) to_tc_datetime.compare_without_coercion(other) else compare_without_coercion(other) end end |
#end_of_day ⇒ Object
147 148 149 |
# File 'lib/time_crisis/support/ext/time/calculations.rb', line 147 def end_of_day change(:hour => 23, :min => 59, :sec => 59, :usec => 999999.999) end |
#end_of_month ⇒ Object Also known as: at_end_of_month
156 157 158 159 |
# File 'lib/time_crisis/support/ext/time/calculations.rb', line 156 def end_of_month last_day = ::TimeCrisis::Time.days_in_month(month, year) change(:day => last_day, :hour => 23, :min => 59, :sec => 59, :usec => 999999.999) end |
#end_of_quarter ⇒ Object Also known as: at_end_of_quarter
167 168 169 |
# File 'lib/time_crisis/support/ext/time/calculations.rb', line 167 def end_of_quarter beginning_of_month.change(:month => [3, 6, 9, 12].detect { |m| m >= month }).end_of_month end |
#end_of_week ⇒ Object Also known as: at_end_of_week
130 131 132 133 |
# File 'lib/time_crisis/support/ext/time/calculations.rb', line 130 def end_of_week days_to_sunday = wday!=0 ? 7-wday : 0 (self + days_to_sunday.days).end_of_day end |
#end_of_year ⇒ Object Also known as: at_end_of_year
177 178 179 |
# File 'lib/time_crisis/support/ext/time/calculations.rb', line 177 def end_of_year change(:month => 12, :day => 31, :hour => 23, :min => 59, :sec => 59, :usec => 999999.999) end |
#formatted_offset(colon = true, alternate_utc_string = nil) ⇒ Object
24 25 26 |
# File 'lib/time_crisis/support/ext/time/conversions.rb', line 24 def formatted_offset(colon = true, alternate_utc_string = nil) utc? && alternate_utc_string || TimeCrisis::TimeZone.seconds_to_utc_offset(utc_offset, colon) end |
#future? ⇒ Boolean
42 43 44 |
# File 'lib/time_crisis/support/ext/time/calculations.rb', line 42 def future? self > ::TimeCrisis::Time.current end |
#in_time_zone(zone = ::TimeCrisis::Time.zone) ⇒ Object
40 41 42 |
# File 'lib/time_crisis/support/ext/time/zones.rb', line 40 def in_time_zone(zone = ::TimeCrisis::Time.zone) TimeCrisis::TimeWithZone.new(utc? ? self : getutc, ::TimeCrisis::Time.__send__(:get_zone, zone)) end |
#last_month ⇒ Object
115 116 117 |
# File 'lib/time_crisis/support/ext/time/calculations.rb', line 115 def last_month months_ago(1) end |
#last_year ⇒ Object
107 108 109 |
# File 'lib/time_crisis/support/ext/time/calculations.rb', line 107 def last_year years_ago(1) end |
#minus_with_coercion(other) ⇒ Object Also known as: -
210 211 212 213 |
# File 'lib/time_crisis/support/ext/time/calculations.rb', line 210 def minus_with_coercion(other) other = other.comparable_time if other.respond_to?(:comparable_time) minus_without_coercion(other) end |
#minus_with_duration(other) ⇒ Object
:nodoc:
200 201 202 203 204 205 206 |
# File 'lib/time_crisis/support/ext/time/calculations.rb', line 200 def minus_with_duration(other) #:nodoc: if TimeCrisis::Duration === other other.until(self) else minus_without_duration(other) end end |
#months_ago(months) ⇒ Object
91 92 93 |
# File 'lib/time_crisis/support/ext/time/calculations.rb', line 91 def months_ago(months) advance(:months => -months) end |
#months_since(months) ⇒ Object
95 96 97 |
# File 'lib/time_crisis/support/ext/time/calculations.rb', line 95 def months_since(months) advance(:months => months) end |
#next_month ⇒ Object
119 120 121 |
# File 'lib/time_crisis/support/ext/time/calculations.rb', line 119 def next_month months_since(1) end |
#next_week(day = :monday) ⇒ Object
136 137 138 |
# File 'lib/time_crisis/support/ext/time/calculations.rb', line 136 def next_week(day = :monday) since(1.week).beginning_of_week.since(DAYS_INTO_WEEK[day].day).change(:hour => 0) end |
#next_year ⇒ Object
111 112 113 |
# File 'lib/time_crisis/support/ext/time/calculations.rb', line 111 def next_year years_since(1) end |
#past? ⇒ Boolean
34 35 36 |
# File 'lib/time_crisis/support/ext/time/calculations.rb', line 34 def past? self < ::TimeCrisis::Time.current end |
#plus_with_duration(other) ⇒ Object Also known as: +
:nodoc:
190 191 192 193 194 195 196 |
# File 'lib/time_crisis/support/ext/time/calculations.rb', line 190 def plus_with_duration(other) #:nodoc: if TimeCrisis::Duration === other other.since(self) else plus_without_duration(other) end end |
#readable_inspect ⇒ Object Also known as: inspect
28 29 30 |
# File 'lib/time_crisis/support/ext/time/conversions.rb', line 28 def readable_inspect "#<TimeCrisis::Time #{self.to_s(:rfc822)}>" end |
#seconds_since_midnight ⇒ Object
46 47 48 |
# File 'lib/time_crisis/support/ext/time/calculations.rb', line 46 def seconds_since_midnight to_i - change(:hour => 0).to_i + (usec / 1.0e+6) end |
#since(seconds) ⇒ Object Also known as: in
84 85 86 87 88 |
# File 'lib/time_crisis/support/ext/time/calculations.rb', line 84 def since(seconds) self + seconds rescue to_tc_datetime.since(seconds) end |
#to_date ⇒ Object
34 35 36 |
# File 'lib/time_crisis/support/ext/time/conversions.rb', line 34 def to_date ::Date.new(year, month, day) end |
#to_datetime ⇒ Object
42 43 44 |
# File 'lib/time_crisis/support/ext/time/conversions.rb', line 42 def to_datetime ::DateTime.civil(year, month, day, hour, min, sec, Rational(utc_offset, 86400)) end |
#to_formatted_s(format = :default) ⇒ Object Also known as: to_s
14 15 16 17 18 19 20 |
# File 'lib/time_crisis/support/ext/time/conversions.rb', line 14 def to_formatted_s(format = :default) if formatter = DATE_FORMATS[format] formatter.respond_to?(:call) ? formatter.call(self).to_s : strftime(formatter) else to_default_s end end |
#to_json(options = nil) ⇒ Object
15 16 17 |
# File 'lib/time_crisis/to_json.rb', line 15 def to_json( = nil) xmlschema.inspect end |
#to_tc_date ⇒ Object
46 47 48 |
# File 'lib/time_crisis/support/ext/time/conversions.rb', line 46 def to_tc_date ::TimeCrisis::Date.civil(year, month, day) end |
#to_tc_datetime ⇒ Object
54 55 56 |
# File 'lib/time_crisis/support/ext/time/conversions.rb', line 54 def to_tc_datetime ::TimeCrisis::DateTime.civil(year, month, day, hour, min, sec, 0, utc_offset) end |
#to_tc_time ⇒ Object
50 51 52 |
# File 'lib/time_crisis/support/ext/time/conversions.rb', line 50 def to_tc_time self end |
#to_time ⇒ Object
38 39 40 |
# File 'lib/time_crisis/support/ext/time/conversions.rb', line 38 def to_time ::Time.at(self.to_f) end |
#today? ⇒ Boolean
38 39 40 |
# File 'lib/time_crisis/support/ext/time/calculations.rb', line 38 def today? to_tc_date == ::TimeCrisis::Date.current end |
#tomorrow ⇒ Object
186 187 188 |
# File 'lib/time_crisis/support/ext/time/calculations.rb', line 186 def tomorrow advance(:days => 1) end |
#years_ago(years) ⇒ Object
99 100 101 |
# File 'lib/time_crisis/support/ext/time/calculations.rb', line 99 def years_ago(years) advance(:years => -years) end |
#years_since(years) ⇒ Object
103 104 105 |
# File 'lib/time_crisis/support/ext/time/calculations.rb', line 103 def years_since(years) advance(:years => years) end |
#yesterday ⇒ Object
182 183 184 |
# File 'lib/time_crisis/support/ext/time/calculations.rb', line 182 def yesterday advance(:days => -1) end |