omg-activesupport 8.0.0.alpha1

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