activesupport 4.2.11.1 → 6.0.3.1

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.

Potentially problematic release.


This version of activesupport might be problematic. Click here for more details.

Files changed (263) hide show
  1. checksums.yaml +4 -4
  2. data/CHANGELOG.md +399 -411
  3. data/MIT-LICENSE +2 -2
  4. data/README.rdoc +7 -7
  5. data/lib/active_support/actionable_error.rb +48 -0
  6. data/lib/active_support/all.rb +5 -3
  7. data/lib/active_support/array_inquirer.rb +48 -0
  8. data/lib/active_support/backtrace_cleaner.rb +34 -6
  9. data/lib/active_support/benchmarkable.rb +6 -4
  10. data/lib/active_support/builder.rb +3 -1
  11. data/lib/active_support/cache/file_store.rb +58 -53
  12. data/lib/active_support/cache/mem_cache_store.rb +95 -91
  13. data/lib/active_support/cache/memory_store.rb +39 -36
  14. data/lib/active_support/cache/null_store.rb +11 -7
  15. data/lib/active_support/cache/redis_cache_store.rb +493 -0
  16. data/lib/active_support/cache/strategy/local_cache.rb +75 -42
  17. data/lib/active_support/cache/strategy/local_cache_middleware.rb +10 -9
  18. data/lib/active_support/cache.rb +331 -217
  19. data/lib/active_support/callbacks.rb +650 -592
  20. data/lib/active_support/concern.rb +35 -6
  21. data/lib/active_support/concurrency/load_interlock_aware_monitor.rb +35 -0
  22. data/lib/active_support/concurrency/share_lock.rb +226 -0
  23. data/lib/active_support/configurable.rb +13 -14
  24. data/lib/active_support/core_ext/array/access.rb +41 -1
  25. data/lib/active_support/core_ext/array/conversions.rb +24 -20
  26. data/lib/active_support/core_ext/array/extract.rb +21 -0
  27. data/lib/active_support/core_ext/array/extract_options.rb +2 -0
  28. data/lib/active_support/core_ext/array/grouping.rb +11 -18
  29. data/lib/active_support/core_ext/array/inquiry.rb +19 -0
  30. data/lib/active_support/core_ext/array/prepend_and_append.rb +4 -6
  31. data/lib/active_support/core_ext/array/wrap.rb +7 -4
  32. data/lib/active_support/core_ext/array.rb +9 -6
  33. data/lib/active_support/core_ext/benchmark.rb +3 -1
  34. data/lib/active_support/core_ext/big_decimal/conversions.rb +10 -12
  35. data/lib/active_support/core_ext/big_decimal.rb +3 -1
  36. data/lib/active_support/core_ext/class/attribute.rb +45 -31
  37. data/lib/active_support/core_ext/class/attribute_accessors.rb +3 -1
  38. data/lib/active_support/core_ext/class/subclasses.rb +20 -6
  39. data/lib/active_support/core_ext/class.rb +4 -3
  40. data/lib/active_support/core_ext/date/acts_like.rb +3 -1
  41. data/lib/active_support/core_ext/date/blank.rb +14 -0
  42. data/lib/active_support/core_ext/date/calculations.rb +17 -14
  43. data/lib/active_support/core_ext/date/conversions.rb +25 -23
  44. data/lib/active_support/core_ext/date/zones.rb +4 -2
  45. data/lib/active_support/core_ext/date.rb +6 -4
  46. data/lib/active_support/core_ext/date_and_time/calculations.rb +154 -65
  47. data/lib/active_support/core_ext/date_and_time/compatibility.rb +4 -3
  48. data/lib/active_support/core_ext/date_and_time/zones.rb +12 -13
  49. data/lib/active_support/core_ext/date_time/acts_like.rb +4 -2
  50. data/lib/active_support/core_ext/date_time/blank.rb +14 -0
  51. data/lib/active_support/core_ext/date_time/calculations.rb +37 -19
  52. data/lib/active_support/core_ext/date_time/compatibility.rb +8 -6
  53. data/lib/active_support/core_ext/date_time/conversions.rb +16 -13
  54. data/lib/active_support/core_ext/date_time.rb +7 -5
  55. data/lib/active_support/core_ext/digest/uuid.rb +7 -5
  56. data/lib/active_support/core_ext/digest.rb +3 -0
  57. data/lib/active_support/core_ext/enumerable.rb +114 -22
  58. data/lib/active_support/core_ext/file/atomic.rb +38 -31
  59. data/lib/active_support/core_ext/file.rb +3 -1
  60. data/lib/active_support/core_ext/hash/compact.rb +4 -23
  61. data/lib/active_support/core_ext/hash/conversions.rb +62 -41
  62. data/lib/active_support/core_ext/hash/deep_merge.rb +9 -13
  63. data/lib/active_support/core_ext/hash/deep_transform_values.rb +46 -0
  64. data/lib/active_support/core_ext/hash/except.rb +12 -9
  65. data/lib/active_support/core_ext/hash/indifferent_access.rb +4 -3
  66. data/lib/active_support/core_ext/hash/keys.rb +19 -42
  67. data/lib/active_support/core_ext/hash/reverse_merge.rb +5 -2
  68. data/lib/active_support/core_ext/hash/slice.rb +5 -27
  69. data/lib/active_support/core_ext/hash/transform_values.rb +4 -22
  70. data/lib/active_support/core_ext/hash.rb +10 -9
  71. data/lib/active_support/core_ext/integer/inflections.rb +3 -1
  72. data/lib/active_support/core_ext/integer/multiple.rb +3 -1
  73. data/lib/active_support/core_ext/integer/time.rb +11 -18
  74. data/lib/active_support/core_ext/integer.rb +5 -3
  75. data/lib/active_support/core_ext/kernel/concern.rb +5 -1
  76. data/lib/active_support/core_ext/kernel/reporting.rb +4 -84
  77. data/lib/active_support/core_ext/kernel/singleton_class.rb +2 -0
  78. data/lib/active_support/core_ext/kernel.rb +5 -5
  79. data/lib/active_support/core_ext/load_error.rb +3 -22
  80. data/lib/active_support/core_ext/marshal.rb +8 -8
  81. data/lib/active_support/core_ext/module/aliasing.rb +6 -44
  82. data/lib/active_support/core_ext/module/anonymous.rb +12 -1
  83. data/lib/active_support/core_ext/module/attr_internal.rb +8 -9
  84. data/lib/active_support/core_ext/module/attribute_accessors.rb +46 -46
  85. data/lib/active_support/core_ext/module/attribute_accessors_per_thread.rb +144 -0
  86. data/lib/active_support/core_ext/module/concerning.rb +11 -12
  87. data/lib/active_support/core_ext/module/delegation.rb +133 -30
  88. data/lib/active_support/core_ext/module/deprecation.rb +4 -2
  89. data/lib/active_support/core_ext/module/introspection.rb +44 -19
  90. data/lib/active_support/core_ext/module/reachable.rb +5 -7
  91. data/lib/active_support/core_ext/module/redefine_method.rb +40 -0
  92. data/lib/active_support/core_ext/module/remove_method.rb +8 -3
  93. data/lib/active_support/core_ext/module.rb +13 -11
  94. data/lib/active_support/core_ext/name_error.rb +22 -2
  95. data/lib/active_support/core_ext/numeric/bytes.rb +22 -0
  96. data/lib/active_support/core_ext/numeric/conversions.rb +129 -136
  97. data/lib/active_support/core_ext/numeric/inquiry.rb +5 -0
  98. data/lib/active_support/core_ext/numeric/time.rb +35 -23
  99. data/lib/active_support/core_ext/numeric.rb +5 -3
  100. data/lib/active_support/core_ext/object/acts_like.rb +12 -1
  101. data/lib/active_support/core_ext/object/blank.rb +27 -3
  102. data/lib/active_support/core_ext/object/conversions.rb +6 -4
  103. data/lib/active_support/core_ext/object/deep_dup.rb +13 -4
  104. data/lib/active_support/core_ext/object/duplicable.rb +13 -93
  105. data/lib/active_support/core_ext/object/inclusion.rb +5 -3
  106. data/lib/active_support/core_ext/object/instance_variables.rb +3 -1
  107. data/lib/active_support/core_ext/object/json.rb +51 -20
  108. data/lib/active_support/core_ext/object/to_param.rb +3 -1
  109. data/lib/active_support/core_ext/object/to_query.rb +10 -5
  110. data/lib/active_support/core_ext/object/try.rb +81 -23
  111. data/lib/active_support/core_ext/object/with_options.rb +16 -3
  112. data/lib/active_support/core_ext/object.rb +14 -13
  113. data/lib/active_support/core_ext/range/compare_range.rb +76 -0
  114. data/lib/active_support/core_ext/range/conversions.rb +37 -15
  115. data/lib/active_support/core_ext/range/each.rb +18 -17
  116. data/lib/active_support/core_ext/range/include_range.rb +7 -21
  117. data/lib/active_support/core_ext/range/include_time_with_zone.rb +23 -0
  118. data/lib/active_support/core_ext/range/overlaps.rb +2 -0
  119. data/lib/active_support/core_ext/range.rb +7 -4
  120. data/lib/active_support/core_ext/regexp.rb +2 -0
  121. data/lib/active_support/core_ext/securerandom.rb +45 -0
  122. data/lib/active_support/core_ext/string/access.rb +16 -6
  123. data/lib/active_support/core_ext/string/behavior.rb +3 -1
  124. data/lib/active_support/core_ext/string/conversions.rb +7 -4
  125. data/lib/active_support/core_ext/string/exclude.rb +2 -0
  126. data/lib/active_support/core_ext/string/filters.rb +48 -6
  127. data/lib/active_support/core_ext/string/indent.rb +6 -4
  128. data/lib/active_support/core_ext/string/inflections.rb +66 -24
  129. data/lib/active_support/core_ext/string/inquiry.rb +3 -1
  130. data/lib/active_support/core_ext/string/multibyte.rb +16 -7
  131. data/lib/active_support/core_ext/string/output_safety.rb +93 -40
  132. data/lib/active_support/core_ext/string/starts_ends_with.rb +2 -0
  133. data/lib/active_support/core_ext/string/strip.rb +6 -5
  134. data/lib/active_support/core_ext/string/zones.rb +4 -2
  135. data/lib/active_support/core_ext/string.rb +15 -13
  136. data/lib/active_support/core_ext/time/acts_like.rb +3 -1
  137. data/lib/active_support/core_ext/time/calculations.rb +115 -52
  138. data/lib/active_support/core_ext/time/compatibility.rb +4 -2
  139. data/lib/active_support/core_ext/time/conversions.rb +20 -13
  140. data/lib/active_support/core_ext/time/zones.rb +41 -7
  141. data/lib/active_support/core_ext/time.rb +7 -6
  142. data/lib/active_support/core_ext/uri.rb +6 -7
  143. data/lib/active_support/core_ext.rb +3 -1
  144. data/lib/active_support/current_attributes.rb +203 -0
  145. data/lib/active_support/dependencies/autoload.rb +2 -0
  146. data/lib/active_support/dependencies/interlock.rb +57 -0
  147. data/lib/active_support/dependencies/zeitwerk_integration.rb +117 -0
  148. data/lib/active_support/dependencies.rb +208 -166
  149. data/lib/active_support/deprecation/behaviors.rb +44 -11
  150. data/lib/active_support/deprecation/constant_accessor.rb +52 -0
  151. data/lib/active_support/deprecation/instance_delegator.rb +17 -2
  152. data/lib/active_support/deprecation/method_wrappers.rb +61 -21
  153. data/lib/active_support/deprecation/proxy_wrappers.rb +81 -30
  154. data/lib/active_support/deprecation/reporting.rb +32 -12
  155. data/lib/active_support/deprecation.rb +12 -9
  156. data/lib/active_support/descendants_tracker.rb +57 -9
  157. data/lib/active_support/digest.rb +20 -0
  158. data/lib/active_support/duration/iso8601_parser.rb +123 -0
  159. data/lib/active_support/duration/iso8601_serializer.rb +53 -0
  160. data/lib/active_support/duration.rb +315 -40
  161. data/lib/active_support/encrypted_configuration.rb +45 -0
  162. data/lib/active_support/encrypted_file.rb +100 -0
  163. data/lib/active_support/evented_file_update_checker.rb +234 -0
  164. data/lib/active_support/execution_wrapper.rb +129 -0
  165. data/lib/active_support/executor.rb +8 -0
  166. data/lib/active_support/file_update_checker.rb +62 -37
  167. data/lib/active_support/gem_version.rb +6 -4
  168. data/lib/active_support/gzip.rb +7 -5
  169. data/lib/active_support/hash_with_indifferent_access.rb +129 -30
  170. data/lib/active_support/i18n.rb +9 -6
  171. data/lib/active_support/i18n_railtie.rb +50 -14
  172. data/lib/active_support/inflections.rb +13 -11
  173. data/lib/active_support/inflector/inflections.rb +58 -13
  174. data/lib/active_support/inflector/methods.rb +159 -145
  175. data/lib/active_support/inflector/transliterate.rb +84 -34
  176. data/lib/active_support/inflector.rb +7 -5
  177. data/lib/active_support/json/decoding.rb +32 -30
  178. data/lib/active_support/json/encoding.rb +17 -60
  179. data/lib/active_support/json.rb +4 -2
  180. data/lib/active_support/key_generator.rb +11 -43
  181. data/lib/active_support/lazy_load_hooks.rb +53 -20
  182. data/lib/active_support/locale/en.rb +33 -0
  183. data/lib/active_support/locale/en.yml +2 -0
  184. data/lib/active_support/log_subscriber/test_helper.rb +14 -12
  185. data/lib/active_support/log_subscriber.rb +44 -19
  186. data/lib/active_support/logger.rb +9 -23
  187. data/lib/active_support/logger_silence.rb +32 -14
  188. data/lib/active_support/logger_thread_safe_level.rb +32 -8
  189. data/lib/active_support/message_encryptor.rb +166 -53
  190. data/lib/active_support/message_verifier.rb +149 -16
  191. data/lib/active_support/messages/metadata.rb +72 -0
  192. data/lib/active_support/messages/rotation_configuration.rb +22 -0
  193. data/lib/active_support/messages/rotator.rb +56 -0
  194. data/lib/active_support/multibyte/chars.rb +56 -63
  195. data/lib/active_support/multibyte/unicode.rb +56 -290
  196. data/lib/active_support/multibyte.rb +4 -2
  197. data/lib/active_support/notifications/fanout.rb +109 -22
  198. data/lib/active_support/notifications/instrumenter.rb +107 -16
  199. data/lib/active_support/notifications.rb +51 -10
  200. data/lib/active_support/number_helper/number_converter.rb +16 -15
  201. data/lib/active_support/number_helper/number_to_currency_converter.rb +14 -15
  202. data/lib/active_support/number_helper/number_to_delimited_converter.rb +11 -4
  203. data/lib/active_support/number_helper/number_to_human_converter.rb +13 -10
  204. data/lib/active_support/number_helper/number_to_human_size_converter.rb +11 -9
  205. data/lib/active_support/number_helper/number_to_percentage_converter.rb +5 -1
  206. data/lib/active_support/number_helper/number_to_phone_converter.rb +15 -5
  207. data/lib/active_support/number_helper/number_to_rounded_converter.rb +25 -57
  208. data/lib/active_support/number_helper/rounding_helper.rb +66 -0
  209. data/lib/active_support/number_helper.rb +105 -68
  210. data/lib/active_support/option_merger.rb +24 -4
  211. data/lib/active_support/ordered_hash.rb +7 -5
  212. data/lib/active_support/ordered_options.rb +27 -5
  213. data/lib/active_support/parameter_filter.rb +128 -0
  214. data/lib/active_support/per_thread_registry.rb +9 -4
  215. data/lib/active_support/proxy_object.rb +2 -0
  216. data/lib/active_support/rails.rb +10 -8
  217. data/lib/active_support/railtie.rb +43 -9
  218. data/lib/active_support/reloader.rb +130 -0
  219. data/lib/active_support/rescuable.rb +108 -53
  220. data/lib/active_support/security_utils.rb +15 -11
  221. data/lib/active_support/string_inquirer.rb +11 -4
  222. data/lib/active_support/subscriber.rb +74 -30
  223. data/lib/active_support/tagged_logging.rb +25 -13
  224. data/lib/active_support/test_case.rb +107 -44
  225. data/lib/active_support/testing/assertions.rb +151 -20
  226. data/lib/active_support/testing/autorun.rb +4 -2
  227. data/lib/active_support/testing/constant_lookup.rb +2 -1
  228. data/lib/active_support/testing/declarative.rb +3 -1
  229. data/lib/active_support/testing/deprecation.rb +13 -10
  230. data/lib/active_support/testing/file_fixtures.rb +38 -0
  231. data/lib/active_support/testing/isolation.rb +35 -26
  232. data/lib/active_support/testing/method_call_assertions.rb +70 -0
  233. data/lib/active_support/testing/parallelization.rb +134 -0
  234. data/lib/active_support/testing/setup_and_teardown.rb +13 -8
  235. data/lib/active_support/testing/stream.rb +43 -0
  236. data/lib/active_support/testing/tagged_logging.rb +3 -1
  237. data/lib/active_support/testing/time_helpers.rb +84 -20
  238. data/lib/active_support/time.rb +14 -12
  239. data/lib/active_support/time_with_zone.rb +179 -39
  240. data/lib/active_support/values/time_zone.rb +203 -63
  241. data/lib/active_support/version.rb +3 -1
  242. data/lib/active_support/xml_mini/jdom.rb +116 -115
  243. data/lib/active_support/xml_mini/libxml.rb +16 -13
  244. data/lib/active_support/xml_mini/libxmlsax.rb +15 -14
  245. data/lib/active_support/xml_mini/nokogiri.rb +14 -12
  246. data/lib/active_support/xml_mini/nokogirisax.rb +14 -13
  247. data/lib/active_support/xml_mini/rexml.rb +11 -9
  248. data/lib/active_support/xml_mini.rb +38 -46
  249. data/lib/active_support.rb +13 -11
  250. metadata +84 -26
  251. data/lib/active_support/concurrency/latch.rb +0 -27
  252. data/lib/active_support/core_ext/big_decimal/yaml_conversions.rb +0 -16
  253. data/lib/active_support/core_ext/class/delegating_attributes.rb +0 -45
  254. data/lib/active_support/core_ext/date_time/zones.rb +0 -6
  255. data/lib/active_support/core_ext/kernel/agnostics.rb +0 -11
  256. data/lib/active_support/core_ext/kernel/debugger.rb +0 -10
  257. data/lib/active_support/core_ext/module/method_transplanting.rb +0 -13
  258. data/lib/active_support/core_ext/module/qualified_const.rb +0 -52
  259. data/lib/active_support/core_ext/object/itself.rb +0 -15
  260. data/lib/active_support/core_ext/struct.rb +0 -6
  261. data/lib/active_support/core_ext/thread.rb +0 -86
  262. data/lib/active_support/core_ext/time/marshal.rb +0 -30
  263. data/lib/active_support/values/unicode_tables.dat +0 -0
