activesupport 6.0.0

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.
Files changed (250) hide show
  1. checksums.yaml +7 -0
  2. data/CHANGELOG.md +572 -0
  3. data/MIT-LICENSE +20 -0
  4. data/README.rdoc +40 -0
  5. data/lib/active_support.rb +96 -0
  6. data/lib/active_support/actionable_error.rb +48 -0
  7. data/lib/active_support/all.rb +5 -0
  8. data/lib/active_support/array_inquirer.rb +48 -0
  9. data/lib/active_support/backtrace_cleaner.rb +132 -0
  10. data/lib/active_support/benchmarkable.rb +51 -0
  11. data/lib/active_support/builder.rb +8 -0
  12. data/lib/active_support/cache.rb +830 -0
  13. data/lib/active_support/cache/file_store.rb +196 -0
  14. data/lib/active_support/cache/mem_cache_store.rb +212 -0
  15. data/lib/active_support/cache/memory_store.rb +174 -0
  16. data/lib/active_support/cache/null_store.rb +48 -0
  17. data/lib/active_support/cache/redis_cache_store.rb +488 -0
  18. data/lib/active_support/cache/strategy/local_cache.rb +194 -0
  19. data/lib/active_support/cache/strategy/local_cache_middleware.rb +45 -0
  20. data/lib/active_support/callbacks.rb +856 -0
  21. data/lib/active_support/concern.rb +171 -0
  22. data/lib/active_support/concurrency/load_interlock_aware_monitor.rb +17 -0
  23. data/lib/active_support/concurrency/share_lock.rb +227 -0
  24. data/lib/active_support/configurable.rb +146 -0
  25. data/lib/active_support/core_ext.rb +5 -0
  26. data/lib/active_support/core_ext/array.rb +9 -0
  27. data/lib/active_support/core_ext/array/access.rb +104 -0
  28. data/lib/active_support/core_ext/array/conversions.rb +213 -0
  29. data/lib/active_support/core_ext/array/extract.rb +21 -0
  30. data/lib/active_support/core_ext/array/extract_options.rb +31 -0
  31. data/lib/active_support/core_ext/array/grouping.rb +109 -0
  32. data/lib/active_support/core_ext/array/inquiry.rb +19 -0
  33. data/lib/active_support/core_ext/array/prepend_and_append.rb +5 -0
  34. data/lib/active_support/core_ext/array/wrap.rb +48 -0
  35. data/lib/active_support/core_ext/benchmark.rb +16 -0
  36. data/lib/active_support/core_ext/big_decimal.rb +3 -0
  37. data/lib/active_support/core_ext/big_decimal/conversions.rb +14 -0
  38. data/lib/active_support/core_ext/class.rb +4 -0
  39. data/lib/active_support/core_ext/class/attribute.rb +141 -0
  40. data/lib/active_support/core_ext/class/attribute_accessors.rb +6 -0
  41. data/lib/active_support/core_ext/class/subclasses.rb +54 -0
  42. data/lib/active_support/core_ext/date.rb +7 -0
  43. data/lib/active_support/core_ext/date/acts_like.rb +10 -0
  44. data/lib/active_support/core_ext/date/blank.rb +14 -0
  45. data/lib/active_support/core_ext/date/calculations.rb +146 -0
  46. data/lib/active_support/core_ext/date/conversions.rb +96 -0
  47. data/lib/active_support/core_ext/date/zones.rb +8 -0
  48. data/lib/active_support/core_ext/date_and_time/calculations.rb +351 -0
  49. data/lib/active_support/core_ext/date_and_time/compatibility.rb +16 -0
  50. data/lib/active_support/core_ext/date_and_time/zones.rb +41 -0
  51. data/lib/active_support/core_ext/date_time.rb +7 -0
  52. data/lib/active_support/core_ext/date_time/acts_like.rb +16 -0
  53. data/lib/active_support/core_ext/date_time/blank.rb +14 -0
  54. data/lib/active_support/core_ext/date_time/calculations.rb +211 -0
  55. data/lib/active_support/core_ext/date_time/compatibility.rb +18 -0
  56. data/lib/active_support/core_ext/date_time/conversions.rb +107 -0
  57. data/lib/active_support/core_ext/digest.rb +3 -0
  58. data/lib/active_support/core_ext/digest/uuid.rb +53 -0
  59. data/lib/active_support/core_ext/enumerable.rb +188 -0
  60. data/lib/active_support/core_ext/file.rb +3 -0
  61. data/lib/active_support/core_ext/file/atomic.rb +70 -0
  62. data/lib/active_support/core_ext/hash.rb +10 -0
  63. data/lib/active_support/core_ext/hash/compact.rb +5 -0
  64. data/lib/active_support/core_ext/hash/conversions.rb +263 -0
  65. data/lib/active_support/core_ext/hash/deep_merge.rb +34 -0
  66. data/lib/active_support/core_ext/hash/deep_transform_values.rb +46 -0
  67. data/lib/active_support/core_ext/hash/except.rb +24 -0
  68. data/lib/active_support/core_ext/hash/indifferent_access.rb +24 -0
  69. data/lib/active_support/core_ext/hash/keys.rb +143 -0
  70. data/lib/active_support/core_ext/hash/reverse_merge.rb +25 -0
  71. data/lib/active_support/core_ext/hash/slice.rb +26 -0
  72. data/lib/active_support/core_ext/hash/transform_values.rb +5 -0
  73. data/lib/active_support/core_ext/integer.rb +5 -0
  74. data/lib/active_support/core_ext/integer/inflections.rb +31 -0
  75. data/lib/active_support/core_ext/integer/multiple.rb +12 -0
  76. data/lib/active_support/core_ext/integer/time.rb +22 -0
  77. data/lib/active_support/core_ext/kernel.rb +5 -0
  78. data/lib/active_support/core_ext/kernel/concern.rb +14 -0
  79. data/lib/active_support/core_ext/kernel/reporting.rb +45 -0
  80. data/lib/active_support/core_ext/kernel/singleton_class.rb +8 -0
  81. data/lib/active_support/core_ext/load_error.rb +9 -0
  82. data/lib/active_support/core_ext/marshal.rb +24 -0
  83. data/lib/active_support/core_ext/module.rb +13 -0
  84. data/lib/active_support/core_ext/module/aliasing.rb +31 -0
  85. data/lib/active_support/core_ext/module/anonymous.rb +30 -0
  86. data/lib/active_support/core_ext/module/attr_internal.rb +38 -0
  87. data/lib/active_support/core_ext/module/attribute_accessors.rb +212 -0
  88. data/lib/active_support/core_ext/module/attribute_accessors_per_thread.rb +144 -0
  89. data/lib/active_support/core_ext/module/concerning.rb +134 -0
  90. data/lib/active_support/core_ext/module/delegation.rb +313 -0
  91. data/lib/active_support/core_ext/module/deprecation.rb +25 -0
  92. data/lib/active_support/core_ext/module/introspection.rb +86 -0
  93. data/lib/active_support/core_ext/module/reachable.rb +6 -0
  94. data/lib/active_support/core_ext/module/redefine_method.rb +40 -0
  95. data/lib/active_support/core_ext/module/remove_method.rb +17 -0
  96. data/lib/active_support/core_ext/name_error.rb +38 -0
  97. data/lib/active_support/core_ext/numeric.rb +5 -0
  98. data/lib/active_support/core_ext/numeric/bytes.rb +66 -0
  99. data/lib/active_support/core_ext/numeric/conversions.rb +136 -0
  100. data/lib/active_support/core_ext/numeric/inquiry.rb +5 -0
  101. data/lib/active_support/core_ext/numeric/time.rb +66 -0
  102. data/lib/active_support/core_ext/object.rb +16 -0
  103. data/lib/active_support/core_ext/object/acts_like.rb +21 -0
  104. data/lib/active_support/core_ext/object/blank.rb +155 -0
  105. data/lib/active_support/core_ext/object/conversions.rb +6 -0
  106. data/lib/active_support/core_ext/object/deep_dup.rb +55 -0
  107. data/lib/active_support/core_ext/object/duplicable.rb +49 -0
  108. data/lib/active_support/core_ext/object/inclusion.rb +29 -0
  109. data/lib/active_support/core_ext/object/instance_variables.rb +30 -0
  110. data/lib/active_support/core_ext/object/json.rb +228 -0
  111. data/lib/active_support/core_ext/object/to_param.rb +3 -0
  112. data/lib/active_support/core_ext/object/to_query.rb +89 -0
  113. data/lib/active_support/core_ext/object/try.rb +156 -0
  114. data/lib/active_support/core_ext/object/with_options.rb +82 -0
  115. data/lib/active_support/core_ext/range.rb +7 -0
  116. data/lib/active_support/core_ext/range/compare_range.rb +70 -0
  117. data/lib/active_support/core_ext/range/conversions.rb +41 -0
  118. data/lib/active_support/core_ext/range/each.rb +25 -0
  119. data/lib/active_support/core_ext/range/include_range.rb +9 -0
  120. data/lib/active_support/core_ext/range/include_time_with_zone.rb +23 -0
  121. data/lib/active_support/core_ext/range/overlaps.rb +10 -0
  122. data/lib/active_support/core_ext/regexp.rb +7 -0
  123. data/lib/active_support/core_ext/securerandom.rb +45 -0
  124. data/lib/active_support/core_ext/string.rb +15 -0
  125. data/lib/active_support/core_ext/string/access.rb +114 -0
  126. data/lib/active_support/core_ext/string/behavior.rb +8 -0
  127. data/lib/active_support/core_ext/string/conversions.rb +59 -0
  128. data/lib/active_support/core_ext/string/exclude.rb +13 -0
  129. data/lib/active_support/core_ext/string/filters.rb +145 -0
  130. data/lib/active_support/core_ext/string/indent.rb +45 -0
  131. data/lib/active_support/core_ext/string/inflections.rb +259 -0
  132. data/lib/active_support/core_ext/string/inquiry.rb +15 -0
  133. data/lib/active_support/core_ext/string/multibyte.rb +58 -0
  134. data/lib/active_support/core_ext/string/output_safety.rb +314 -0
  135. data/lib/active_support/core_ext/string/starts_ends_with.rb +6 -0
  136. data/lib/active_support/core_ext/string/strip.rb +27 -0
  137. data/lib/active_support/core_ext/string/zones.rb +16 -0
  138. data/lib/active_support/core_ext/time.rb +7 -0
  139. data/lib/active_support/core_ext/time/acts_like.rb +10 -0
  140. data/lib/active_support/core_ext/time/calculations.rb +344 -0
  141. data/lib/active_support/core_ext/time/compatibility.rb +16 -0
  142. data/lib/active_support/core_ext/time/conversions.rb +72 -0
  143. data/lib/active_support/core_ext/time/zones.rb +113 -0
  144. data/lib/active_support/core_ext/uri.rb +25 -0
  145. data/lib/active_support/current_attributes.rb +203 -0
  146. data/lib/active_support/dependencies.rb +806 -0
  147. data/lib/active_support/dependencies/autoload.rb +79 -0
  148. data/lib/active_support/dependencies/interlock.rb +57 -0
  149. data/lib/active_support/dependencies/zeitwerk_integration.rb +110 -0
  150. data/lib/active_support/deprecation.rb +46 -0
  151. data/lib/active_support/deprecation/behaviors.rb +109 -0
  152. data/lib/active_support/deprecation/constant_accessor.rb +52 -0
  153. data/lib/active_support/deprecation/instance_delegator.rb +39 -0
  154. data/lib/active_support/deprecation/method_wrappers.rb +78 -0
  155. data/lib/active_support/deprecation/proxy_wrappers.rb +173 -0
  156. data/lib/active_support/deprecation/reporting.rb +114 -0
  157. data/lib/active_support/descendants_tracker.rb +109 -0
  158. data/lib/active_support/digest.rb +20 -0
  159. data/lib/active_support/duration.rb +433 -0
  160. data/lib/active_support/duration/iso8601_parser.rb +124 -0
  161. data/lib/active_support/duration/iso8601_serializer.rb +54 -0
  162. data/lib/active_support/encrypted_configuration.rb +45 -0
  163. data/lib/active_support/encrypted_file.rb +100 -0
  164. data/lib/active_support/evented_file_update_checker.rb +235 -0
  165. data/lib/active_support/execution_wrapper.rb +129 -0
  166. data/lib/active_support/executor.rb +8 -0
  167. data/lib/active_support/file_update_checker.rb +163 -0
  168. data/lib/active_support/gem_version.rb +17 -0
  169. data/lib/active_support/gzip.rb +38 -0
  170. data/lib/active_support/hash_with_indifferent_access.rb +399 -0
  171. data/lib/active_support/i18n.rb +16 -0
  172. data/lib/active_support/i18n_railtie.rb +126 -0
  173. data/lib/active_support/inflections.rb +72 -0
  174. data/lib/active_support/inflector.rb +9 -0
  175. data/lib/active_support/inflector/inflections.rb +257 -0
  176. data/lib/active_support/inflector/methods.rb +398 -0
  177. data/lib/active_support/inflector/transliterate.rb +147 -0
  178. data/lib/active_support/json.rb +4 -0
  179. data/lib/active_support/json/decoding.rb +76 -0
  180. data/lib/active_support/json/encoding.rb +134 -0
  181. data/lib/active_support/key_generator.rb +41 -0
  182. data/lib/active_support/lazy_load_hooks.rb +82 -0
  183. data/lib/active_support/locale/en.rb +31 -0
  184. data/lib/active_support/locale/en.yml +135 -0
  185. data/lib/active_support/log_subscriber.rb +135 -0
  186. data/lib/active_support/log_subscriber/test_helper.rb +106 -0
  187. data/lib/active_support/logger.rb +93 -0
  188. data/lib/active_support/logger_silence.rb +45 -0
  189. data/lib/active_support/logger_thread_safe_level.rb +56 -0
  190. data/lib/active_support/message_encryptor.rb +227 -0
  191. data/lib/active_support/message_verifier.rb +205 -0
  192. data/lib/active_support/messages/metadata.rb +71 -0
  193. data/lib/active_support/messages/rotation_configuration.rb +22 -0
  194. data/lib/active_support/messages/rotator.rb +56 -0
  195. data/lib/active_support/multibyte.rb +23 -0
  196. data/lib/active_support/multibyte/chars.rb +216 -0
  197. data/lib/active_support/multibyte/unicode.rb +157 -0
  198. data/lib/active_support/notifications.rb +253 -0
  199. data/lib/active_support/notifications/fanout.rb +244 -0
  200. data/lib/active_support/notifications/instrumenter.rb +164 -0
  201. data/lib/active_support/number_helper.rb +378 -0
  202. data/lib/active_support/number_helper/number_converter.rb +184 -0
  203. data/lib/active_support/number_helper/number_to_currency_converter.rb +46 -0
  204. data/lib/active_support/number_helper/number_to_delimited_converter.rb +31 -0
  205. data/lib/active_support/number_helper/number_to_human_converter.rb +70 -0
  206. data/lib/active_support/number_helper/number_to_human_size_converter.rb +61 -0
  207. data/lib/active_support/number_helper/number_to_percentage_converter.rb +16 -0
  208. data/lib/active_support/number_helper/number_to_phone_converter.rb +60 -0
  209. data/lib/active_support/number_helper/number_to_rounded_converter.rb +56 -0
  210. data/lib/active_support/number_helper/rounding_helper.rb +66 -0
  211. data/lib/active_support/option_merger.rb +27 -0
  212. data/lib/active_support/ordered_hash.rb +50 -0
  213. data/lib/active_support/ordered_options.rb +85 -0
  214. data/lib/active_support/parameter_filter.rb +129 -0
  215. data/lib/active_support/per_thread_registry.rb +60 -0
  216. data/lib/active_support/proxy_object.rb +15 -0
  217. data/lib/active_support/rails.rb +29 -0
  218. data/lib/active_support/railtie.rb +80 -0
  219. data/lib/active_support/reloader.rb +130 -0
  220. data/lib/active_support/rescuable.rb +174 -0
  221. data/lib/active_support/security_utils.rb +31 -0
  222. data/lib/active_support/string_inquirer.rb +34 -0
  223. data/lib/active_support/subscriber.rb +169 -0
  224. data/lib/active_support/tagged_logging.rb +88 -0
  225. data/lib/active_support/test_case.rb +163 -0
  226. data/lib/active_support/testing/assertions.rb +228 -0
  227. data/lib/active_support/testing/autorun.rb +7 -0
  228. data/lib/active_support/testing/constant_lookup.rb +51 -0
  229. data/lib/active_support/testing/declarative.rb +28 -0
  230. data/lib/active_support/testing/deprecation.rb +38 -0
  231. data/lib/active_support/testing/file_fixtures.rb +38 -0
  232. data/lib/active_support/testing/isolation.rb +110 -0
  233. data/lib/active_support/testing/method_call_assertions.rb +70 -0
  234. data/lib/active_support/testing/parallelization.rb +128 -0
  235. data/lib/active_support/testing/setup_and_teardown.rb +55 -0
  236. data/lib/active_support/testing/stream.rb +44 -0
  237. data/lib/active_support/testing/tagged_logging.rb +27 -0
  238. data/lib/active_support/testing/time_helpers.rb +200 -0
  239. data/lib/active_support/time.rb +20 -0
  240. data/lib/active_support/time_with_zone.rb +561 -0
  241. data/lib/active_support/values/time_zone.rb +570 -0
  242. data/lib/active_support/version.rb +10 -0
  243. data/lib/active_support/xml_mini.rb +202 -0
  244. data/lib/active_support/xml_mini/jdom.rb +183 -0
  245. data/lib/active_support/xml_mini/libxml.rb +80 -0
  246. data/lib/active_support/xml_mini/libxmlsax.rb +83 -0
  247. data/lib/active_support/xml_mini/nokogiri.rb +83 -0
  248. data/lib/active_support/xml_mini/nokogirisax.rb +86 -0
  249. data/lib/active_support/xml_mini/rexml.rb +130 -0
  250. metadata +385 -0
