core_ext 0.0.1
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +7 -0
- data/README.md +3 -0
- data/lib/core_ext/array/access.rb +76 -0
- data/lib/core_ext/array/conversions.rb +211 -0
- data/lib/core_ext/array/extract_options.rb +29 -0
- data/lib/core_ext/array/grouping.rb +116 -0
- data/lib/core_ext/array/inquiry.rb +17 -0
- data/lib/core_ext/array/prepend_and_append.rb +7 -0
- data/lib/core_ext/array/wrap.rb +46 -0
- data/lib/core_ext/array.rb +7 -0
- data/lib/core_ext/array_inquirer.rb +44 -0
- data/lib/core_ext/benchmark.rb +14 -0
- data/lib/core_ext/benchmarkable.rb +49 -0
- data/lib/core_ext/big_decimal/conversions.rb +14 -0
- data/lib/core_ext/big_decimal.rb +1 -0
- data/lib/core_ext/builder.rb +6 -0
- data/lib/core_ext/callbacks.rb +770 -0
- data/lib/core_ext/class/attribute.rb +128 -0
- data/lib/core_ext/class/attribute_accessors.rb +4 -0
- data/lib/core_ext/class/subclasses.rb +42 -0
- data/lib/core_ext/class.rb +2 -0
- data/lib/core_ext/concern.rb +142 -0
- data/lib/core_ext/configurable.rb +148 -0
- data/lib/core_ext/date/acts_like.rb +8 -0
- data/lib/core_ext/date/blank.rb +12 -0
- data/lib/core_ext/date/calculations.rb +143 -0
- data/lib/core_ext/date/conversions.rb +93 -0
- data/lib/core_ext/date/zones.rb +6 -0
- data/lib/core_ext/date.rb +5 -0
- data/lib/core_ext/date_and_time/calculations.rb +328 -0
- data/lib/core_ext/date_and_time/zones.rb +40 -0
- data/lib/core_ext/date_time/acts_like.rb +14 -0
- data/lib/core_ext/date_time/blank.rb +12 -0
- data/lib/core_ext/date_time/calculations.rb +177 -0
- data/lib/core_ext/date_time/conversions.rb +104 -0
- data/lib/core_ext/date_time/zones.rb +6 -0
- data/lib/core_ext/date_time.rb +5 -0
- data/lib/core_ext/deprecation/behaviors.rb +86 -0
- data/lib/core_ext/deprecation/instance_delegator.rb +24 -0
- data/lib/core_ext/deprecation/method_wrappers.rb +70 -0
- data/lib/core_ext/deprecation/proxy_wrappers.rb +149 -0
- data/lib/core_ext/deprecation/reporting.rb +105 -0
- data/lib/core_ext/deprecation.rb +43 -0
- data/lib/core_ext/digest/uuid.rb +51 -0
- data/lib/core_ext/duration.rb +157 -0
- data/lib/core_ext/enumerable.rb +106 -0
- data/lib/core_ext/file/atomic.rb +68 -0
- data/lib/core_ext/file.rb +1 -0
- data/lib/core_ext/hash/compact.rb +20 -0
- data/lib/core_ext/hash/conversions.rb +261 -0
- data/lib/core_ext/hash/deep_merge.rb +38 -0
- data/lib/core_ext/hash/except.rb +22 -0
- data/lib/core_ext/hash/indifferent_access.rb +23 -0
- data/lib/core_ext/hash/keys.rb +170 -0
- data/lib/core_ext/hash/reverse_merge.rb +22 -0
- data/lib/core_ext/hash/slice.rb +48 -0
- data/lib/core_ext/hash/transform_values.rb +29 -0
- data/lib/core_ext/hash.rb +9 -0
- data/lib/core_ext/hash_with_indifferent_access.rb +298 -0
- data/lib/core_ext/inflections.rb +70 -0
- data/lib/core_ext/inflector/inflections.rb +244 -0
- data/lib/core_ext/inflector/methods.rb +381 -0
- data/lib/core_ext/inflector/transliterate.rb +112 -0
- data/lib/core_ext/inflector.rb +7 -0
- data/lib/core_ext/integer/inflections.rb +29 -0
- data/lib/core_ext/integer/multiple.rb +10 -0
- data/lib/core_ext/integer/time.rb +29 -0
- data/lib/core_ext/integer.rb +3 -0
- data/lib/core_ext/json/decoding.rb +67 -0
- data/lib/core_ext/json/encoding.rb +127 -0
- data/lib/core_ext/json.rb +2 -0
- data/lib/core_ext/kernel/agnostics.rb +11 -0
- data/lib/core_ext/kernel/concern.rb +10 -0
- data/lib/core_ext/kernel/reporting.rb +41 -0
- data/lib/core_ext/kernel/singleton_class.rb +6 -0
- data/lib/core_ext/kernel.rb +4 -0
- data/lib/core_ext/load_error.rb +30 -0
- data/lib/core_ext/logger.rb +57 -0
- data/lib/core_ext/logger_silence.rb +24 -0
- data/lib/core_ext/marshal.rb +19 -0
- data/lib/core_ext/module/aliasing.rb +74 -0
- data/lib/core_ext/module/anonymous.rb +28 -0
- data/lib/core_ext/module/attr_internal.rb +36 -0
- data/lib/core_ext/module/attribute_accessors.rb +212 -0
- data/lib/core_ext/module/concerning.rb +135 -0
- data/lib/core_ext/module/delegation.rb +218 -0
- data/lib/core_ext/module/deprecation.rb +23 -0
- data/lib/core_ext/module/introspection.rb +62 -0
- data/lib/core_ext/module/method_transplanting.rb +3 -0
- data/lib/core_ext/module/qualified_const.rb +52 -0
- data/lib/core_ext/module/reachable.rb +8 -0
- data/lib/core_ext/module/remove_method.rb +35 -0
- data/lib/core_ext/module.rb +11 -0
- data/lib/core_ext/multibyte/chars.rb +231 -0
- data/lib/core_ext/multibyte/unicode.rb +388 -0
- data/lib/core_ext/multibyte.rb +21 -0
- data/lib/core_ext/name_error.rb +31 -0
- data/lib/core_ext/numeric/bytes.rb +64 -0
- data/lib/core_ext/numeric/conversions.rb +132 -0
- data/lib/core_ext/numeric/inquiry.rb +26 -0
- data/lib/core_ext/numeric/time.rb +74 -0
- data/lib/core_ext/numeric.rb +4 -0
- data/lib/core_ext/object/acts_like.rb +10 -0
- data/lib/core_ext/object/blank.rb +140 -0
- data/lib/core_ext/object/conversions.rb +4 -0
- data/lib/core_ext/object/deep_dup.rb +53 -0
- data/lib/core_ext/object/duplicable.rb +98 -0
- data/lib/core_ext/object/inclusion.rb +27 -0
- data/lib/core_ext/object/instance_variables.rb +28 -0
- data/lib/core_ext/object/json.rb +199 -0
- data/lib/core_ext/object/to_param.rb +1 -0
- data/lib/core_ext/object/to_query.rb +84 -0
- data/lib/core_ext/object/try.rb +146 -0
- data/lib/core_ext/object/with_options.rb +69 -0
- data/lib/core_ext/object.rb +14 -0
- data/lib/core_ext/option_merger.rb +25 -0
- data/lib/core_ext/ordered_hash.rb +48 -0
- data/lib/core_ext/ordered_options.rb +81 -0
- data/lib/core_ext/range/conversions.rb +34 -0
- data/lib/core_ext/range/each.rb +21 -0
- data/lib/core_ext/range/include_range.rb +23 -0
- data/lib/core_ext/range/overlaps.rb +8 -0
- data/lib/core_ext/range.rb +4 -0
- data/lib/core_ext/regexp.rb +5 -0
- data/lib/core_ext/rescuable.rb +119 -0
- data/lib/core_ext/securerandom.rb +23 -0
- data/lib/core_ext/security_utils.rb +20 -0
- data/lib/core_ext/string/access.rb +104 -0
- data/lib/core_ext/string/behavior.rb +6 -0
- data/lib/core_ext/string/conversions.rb +56 -0
- data/lib/core_ext/string/exclude.rb +11 -0
- data/lib/core_ext/string/filters.rb +102 -0
- data/lib/core_ext/string/indent.rb +43 -0
- data/lib/core_ext/string/inflections.rb +235 -0
- data/lib/core_ext/string/inquiry.rb +13 -0
- data/lib/core_ext/string/multibyte.rb +53 -0
- data/lib/core_ext/string/output_safety.rb +261 -0
- data/lib/core_ext/string/starts_ends_with.rb +4 -0
- data/lib/core_ext/string/strip.rb +23 -0
- data/lib/core_ext/string/zones.rb +14 -0
- data/lib/core_ext/string.rb +13 -0
- data/lib/core_ext/string_inquirer.rb +26 -0
- data/lib/core_ext/tagged_logging.rb +78 -0
- data/lib/core_ext/test_case.rb +88 -0
- data/lib/core_ext/testing/assertions.rb +99 -0
- data/lib/core_ext/testing/autorun.rb +12 -0
- data/lib/core_ext/testing/composite_filter.rb +54 -0
- data/lib/core_ext/testing/constant_lookup.rb +50 -0
- data/lib/core_ext/testing/declarative.rb +26 -0
- data/lib/core_ext/testing/deprecation.rb +36 -0
- data/lib/core_ext/testing/file_fixtures.rb +34 -0
- data/lib/core_ext/testing/isolation.rb +115 -0
- data/lib/core_ext/testing/method_call_assertions.rb +41 -0
- data/lib/core_ext/testing/setup_and_teardown.rb +50 -0
- data/lib/core_ext/testing/stream.rb +42 -0
- data/lib/core_ext/testing/tagged_logging.rb +25 -0
- data/lib/core_ext/testing/time_helpers.rb +134 -0
- data/lib/core_ext/time/acts_like.rb +8 -0
- data/lib/core_ext/time/calculations.rb +284 -0
- data/lib/core_ext/time/conversions.rb +66 -0
- data/lib/core_ext/time/zones.rb +95 -0
- data/lib/core_ext/time.rb +20 -0
- data/lib/core_ext/time_with_zone.rb +503 -0
- data/lib/core_ext/time_zone.rb +464 -0
- data/lib/core_ext/uri.rb +25 -0
- data/lib/core_ext/version.rb +3 -0
- data/lib/core_ext/xml_mini/jdom.rb +181 -0
- data/lib/core_ext/xml_mini/libxml.rb +79 -0
- data/lib/core_ext/xml_mini/libxmlsax.rb +85 -0
- data/lib/core_ext/xml_mini/nokogiri.rb +83 -0
- data/lib/core_ext/xml_mini/nokogirisax.rb +87 -0
- data/lib/core_ext/xml_mini/rexml.rb +130 -0
- data/lib/core_ext/xml_mini.rb +194 -0
- data/lib/core_ext.rb +3 -0
- metadata +310 -0
@@ -0,0 +1,503 @@
|
|
1
|
+
require 'core_ext/duration'
|
2
|
+
require 'core_ext/time_zone'
|
3
|
+
require 'core_ext/object/acts_like'
|
4
|
+
|
5
|
+
module CoreExt
|
6
|
+
# A Time-like class that can represent a time in any time zone. Necessary
|
7
|
+
# because standard Ruby Time instances are limited to UTC and the
|
8
|
+
# system's <tt>ENV['TZ']</tt> zone.
|
9
|
+
#
|
10
|
+
# You shouldn't ever need to create a TimeWithZone instance directly via +new+.
|
11
|
+
# Instead use methods +local+, +parse+, +at+ and +now+ on TimeZone instances,
|
12
|
+
# and +in_time_zone+ on Time and DateTime instances.
|
13
|
+
#
|
14
|
+
# Time.zone = 'Eastern Time (US & Canada)' # => 'Eastern Time (US & Canada)'
|
15
|
+
# Time.zone.local(2007, 2, 10, 15, 30, 45) # => Sat, 10 Feb 2007 15:30:45 EST -05:00
|
16
|
+
# Time.zone.parse('2007-02-10 15:30:45') # => Sat, 10 Feb 2007 15:30:45 EST -05:00
|
17
|
+
# Time.zone.at(1171139445) # => Sat, 10 Feb 2007 15:30:45 EST -05:00
|
18
|
+
# Time.zone.now # => Sun, 18 May 2008 13:07:55 EDT -04:00
|
19
|
+
# Time.utc(2007, 2, 10, 20, 30, 45).in_time_zone # => Sat, 10 Feb 2007 15:30:45 EST -05:00
|
20
|
+
#
|
21
|
+
# See Time and TimeZone for further documentation of these methods.
|
22
|
+
#
|
23
|
+
# TimeWithZone instances implement the same API as Ruby Time instances, so
|
24
|
+
# that Time and TimeWithZone instances are interchangeable.
|
25
|
+
#
|
26
|
+
# t = Time.zone.now # => Sun, 18 May 2008 13:27:25 EDT -04:00
|
27
|
+
# t.hour # => 13
|
28
|
+
# t.dst? # => true
|
29
|
+
# t.utc_offset # => -14400
|
30
|
+
# t.zone # => "EDT"
|
31
|
+
# t.to_s(:rfc822) # => "Sun, 18 May 2008 13:27:25 -0400"
|
32
|
+
# t + 1.day # => Mon, 19 May 2008 13:27:25 EDT -04:00
|
33
|
+
# t.beginning_of_year # => Tue, 01 Jan 2008 00:00:00 EST -05:00
|
34
|
+
# t > Time.utc(1999) # => true
|
35
|
+
# t.is_a?(Time) # => true
|
36
|
+
# t.is_a?(CoreExt::TimeWithZone) # => true
|
37
|
+
class TimeWithZone
|
38
|
+
|
39
|
+
# Report class name as 'Time' to thwart type checking.
|
40
|
+
def self.name
|
41
|
+
'Time'
|
42
|
+
end
|
43
|
+
|
44
|
+
PRECISIONS = Hash.new { |h, n| h[n] = "%FT%T.%#{n}N".freeze }
|
45
|
+
PRECISIONS[0] = '%FT%T'.freeze
|
46
|
+
|
47
|
+
include Comparable
|
48
|
+
attr_reader :time_zone
|
49
|
+
|
50
|
+
def initialize(utc_time, time_zone, local_time = nil, period = nil)
|
51
|
+
@utc, @time_zone, @time = utc_time, time_zone, local_time
|
52
|
+
@period = @utc ? period : get_period_and_ensure_valid_local_time(period)
|
53
|
+
end
|
54
|
+
|
55
|
+
# Returns a Time or DateTime instance that represents the time in +time_zone+.
|
56
|
+
def time
|
57
|
+
@time ||= period.to_local(@utc)
|
58
|
+
end
|
59
|
+
|
60
|
+
# Returns a Time or DateTime instance that represents the time in UTC.
|
61
|
+
def utc
|
62
|
+
@utc ||= period.to_utc(@time)
|
63
|
+
end
|
64
|
+
alias_method :comparable_time, :utc
|
65
|
+
alias_method :getgm, :utc
|
66
|
+
alias_method :getutc, :utc
|
67
|
+
alias_method :gmtime, :utc
|
68
|
+
|
69
|
+
# Returns the underlying TZInfo::TimezonePeriod.
|
70
|
+
def period
|
71
|
+
@period ||= time_zone.period_for_utc(@utc)
|
72
|
+
end
|
73
|
+
|
74
|
+
# Returns the simultaneous time in <tt>Time.zone</tt>, or the specified zone.
|
75
|
+
def in_time_zone(new_zone = ::Time.zone)
|
76
|
+
return self if time_zone == new_zone
|
77
|
+
utc.in_time_zone(new_zone)
|
78
|
+
end
|
79
|
+
|
80
|
+
# Returns a <tt>Time.local()</tt> instance of the simultaneous time in your
|
81
|
+
# system's <tt>ENV['TZ']</tt> zone.
|
82
|
+
def localtime(utc_offset = nil)
|
83
|
+
utc.respond_to?(:getlocal) ? utc.getlocal(utc_offset) : utc.to_time.getlocal(utc_offset)
|
84
|
+
end
|
85
|
+
alias_method :getlocal, :localtime
|
86
|
+
|
87
|
+
# Returns true if the current time is within Daylight Savings Time for the
|
88
|
+
# specified time zone.
|
89
|
+
#
|
90
|
+
# Time.zone = 'Eastern Time (US & Canada)' # => 'Eastern Time (US & Canada)'
|
91
|
+
# Time.zone.parse("2012-5-30").dst? # => true
|
92
|
+
# Time.zone.parse("2012-11-30").dst? # => false
|
93
|
+
def dst?
|
94
|
+
period.dst?
|
95
|
+
end
|
96
|
+
alias_method :isdst, :dst?
|
97
|
+
|
98
|
+
# Returns true if the current time zone is set to UTC.
|
99
|
+
#
|
100
|
+
# Time.zone = 'UTC' # => 'UTC'
|
101
|
+
# Time.zone.now.utc? # => true
|
102
|
+
# Time.zone = 'Eastern Time (US & Canada)' # => 'Eastern Time (US & Canada)'
|
103
|
+
# Time.zone.now.utc? # => false
|
104
|
+
def utc?
|
105
|
+
period.offset.abbreviation == :UTC || period.offset.abbreviation == :UCT
|
106
|
+
end
|
107
|
+
alias_method :gmt?, :utc?
|
108
|
+
|
109
|
+
# Returns the offset from current time to UTC time in seconds.
|
110
|
+
def utc_offset
|
111
|
+
period.utc_total_offset
|
112
|
+
end
|
113
|
+
alias_method :gmt_offset, :utc_offset
|
114
|
+
alias_method :gmtoff, :utc_offset
|
115
|
+
|
116
|
+
# Returns a formatted string of the offset from UTC, or an alternative
|
117
|
+
# string if the time zone is already UTC.
|
118
|
+
#
|
119
|
+
# Time.zone = 'Eastern Time (US & Canada)' # => "Eastern Time (US & Canada)"
|
120
|
+
# Time.zone.now.formatted_offset(true) # => "-05:00"
|
121
|
+
# Time.zone.now.formatted_offset(false) # => "-0500"
|
122
|
+
# Time.zone = 'UTC' # => "UTC"
|
123
|
+
# Time.zone.now.formatted_offset(true, "0") # => "0"
|
124
|
+
def formatted_offset(colon = true, alternate_utc_string = nil)
|
125
|
+
utc? && alternate_utc_string || TimeZone.seconds_to_utc_offset(utc_offset, colon)
|
126
|
+
end
|
127
|
+
|
128
|
+
# Returns the time zone abbreviation.
|
129
|
+
#
|
130
|
+
# Time.zone = 'Eastern Time (US & Canada)' # => "Eastern Time (US & Canada)"
|
131
|
+
# Time.zone.now.zone # => "EST"
|
132
|
+
def zone
|
133
|
+
period.zone_identifier.to_s
|
134
|
+
end
|
135
|
+
|
136
|
+
# Returns a string of the object's date, time, zone and offset from UTC.
|
137
|
+
#
|
138
|
+
# Time.zone.now.inspect # => "Thu, 04 Dec 2014 11:00:25 EST -05:00"
|
139
|
+
def inspect
|
140
|
+
"#{time.strftime('%a, %d %b %Y %H:%M:%S')} #{zone} #{formatted_offset}"
|
141
|
+
end
|
142
|
+
|
143
|
+
# Returns a string of the object's date and time in the ISO 8601 standard
|
144
|
+
# format.
|
145
|
+
#
|
146
|
+
# Time.zone.now.xmlschema # => "2014-12-04T11:02:37-05:00"
|
147
|
+
def xmlschema(fraction_digits = 0)
|
148
|
+
"#{time.strftime(PRECISIONS[fraction_digits.to_i])}#{formatted_offset(true, 'Z'.freeze)}"
|
149
|
+
end
|
150
|
+
alias_method :iso8601, :xmlschema
|
151
|
+
|
152
|
+
# Coerces time to a string for JSON encoding. The default format is ISO 8601.
|
153
|
+
# You can get %Y/%m/%d %H:%M:%S +offset style by setting
|
154
|
+
# <tt>CoreExt::JSON::Encoding.use_standard_json_time_format</tt>
|
155
|
+
# to +false+.
|
156
|
+
#
|
157
|
+
# # With CoreExt::JSON::Encoding.use_standard_json_time_format = true
|
158
|
+
# Time.utc(2005,2,1,15,15,10).in_time_zone("Hawaii").to_json
|
159
|
+
# # => "2005-02-01T05:15:10.000-10:00"
|
160
|
+
#
|
161
|
+
# # With CoreExt::JSON::Encoding.use_standard_json_time_format = false
|
162
|
+
# Time.utc(2005,2,1,15,15,10).in_time_zone("Hawaii").to_json
|
163
|
+
# # => "2005/02/01 05:15:10 -1000"
|
164
|
+
def as_json(options = nil)
|
165
|
+
if CoreExt::JSON::Encoding.use_standard_json_time_format
|
166
|
+
xmlschema(CoreExt::JSON::Encoding.time_precision)
|
167
|
+
else
|
168
|
+
%(#{time.strftime("%Y/%m/%d %H:%M:%S")} #{formatted_offset(false)})
|
169
|
+
end
|
170
|
+
end
|
171
|
+
|
172
|
+
def init_with(coder) #:nodoc:
|
173
|
+
initialize(coder['utc'], coder['zone'], coder['time'])
|
174
|
+
end
|
175
|
+
|
176
|
+
def encode_with(coder) #:nodoc:
|
177
|
+
coder.tag = '!ruby/object:CoreExt::TimeWithZone'
|
178
|
+
coder.map = { 'utc' => utc, 'zone' => time_zone, 'time' => time }
|
179
|
+
end
|
180
|
+
|
181
|
+
# Returns a string of the object's date and time in the format used by
|
182
|
+
# HTTP requests.
|
183
|
+
#
|
184
|
+
# Time.zone.now.httpdate # => "Tue, 01 Jan 2013 04:39:43 GMT"
|
185
|
+
def httpdate
|
186
|
+
utc.httpdate
|
187
|
+
end
|
188
|
+
|
189
|
+
# Returns a string of the object's date and time in the RFC 2822 standard
|
190
|
+
# format.
|
191
|
+
#
|
192
|
+
# Time.zone.now.rfc2822 # => "Tue, 01 Jan 2013 04:51:39 +0000"
|
193
|
+
def rfc2822
|
194
|
+
to_s(:rfc822)
|
195
|
+
end
|
196
|
+
alias_method :rfc822, :rfc2822
|
197
|
+
|
198
|
+
# Returns a string of the object's date and time.
|
199
|
+
# Accepts an optional <tt>format</tt>:
|
200
|
+
# * <tt>:default</tt> - default value, mimics Ruby Time#to_s format.
|
201
|
+
# * <tt>:db</tt> - format outputs time in UTC :db time. See Time#to_formatted_s(:db).
|
202
|
+
# * Any key in <tt>Time::DATE_FORMATS</tt> can be used. See active_support/core_ext/time/conversions.rb.
|
203
|
+
def to_s(format = :default)
|
204
|
+
if format == :db
|
205
|
+
utc.to_s(format)
|
206
|
+
elsif formatter = ::Time::DATE_FORMATS[format]
|
207
|
+
formatter.respond_to?(:call) ? formatter.call(self).to_s : strftime(formatter)
|
208
|
+
else
|
209
|
+
"#{time.strftime("%Y-%m-%d %H:%M:%S")} #{formatted_offset(false, 'UTC')}" # mimicking Ruby Time#to_s format
|
210
|
+
end
|
211
|
+
end
|
212
|
+
alias_method :to_formatted_s, :to_s
|
213
|
+
|
214
|
+
# Replaces <tt>%Z</tt> directive with +zone before passing to Time#strftime,
|
215
|
+
# so that zone information is correct.
|
216
|
+
def strftime(format)
|
217
|
+
format = format.gsub(/((?:\A|[^%])(?:%%)*)%Z/, "\\1#{zone}")
|
218
|
+
getlocal(utc_offset).strftime(format)
|
219
|
+
end
|
220
|
+
|
221
|
+
# Use the time in UTC for comparisons.
|
222
|
+
def <=>(other)
|
223
|
+
utc <=> other
|
224
|
+
end
|
225
|
+
|
226
|
+
# Returns true if the current object's time is within the specified
|
227
|
+
# +min+ and +max+ time.
|
228
|
+
def between?(min, max)
|
229
|
+
utc.between?(min, max)
|
230
|
+
end
|
231
|
+
|
232
|
+
# Returns true if the current object's time is in the past.
|
233
|
+
def past?
|
234
|
+
utc.past?
|
235
|
+
end
|
236
|
+
|
237
|
+
# Returns true if the current object's time falls within
|
238
|
+
# the current day.
|
239
|
+
def today?
|
240
|
+
time.today?
|
241
|
+
end
|
242
|
+
|
243
|
+
# Returns true if the current object's time is in the future.
|
244
|
+
def future?
|
245
|
+
utc.future?
|
246
|
+
end
|
247
|
+
|
248
|
+
# Returns +true+ if +other+ is equal to current object.
|
249
|
+
def eql?(other)
|
250
|
+
other.eql?(utc)
|
251
|
+
end
|
252
|
+
|
253
|
+
def hash
|
254
|
+
utc.hash
|
255
|
+
end
|
256
|
+
|
257
|
+
# Adds an interval of time to the current object's time and returns that
|
258
|
+
# value as a new TimeWithZone object.
|
259
|
+
#
|
260
|
+
# Time.zone = 'Eastern Time (US & Canada)' # => 'Eastern Time (US & Canada)'
|
261
|
+
# now = Time.zone.now # => Sun, 02 Nov 2014 01:26:28 EDT -04:00
|
262
|
+
# now + 1000 # => Sun, 02 Nov 2014 01:43:08 EDT -04:00
|
263
|
+
#
|
264
|
+
# If we're adding a Duration of variable length (i.e., years, months, days),
|
265
|
+
# move forward from #time, otherwise move forward from #utc, for accuracy
|
266
|
+
# when moving across DST boundaries.
|
267
|
+
#
|
268
|
+
# For instance, a time + 24.hours will advance exactly 24 hours, while a
|
269
|
+
# time + 1.day will advance 23-25 hours, depending on the day.
|
270
|
+
#
|
271
|
+
# now + 24.hours # => Mon, 03 Nov 2014 00:26:28 EST -05:00
|
272
|
+
# now + 1.day # => Mon, 03 Nov 2014 01:26:28 EST -05:00
|
273
|
+
def +(other)
|
274
|
+
if duration_of_variable_length?(other)
|
275
|
+
method_missing(:+, other)
|
276
|
+
else
|
277
|
+
result = utc.acts_like?(:date) ? utc.since(other) : utc + other rescue utc.since(other)
|
278
|
+
result.in_time_zone(time_zone)
|
279
|
+
end
|
280
|
+
end
|
281
|
+
alias_method :since, :+
|
282
|
+
|
283
|
+
# Returns a new TimeWithZone object that represents the difference between
|
284
|
+
# the current object's time and the +other+ time.
|
285
|
+
#
|
286
|
+
# Time.zone = 'Eastern Time (US & Canada)' # => 'Eastern Time (US & Canada)'
|
287
|
+
# now = Time.zone.now # => Mon, 03 Nov 2014 00:26:28 EST -05:00
|
288
|
+
# now - 1000 # => Mon, 03 Nov 2014 00:09:48 EST -05:00
|
289
|
+
#
|
290
|
+
# If subtracting a Duration of variable length (i.e., years, months, days),
|
291
|
+
# move backward from #time, otherwise move backward from #utc, for accuracy
|
292
|
+
# when moving across DST boundaries.
|
293
|
+
#
|
294
|
+
# For instance, a time - 24.hours will go subtract exactly 24 hours, while a
|
295
|
+
# time - 1.day will subtract 23-25 hours, depending on the day.
|
296
|
+
#
|
297
|
+
# now - 24.hours # => Sun, 02 Nov 2014 01:26:28 EDT -04:00
|
298
|
+
# now - 1.day # => Sun, 02 Nov 2014 00:26:28 EDT -04:00
|
299
|
+
def -(other)
|
300
|
+
if other.acts_like?(:time)
|
301
|
+
to_time - other.to_time
|
302
|
+
elsif duration_of_variable_length?(other)
|
303
|
+
method_missing(:-, other)
|
304
|
+
else
|
305
|
+
result = utc.acts_like?(:date) ? utc.ago(other) : utc - other rescue utc.ago(other)
|
306
|
+
result.in_time_zone(time_zone)
|
307
|
+
end
|
308
|
+
end
|
309
|
+
|
310
|
+
# Subtracts an interval of time from the current object's time and returns
|
311
|
+
# the result as a new TimeWithZone object.
|
312
|
+
#
|
313
|
+
# Time.zone = 'Eastern Time (US & Canada)' # => 'Eastern Time (US & Canada)'
|
314
|
+
# now = Time.zone.now # => Mon, 03 Nov 2014 00:26:28 EST -05:00
|
315
|
+
# now.ago(1000) # => Mon, 03 Nov 2014 00:09:48 EST -05:00
|
316
|
+
#
|
317
|
+
# If we're subtracting a Duration of variable length (i.e., years, months,
|
318
|
+
# days), move backward from #time, otherwise move backward from #utc, for
|
319
|
+
# accuracy when moving across DST boundaries.
|
320
|
+
#
|
321
|
+
# For instance, <tt>time.ago(24.hours)</tt> will move back exactly 24 hours,
|
322
|
+
# while <tt>time.ago(1.day)</tt> will move back 23-25 hours, depending on
|
323
|
+
# the day.
|
324
|
+
#
|
325
|
+
# now.ago(24.hours) # => Sun, 02 Nov 2014 01:26:28 EDT -04:00
|
326
|
+
# now.ago(1.day) # => Sun, 02 Nov 2014 00:26:28 EDT -04:00
|
327
|
+
def ago(other)
|
328
|
+
since(-other)
|
329
|
+
end
|
330
|
+
|
331
|
+
# Uses Date to provide precise Time calculations for years, months, and days
|
332
|
+
# according to the proleptic Gregorian calendar. The result is returned as a
|
333
|
+
# new TimeWithZone object.
|
334
|
+
#
|
335
|
+
# The +options+ parameter takes a hash with any of these keys:
|
336
|
+
# <tt>:years</tt>, <tt>:months</tt>, <tt>:weeks</tt>, <tt>:days</tt>,
|
337
|
+
# <tt>:hours</tt>, <tt>:minutes</tt>, <tt>:seconds</tt>.
|
338
|
+
#
|
339
|
+
# If advancing by a value of variable length (i.e., years, weeks, months,
|
340
|
+
# days), move forward from #time, otherwise move forward from #utc, for
|
341
|
+
# accuracy when moving across DST boundaries.
|
342
|
+
#
|
343
|
+
# Time.zone = 'Eastern Time (US & Canada)' # => 'Eastern Time (US & Canada)'
|
344
|
+
# now = Time.zone.now # => Sun, 02 Nov 2014 01:26:28 EDT -04:00
|
345
|
+
# now.advance(seconds: 1) # => Sun, 02 Nov 2014 01:26:29 EDT -04:00
|
346
|
+
# now.advance(minutes: 1) # => Sun, 02 Nov 2014 01:27:28 EDT -04:00
|
347
|
+
# now.advance(hours: 1) # => Sun, 02 Nov 2014 01:26:28 EST -05:00
|
348
|
+
# now.advance(days: 1) # => Mon, 03 Nov 2014 01:26:28 EST -05:00
|
349
|
+
# now.advance(weeks: 1) # => Sun, 09 Nov 2014 01:26:28 EST -05:00
|
350
|
+
# now.advance(months: 1) # => Tue, 02 Dec 2014 01:26:28 EST -05:00
|
351
|
+
# now.advance(years: 1) # => Mon, 02 Nov 2015 01:26:28 EST -05:00
|
352
|
+
def advance(options)
|
353
|
+
# If we're advancing a value of variable length (i.e., years, weeks, months, days), advance from #time,
|
354
|
+
# otherwise advance from #utc, for accuracy when moving across DST boundaries
|
355
|
+
if options.values_at(:years, :weeks, :months, :days).any?
|
356
|
+
method_missing(:advance, options)
|
357
|
+
else
|
358
|
+
utc.advance(options).in_time_zone(time_zone)
|
359
|
+
end
|
360
|
+
end
|
361
|
+
|
362
|
+
%w(year mon month day mday wday yday hour min sec usec nsec to_date).each do |method_name|
|
363
|
+
class_eval <<-EOV, __FILE__, __LINE__ + 1
|
364
|
+
def #{method_name} # def month
|
365
|
+
time.#{method_name} # time.month
|
366
|
+
end # end
|
367
|
+
EOV
|
368
|
+
end
|
369
|
+
|
370
|
+
# Returns Array of parts of Time in sequence of
|
371
|
+
# [seconds, minutes, hours, day, month, year, weekday, yearday, dst?, zone].
|
372
|
+
#
|
373
|
+
# now = Time.zone.now # => Tue, 18 Aug 2015 02:29:27 UTC +00:00
|
374
|
+
# now.to_a # => [27, 29, 2, 18, 8, 2015, 2, 230, false, "UTC"]
|
375
|
+
def to_a
|
376
|
+
[time.sec, time.min, time.hour, time.day, time.mon, time.year, time.wday, time.yday, dst?, zone]
|
377
|
+
end
|
378
|
+
|
379
|
+
# Returns the object's date and time as a floating point number of seconds
|
380
|
+
# since the Epoch (January 1, 1970 00:00 UTC).
|
381
|
+
#
|
382
|
+
# Time.zone.now.to_f # => 1417709320.285418
|
383
|
+
def to_f
|
384
|
+
utc.to_f
|
385
|
+
end
|
386
|
+
|
387
|
+
# Returns the object's date and time as an integer number of seconds
|
388
|
+
# since the Epoch (January 1, 1970 00:00 UTC).
|
389
|
+
#
|
390
|
+
# Time.zone.now.to_i # => 1417709320
|
391
|
+
def to_i
|
392
|
+
utc.to_i
|
393
|
+
end
|
394
|
+
alias_method :tv_sec, :to_i
|
395
|
+
|
396
|
+
# Returns the object's date and time as a rational number of seconds
|
397
|
+
# since the Epoch (January 1, 1970 00:00 UTC).
|
398
|
+
#
|
399
|
+
# Time.zone.now.to_r # => (708854548642709/500000)
|
400
|
+
def to_r
|
401
|
+
utc.to_r
|
402
|
+
end
|
403
|
+
|
404
|
+
# Returns an instance of Time in the system timezone.
|
405
|
+
def to_time
|
406
|
+
utc.to_time
|
407
|
+
end
|
408
|
+
|
409
|
+
# Returns an instance of DateTime with the timezone's UTC offset
|
410
|
+
#
|
411
|
+
# Time.zone.now.to_datetime # => Tue, 18 Aug 2015 02:32:20 +0000
|
412
|
+
# Time.current.in_time_zone('Hawaii').to_datetime # => Mon, 17 Aug 2015 16:32:20 -1000
|
413
|
+
def to_datetime
|
414
|
+
utc.to_datetime.new_offset(Rational(utc_offset, 86_400))
|
415
|
+
end
|
416
|
+
|
417
|
+
# So that +self+ <tt>acts_like?(:time)</tt>.
|
418
|
+
def acts_like_time?
|
419
|
+
true
|
420
|
+
end
|
421
|
+
|
422
|
+
# Say we're a Time to thwart type checking.
|
423
|
+
def is_a?(klass)
|
424
|
+
klass == ::Time || super
|
425
|
+
end
|
426
|
+
alias_method :kind_of?, :is_a?
|
427
|
+
|
428
|
+
# An instance of CoreExt::TimeWithZone is never blank
|
429
|
+
def blank?
|
430
|
+
false
|
431
|
+
end
|
432
|
+
|
433
|
+
def freeze
|
434
|
+
period; utc; time # preload instance variables before freezing
|
435
|
+
super
|
436
|
+
end
|
437
|
+
|
438
|
+
def marshal_dump
|
439
|
+
[utc, time_zone.name, time]
|
440
|
+
end
|
441
|
+
|
442
|
+
def marshal_load(variables)
|
443
|
+
initialize(variables[0].utc, ::Time.find_zone(variables[1]), variables[2].utc)
|
444
|
+
end
|
445
|
+
|
446
|
+
# respond_to_missing? is not called in some cases, such as when type conversion is
|
447
|
+
# performed with Kernel#String
|
448
|
+
def respond_to?(sym, include_priv = false)
|
449
|
+
# ensure that we're not going to throw and rescue from NoMethodError in method_missing which is slow
|
450
|
+
return false if sym.to_sym == :to_str
|
451
|
+
super
|
452
|
+
end
|
453
|
+
|
454
|
+
# Ensure proxy class responds to all methods that underlying time instance
|
455
|
+
# responds to.
|
456
|
+
def respond_to_missing?(sym, include_priv)
|
457
|
+
# consistently respond false to acts_like?(:date), regardless of whether #time is a Time or DateTime
|
458
|
+
return false if sym.to_sym == :acts_like_date?
|
459
|
+
time.respond_to?(sym, include_priv)
|
460
|
+
end
|
461
|
+
|
462
|
+
# Send the missing method to +time+ instance, and wrap result in a new
|
463
|
+
# TimeWithZone with the existing +time_zone+.
|
464
|
+
def method_missing(sym, *args, &block)
|
465
|
+
wrap_with_time_zone time.__send__(sym, *args, &block)
|
466
|
+
rescue NoMethodError => e
|
467
|
+
raise e, e.message.sub(time.inspect, self.inspect), e.backtrace
|
468
|
+
end
|
469
|
+
|
470
|
+
private
|
471
|
+
def get_period_and_ensure_valid_local_time(period)
|
472
|
+
# we don't want a Time.local instance enforcing its own DST rules as well,
|
473
|
+
# so transfer time values to a utc constructor if necessary
|
474
|
+
@time = transfer_time_values_to_utc_constructor(@time) unless @time.utc?
|
475
|
+
begin
|
476
|
+
period || @time_zone.period_for_local(@time)
|
477
|
+
rescue ::TZInfo::PeriodNotFound
|
478
|
+
# time is in the "spring forward" hour gap, so we're moving the time forward one hour and trying again
|
479
|
+
@time += 1.hour
|
480
|
+
retry
|
481
|
+
end
|
482
|
+
end
|
483
|
+
|
484
|
+
def transfer_time_values_to_utc_constructor(time)
|
485
|
+
::Time.utc(time.year, time.month, time.day, time.hour, time.min, time.sec, Rational(time.nsec, 1000))
|
486
|
+
end
|
487
|
+
|
488
|
+
def duration_of_variable_length?(obj)
|
489
|
+
CoreExt::Duration === obj && obj.parts.any? {|p| [:years, :months, :days].include?(p[0]) }
|
490
|
+
end
|
491
|
+
|
492
|
+
def wrap_with_time_zone(time)
|
493
|
+
if time.acts_like?(:time)
|
494
|
+
periods = time_zone.periods_for_local(time)
|
495
|
+
self.class.new(nil, time_zone, time, periods.include?(period) ? period : nil)
|
496
|
+
elsif time.is_a?(Range)
|
497
|
+
wrap_with_time_zone(time.begin)..wrap_with_time_zone(time.end)
|
498
|
+
else
|
499
|
+
time
|
500
|
+
end
|
501
|
+
end
|
502
|
+
end
|
503
|
+
end
|