@@ -1,13 +1,13 @@
1
- require 'tzinfo'
2
- require 'thread_safe'
3
- require 'active_support/core_ext/object/blank'
4
- require 'active_support/core_ext/object/try'
1
+ # frozen_string_literal: true
2
+
3
+ require "tzinfo"
4
+ require "concurrent/map"
5
5
 
6
6
  module ActiveSupport
7
7
  # The TimeZone class serves as a wrapper around TZInfo::Timezone instances.
8
8
  # It allows us to do the following:
9
9
  #
10
- # * Limit the set of zones provided by TZInfo to a meaningful subset of 146
10
+ # * Limit the set of zones provided by TZInfo to a meaningful subset of 134
11
11
  # zones.
12
12
  # * Retrieve and display zones with a friendlier name
13
13
  # (e.g., "Eastern Time (US & Canada)" instead of "America/New_York").
@@ -23,19 +23,13 @@ module ActiveSupport
23
23
  # config.time_zone = 'Eastern Time (US & Canada)'
24
24
  # end
25
25
  #
26
- # Time.zone # => #<TimeZone:0x514834...>
26
+ # Time.zone # => #<ActiveSupport::TimeZone:0x514834...>
27
27
  # Time.zone.name # => "Eastern Time (US & Canada)"
28
28
  # Time.zone.now # => Sun, 18 May 2008 14:30:44 EDT -04:00