@@ -0,0 +1,570 @@
1
+ # frozen_string_literal: true
2
+
3
+ require "tzinfo"
4
+ require "concurrent/map"
5
+
6
+ module ActiveSupport
7
+ # The TimeZone class serves as a wrapper around TZInfo::Timezone instances.
8
+ # It allows us to do the following:
9
+ #
10
+ # * Limit the set of zones provided by TZInfo to a meaningful subset of 134
11
+ # zones.
12
+ # * Retrieve and display zones with a friendlier name
13
+ # (e.g., "Eastern Time (US & Canada)" instead of "America/New_York").
14
+ # * Lazily load TZInfo::Timezone instances only when they're needed.
15
+ # * Create ActiveSupport::TimeWithZone instances via TimeZone's +local+,
16
+ # +parse+, +at+ and +now+ methods.
17
+ #
18
+ # If you set <tt>config.time_zone</tt> in the Rails Application, you can
19
+ # access this TimeZone object via <tt>Time.zone</tt>:
20
+ #
21
+ # # application.rb:
22
+ # class Application < Rails::Application
23
+ # config.time_zone = 'Eastern Time (US & Canada)'
24
+ # end
25
+ #
26
+ # Time.zone # => #<ActiveSupport::TimeZone:0x514834...>
27
+ # Time.zone.name # => "Eastern Time (US & Canada)"
28
+ # Time.zone.now # => Sun, 18 May 2008 14:30:44 EDT -04:00
29
+ class TimeZone
30
+ # Keys are Rails TimeZone names, values are TZInfo identifiers.
31
+ MAPPING = {
32
+ "International Date Line West" => "Etc/GMT+12",
33
+ "Midway Island" => "Pacific/Midway",
34
+ "American Samoa" => "Pacific/Pago_Pago",
35
+ "Hawaii" => "Pacific/Honolulu",
36
+ "Alaska" => "America/Juneau",
37
+ "Pacific Time (US & Canada)" => "America/Los_Angeles",
38
+ "Tijuana" => "America/Tijuana",
39
+ "Mountain Time (US & Canada)" => "America/Denver",
40
+ "Arizona" => "America/Phoenix",
41
+ "Chihuahua" => "America/Chihuahua",
42
+ "Mazatlan" => "America/Mazatlan",
43
+ "Central Time (US & Canada)" => "America/Chicago",
44
+ "Saskatchewan" => "America/Regina",
45
+ "Guadalajara" => "America/Mexico_City",
46
+ "Mexico City" => "America/Mexico_City",
47
+ "Monterrey" => "America/Monterrey",
48
+ "Central America" => "America/Guatemala",
49
+ "Eastern Time (US & Canada)" => "America/New_York",
50
+ "Indiana (East)" => "America/Indiana/Indianapolis",
51
+ "Bogota" => "America/Bogota",
52
+ "Lima" => "America/Lima",
53
+ "Quito" => "America/Lima",
54
+ "Atlantic Time (Canada)" => "America/Halifax",
55
+ "Caracas" => "America/Caracas",
56
+ "La Paz" => "America/La_Paz",
57
+ "Santiago" => "America/Santiago",
58
+ "Newfoundland" => "America/St_Johns",
59
+ "Brasilia" => "America/Sao_Paulo",
60
+ "Buenos Aires" => "America/Argentina/Buenos_Aires",
61
+ "Montevideo" => "America/Montevideo",
62
+ "Georgetown" => "America/Guyana",
63
+ "Puerto Rico" => "America/Puerto_Rico",
64
+ "Greenland" => "America/Godthab",
65
+ "Mid-Atlantic" => "Atlantic/South_Georgia",
66
+ "Azores" => "Atlantic/Azores",
67
+ "Cape Verde Is." => "Atlantic/Cape_Verde",
68
+ "Dublin" => "Europe/Dublin",
69
+ "Edinburgh" => "Europe/London",
70
+ "Lisbon" => "Europe/Lisbon",
71
+ "London" => "Europe/London",
72
+ "Casablanca" => "Africa/Casablanca",
73
+ "Monrovia" => "Africa/Monrovia",
74
+ "UTC" => "Etc/UTC",
75
+ "Belgrade" => "Europe/Belgrade",
76
+ "Bratislava" => "Europe/Bratislava",
77
+ "Budapest" => "Europe/Budapest",
78
+ "Ljubljana" => "Europe/Ljubljana",
79
+ "Prague" => "Europe/Prague",
80
+ "Sarajevo" => "Europe/Sarajevo",
81
+ "Skopje" => "Europe/Skopje",
82
+ "Warsaw" => "Europe/Warsaw",
83
+ "Zagreb" => "Europe/Zagreb",
84
+ "Brussels" => "Europe/Brussels",
85
+ "Copenhagen" => "Europe/Copenhagen",
86
+ "Madrid" => "Europe/Madrid",
87
+ "Paris" => "Europe/Paris",
88
+ "Amsterdam" => "Europe/Amsterdam",
89
+ "Berlin" => "Europe/Berlin",
90
+ "Bern" => "Europe/Zurich",
91
+ "Zurich" => "Europe/Zurich",
92
+ "Rome" => "Europe/Rome",
93
+ "Stockholm" => "Europe/Stockholm",
94
+ "Vienna" => "Europe/Vienna",
95
+ "West Central Africa" => "Africa/Algiers",
96
+ "Bucharest" => "Europe/Bucharest",
97
+ "Cairo" => "Africa/Cairo",
98
+ "Helsinki" => "Europe/Helsinki",
99
+ "Kyiv" => "Europe/Kiev",
100
+ "Riga" => "Europe/Riga",
101
+ "Sofia" => "Europe/Sofia",
102
+ "Tallinn" => "Europe/Tallinn",
103
+ "Vilnius" => "Europe/Vilnius",
104
+ "Athens" => "Europe/Athens",
105
+ "Istanbul" => "Europe/Istanbul",
106
+ "Minsk" => "Europe/Minsk",
107
+ "Jerusalem" => "Asia/Jerusalem",
108
+ "Harare" => "Africa/Harare",
109
+ "Pretoria" => "Africa/Johannesburg",
110
+ "Kaliningrad" => "Europe/Kaliningrad",
111
+ "Moscow" => "Europe/Moscow",
112
+ "St. Petersburg" => "Europe/Moscow",
113
+ "Volgograd" => "Europe/Volgograd",
114
+ "Samara" => "Europe/Samara",
115
+ "Kuwait" => "Asia/Kuwait",
116
+ "Riyadh" => "Asia/Riyadh",
117
+ "Nairobi" => "Africa/Nairobi",
118
+ "Baghdad" => "Asia/Baghdad",
119
+ "Tehran" => "Asia/Tehran",
120
+ "Abu Dhabi" => "Asia/Muscat",
121
+ "Muscat" => "Asia/Muscat",
122
+ "Baku" => "Asia/Baku",
123
+ "Tbilisi" => "Asia/Tbilisi",
124
+ "Yerevan" => "Asia/Yerevan",
125
+ "Kabul" => "Asia/Kabul",
126
+ "Ekaterinburg" => "Asia/Yekaterinburg",
127
+ "Islamabad" => "Asia/Karachi",
128
+ "Karachi" => "Asia/Karachi",
129
+ "Tashkent" => "Asia/Tashkent",
130
+ "Chennai" => "Asia/Kolkata",
131
+ "Kolkata" => "Asia/Kolkata",
132
+ "Mumbai" => "Asia/Kolkata",
133
+ "New Delhi" => "Asia/Kolkata",
134
+ "Kathmandu" => "Asia/Kathmandu",
135
+ "Astana" => "Asia/Dhaka",
136
+ "Dhaka" => "Asia/Dhaka",
137
+ "Sri Jayawardenepura" => "Asia/Colombo",
138
+ "Almaty" => "Asia/Almaty",
139
+ "Novosibirsk" => "Asia/Novosibirsk",
140
+ "Rangoon" => "Asia/Rangoon",
141
+ "Bangkok" => "Asia/Bangkok",
142
+ "Hanoi" => "Asia/Bangkok",
143
+ "Jakarta" => "Asia/Jakarta",
144
+ "Krasnoyarsk" => "Asia/Krasnoyarsk",
145
+ "Beijing" => "Asia/Shanghai",
146
+ "Chongqing" => "Asia/Chongqing",
147
+ "Hong Kong" => "Asia/Hong_Kong",
148
+ "Urumqi" => "Asia/Urumqi",
149
+ "Kuala Lumpur" => "Asia/Kuala_Lumpur",
150
+ "Singapore" => "Asia/Singapore",
151
+ "Taipei" => "Asia/Taipei",
152
+ "Perth" => "Australia/Perth",
153
+ "Irkutsk" => "Asia/Irkutsk",
154
+ "Ulaanbaatar" => "Asia/Ulaanbaatar",
155
+ "Seoul" => "Asia/Seoul",
156
+ "Osaka" => "Asia/Tokyo",
157
+ "Sapporo" => "Asia/Tokyo",
158
+ "Tokyo" => "Asia/Tokyo",
159
+ "Yakutsk" => "Asia/Yakutsk",
160
+ "Darwin" => "Australia/Darwin",
161
+ "Adelaide" => "Australia/Adelaide",
162
+ "Canberra" => "Australia/Melbourne",
163
+ "Melbourne" => "Australia/Melbourne",
164
+ "Sydney" => "Australia/Sydney",
165
+ "Brisbane" => "Australia/Brisbane",
166
+ "Hobart" => "Australia/Hobart",
167
+ "Vladivostok" => "Asia/Vladivostok",
168
+ "Guam" => "Pacific/Guam",
169
+ "Port Moresby" => "Pacific/Port_Moresby",
170
+ "Magadan" => "Asia/Magadan",
171
+ "Srednekolymsk" => "Asia/Srednekolymsk",
172
+ "Solomon Is." => "Pacific/Guadalcanal",
173
+ "New Caledonia" => "Pacific/Noumea",
174
+ "Fiji" => "Pacific/Fiji",
175
+ "Kamchatka" => "Asia/Kamchatka",
176
+ "Marshall Is." => "Pacific/Majuro",
177
+ "Auckland" => "Pacific/Auckland",
178
+ "Wellington" => "Pacific/Auckland",
179
+ "Nuku'alofa" => "Pacific/Tongatapu",
180
+ "Tokelau Is." => "Pacific/Fakaofo",
181
+ "Chatham Is." => "Pacific/Chatham",
182
+ "Samoa" => "Pacific/Apia"
183
+ }
184
+
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
188
+
189
+ @lazy_zones_map = Concurrent::Map.new
190
+ @country_zones = Concurrent::Map.new
191
+
192
+ class << self
193
+ # Assumes self represents an offset from UTC in seconds (as returned from
194
+ # Time#utc_offset) and turns this into an +HH:MM formatted string.
195
+ #
196
+ # ActiveSupport::TimeZone.seconds_to_utc_offset(-21_600) # => "-06:00"
197
+ def seconds_to_utc_offset(seconds, colon = true)
198
+ format = colon ? UTC_OFFSET_WITH_COLON : UTC_OFFSET_WITHOUT_COLON
199
+ sign = (seconds < 0 ? "-" : "+")
200
+ hours = seconds.abs / 3600
201
+ minutes = (seconds.abs % 3600) / 60
202
+ format % [sign, hours, minutes]
203
+ end
204
+
205
+ def find_tzinfo(name)
206
+ TZInfo::Timezone.new(MAPPING[name] || name)
207
+ end
208
+
209
+ alias_method :create, :new
210
+
211
+ # Returns a TimeZone instance with the given name, or +nil+ if no
212
+ # such TimeZone instance exists. (This exists to support the use of
213
+ # this class with the +composed_of+ macro.)
214
+ def new(name)
215
+ self[name]
216
+ end
217
+
218
+ # Returns an array of all TimeZone objects. There are multiple
219
+ # TimeZone objects per time zone, in many cases, to make it easier
220
+ # for users to find their own time zone.
221
+ def all
222
+ @zones ||= zones_map.values.sort
223
+ end
224
+
225
+ # Locate a specific time zone object. If the argument is a string, it
226
+ # is interpreted to mean the name of the timezone to locate. If it is a
227
+ # numeric value it is either the hour offset, or the second offset, of the
228
+ # timezone to find. (The first one with that offset will be returned.)
229
+ # Returns +nil+ if no such time zone is known to the system.
230
+ def [](arg)
231
+ case arg
232
+ when String
233
+ begin
234
+ @lazy_zones_map[arg] ||= create(arg)
235
+ rescue TZInfo::InvalidTimezoneIdentifier
236
+ nil
237
+ end
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}"
243
+ end
244
+ end
245
+
246
+ # A convenience method for returning a collection of TimeZone objects
247
+ # for time zones in the USA.
248
+ def us_zones
249
+ country_zones(:us)
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
287
+ end
288
+
289
+ include Comparable
290
+ attr_reader :name
291
+ attr_reader :tzinfo
292
+
293
+ # Create a new TimeZone object with the given name and offset. The
294
+ # offset is the number of seconds that this time zone is offset from UTC
295
+ # (GMT). Seconds were chosen as the offset unit because that is the unit
296
+ # that Ruby uses to represent time zone offsets (see Time#utc_offset).
297
+ def initialize(name, utc_offset = nil, tzinfo = nil)
298
+ @name = name
299
+ @utc_offset = utc_offset
300
+ @tzinfo = tzinfo || TimeZone.find_tzinfo(name)
301
+ end
302
+
303
+ # Returns the offset of this time zone from UTC in seconds.
304
+ def utc_offset
305
+ if @utc_offset
306
+ @utc_offset
307
+ else
308
+ tzinfo.current_period.utc_offset if tzinfo && tzinfo.current_period
309
+ end
310
+ end
311
+
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)
319
+ utc_offset == 0 && alternate_utc_string || self.class.seconds_to_utc_offset(utc_offset, colon)
320
+ end
321
+
322
+ # Compare this time zone to the parameter. The two are compared first on
323
+ # their offsets, and then by name.
324
+ def <=>(zone)
325
+ return unless zone.respond_to? :utc_offset
326
+ result = (utc_offset <=> zone.utc_offset)
327
+ result = (name <=> zone.name) if result == 0
328
+ result
329
+ end
330
+
331
+ # Compare #name and TZInfo identifier to a supplied regexp, returning +true+
332
+ # if a match is found.
333
+ def =~(re)
334
+ re === name || re === MAPPING[name]
335
+ end
336
+
337
+ # Returns a textual representation of this time zone.
338
+ def to_s
339
+ "(GMT#{formatted_offset}) #{name}"
340
+ end
341
+
342
+ # Method for creating new ActiveSupport::TimeWithZone instance in time zone
343
+ # of +self+ from given values.
344
+ #
345
+ # Time.zone = 'Hawaii' # => "Hawaii"
346
+ # Time.zone.local(2007, 2, 1, 15, 30, 45) # => Thu, 01 Feb 2007 15:30:45 HST -10:00
347
+ def local(*args)
348
+ time = Time.utc(*args)
349
+ ActiveSupport::TimeWithZone.new(nil, self, time)
350
+ end
351
+
352
+ # Method for creating new ActiveSupport::TimeWithZone instance in time zone
353
+ # of +self+ from number of seconds since the Unix epoch.
354
+ #
355
+ # Time.zone = 'Hawaii' # => "Hawaii"
356
+ # Time.utc(2000).to_f # => 946684800.0
357
+ # Time.zone.at(946684800.0) # => Fri, 31 Dec 1999 14:00:00 HST -10:00
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
400
+ end
401
+
402
+ # Method for creating new ActiveSupport::TimeWithZone instance in time zone
403
+ # of +self+ from parsed string.
404
+ #
405
+ # Time.zone = 'Hawaii' # => "Hawaii"
406
+ # Time.zone.parse('1999-12-31 14:00:00') # => Fri, 31 Dec 1999 14:00:00 HST -10:00
407
+ #
408
+ # If upper components are missing from the string, they are supplied from
409
+ # TimeZone#now:
410
+ #
411
+ # Time.zone.now # => Fri, 31 Dec 1999 14:00:00 HST -10:00
412
+ # Time.zone.parse('22:30:00') # => Fri, 31 Dec 1999 22:30:00 HST -10:00
413
+ #
414
+ # However, if the date component is not provided, but any other upper
415
+ # components are supplied, then the day of the month defaults to 1:
416
+ #
417
+ # Time.zone.parse('Mar 2000') # => Wed, 01 Mar 2000 00:00:00 HST -10:00
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?
440
+
441
+ time = Time.new(
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)
449
+ )
450
+
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)
476
+ end
477
+
478
+ # Returns an ActiveSupport::TimeWithZone instance representing the current
479
+ # time in the time zone represented by +self+.
480
+ #
481
+ # Time.zone = 'Hawaii' # => "Hawaii"
482
+ # Time.zone.now # => Wed, 23 Jan 2008 20:24:27 HST -10:00
483
+ def now
484
+ time_now.utc.in_time_zone(self)
485
+ end
486
+
487
+ # Returns the current date in this time zone.
488
+ def today
489
+ tzinfo.now.to_date
490
+ end
491
+
492
+ # Returns the next date in this time zone.
493
+ def tomorrow
494
+ today + 1
495
+ end
496
+
497
+ # Returns the previous date in this time zone.
498
+ def yesterday
499
+ today - 1
500
+ end
501
+
502
+ # Adjust the given time to the simultaneous time in the time zone
503
+ # represented by +self+. Returns a Time.utc() instance -- if you want an
504
+ # ActiveSupport::TimeWithZone instance, use Time#in_time_zone() instead.
505
+ def utc_to_local(time)
506
+ tzinfo.utc_to_local(time)
507
+ end
508
+
509
+ # Adjust the given time to the simultaneous time in UTC. Returns a
510
+ # Time.utc() instance.
511
+ def local_to_utc(time, dst = true)
512
+ tzinfo.local_to_utc(time, dst)
513
+ end
514
+
515
+ # Available so that TimeZone instances respond like TZInfo::Timezone
516
+ # instances.
517
+ def period_for_utc(time)
518
+ tzinfo.period_for_utc(time)
519
+ end
520
+
521
+ # Available so that TimeZone instances respond like TZInfo::Timezone
522
+ # instances.
523
+ def period_for_local(time, dst = true)
524
+ tzinfo.period_for_local(time, dst) { |periods| periods.last }
525
+ end
526
+
527
+ def periods_for_local(time) #:nodoc:
528
+ tzinfo.periods_for_local(time)
529
+ end
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
+
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
+
566
+ def time_now
567
+ Time.now
568
+ end
569
+ end
570
+ end