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