29
- #
30
- # The version of TZInfo bundled with Active Support only includes the
31
- # definitions necessary to support the zones defined by the TimeZone class.
32
- # If you need to use zones that aren't defined by TimeZone, you'll need to
33
- # install the TZInfo gem (if a recent version of the gem is installed locally,
34
- # this will be used instead of the bundled version.)
35
29
  class TimeZone
36
30
  # Keys are Rails TimeZone names, values are TZInfo identifiers.
37
31
  MAPPING = {
38
- "International Date Line West" => "Pacific/Midway",
32
+ "International Date Line West" => "Etc/GMT+12",
39
33
  "Midway Island" => "Pacific/Midway",
40
34
  "American Samoa" => "Pacific/Pago_Pago",
41
35
  "Hawaii" => "Pacific/Honolulu",
@@ -66,6 +60,7 @@ module ActiveSupport
66
60
  "Buenos Aires" => "America/Argentina/Buenos_Aires",
67
61
  "Montevideo" => "America/Montevideo",
68
62
  "Georgetown" => "America/Guyana",
63
+ "Puerto Rico" => "America/Puerto_Rico",
69
64
  "Greenland" => "America/Godthab",
70
65
  "Mid-Atlantic" => "Atlantic/South_Georgia",
71
66
  "Azores" => "Atlantic/Azores",
@@ -92,7 +87,8 @@ module ActiveSupport
92
87
  "Paris" => "Europe/Paris",
93
88
  "Amsterdam" => "Europe/Amsterdam",
94
89
  "Berlin" => "Europe/Berlin",
95
- "Bern" => "Europe/Berlin",
90
+ "Bern" => "Europe/Zurich",
91
+ "Zurich" => "Europe/Zurich",
96
92
  "Rome" => "Europe/Rome",
97
93
  "Stockholm" => "Europe/Stockholm",
98
94
  "Vienna" => "Europe/Vienna",
@@ -186,26 +182,28 @@ module ActiveSupport
186
182
  "Samoa" => "Pacific/Apia"
187
183
  }
