activesupport 1.2.4 → 8.1.2
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- checksums.yaml +7 -0
- data/CHANGELOG.md +505 -0
- data/MIT-LICENSE +20 -0
- data/README.rdoc +40 -0
- data/lib/active_support/actionable_error.rb +50 -0
- data/lib/active_support/all.rb +5 -0
- data/lib/active_support/array_inquirer.rb +50 -0
- data/lib/active_support/backtrace_cleaner.rb +234 -0
- data/lib/active_support/benchmark.rb +21 -0
- data/lib/active_support/benchmarkable.rb +53 -0
- data/lib/active_support/broadcast_logger.rb +238 -0
- data/lib/active_support/builder.rb +8 -0
- data/lib/active_support/cache/coder.rb +153 -0
- data/lib/active_support/cache/entry.rb +134 -0
- data/lib/active_support/cache/file_store.rb +244 -0
- data/lib/active_support/cache/mem_cache_store.rb +288 -0
- data/lib/active_support/cache/memory_store.rb +264 -0
- data/lib/active_support/cache/null_store.rb +62 -0
- data/lib/active_support/cache/redis_cache_store.rb +498 -0
- data/lib/active_support/cache/serializer_with_fallback.rb +152 -0
- data/lib/active_support/cache/strategy/local_cache.rb +246 -0
- data/lib/active_support/cache/strategy/local_cache_middleware.rb +45 -0
- data/lib/active_support/cache.rb +1170 -0
- data/lib/active_support/callbacks.rb +960 -0
- data/lib/active_support/class_attribute.rb +33 -0
- data/lib/active_support/code_generator.rb +79 -0
- data/lib/active_support/concern.rb +217 -0
- data/lib/active_support/concurrency/load_interlock_aware_monitor.rb +18 -0
- data/lib/active_support/concurrency/null_lock.rb +13 -0
- data/lib/active_support/concurrency/share_lock.rb +225 -0
- data/lib/active_support/concurrency/thread_monitor.rb +55 -0
- data/lib/active_support/configurable.rb +193 -0
- data/lib/active_support/configuration_file.rb +60 -0
- data/lib/active_support/continuous_integration.rb +145 -0
- data/lib/active_support/core_ext/array/access.rb +100 -0
- data/lib/active_support/core_ext/array/conversions.rb +209 -26
- data/lib/active_support/core_ext/array/extract.rb +21 -0
- data/lib/active_support/core_ext/array/extract_options.rb +31 -0
- data/lib/active_support/core_ext/array/grouping.rb +109 -0
- data/lib/active_support/core_ext/array/inquiry.rb +19 -0
- data/lib/active_support/core_ext/array/wrap.rb +48 -0
- data/lib/active_support/core_ext/array.rb +8 -4
- data/lib/active_support/core_ext/benchmark.rb +6 -0
- data/lib/active_support/core_ext/big_decimal/conversions.rb +14 -0
- data/lib/active_support/core_ext/big_decimal.rb +3 -0
- data/lib/active_support/core_ext/class/attribute.rb +137 -0
- data/lib/active_support/core_ext/class/attribute_accessors.rb +6 -0
- data/lib/active_support/core_ext/class/subclasses.rb +24 -0
- data/lib/active_support/core_ext/class.rb +4 -0
- data/lib/active_support/core_ext/date/acts_like.rb +10 -0
- data/lib/active_support/core_ext/date/blank.rb +18 -0
- data/lib/active_support/core_ext/date/calculations.rb +161 -0
- data/lib/active_support/core_ext/date/conversions.rb +95 -28
- data/lib/active_support/core_ext/date/zones.rb +8 -0
- data/lib/active_support/core_ext/date.rb +6 -5
- data/lib/active_support/core_ext/date_and_time/calculations.rb +374 -0
- data/lib/active_support/core_ext/date_and_time/compatibility.rb +23 -0
- data/lib/active_support/core_ext/date_and_time/zones.rb +40 -0
- data/lib/active_support/core_ext/date_time/acts_like.rb +16 -0
- data/lib/active_support/core_ext/date_time/blank.rb +18 -0
- data/lib/active_support/core_ext/date_time/calculations.rb +215 -0
- data/lib/active_support/core_ext/date_time/compatibility.rb +16 -0
- data/lib/active_support/core_ext/date_time/conversions.rb +108 -0
- data/lib/active_support/core_ext/date_time.rb +7 -0
- data/lib/active_support/core_ext/digest/uuid.rb +76 -0
- data/lib/active_support/core_ext/digest.rb +3 -0
- data/lib/active_support/core_ext/enumerable.rb +277 -7
- data/lib/active_support/core_ext/erb/util.rb +201 -0
- data/lib/active_support/core_ext/file/atomic.rb +72 -0
- data/lib/active_support/core_ext/file.rb +3 -0
- data/lib/active_support/core_ext/hash/conversions.rb +262 -0
- data/lib/active_support/core_ext/hash/deep_merge.rb +43 -0
- data/lib/active_support/core_ext/hash/deep_transform_values.rb +46 -0
- data/lib/active_support/core_ext/hash/except.rb +12 -0
- data/lib/active_support/core_ext/hash/indifferent_access.rb +19 -55
- data/lib/active_support/core_ext/hash/keys.rb +134 -44
- data/lib/active_support/core_ext/hash/reverse_merge.rb +22 -22
- data/lib/active_support/core_ext/hash/slice.rb +27 -0
- data/lib/active_support/core_ext/hash.rb +9 -8
- data/lib/active_support/core_ext/integer/inflections.rb +29 -13
- data/lib/active_support/core_ext/integer/multiple.rb +12 -0
- data/lib/active_support/core_ext/integer/time.rb +22 -0
- data/lib/active_support/core_ext/integer.rb +4 -6
- data/lib/active_support/core_ext/kernel/concern.rb +14 -0
- data/lib/active_support/core_ext/kernel/reporting.rb +45 -0
- data/lib/active_support/core_ext/kernel/singleton_class.rb +8 -0
- data/lib/active_support/core_ext/kernel.rb +4 -78
- data/lib/active_support/core_ext/load_error.rb +6 -35
- data/lib/active_support/core_ext/module/aliasing.rb +31 -0
- data/lib/active_support/core_ext/module/anonymous.rb +30 -0
- data/lib/active_support/core_ext/module/attr_internal.rb +48 -0
- data/lib/active_support/core_ext/module/attribute_accessors.rb +214 -0
- data/lib/active_support/core_ext/module/attribute_accessors_per_thread.rb +175 -0
- data/lib/active_support/core_ext/module/concerning.rb +140 -0
- data/lib/active_support/core_ext/module/delegation.rb +225 -0
- data/lib/active_support/core_ext/module/deprecation.rb +25 -0
- data/lib/active_support/core_ext/module/introspection.rb +65 -0
- data/lib/active_support/core_ext/module/redefine_method.rb +40 -0
- data/lib/active_support/core_ext/module/remove_method.rb +17 -0
- data/lib/active_support/core_ext/module.rb +13 -0
- data/lib/active_support/core_ext/name_error.rb +59 -0
- data/lib/active_support/core_ext/numeric/bytes.rb +73 -42
- data/lib/active_support/core_ext/numeric/conversions.rb +145 -0
- data/lib/active_support/core_ext/numeric/time.rb +64 -57
- data/lib/active_support/core_ext/numeric.rb +4 -6
- data/lib/active_support/core_ext/object/acts_like.rb +45 -0
- data/lib/active_support/core_ext/object/blank.rb +199 -0
- data/lib/active_support/core_ext/object/conversions.rb +6 -0
- data/lib/active_support/core_ext/object/deep_dup.rb +71 -0
- data/lib/active_support/core_ext/object/duplicable.rb +69 -0
- data/lib/active_support/core_ext/object/inclusion.rb +37 -0
- data/lib/active_support/core_ext/object/instance_variables.rb +32 -0
- data/lib/active_support/core_ext/object/json.rb +267 -0
- data/lib/active_support/core_ext/object/to_param.rb +3 -0
- data/lib/active_support/core_ext/object/to_query.rb +93 -0
- data/lib/active_support/core_ext/object/try.rb +158 -0
- data/lib/active_support/core_ext/object/with.rb +46 -0
- data/lib/active_support/core_ext/object/with_options.rb +101 -0
- data/lib/active_support/core_ext/object.rb +17 -0
- data/lib/active_support/core_ext/pathname/blank.rb +20 -0
- data/lib/active_support/core_ext/pathname/existence.rb +23 -0
- data/lib/active_support/core_ext/pathname.rb +4 -0
- data/lib/active_support/core_ext/range/compare_range.rb +57 -0
- data/lib/active_support/core_ext/range/conversions.rb +58 -17
- data/lib/active_support/core_ext/range/overlap.rb +40 -0
- data/lib/active_support/core_ext/range/sole.rb +17 -0
- data/lib/active_support/core_ext/range.rb +5 -4
- data/lib/active_support/core_ext/regexp.rb +14 -0
- data/lib/active_support/core_ext/securerandom.rb +57 -0
- data/lib/active_support/core_ext/string/access.rb +93 -56
- data/lib/active_support/core_ext/string/behavior.rb +8 -0
- data/lib/active_support/core_ext/string/conversions.rb +57 -16
- data/lib/active_support/core_ext/string/exclude.rb +13 -0
- data/lib/active_support/core_ext/string/filters.rb +151 -0
- data/lib/active_support/core_ext/string/indent.rb +45 -0
- data/lib/active_support/core_ext/string/inflections.rb +297 -54
- data/lib/active_support/core_ext/string/inquiry.rb +16 -0
- data/lib/active_support/core_ext/string/multibyte.rb +67 -0
- data/lib/active_support/core_ext/string/output_safety.rb +235 -0
- data/lib/active_support/core_ext/string/starts_ends_with.rb +4 -18
- data/lib/active_support/core_ext/string/strip.rb +27 -0
- data/lib/active_support/core_ext/string/zones.rb +16 -0
- data/lib/active_support/core_ext/string.rb +14 -10
- data/lib/active_support/core_ext/symbol/starts_ends_with.rb +6 -0
- data/lib/active_support/core_ext/symbol.rb +3 -0
- data/lib/active_support/core_ext/thread/backtrace/location.rb +7 -0
- data/lib/active_support/core_ext/time/acts_like.rb +10 -0
- data/lib/active_support/core_ext/time/calculations.rb +358 -153
- data/lib/active_support/core_ext/time/compatibility.rb +15 -0
- data/lib/active_support/core_ext/time/conversions.rb +69 -30
- data/lib/active_support/core_ext/time/zones.rb +97 -0
- data/lib/active_support/core_ext/time.rb +6 -6
- data/lib/active_support/core_ext.rb +5 -1
- data/lib/active_support/current_attributes/test_helper.rb +13 -0
- data/lib/active_support/current_attributes.rb +243 -0
- data/lib/active_support/deep_mergeable.rb +53 -0
- data/lib/active_support/delegation.rb +183 -0
- data/lib/active_support/dependencies/autoload.rb +72 -0
- data/lib/active_support/dependencies/interlock.rb +55 -0
- data/lib/active_support/dependencies/require_dependency.rb +28 -0
- data/lib/active_support/dependencies.rb +84 -222
- data/lib/active_support/deprecation/behaviors.rb +148 -0
- data/lib/active_support/deprecation/constant_accessor.rb +74 -0
- data/lib/active_support/deprecation/deprecators.rb +104 -0
- data/lib/active_support/deprecation/disallowed.rb +54 -0
- data/lib/active_support/deprecation/method_wrappers.rb +68 -0
- data/lib/active_support/deprecation/proxy_wrappers.rb +189 -0
- data/lib/active_support/deprecation/reporting.rb +162 -0
- data/lib/active_support/deprecation.rb +81 -0
- data/lib/active_support/deprecator.rb +7 -0
- data/lib/active_support/descendants_tracker.rb +112 -0
- data/lib/active_support/digest.rb +22 -0
- data/lib/active_support/duration/iso8601_parser.rb +123 -0
- data/lib/active_support/duration/iso8601_serializer.rb +64 -0
- data/lib/active_support/duration.rb +524 -0
- data/lib/active_support/editor.rb +70 -0
- data/lib/active_support/encrypted_configuration.rb +126 -0
- data/lib/active_support/encrypted_file.rb +133 -0
- data/lib/active_support/environment_inquirer.rb +40 -0
- data/lib/active_support/error_reporter/test_helper.rb +15 -0
- data/lib/active_support/error_reporter.rb +318 -0
- data/lib/active_support/event_reporter/test_helper.rb +32 -0
- data/lib/active_support/event_reporter.rb +592 -0
- data/lib/active_support/evented_file_update_checker.rb +185 -0
- data/lib/active_support/execution_context/test_helper.rb +13 -0
- data/lib/active_support/execution_context.rb +110 -0
- data/lib/active_support/execution_wrapper.rb +150 -0
- data/lib/active_support/executor/test_helper.rb +7 -0
- data/lib/active_support/executor.rb +8 -0
- data/lib/active_support/file_update_checker.rb +166 -0
- data/lib/active_support/fork_tracker.rb +43 -0
- data/lib/active_support/gem_version.rb +17 -0
- data/lib/active_support/gzip.rb +41 -0
- data/lib/active_support/hash_with_indifferent_access.rb +464 -0
- data/lib/active_support/html_safe_translation.rb +56 -0
- data/lib/active_support/i18n.rb +17 -0
- data/lib/active_support/i18n_railtie.rb +140 -0
- data/lib/active_support/inflections.rb +68 -49
- data/lib/active_support/inflector/inflections.rb +290 -0
- data/lib/active_support/inflector/methods.rb +387 -0
- data/lib/active_support/inflector/transliterate.rb +147 -0
- data/lib/active_support/inflector.rb +7 -164
- data/lib/active_support/isolated_execution_state.rb +76 -0
- data/lib/active_support/json/decoding.rb +78 -0
- data/lib/active_support/json/encoding.rb +256 -0
- data/lib/active_support/json.rb +4 -0
- data/lib/active_support/key_generator.rb +66 -0
- data/lib/active_support/lazy_load_hooks.rb +107 -0
- data/lib/active_support/locale/en.rb +33 -0
- data/lib/active_support/locale/en.yml +141 -0
- data/lib/active_support/log_subscriber/test_helper.rb +106 -0
- data/lib/active_support/log_subscriber.rb +188 -0
- data/lib/active_support/logger.rb +55 -0
- data/lib/active_support/logger_silence.rb +21 -0
- data/lib/active_support/logger_thread_safe_level.rb +50 -0
- data/lib/active_support/message_encryptor.rb +374 -0
- data/lib/active_support/message_encryptors.rb +193 -0
- data/lib/active_support/message_pack/cache_serializer.rb +23 -0
- data/lib/active_support/message_pack/extensions.rb +310 -0
- data/lib/active_support/message_pack/serializer.rb +63 -0
- data/lib/active_support/message_pack.rb +50 -0
- data/lib/active_support/message_verifier.rb +377 -0
- data/lib/active_support/message_verifiers.rb +189 -0
- data/lib/active_support/messages/codec.rb +65 -0
- data/lib/active_support/messages/metadata.rb +146 -0
- data/lib/active_support/messages/rotation_configuration.rb +23 -0
- data/lib/active_support/messages/rotation_coordinator.rb +102 -0
- data/lib/active_support/messages/rotator.rb +69 -0
- data/lib/active_support/messages/serializer_with_fallback.rb +158 -0
- data/lib/active_support/multibyte/chars.rb +188 -0
- data/lib/active_support/multibyte/unicode.rb +42 -0
- data/lib/active_support/multibyte.rb +27 -0
- data/lib/active_support/notifications/fanout.rb +467 -0
- data/lib/active_support/notifications/instrumenter.rb +240 -0
- data/lib/active_support/notifications.rb +281 -0
- data/lib/active_support/number_helper/number_converter.rb +190 -0
- data/lib/active_support/number_helper/number_to_currency_converter.rb +46 -0
- data/lib/active_support/number_helper/number_to_delimited_converter.rb +30 -0
- data/lib/active_support/number_helper/number_to_human_converter.rb +69 -0
- data/lib/active_support/number_helper/number_to_human_size_converter.rb +60 -0
- data/lib/active_support/number_helper/number_to_percentage_converter.rb +16 -0
- data/lib/active_support/number_helper/number_to_phone_converter.rb +60 -0
- data/lib/active_support/number_helper/number_to_rounded_converter.rb +59 -0
- data/lib/active_support/number_helper/rounding_helper.rb +46 -0
- data/lib/active_support/number_helper.rb +479 -0
- data/lib/active_support/option_merger.rb +38 -0
- data/lib/active_support/ordered_hash.rb +50 -0
- data/lib/active_support/ordered_options.rb +141 -25
- data/lib/active_support/parameter_filter.rb +157 -0
- data/lib/active_support/rails.rb +26 -0
- data/lib/active_support/railtie.rb +180 -0
- data/lib/active_support/reloader.rb +138 -0
- data/lib/active_support/rescuable.rb +176 -0
- data/lib/active_support/secure_compare_rotator.rb +58 -0
- data/lib/active_support/security_utils.rb +38 -0
- data/lib/active_support/string_inquirer.rb +35 -0
- data/lib/active_support/structured_event_subscriber.rb +99 -0
- data/lib/active_support/subscriber.rb +141 -0
- data/lib/active_support/syntax_error_proxy.rb +67 -0
- data/lib/active_support/tagged_logging.rb +157 -0
- data/lib/active_support/test_case.rb +365 -0
- data/lib/active_support/testing/assertions.rb +369 -0
- data/lib/active_support/testing/autorun.rb +10 -0
- data/lib/active_support/testing/constant_lookup.rb +51 -0
- data/lib/active_support/testing/constant_stubbing.rb +54 -0
- data/lib/active_support/testing/declarative.rb +28 -0
- data/lib/active_support/testing/deprecation.rb +82 -0
- data/lib/active_support/testing/error_reporter_assertions.rb +124 -0
- data/lib/active_support/testing/event_reporter_assertions.rb +227 -0
- data/lib/active_support/testing/file_fixtures.rb +38 -0
- data/lib/active_support/testing/isolation.rb +121 -0
- data/lib/active_support/testing/method_call_assertions.rb +69 -0
- data/lib/active_support/testing/notification_assertions.rb +92 -0
- data/lib/active_support/testing/parallelization/server.rb +98 -0
- data/lib/active_support/testing/parallelization/worker.rb +107 -0
- data/lib/active_support/testing/parallelization.rb +79 -0
- data/lib/active_support/testing/parallelize_executor.rb +81 -0
- data/lib/active_support/testing/setup_and_teardown.rb +57 -0
- data/lib/active_support/testing/stream.rb +41 -0
- data/lib/active_support/testing/tagged_logging.rb +27 -0
- data/lib/active_support/testing/tests_without_assertions.rb +19 -0
- data/lib/active_support/testing/time_helpers.rb +273 -0
- data/lib/active_support/time.rb +20 -0
- data/lib/active_support/time_with_zone.rb +613 -0
- data/lib/active_support/values/time_zone.rb +599 -158
- data/lib/active_support/version.rb +7 -6
- data/lib/active_support/xml_mini/jdom.rb +175 -0
- data/lib/active_support/xml_mini/libxml.rb +80 -0
- data/lib/active_support/xml_mini/libxmlsax.rb +83 -0
- data/lib/active_support/xml_mini/nokogiri.rb +83 -0
- data/lib/active_support/xml_mini/nokogirisax.rb +86 -0
- data/lib/active_support/xml_mini/rexml.rb +137 -0
- data/lib/active_support/xml_mini.rb +212 -0
- data/lib/active_support.rb +122 -10
- metadata +524 -93
- data/CHANGELOG +0 -283
- data/lib/active_support/binding_of_caller.rb +0 -84
- data/lib/active_support/breakpoint.rb +0 -523
- data/lib/active_support/class_attribute_accessors.rb +0 -57
- data/lib/active_support/class_inheritable_attributes.rb +0 -117
- data/lib/active_support/clean_logger.rb +0 -36
- data/lib/active_support/core_ext/blank.rb +0 -38
- data/lib/active_support/core_ext/cgi/escape_skipping_slashes.rb +0 -14
- data/lib/active_support/core_ext/cgi.rb +0 -5
- data/lib/active_support/core_ext/exception.rb +0 -29
- data/lib/active_support/core_ext/integer/even_odd.rb +0 -24
- data/lib/active_support/core_ext/object_and_class.rb +0 -44
- data/lib/active_support/module_attribute_accessors.rb +0 -57
- data/lib/active_support/whiny_nil.rb +0 -38
|
@@ -0,0 +1,613 @@
|
|
|
1
|
+
# frozen_string_literal: true
|
|
2
|
+
|
|
3
|
+
require "yaml"
|
|
4
|
+
|
|
5
|
+
require "active_support/duration"
|
|
6
|
+
require "active_support/values/time_zone"
|
|
7
|
+
require "active_support/core_ext/object/acts_like"
|
|
8
|
+
require "active_support/core_ext/date_and_time/compatibility"
|
|
9
|
+
|
|
10
|
+
module ActiveSupport
|
|
11
|
+
# = Active Support \Time With Zone
|
|
12
|
+
#
|
|
13
|
+
# A Time-like class that can represent a time in any time zone. Necessary
|
|
14
|
+
# because standard Ruby Time instances are limited to UTC and the
|
|
15
|
+
# system's <tt>ENV['TZ']</tt> zone.
|
|
16
|
+
#
|
|
17
|
+
# You shouldn't ever need to create a TimeWithZone instance directly via +new+.
|
|
18
|
+
# Instead use methods +local+, +parse+, +at+, and +now+ on TimeZone instances,
|
|
19
|
+
# and +in_time_zone+ on Time and DateTime instances.
|
|
20
|
+
#
|
|
21
|
+
# Time.zone = 'Eastern Time (US & Canada)' # => 'Eastern Time (US & Canada)'
|
|
22
|
+
# Time.zone.local(2007, 2, 10, 15, 30, 45) # => Sat, 10 Feb 2007 15:30:45.000000000 EST -05:00
|
|
23
|
+
# Time.zone.parse('2007-02-10 15:30:45') # => Sat, 10 Feb 2007 15:30:45.000000000 EST -05:00
|
|
24
|
+
# Time.zone.at(1171139445) # => Sat, 10 Feb 2007 15:30:45.000000000 EST -05:00
|
|
25
|
+
# Time.zone.now # => Sun, 18 May 2008 13:07:55.754107581 EDT -04:00
|
|
26
|
+
# Time.utc(2007, 2, 10, 20, 30, 45).in_time_zone # => Sat, 10 Feb 2007 15:30:45.000000000 EST -05:00
|
|
27
|
+
#
|
|
28
|
+
# See Time and TimeZone for further documentation of these methods.
|
|
29
|
+
#
|
|
30
|
+
# TimeWithZone instances implement the same API as Ruby Time instances, so
|
|
31
|
+
# that Time and TimeWithZone instances are interchangeable.
|
|
32
|
+
#
|
|
33
|
+
# t = Time.zone.now # => Sun, 18 May 2008 13:27:25.031505668 EDT -04:00
|
|
34
|
+
# t.hour # => 13
|
|
35
|
+
# t.dst? # => true
|
|
36
|
+
# t.utc_offset # => -14400
|
|
37
|
+
# t.zone # => "EDT"
|
|
38
|
+
# t.to_fs(:rfc822) # => "Sun, 18 May 2008 13:27:25 -0400"
|
|
39
|
+
# t + 1.day # => Mon, 19 May 2008 13:27:25.031505668 EDT -04:00
|
|
40
|
+
# t.beginning_of_year # => Tue, 01 Jan 2008 00:00:00.000000000 EST -05:00
|
|
41
|
+
# t > Time.utc(1999) # => true
|
|
42
|
+
# t.is_a?(Time) # => true
|
|
43
|
+
# t.is_a?(ActiveSupport::TimeWithZone) # => true
|
|
44
|
+
class TimeWithZone
|
|
45
|
+
PRECISIONS = Hash.new { |h, n| h[n] = "%FT%T.%#{n}N" }
|
|
46
|
+
PRECISIONS[0] = "%FT%T"
|
|
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
|
+
@time_zone, @time = time_zone, local_time
|
|
53
|
+
if utc_time
|
|
54
|
+
@utc = transfer_time_values_to_utc_constructor(utc_time)
|
|
55
|
+
@period = period
|
|
56
|
+
else
|
|
57
|
+
@utc = nil
|
|
58
|
+
@period = get_period_and_ensure_valid_local_time(period)
|
|
59
|
+
end
|
|
60
|
+
@is_utc = zone == "UTC" || zone == "UCT"
|
|
61
|
+
end
|
|
62
|
+
|
|
63
|
+
# Returns a <tt>Time</tt> instance that represents the time in +time_zone+.
|
|
64
|
+
def time
|
|
65
|
+
@time ||= incorporate_utc_offset(@utc, utc_offset)
|
|
66
|
+
end
|
|
67
|
+
|
|
68
|
+
# Returns a <tt>Time</tt> instance of the simultaneous time in the UTC timezone.
|
|
69
|
+
def utc
|
|
70
|
+
@utc ||= incorporate_utc_offset(@time, -utc_offset)
|
|
71
|
+
end
|
|
72
|
+
alias_method :comparable_time, :utc
|
|
73
|
+
alias_method :getgm, :utc
|
|
74
|
+
alias_method :getutc, :utc
|
|
75
|
+
alias_method :gmtime, :utc
|
|
76
|
+
|
|
77
|
+
# Returns the underlying +TZInfo::TimezonePeriod+.
|
|
78
|
+
def period
|
|
79
|
+
@period ||= time_zone.period_for_utc(@utc)
|
|
80
|
+
end
|
|
81
|
+
|
|
82
|
+
# Returns the simultaneous time in <tt>Time.zone</tt>, or the specified zone.
|
|
83
|
+
def in_time_zone(new_zone = ::Time.zone)
|
|
84
|
+
return self if time_zone == new_zone
|
|
85
|
+
utc.in_time_zone(new_zone)
|
|
86
|
+
end
|
|
87
|
+
|
|
88
|
+
# Returns a <tt>Time</tt> instance of the simultaneous time in the system timezone.
|
|
89
|
+
def localtime(utc_offset = nil)
|
|
90
|
+
utc.getlocal(utc_offset)
|
|
91
|
+
end
|
|
92
|
+
alias_method :getlocal, :localtime
|
|
93
|
+
|
|
94
|
+
# Returns true if the current time is within Daylight Savings \Time for the
|
|
95
|
+
# specified time zone.
|
|
96
|
+
#
|
|
97
|
+
# Time.zone = 'Eastern Time (US & Canada)' # => 'Eastern Time (US & Canada)'
|
|
98
|
+
# Time.zone.parse("2012-5-30").dst? # => true
|
|
99
|
+
# Time.zone.parse("2012-11-30").dst? # => false
|
|
100
|
+
def dst?
|
|
101
|
+
period.dst?
|
|
102
|
+
end
|
|
103
|
+
alias_method :isdst, :dst?
|
|
104
|
+
|
|
105
|
+
# Returns true if the current time zone is set to UTC.
|
|
106
|
+
#
|
|
107
|
+
# Time.zone = 'UTC' # => 'UTC'
|
|
108
|
+
# Time.zone.now.utc? # => true
|
|
109
|
+
# Time.zone = 'Eastern Time (US & Canada)' # => 'Eastern Time (US & Canada)'
|
|
110
|
+
# Time.zone.now.utc? # => false
|
|
111
|
+
def utc?
|
|
112
|
+
@is_utc
|
|
113
|
+
end
|
|
114
|
+
alias_method :gmt?, :utc?
|
|
115
|
+
|
|
116
|
+
# Returns the offset from current time to UTC time in seconds.
|
|
117
|
+
def utc_offset
|
|
118
|
+
period.observed_utc_offset
|
|
119
|
+
end
|
|
120
|
+
alias_method :gmt_offset, :utc_offset
|
|
121
|
+
alias_method :gmtoff, :utc_offset
|
|
122
|
+
|
|
123
|
+
# Returns a formatted string of the offset from UTC, or an alternative
|
|
124
|
+
# string if the time zone is already UTC.
|
|
125
|
+
#
|
|
126
|
+
# Time.zone = 'Eastern Time (US & Canada)' # => "Eastern Time (US & Canada)"
|
|
127
|
+
# Time.zone.now.formatted_offset(true) # => "-05:00"
|
|
128
|
+
# Time.zone.now.formatted_offset(false) # => "-0500"
|
|
129
|
+
# Time.zone = 'UTC' # => "UTC"
|
|
130
|
+
# Time.zone.now.formatted_offset(true, "0") # => "0"
|
|
131
|
+
def formatted_offset(colon = true, alternate_utc_string = nil)
|
|
132
|
+
utc? && alternate_utc_string || TimeZone.seconds_to_utc_offset(utc_offset, colon)
|
|
133
|
+
end
|
|
134
|
+
|
|
135
|
+
# Returns the time zone abbreviation.
|
|
136
|
+
#
|
|
137
|
+
# Time.zone = 'Eastern Time (US & Canada)' # => "Eastern Time (US & Canada)"
|
|
138
|
+
# Time.zone.now.zone # => "EST"
|
|
139
|
+
def zone
|
|
140
|
+
period.abbreviation
|
|
141
|
+
end
|
|
142
|
+
|
|
143
|
+
# Returns a string of the object's date, time, zone, and offset from UTC.
|
|
144
|
+
#
|
|
145
|
+
# Time.zone.now.inspect # => "2024-11-13 07:00:10.528054960 UTC +00:00"
|
|
146
|
+
def inspect
|
|
147
|
+
"#{time.strftime('%F %H:%M:%S.%9N')} #{zone} #{formatted_offset}"
|
|
148
|
+
end
|
|
149
|
+
|
|
150
|
+
# Returns a string of the object's date and time in the ISO 8601 standard
|
|
151
|
+
# format.
|
|
152
|
+
#
|
|
153
|
+
# Time.zone.now.xmlschema # => "2014-12-04T11:02:37-05:00"
|
|
154
|
+
def xmlschema(fraction_digits = 0)
|
|
155
|
+
precision = fraction_digits || 0
|
|
156
|
+
|
|
157
|
+
if @is_utc
|
|
158
|
+
utc.iso8601(precision)
|
|
159
|
+
else
|
|
160
|
+
str = time.iso8601(precision)
|
|
161
|
+
offset = formatted_offset(true, "Z")
|
|
162
|
+
|
|
163
|
+
str.sub!(/(Z|[+-]\d{2}:\d{2})\z/, offset)
|
|
164
|
+
str
|
|
165
|
+
end
|
|
166
|
+
end
|
|
167
|
+
alias_method :iso8601, :xmlschema
|
|
168
|
+
alias_method :rfc3339, :xmlschema
|
|
169
|
+
|
|
170
|
+
# Coerces time to a string for JSON encoding. The default format is ISO 8601.
|
|
171
|
+
# You can get %Y/%m/%d %H:%M:%S +offset style by setting
|
|
172
|
+
# <tt>ActiveSupport::JSON::Encoding.use_standard_json_time_format</tt>
|
|
173
|
+
# to +false+.
|
|
174
|
+
#
|
|
175
|
+
# # With ActiveSupport::JSON::Encoding.use_standard_json_time_format = true
|
|
176
|
+
# Time.utc(2005,2,1,15,15,10).in_time_zone("Hawaii").as_json
|
|
177
|
+
# # => "2005-02-01T05:15:10.000-10:00"
|
|
178
|
+
#
|
|
179
|
+
# # With ActiveSupport::JSON::Encoding.use_standard_json_time_format = false
|
|
180
|
+
# Time.utc(2005,2,1,15,15,10).in_time_zone("Hawaii").as_json
|
|
181
|
+
# # => "2005/02/01 05:15:10 -1000"
|
|
182
|
+
def as_json(options = nil)
|
|
183
|
+
if ActiveSupport::JSON::Encoding.use_standard_json_time_format
|
|
184
|
+
xmlschema(ActiveSupport::JSON::Encoding.time_precision).force_encoding(Encoding::UTF_8)
|
|
185
|
+
else
|
|
186
|
+
%(#{time.strftime("%Y/%m/%d %H:%M:%S")} #{formatted_offset(false)})
|
|
187
|
+
end
|
|
188
|
+
end
|
|
189
|
+
|
|
190
|
+
def init_with(coder) # :nodoc:
|
|
191
|
+
initialize(coder["utc"], coder["zone"], coder["time"])
|
|
192
|
+
end
|
|
193
|
+
|
|
194
|
+
def encode_with(coder) # :nodoc:
|
|
195
|
+
coder.map = { "utc" => utc, "zone" => time_zone, "time" => time }
|
|
196
|
+
end
|
|
197
|
+
|
|
198
|
+
# Returns a string of the object's date and time in the format used by
|
|
199
|
+
# HTTP requests.
|
|
200
|
+
#
|
|
201
|
+
# Time.zone.now.httpdate # => "Tue, 01 Jan 2013 04:39:43 GMT"
|
|
202
|
+
def httpdate
|
|
203
|
+
utc.httpdate
|
|
204
|
+
end
|
|
205
|
+
|
|
206
|
+
# Returns a string of the object's date and time in the RFC 2822 standard
|
|
207
|
+
# format.
|
|
208
|
+
#
|
|
209
|
+
# Time.zone.now.rfc2822 # => "Tue, 01 Jan 2013 04:51:39 +0000"
|
|
210
|
+
def rfc2822
|
|
211
|
+
to_fs(:rfc822)
|
|
212
|
+
end
|
|
213
|
+
alias_method :rfc822, :rfc2822
|
|
214
|
+
|
|
215
|
+
# Returns a string of the object's date and time.
|
|
216
|
+
def to_s
|
|
217
|
+
"#{time.strftime("%Y-%m-%d %H:%M:%S")} #{formatted_offset(false, 'UTC')}" # mimicking Ruby Time#to_s format
|
|
218
|
+
end
|
|
219
|
+
|
|
220
|
+
# Returns a string of the object's date and time.
|
|
221
|
+
#
|
|
222
|
+
# This method is aliased to <tt>to_formatted_s</tt>.
|
|
223
|
+
#
|
|
224
|
+
# Accepts an optional <tt>format</tt>:
|
|
225
|
+
# * <tt>:default</tt> - default value, mimics Ruby Time#to_s format.
|
|
226
|
+
# * <tt>:db</tt> - format outputs time in UTC :db time. See Time#to_fs(:db).
|
|
227
|
+
# * Any key in +Time::DATE_FORMATS+ can be used. See active_support/core_ext/time/conversions.rb.
|
|
228
|
+
def to_fs(format = :default)
|
|
229
|
+
if format == :db
|
|
230
|
+
utc.to_fs(format)
|
|
231
|
+
elsif formatter = ::Time::DATE_FORMATS[format]
|
|
232
|
+
formatter.respond_to?(:call) ? formatter.call(self).to_s : strftime(formatter)
|
|
233
|
+
else
|
|
234
|
+
to_s
|
|
235
|
+
end
|
|
236
|
+
end
|
|
237
|
+
alias_method :to_formatted_s, :to_fs
|
|
238
|
+
|
|
239
|
+
# Replaces <tt>%Z</tt> directive with +zone before passing to Time#strftime,
|
|
240
|
+
# so that zone information is correct.
|
|
241
|
+
def strftime(format)
|
|
242
|
+
format = format.gsub(/((?:\A|[^%])(?:%%)*)%Z/, "\\1#{zone}")
|
|
243
|
+
getlocal(utc_offset).strftime(format)
|
|
244
|
+
end
|
|
245
|
+
|
|
246
|
+
# Use the time in UTC for comparisons.
|
|
247
|
+
def <=>(other)
|
|
248
|
+
utc <=> other
|
|
249
|
+
end
|
|
250
|
+
alias_method :before?, :<
|
|
251
|
+
alias_method :after?, :>
|
|
252
|
+
|
|
253
|
+
# Returns true if the current object's time is within the specified
|
|
254
|
+
# +min+ and +max+ time.
|
|
255
|
+
def between?(min, max)
|
|
256
|
+
utc.between?(min, max)
|
|
257
|
+
end
|
|
258
|
+
|
|
259
|
+
# Returns true if the current object's time is in the past.
|
|
260
|
+
def past?
|
|
261
|
+
utc.past?
|
|
262
|
+
end
|
|
263
|
+
|
|
264
|
+
# Returns true if the current object's time falls within
|
|
265
|
+
# the current day.
|
|
266
|
+
def today?
|
|
267
|
+
time.today?
|
|
268
|
+
end
|
|
269
|
+
|
|
270
|
+
# Returns true if the current object's time falls within
|
|
271
|
+
# the next day (tomorrow).
|
|
272
|
+
def tomorrow?
|
|
273
|
+
time.tomorrow?
|
|
274
|
+
end
|
|
275
|
+
alias :next_day? :tomorrow?
|
|
276
|
+
|
|
277
|
+
# Returns true if the current object's time falls within
|
|
278
|
+
# the previous day (yesterday).
|
|
279
|
+
def yesterday?
|
|
280
|
+
time.yesterday?
|
|
281
|
+
end
|
|
282
|
+
alias :prev_day? :yesterday?
|
|
283
|
+
|
|
284
|
+
# Returns true if the current object's time is in the future.
|
|
285
|
+
def future?
|
|
286
|
+
utc.future?
|
|
287
|
+
end
|
|
288
|
+
|
|
289
|
+
# Returns +true+ if +other+ is equal to current object.
|
|
290
|
+
def eql?(other)
|
|
291
|
+
other.eql?(utc)
|
|
292
|
+
end
|
|
293
|
+
|
|
294
|
+
def hash
|
|
295
|
+
utc.hash
|
|
296
|
+
end
|
|
297
|
+
|
|
298
|
+
# Adds an interval of time to the current object's time and returns that
|
|
299
|
+
# value as a new TimeWithZone object.
|
|
300
|
+
#
|
|
301
|
+
# Time.zone = 'Eastern Time (US & Canada)' # => 'Eastern Time (US & Canada)'
|
|
302
|
+
# now = Time.zone.now # => Sun, 02 Nov 2014 01:26:28.725182881 EDT -04:00
|
|
303
|
+
# now + 1000 # => Sun, 02 Nov 2014 01:43:08.725182881 EDT -04:00
|
|
304
|
+
#
|
|
305
|
+
# If we're adding a Duration of variable length (i.e., years, months, days),
|
|
306
|
+
# move forward from #time, otherwise move forward from #utc, for accuracy
|
|
307
|
+
# when moving across DST boundaries.
|
|
308
|
+
#
|
|
309
|
+
# For instance, a time + 24.hours will advance exactly 24 hours, while a
|
|
310
|
+
# time + 1.day will advance 23-25 hours, depending on the day.
|
|
311
|
+
#
|
|
312
|
+
# now + 24.hours # => Mon, 03 Nov 2014 00:26:28.725182881 EST -05:00
|
|
313
|
+
# now + 1.day # => Mon, 03 Nov 2014 01:26:28.725182881 EST -05:00
|
|
314
|
+
def +(other)
|
|
315
|
+
if duration_of_variable_length?(other)
|
|
316
|
+
method_missing(:+, other)
|
|
317
|
+
else
|
|
318
|
+
result = utc + other
|
|
319
|
+
|
|
320
|
+
result.in_time_zone(time_zone)
|
|
321
|
+
end
|
|
322
|
+
end
|
|
323
|
+
alias_method :since, :+
|
|
324
|
+
alias_method :in, :+
|
|
325
|
+
|
|
326
|
+
# Subtracts an interval of time and returns a new TimeWithZone object unless
|
|
327
|
+
# the other value +acts_like?+ time. In which case, it will subtract the
|
|
328
|
+
# other time and return the difference in seconds as a Float.
|
|
329
|
+
#
|
|
330
|
+
# Time.zone = 'Eastern Time (US & Canada)' # => 'Eastern Time (US & Canada)'
|
|
331
|
+
# now = Time.zone.now # => Mon, 03 Nov 2014 00:26:28.725182881 EST -05:00
|
|
332
|
+
# now - 1000 # => Mon, 03 Nov 2014 00:09:48.725182881 EST -05:00
|
|
333
|
+
#
|
|
334
|
+
# If subtracting a Duration of variable length (i.e., years, months, days),
|
|
335
|
+
# move backward from #time, otherwise move backward from #utc, for accuracy
|
|
336
|
+
# when moving across DST boundaries.
|
|
337
|
+
#
|
|
338
|
+
# For instance, a time - 24.hours will go subtract exactly 24 hours, while a
|
|
339
|
+
# time - 1.day will subtract 23-25 hours, depending on the day.
|
|
340
|
+
#
|
|
341
|
+
# now - 24.hours # => Sun, 02 Nov 2014 01:26:28.725182881 EDT -04:00
|
|
342
|
+
# now - 1.day # => Sun, 02 Nov 2014 00:26:28.725182881 EDT -04:00
|
|
343
|
+
#
|
|
344
|
+
# If both the TimeWithZone object and the other value act like Time, a Float
|
|
345
|
+
# will be returned.
|
|
346
|
+
#
|
|
347
|
+
# Time.zone.now - 1.day.ago # => 86399.999967
|
|
348
|
+
#
|
|
349
|
+
def -(other)
|
|
350
|
+
if other.acts_like?(:time)
|
|
351
|
+
getutc - other.getutc
|
|
352
|
+
elsif duration_of_variable_length?(other)
|
|
353
|
+
method_missing(:-, other)
|
|
354
|
+
else
|
|
355
|
+
result = utc - other
|
|
356
|
+
result.in_time_zone(time_zone)
|
|
357
|
+
end
|
|
358
|
+
end
|
|
359
|
+
|
|
360
|
+
# Subtracts an interval of time from the current object's time and returns
|
|
361
|
+
# the result as a new TimeWithZone object.
|
|
362
|
+
#
|
|
363
|
+
# Time.zone = 'Eastern Time (US & Canada)' # => 'Eastern Time (US & Canada)'
|
|
364
|
+
# now = Time.zone.now # => Mon, 03 Nov 2014 00:26:28.725182881 EST -05:00
|
|
365
|
+
# now.ago(1000) # => Mon, 03 Nov 2014 00:09:48.725182881 EST -05:00
|
|
366
|
+
#
|
|
367
|
+
# If we're subtracting a Duration of variable length (i.e., years, months,
|
|
368
|
+
# days), move backward from #time, otherwise move backward from #utc, for
|
|
369
|
+
# accuracy when moving across DST boundaries.
|
|
370
|
+
#
|
|
371
|
+
# For instance, <tt>time.ago(24.hours)</tt> will move back exactly 24 hours,
|
|
372
|
+
# while <tt>time.ago(1.day)</tt> will move back 23-25 hours, depending on
|
|
373
|
+
# the day.
|
|
374
|
+
#
|
|
375
|
+
# now.ago(24.hours) # => Sun, 02 Nov 2014 01:26:28.725182881 EDT -04:00
|
|
376
|
+
# now.ago(1.day) # => Sun, 02 Nov 2014 00:26:28.725182881 EDT -04:00
|
|
377
|
+
def ago(other)
|
|
378
|
+
since(-other)
|
|
379
|
+
end
|
|
380
|
+
|
|
381
|
+
# Returns a new +ActiveSupport::TimeWithZone+ where one or more of the elements have
|
|
382
|
+
# been changed according to the +options+ parameter. The time options (<tt>:hour</tt>,
|
|
383
|
+
# <tt>:min</tt>, <tt>:sec</tt>, <tt>:usec</tt>, <tt>:nsec</tt>) reset cascadingly,
|
|
384
|
+
# so if only the hour is passed, then minute, sec, usec, and nsec is set to 0. If the
|
|
385
|
+
# hour and minute is passed, then sec, usec, and nsec is set to 0. The +options+
|
|
386
|
+
# parameter takes a hash with any of these keys: <tt>:year</tt>, <tt>:month</tt>,
|
|
387
|
+
# <tt>:day</tt>, <tt>:hour</tt>, <tt>:min</tt>, <tt>:sec</tt>, <tt>:usec</tt>,
|
|
388
|
+
# <tt>:nsec</tt>, <tt>:offset</tt>, <tt>:zone</tt>. Pass either <tt>:usec</tt>
|
|
389
|
+
# or <tt>:nsec</tt>, not both. Similarly, pass either <tt>:zone</tt> or
|
|
390
|
+
# <tt>:offset</tt>, not both.
|
|
391
|
+
#
|
|
392
|
+
# t = Time.zone.now # => Fri, 14 Apr 2017 11:45:15.116992711 EST -05:00
|
|
393
|
+
# t.change(year: 2020) # => Tue, 14 Apr 2020 11:45:15.116992711 EST -05:00
|
|
394
|
+
# t.change(hour: 12) # => Fri, 14 Apr 2017 12:00:00.000000000 EST -05:00
|
|
395
|
+
# t.change(min: 30) # => Fri, 14 Apr 2017 11:30:00.000000000 EST -05:00
|
|
396
|
+
# t.change(offset: "-10:00") # => Fri, 14 Apr 2017 11:45:15.116992711 HST -10:00
|
|
397
|
+
# t.change(zone: "Hawaii") # => Fri, 14 Apr 2017 11:45:15.116992711 HST -10:00
|
|
398
|
+
def change(options)
|
|
399
|
+
if options[:zone] && options[:offset]
|
|
400
|
+
raise ArgumentError, "Can't change both :offset and :zone at the same time: #{options.inspect}"
|
|
401
|
+
end
|
|
402
|
+
|
|
403
|
+
new_time = time.change(options)
|
|
404
|
+
|
|
405
|
+
if options[:zone]
|
|
406
|
+
new_zone = ::Time.find_zone(options[:zone])
|
|
407
|
+
elsif options[:offset]
|
|
408
|
+
new_zone = ::Time.find_zone(new_time.utc_offset)
|
|
409
|
+
end
|
|
410
|
+
|
|
411
|
+
new_zone ||= time_zone
|
|
412
|
+
periods = new_zone.periods_for_local(new_time)
|
|
413
|
+
|
|
414
|
+
self.class.new(nil, new_zone, new_time, periods.include?(period) ? period : nil)
|
|
415
|
+
end
|
|
416
|
+
|
|
417
|
+
# Uses Date to provide precise Time calculations for years, months, and days
|
|
418
|
+
# according to the proleptic Gregorian calendar. The result is returned as a
|
|
419
|
+
# new TimeWithZone object.
|
|
420
|
+
#
|
|
421
|
+
# The +options+ parameter takes a hash with any of these keys:
|
|
422
|
+
# <tt>:years</tt>, <tt>:months</tt>, <tt>:weeks</tt>, <tt>:days</tt>,
|
|
423
|
+
# <tt>:hours</tt>, <tt>:minutes</tt>, <tt>:seconds</tt>.
|
|
424
|
+
#
|
|
425
|
+
# If advancing by a value of variable length (i.e., years, weeks, months,
|
|
426
|
+
# days), move forward from #time, otherwise move forward from #utc, for
|
|
427
|
+
# accuracy when moving across DST boundaries.
|
|
428
|
+
#
|
|
429
|
+
# Time.zone = 'Eastern Time (US & Canada)' # => 'Eastern Time (US & Canada)'
|
|
430
|
+
# now = Time.zone.now # => Sun, 02 Nov 2014 01:26:28.558049687 EDT -04:00
|
|
431
|
+
# now.advance(seconds: 1) # => Sun, 02 Nov 2014 01:26:29.558049687 EDT -04:00
|
|
432
|
+
# now.advance(minutes: 1) # => Sun, 02 Nov 2014 01:27:28.558049687 EDT -04:00
|
|
433
|
+
# now.advance(hours: 1) # => Sun, 02 Nov 2014 01:26:28.558049687 EST -05:00
|
|
434
|
+
# now.advance(days: 1) # => Mon, 03 Nov 2014 01:26:28.558049687 EST -05:00
|
|
435
|
+
# now.advance(weeks: 1) # => Sun, 09 Nov 2014 01:26:28.558049687 EST -05:00
|
|
436
|
+
# now.advance(months: 1) # => Tue, 02 Dec 2014 01:26:28.558049687 EST -05:00
|
|
437
|
+
# now.advance(years: 1) # => Mon, 02 Nov 2015 01:26:28.558049687 EST -05:00
|
|
438
|
+
def advance(options)
|
|
439
|
+
# If we're advancing a value of variable length (i.e., years, weeks, months, days), advance from #time,
|
|
440
|
+
# otherwise advance from #utc, for accuracy when moving across DST boundaries
|
|
441
|
+
if options.values_at(:years, :weeks, :months, :days).any?
|
|
442
|
+
method_missing(:advance, options)
|
|
443
|
+
else
|
|
444
|
+
utc.advance(options).in_time_zone(time_zone)
|
|
445
|
+
end
|
|
446
|
+
end
|
|
447
|
+
|
|
448
|
+
%w(year mon month day mday wday yday hour min sec usec nsec to_date).each do |method_name|
|
|
449
|
+
class_eval <<-EOV, __FILE__, __LINE__ + 1
|
|
450
|
+
def #{method_name} # def month
|
|
451
|
+
time.#{method_name} # time.month
|
|
452
|
+
end # end
|
|
453
|
+
EOV
|
|
454
|
+
end
|
|
455
|
+
|
|
456
|
+
# Returns Array of parts of Time in sequence of
|
|
457
|
+
# [seconds, minutes, hours, day, month, year, weekday, yearday, dst?, zone].
|
|
458
|
+
#
|
|
459
|
+
# now = Time.zone.now # => Tue, 18 Aug 2015 02:29:27.485278555 UTC +00:00
|
|
460
|
+
# now.to_a # => [27, 29, 2, 18, 8, 2015, 2, 230, false, "UTC"]
|
|
461
|
+
def to_a
|
|
462
|
+
[time.sec, time.min, time.hour, time.day, time.mon, time.year, time.wday, time.yday, dst?, zone]
|
|
463
|
+
end
|
|
464
|
+
|
|
465
|
+
# Returns the object's date and time as a floating-point number of seconds
|
|
466
|
+
# since the Epoch (January 1, 1970 00:00 UTC).
|
|
467
|
+
#
|
|
468
|
+
# Time.zone.now.to_f # => 1417709320.285418
|
|
469
|
+
def to_f
|
|
470
|
+
utc.to_f
|
|
471
|
+
end
|
|
472
|
+
|
|
473
|
+
# Returns the object's date and time as an integer number of seconds
|
|
474
|
+
# since the Epoch (January 1, 1970 00:00 UTC).
|
|
475
|
+
#
|
|
476
|
+
# Time.zone.now.to_i # => 1417709320
|
|
477
|
+
def to_i
|
|
478
|
+
utc.to_i
|
|
479
|
+
end
|
|
480
|
+
alias_method :tv_sec, :to_i
|
|
481
|
+
|
|
482
|
+
# Returns the object's date and time as a rational number of seconds
|
|
483
|
+
# since the Epoch (January 1, 1970 00:00 UTC).
|
|
484
|
+
#
|
|
485
|
+
# Time.zone.now.to_r # => (708854548642709/500000)
|
|
486
|
+
def to_r
|
|
487
|
+
utc.to_r
|
|
488
|
+
end
|
|
489
|
+
|
|
490
|
+
# Returns an instance of DateTime with the timezone's UTC offset
|
|
491
|
+
#
|
|
492
|
+
# Time.zone.now.to_datetime # => Tue, 18 Aug 2015 02:32:20 +0000
|
|
493
|
+
# Time.current.in_time_zone('Hawaii').to_datetime # => Mon, 17 Aug 2015 16:32:20 -1000
|
|
494
|
+
def to_datetime
|
|
495
|
+
@to_datetime ||= utc.to_datetime.new_offset(Rational(utc_offset, 86_400))
|
|
496
|
+
end
|
|
497
|
+
|
|
498
|
+
# Returns an instance of +Time+, either with the same timezone as +self+,
|
|
499
|
+
# with the same UTC offset as +self+ or in the local system timezone
|
|
500
|
+
# depending on the setting of +ActiveSupport.to_time_preserves_timezone+.
|
|
501
|
+
def to_time
|
|
502
|
+
@to_time_with_timezone ||= getlocal(time_zone)
|
|
503
|
+
end
|
|
504
|
+
|
|
505
|
+
# So that +self+ <tt>acts_like?(:time)</tt>.
|
|
506
|
+
def acts_like_time?
|
|
507
|
+
true
|
|
508
|
+
end
|
|
509
|
+
|
|
510
|
+
# Say we're a Time to thwart type checking.
|
|
511
|
+
def is_a?(klass)
|
|
512
|
+
klass == ::Time || super
|
|
513
|
+
end
|
|
514
|
+
alias_method :kind_of?, :is_a?
|
|
515
|
+
|
|
516
|
+
# An instance of ActiveSupport::TimeWithZone is never blank
|
|
517
|
+
def blank?
|
|
518
|
+
false
|
|
519
|
+
end
|
|
520
|
+
|
|
521
|
+
def present? # :nodoc:
|
|
522
|
+
true
|
|
523
|
+
end
|
|
524
|
+
|
|
525
|
+
def freeze
|
|
526
|
+
# preload instance variables before freezing
|
|
527
|
+
period; utc; time; to_datetime; to_time
|
|
528
|
+
super
|
|
529
|
+
end
|
|
530
|
+
|
|
531
|
+
def marshal_dump
|
|
532
|
+
[utc, time_zone.name, time]
|
|
533
|
+
end
|
|
534
|
+
|
|
535
|
+
def marshal_load(variables)
|
|
536
|
+
initialize(variables[0].utc, ::Time.find_zone(variables[1]), variables[2].utc)
|
|
537
|
+
end
|
|
538
|
+
|
|
539
|
+
# respond_to_missing? is not called in some cases, such as when type conversion is
|
|
540
|
+
# performed with Kernel#String
|
|
541
|
+
def respond_to?(sym, include_priv = false)
|
|
542
|
+
# ensure that we're not going to throw and rescue from NoMethodError in method_missing which is slow
|
|
543
|
+
return false if sym.to_sym == :to_str
|
|
544
|
+
super
|
|
545
|
+
end
|
|
546
|
+
|
|
547
|
+
# Ensure proxy class responds to all methods that underlying time instance
|
|
548
|
+
# responds to.
|
|
549
|
+
def respond_to_missing?(sym, include_priv)
|
|
550
|
+
time.respond_to?(sym, include_priv)
|
|
551
|
+
end
|
|
552
|
+
|
|
553
|
+
# Send the missing method to +time+ instance, and wrap result in a new
|
|
554
|
+
# TimeWithZone with the existing +time_zone+.
|
|
555
|
+
def method_missing(...)
|
|
556
|
+
wrap_with_time_zone time.__send__(...)
|
|
557
|
+
rescue NoMethodError => e
|
|
558
|
+
raise e, e.message.sub(time.inspect, inspect).sub("Time", "ActiveSupport::TimeWithZone"), e.backtrace
|
|
559
|
+
end
|
|
560
|
+
|
|
561
|
+
private
|
|
562
|
+
SECONDS_PER_DAY = 86400
|
|
563
|
+
|
|
564
|
+
def incorporate_utc_offset(time, offset)
|
|
565
|
+
if offset.zero?
|
|
566
|
+
time
|
|
567
|
+
elsif time.kind_of?(Date)
|
|
568
|
+
time + Rational(offset, SECONDS_PER_DAY)
|
|
569
|
+
else
|
|
570
|
+
time + offset
|
|
571
|
+
end
|
|
572
|
+
end
|
|
573
|
+
|
|
574
|
+
def get_period_and_ensure_valid_local_time(period)
|
|
575
|
+
# we don't want a Time.local instance enforcing its own DST rules as well,
|
|
576
|
+
# so transfer time values to a utc constructor if necessary
|
|
577
|
+
@time = transfer_time_values_to_utc_constructor(@time) unless @time.utc?
|
|
578
|
+
begin
|
|
579
|
+
period || @time_zone.period_for_local(@time)
|
|
580
|
+
rescue ::TZInfo::PeriodNotFound
|
|
581
|
+
# time is in the "spring forward" hour gap, so we're moving the time forward one hour and trying again
|
|
582
|
+
@time += 1.hour
|
|
583
|
+
retry
|
|
584
|
+
end
|
|
585
|
+
end
|
|
586
|
+
|
|
587
|
+
def transfer_time_values_to_utc_constructor(time)
|
|
588
|
+
# avoid creating another Time object if possible
|
|
589
|
+
return time if time.instance_of?(::Time) && time.utc?
|
|
590
|
+
::Time.utc(time.year, time.month, time.day, time.hour, time.min, time.sec + time.subsec)
|
|
591
|
+
end
|
|
592
|
+
|
|
593
|
+
def duration_of_variable_length?(obj)
|
|
594
|
+
ActiveSupport::Duration === obj && obj.variable?
|
|
595
|
+
end
|
|
596
|
+
|
|
597
|
+
def wrap_with_time_zone(time)
|
|
598
|
+
if time.acts_like?(:time)
|
|
599
|
+
periods = time_zone.periods_for_local(time)
|
|
600
|
+
self.class.new(nil, time_zone, time, periods.include?(period) ? period : nil)
|
|
601
|
+
elsif time.is_a?(Range)
|
|
602
|
+
wrap_with_time_zone(time.begin)..wrap_with_time_zone(time.end)
|
|
603
|
+
else
|
|
604
|
+
time
|
|
605
|
+
end
|
|
606
|
+
end
|
|
607
|
+
end
|
|
608
|
+
end
|
|
609
|
+
|
|
610
|
+
# These prevent Psych from calling `ActiveSupport::TimeWithZone.name`
|
|
611
|
+
# and triggering the deprecation warning about the change in Rails 7.1.
|
|
612
|
+
YAML.load_tags["!ruby/object:ActiveSupport::TimeWithZone"] = "ActiveSupport::TimeWithZone"
|
|
613
|
+
YAML.dump_tags[ActiveSupport::TimeWithZone] = "!ruby/object:ActiveSupport::TimeWithZone"
|