188
184
 
189
- UTC_OFFSET_WITH_COLON = '%s%02d:%02d'
190
- UTC_OFFSET_WITHOUT_COLON = UTC_OFFSET_WITH_COLON.tr(':', '')
185
+ UTC_OFFSET_WITH_COLON = "%s%02d:%02d" # :nodoc:
186
+ UTC_OFFSET_WITHOUT_COLON = UTC_OFFSET_WITH_COLON.tr(":", "") # :nodoc:
187
+ private_constant :UTC_OFFSET_WITH_COLON, :UTC_OFFSET_WITHOUT_COLON
191
188
 
192
- @lazy_zones_map = ThreadSafe::Cache.new
189
+ @lazy_zones_map = Concurrent::Map.new
190
+ @country_zones = Concurrent::Map.new
193
191
 
194
192
  class << self
195
193
  # Assumes self represents an offset from UTC in seconds (as returned from
196
194
  # Time#utc_offset) and turns this into an +HH:MM formatted string.
197
195
  #
198
- # TimeZone.seconds_to_utc_offset(-21_600) # => "-06:00"
196
+ # ActiveSupport::TimeZone.seconds_to_utc_offset(-21_600) # => "-06:00"
199
197
  def seconds_to_utc_offset(seconds, colon = true)
200
198
  format = colon ? UTC_OFFSET_WITH_COLON : UTC_OFFSET_WITHOUT_COLON
201
- sign = (seconds < 0 ? '-' : '+')
199
+ sign = (seconds < 0 ? "-" : "+")
202
200
  hours = seconds.abs / 3600
203
201
  minutes = (seconds.abs % 3600) / 60
204
202
  format % [sign, hours, minutes]
205
203
  end
206
204
 
207
205
  def find_tzinfo(name)
208
- TZInfo::TimezoneProxy.new(MAPPING[name] || name)
206
+ TZInfo::Timezone.new(MAPPING[name] || name)
209
207
  end
210
208
 
211
209
  alias_method :create, :new
@@ -224,13 +222,6 @@ module ActiveSupport
224
222
  @zones ||= zones_map.values.sort
225
223
  end
226
224
 
227
- def zones_map #:nodoc:
228
- @zones_map ||= begin
229
- MAPPING.each_key {|place| self[place]} # load all the zones
230
- @lazy_zones_map
231
- end
232
- end
233
-
234
225
  # Locate a specific time zone object. If the argument is a string, it
235
226
  # is interpreted to mean the name of the timezone to locate. If it is a
236
227
  # numeric value it is either the hour offset, or the second offset, of the
@@ -238,25 +229,61 @@ module ActiveSupport
238
229
  # Returns +nil+ if no such time zone is known to the system.
239
230
  def [](arg)
240
231
  case arg
241
- when String
232
+ when String
242
233
  begin
243
- @lazy_zones_map[arg] ||= create(arg).tap { |tz| tz.utc_offset }
234
+ @lazy_zones_map[arg] ||= create(arg)
244
235
  rescue TZInfo::InvalidTimezoneIdentifier
245
236
  nil
246
237
  end
247
- when Numeric, ActiveSupport::Duration
248
- arg *= 3600 if arg.abs <= 13
249
- all.find { |z| z.utc_offset == arg.to_i }
250
- else
251
- raise ArgumentError, "invalid argument to TimeZone[]: #{arg.inspect}"
238
+ when Numeric, ActiveSupport::Duration
239
+ arg *= 3600 if arg.abs <= 13
240
+ all.find { |z| z.utc_offset == arg.to_i }
241
+ else
242
+ raise ArgumentError, "invalid argument to TimeZone[]: #{arg.inspect}"
252
243
  end
253
244
  end
254
245
 
255
246
  # A convenience method for returning a collection of TimeZone objects
256
247
  # for time zones in the USA.
257
248
  def us_zones
258
- @us_zones ||= all.find_all { |z| z.name =~ /US|Arizona|Indiana|Hawaii|Alaska/ }
249
+ country_zones(:us)
259
250
  end
251
+
252
+ # A convenience method for returning a collection of TimeZone objects
253
+ # for time zones in the country specified by its ISO 3166-1 Alpha2 code.
254
+ def country_zones(country_code)
255
+ code = country_code.to_s.upcase
256
+ @country_zones[code] ||= load_country_zones(code)
257
+ end
258
+
259
+ def clear #:nodoc:
260
+ @lazy_zones_map = Concurrent::Map.new
261
+ @country_zones = Concurrent::Map.new
262
+ @zones = nil
263
+ @zones_map = nil
264
+ end
265
+
266
+ private
267
+ def load_country_zones(code)
268
+ country = TZInfo::Country.get(code)
269
+ country.zone_identifiers.flat_map do |tz_id|
270
+ if MAPPING.value?(tz_id)
271
+ MAPPING.inject([]) do |memo, (key, value)|
272
+ memo << self[key] if value == tz_id
273
+ memo
274
+ end
275
+ else
276
+ create(tz_id, nil, TZInfo::Timezone.new(tz_id))
277
+ end
278
+ end.sort!
279
+ end
280
+
281
+ def zones_map
282
+ @zones_map ||= MAPPING.each_with_object({}) do |(name, _), zones|
283
+ timezone = self[name]
284
+ zones[name] = timezone if timezone
285
+ end
286
+ end
260
287
  end
261
288
 
262
289
  include Comparable
@@ -273,10 +300,6 @@ module ActiveSupport
273
300
  @tzinfo = tzinfo || TimeZone.find_tzinfo(name)
274
301
  end
275
302
 
276
- def init_with(coder) #:nodoc:
277
- initialize(coder['name'])
278
- end
279
-
280
303
  # Returns the offset of this time zone from UTC in seconds.
281
304
  def utc_offset
282
305
  if @utc_offset
@@ -286,9 +309,13 @@ module ActiveSupport
286
309
  end
287
310
  end
288
311
 
289
- # Returns the offset of this time zone as a formatted string, of the
290
- # format "+HH:MM".
291
- def formatted_offset(colon=true, alternate_utc_string = nil)
312
+ # Returns a formatted string of the offset from UTC, or an alternative
313
+ # string if the time zone is already UTC.
314
+ #
315
+ # zone = ActiveSupport::TimeZone['Central Time (US & Canada)']
316
+ # zone.formatted_offset # => "-06:00"
317
+ # zone.formatted_offset(false) # => "-0600"
318
+ def formatted_offset(colon = true, alternate_utc_string = nil)
292
319
  utc_offset == 0 && alternate_utc_string || self.class.seconds_to_utc_offset(utc_offset, colon)
293
320
  end
294
321
 
@@ -328,8 +355,48 @@ module ActiveSupport
328
355
  # Time.zone = 'Hawaii' # => "Hawaii"
329
356
  # Time.utc(2000).to_f # => 946684800.0
330
357
  # Time.zone.at(946684800.0) # => Fri, 31 Dec 1999 14:00:00 HST -10:00
331
- def at(secs)
332
- Time.at(secs).utc.in_time_zone(self)
358
+ #
359
+ # A second argument can be supplied to specify sub-second precision.
360
+ #
361
+ # Time.zone = 'Hawaii' # => "Hawaii"
362
+ # Time.at(946684800, 123456.789).nsec # => 123456789
363
+ def at(*args)
364
+ Time.at(*args).utc.in_time_zone(self)
365
+ end
366
+
367
+ # Method for creating new ActiveSupport::TimeWithZone instance in time zone
368
+ # of +self+ from an ISO 8601 string.
369
+ #
370
+ # Time.zone = 'Hawaii' # => "Hawaii"
371
+ # Time.zone.iso8601('1999-12-31T14:00:00') # => Fri, 31 Dec 1999 14:00:00 HST -10:00
372
+ #
373
+ # If the time components are missing then they will be set to zero.
374
+ #
375
+ # Time.zone = 'Hawaii' # => "Hawaii"
376
+ # Time.zone.iso8601('1999-12-31') # => Fri, 31 Dec 1999 00:00:00 HST -10:00
377
+ #
378
+ # If the string is invalid then an +ArgumentError+ will be raised unlike +parse+
379
+ # which usually returns +nil+ when given an invalid date string.
380
+ def iso8601(str)
381
+ parts = Date._iso8601(str)
382
+
383
+ raise ArgumentError, "invalid date" if parts.empty?
384
+
385
+ time = Time.new(
386
+ parts.fetch(:year),
387
+ parts.fetch(:mon),
388
+ parts.fetch(:mday),
389
+ parts.fetch(:hour, 0),
390
+ parts.fetch(:min, 0),
391
+ parts.fetch(:sec, 0) + parts.fetch(:sec_fraction, 0),
392
+ parts.fetch(:offset, 0)
393
+ )
394
+
395
+ if parts[:offset]
396
+ TimeWithZone.new(time.utc, self)
397
+ else
398
+ TimeWithZone.new(nil, self, time)
399
+ end
333
400
  end
334
401
 
335
402
  # Method for creating new ActiveSupport::TimeWithZone instance in time zone
@@ -348,25 +415,64 @@ module ActiveSupport
348
415
  # components are supplied, then the day of the month defaults to 1:
349
416
  #
350
417
  # Time.zone.parse('Mar 2000') # => Wed, 01 Mar 2000 00:00:00 HST -10:00
351
- def parse(str, now=now())
352
- parts = Date._parse(str, false)
353
- return if parts.empty?
418
+ #
419
+ # If the string is invalid then an +ArgumentError+ could be raised.
420
+ def parse(str, now = now())
421
+ parts_to_time(Date._parse(str, false), now)
422
+ end
423
+
424
+ # Method for creating new ActiveSupport::TimeWithZone instance in time zone
425
+ # of +self+ from an RFC 3339 string.
426
+ #
427
+ # Time.zone = 'Hawaii' # => "Hawaii"
428
+ # Time.zone.rfc3339('2000-01-01T00:00:00Z') # => Fri, 31 Dec 1999 14:00:00 HST -10:00
429
+ #
430
+ # If the time or zone components are missing then an +ArgumentError+ will
431
+ # be raised. This is much stricter than either +parse+ or +iso8601+ which
432
+ # allow for missing components.
433
+ #
434
+ # Time.zone = 'Hawaii' # => "Hawaii"
435
+ # Time.zone.rfc3339('1999-12-31') # => ArgumentError: invalid date
436
+ def rfc3339(str)
437
+ parts = Date._rfc3339(str)
438
+
439
+ raise ArgumentError, "invalid date" if parts.empty?
354
440
 
355
441
  time = Time.new(
356
- parts.fetch(:year, now.year),
357
- parts.fetch(:mon, now.month),
358
- parts.fetch(:mday, parts[:year] || parts[:mon] ? 1 : now.day),
359
- parts.fetch(:hour, 0),
360
- parts.fetch(:min, 0),
361
- parts.fetch(:sec, 0) + parts.fetch(:sec_fraction, 0),
362
- parts.fetch(:offset, 0)
442
+ parts.fetch(:year),
443
+ parts.fetch(:mon),
444
+ parts.fetch(:mday),
445
+ parts.fetch(:hour),
446
+ parts.fetch(:min),
447
+ parts.fetch(:sec) + parts.fetch(:sec_fraction, 0),
448
+ parts.fetch(:offset)
363
449
  )
364
450
 
365
- if parts[:offset]
366
- TimeWithZone.new(time.utc, self)
367
- else
368
- TimeWithZone.new(nil, self, time)
369
- end
451
+ TimeWithZone.new(time.utc, self)
452
+ end
453
+
454
+ # Parses +str+ according to +format+ and returns an ActiveSupport::TimeWithZone.
455
+ #
456
+ # Assumes that +str+ is a time in the time zone +self+,
457
+ # unless +format+ includes an explicit time zone.
458
+ # (This is the same behavior as +parse+.)
459
+ # In either case, the returned TimeWithZone has the timezone of +self+.
460
+ #
461
+ # Time.zone = 'Hawaii' # => "Hawaii"
462
+ # Time.zone.strptime('1999-12-31 14:00:00', '%Y-%m-%d %H:%M:%S') # => Fri, 31 Dec 1999 14:00:00 HST -10:00
463
+ #
464
+ # If upper components are missing from the string, they are supplied from
465
+ # TimeZone#now:
466
+ #
467
+ # Time.zone.now # => Fri, 31 Dec 1999 14:00:00 HST -10:00
468
+ # Time.zone.strptime('22:30:00', '%H:%M:%S') # => Fri, 31 Dec 1999 22:30:00 HST -10:00
469
+ #
470
+ # However, if the date component is not provided, but any other upper
471
+ # components are supplied, then the day of the month defaults to 1:
472
+ #
473
+ # Time.zone.strptime('Mar 2000', '%b %Y') # => Wed, 01 Mar 2000 00:00:00 HST -10:00
474
+ def strptime(str, format, now = now())
475
+ parts_to_time(DateTime._strptime(str, format), now)
370
476
  end
371
477
 
372
478
  # Returns an ActiveSupport::TimeWithZone instance representing the current
@@ -378,7 +484,7 @@ module ActiveSupport
378
484
  time_now.utc.in_time_zone(self)
379
485
  end
380
486
 
381
- # Return the current date in this time zone.
487
+ # Returns the current date in this time zone.
382
488
  def today
383
489
  tzinfo.now.to_date
384
490
  end
@@ -402,7 +508,7 @@ module ActiveSupport
402
508
 
403
509
  # Adjust the given time to the simultaneous time in UTC. Returns a
404
510
  # Time.utc() instance.
405
- def local_to_utc(time, dst=true)
511
+ def local_to_utc(time, dst = true)
406
512
  tzinfo.local_to_utc(time, dst)
407
513
  end
408
514
 
@@ -414,15 +520,49 @@ module ActiveSupport
414
520
 
415
521
  # Available so that TimeZone instances respond like TZInfo::Timezone
416
522
  # instances.
417
- def period_for_local(time, dst=true)
418
- tzinfo.period_for_local(time, dst)
523
+ def period_for_local(time, dst = true)
524
+ tzinfo.period_for_local(time, dst) { |periods| periods.last }
419
525
  end
420
526
 
421
527
  def periods_for_local(time) #:nodoc:
422
528
  tzinfo.periods_for_local(time)
423
529
  end
424
530
 
531
+ def init_with(coder) #:nodoc:
532
+ initialize(coder["name"])
533
+ end
534
+
535
+ def encode_with(coder) #:nodoc:
536
+ coder.tag = "!ruby/object:#{self.class}"
537
+ coder.map = { "name" => tzinfo.name }
538
+ end
539
+
425
540
  private
541
+ def parts_to_time(parts, now)
542
+ raise ArgumentError, "invalid date" if parts.nil?
543
+ return if parts.empty?
544
+
545
+ if parts[:seconds]
546
+ time = Time.at(parts[:seconds])
547
+ else
548
+ time = Time.new(
549
+ parts.fetch(:year, now.year),
550
+ parts.fetch(:mon, now.month),
551
+ parts.fetch(:mday, parts[:year] || parts[:mon] ? 1 : now.day),
552
+ parts.fetch(:hour, 0),
553
+ parts.fetch(:min, 0),
554
+ parts.fetch(:sec, 0) + parts.fetch(:sec_fraction, 0),
555
+ parts.fetch(:offset, 0)
556
+ )
557
+ end
558
+
559
+ if parts[:offset] || parts[:seconds]
560
+ TimeWithZone.new(time.utc, self)
561
+ else
562
+ TimeWithZone.new(nil, self, time)
563
+ end
564
+ end
565
+
426
566
  def time_now
427
567
  Time.now
428
568
  end
@@ -1,4 +1,6 @@
1
- require_relative 'gem_version'
1
+ # frozen_string_literal: true
2
+
3
+ require_relative "gem_version"
2
4
 
3
5
  module ActiveSupport
4
6
  # Returns the version of the currently loaded ActiveSupport as a <tt>Gem::Version</tt>