activesupport 5.0.7.2

Sign up to get free protection for your applications and to get access to all the features.

Potentially problematic release.


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

Files changed (236) hide show
  1. checksums.yaml +7 -0
  2. data/CHANGELOG.md +1018 -0
  3. data/MIT-LICENSE +20 -0
  4. data/README.rdoc +39 -0
  5. data/lib/active_support.rb +99 -0
  6. data/lib/active_support/all.rb +3 -0
  7. data/lib/active_support/array_inquirer.rb +44 -0
  8. data/lib/active_support/backtrace_cleaner.rb +103 -0
  9. data/lib/active_support/benchmarkable.rb +49 -0
  10. data/lib/active_support/builder.rb +6 -0
  11. data/lib/active_support/cache.rb +701 -0
  12. data/lib/active_support/cache/file_store.rb +204 -0
  13. data/lib/active_support/cache/mem_cache_store.rb +207 -0
  14. data/lib/active_support/cache/memory_store.rb +167 -0
  15. data/lib/active_support/cache/null_store.rb +41 -0
  16. data/lib/active_support/cache/strategy/local_cache.rb +172 -0
  17. data/lib/active_support/cache/strategy/local_cache_middleware.rb +44 -0
  18. data/lib/active_support/callbacks.rb +791 -0
  19. data/lib/active_support/concern.rb +142 -0
  20. data/lib/active_support/concurrency/latch.rb +26 -0
  21. data/lib/active_support/concurrency/share_lock.rb +226 -0
  22. data/lib/active_support/configurable.rb +148 -0
  23. data/lib/active_support/core_ext.rb +4 -0
  24. data/lib/active_support/core_ext/array.rb +7 -0
  25. data/lib/active_support/core_ext/array/access.rb +90 -0
  26. data/lib/active_support/core_ext/array/conversions.rb +211 -0
  27. data/lib/active_support/core_ext/array/extract_options.rb +29 -0
  28. data/lib/active_support/core_ext/array/grouping.rb +107 -0
  29. data/lib/active_support/core_ext/array/inquiry.rb +17 -0
  30. data/lib/active_support/core_ext/array/prepend_and_append.rb +7 -0
  31. data/lib/active_support/core_ext/array/wrap.rb +46 -0
  32. data/lib/active_support/core_ext/benchmark.rb +14 -0
  33. data/lib/active_support/core_ext/big_decimal.rb +1 -0
  34. data/lib/active_support/core_ext/big_decimal/conversions.rb +14 -0
  35. data/lib/active_support/core_ext/class.rb +2 -0
  36. data/lib/active_support/core_ext/class/attribute.rb +128 -0
  37. data/lib/active_support/core_ext/class/attribute_accessors.rb +4 -0
  38. data/lib/active_support/core_ext/class/subclasses.rb +41 -0
  39. data/lib/active_support/core_ext/date.rb +5 -0
  40. data/lib/active_support/core_ext/date/acts_like.rb +8 -0
  41. data/lib/active_support/core_ext/date/blank.rb +12 -0
  42. data/lib/active_support/core_ext/date/calculations.rb +143 -0
  43. data/lib/active_support/core_ext/date/conversions.rb +95 -0
  44. data/lib/active_support/core_ext/date/zones.rb +6 -0
  45. data/lib/active_support/core_ext/date_and_time/calculations.rb +335 -0
  46. data/lib/active_support/core_ext/date_and_time/compatibility.rb +14 -0
  47. data/lib/active_support/core_ext/date_and_time/zones.rb +40 -0
  48. data/lib/active_support/core_ext/date_time.rb +5 -0
  49. data/lib/active_support/core_ext/date_time/acts_like.rb +14 -0
  50. data/lib/active_support/core_ext/date_time/blank.rb +12 -0
  51. data/lib/active_support/core_ext/date_time/calculations.rb +199 -0
  52. data/lib/active_support/core_ext/date_time/compatibility.rb +16 -0
  53. data/lib/active_support/core_ext/date_time/conversions.rb +105 -0
  54. data/lib/active_support/core_ext/digest/uuid.rb +51 -0
  55. data/lib/active_support/core_ext/enumerable.rb +146 -0
  56. data/lib/active_support/core_ext/file.rb +1 -0
  57. data/lib/active_support/core_ext/file/atomic.rb +68 -0
  58. data/lib/active_support/core_ext/hash.rb +9 -0
  59. data/lib/active_support/core_ext/hash/compact.rb +24 -0
  60. data/lib/active_support/core_ext/hash/conversions.rb +262 -0
  61. data/lib/active_support/core_ext/hash/deep_merge.rb +38 -0
  62. data/lib/active_support/core_ext/hash/except.rb +22 -0
  63. data/lib/active_support/core_ext/hash/indifferent_access.rb +23 -0
  64. data/lib/active_support/core_ext/hash/keys.rb +170 -0
  65. data/lib/active_support/core_ext/hash/reverse_merge.rb +22 -0
  66. data/lib/active_support/core_ext/hash/slice.rb +48 -0
  67. data/lib/active_support/core_ext/hash/transform_values.rb +29 -0
  68. data/lib/active_support/core_ext/integer.rb +3 -0
  69. data/lib/active_support/core_ext/integer/inflections.rb +29 -0
  70. data/lib/active_support/core_ext/integer/multiple.rb +10 -0
  71. data/lib/active_support/core_ext/integer/time.rb +29 -0
  72. data/lib/active_support/core_ext/kernel.rb +4 -0
  73. data/lib/active_support/core_ext/kernel/agnostics.rb +11 -0
  74. data/lib/active_support/core_ext/kernel/concern.rb +12 -0
  75. data/lib/active_support/core_ext/kernel/debugger.rb +3 -0
  76. data/lib/active_support/core_ext/kernel/reporting.rb +43 -0
  77. data/lib/active_support/core_ext/kernel/singleton_class.rb +6 -0
  78. data/lib/active_support/core_ext/load_error.rb +31 -0
  79. data/lib/active_support/core_ext/marshal.rb +22 -0
  80. data/lib/active_support/core_ext/module.rb +12 -0
  81. data/lib/active_support/core_ext/module/aliasing.rb +74 -0
  82. data/lib/active_support/core_ext/module/anonymous.rb +28 -0
  83. data/lib/active_support/core_ext/module/attr_internal.rb +36 -0
  84. data/lib/active_support/core_ext/module/attribute_accessors.rb +212 -0
  85. data/lib/active_support/core_ext/module/attribute_accessors_per_thread.rb +141 -0
  86. data/lib/active_support/core_ext/module/concerning.rb +135 -0
  87. data/lib/active_support/core_ext/module/delegation.rb +216 -0
  88. data/lib/active_support/core_ext/module/deprecation.rb +23 -0
  89. data/lib/active_support/core_ext/module/introspection.rb +68 -0
  90. data/lib/active_support/core_ext/module/method_transplanting.rb +3 -0
  91. data/lib/active_support/core_ext/module/qualified_const.rb +70 -0
  92. data/lib/active_support/core_ext/module/reachable.rb +8 -0
  93. data/lib/active_support/core_ext/module/remove_method.rb +35 -0
  94. data/lib/active_support/core_ext/name_error.rb +31 -0
  95. data/lib/active_support/core_ext/numeric.rb +4 -0
  96. data/lib/active_support/core_ext/numeric/bytes.rb +64 -0
  97. data/lib/active_support/core_ext/numeric/conversions.rb +144 -0
  98. data/lib/active_support/core_ext/numeric/inquiry.rb +26 -0
  99. data/lib/active_support/core_ext/numeric/time.rb +74 -0
  100. data/lib/active_support/core_ext/object.rb +14 -0
  101. data/lib/active_support/core_ext/object/acts_like.rb +10 -0
  102. data/lib/active_support/core_ext/object/blank.rb +143 -0
  103. data/lib/active_support/core_ext/object/conversions.rb +4 -0
  104. data/lib/active_support/core_ext/object/deep_dup.rb +53 -0
  105. data/lib/active_support/core_ext/object/duplicable.rb +124 -0
  106. data/lib/active_support/core_ext/object/inclusion.rb +27 -0
  107. data/lib/active_support/core_ext/object/instance_variables.rb +28 -0
  108. data/lib/active_support/core_ext/object/json.rb +205 -0
  109. data/lib/active_support/core_ext/object/to_param.rb +1 -0
  110. data/lib/active_support/core_ext/object/to_query.rb +84 -0
  111. data/lib/active_support/core_ext/object/try.rb +146 -0
  112. data/lib/active_support/core_ext/object/with_options.rb +69 -0
  113. data/lib/active_support/core_ext/range.rb +4 -0
  114. data/lib/active_support/core_ext/range/conversions.rb +31 -0
  115. data/lib/active_support/core_ext/range/each.rb +21 -0
  116. data/lib/active_support/core_ext/range/include_range.rb +23 -0
  117. data/lib/active_support/core_ext/range/overlaps.rb +8 -0
  118. data/lib/active_support/core_ext/regexp.rb +5 -0
  119. data/lib/active_support/core_ext/securerandom.rb +23 -0
  120. data/lib/active_support/core_ext/string.rb +13 -0
  121. data/lib/active_support/core_ext/string/access.rb +104 -0
  122. data/lib/active_support/core_ext/string/behavior.rb +6 -0
  123. data/lib/active_support/core_ext/string/conversions.rb +57 -0
  124. data/lib/active_support/core_ext/string/exclude.rb +11 -0
  125. data/lib/active_support/core_ext/string/filters.rb +102 -0
  126. data/lib/active_support/core_ext/string/indent.rb +43 -0
  127. data/lib/active_support/core_ext/string/inflections.rb +244 -0
  128. data/lib/active_support/core_ext/string/inquiry.rb +13 -0
  129. data/lib/active_support/core_ext/string/multibyte.rb +53 -0
  130. data/lib/active_support/core_ext/string/output_safety.rb +260 -0
  131. data/lib/active_support/core_ext/string/starts_ends_with.rb +4 -0
  132. data/lib/active_support/core_ext/string/strip.rb +23 -0
  133. data/lib/active_support/core_ext/string/zones.rb +14 -0
  134. data/lib/active_support/core_ext/struct.rb +3 -0
  135. data/lib/active_support/core_ext/time.rb +5 -0
  136. data/lib/active_support/core_ext/time/acts_like.rb +8 -0
  137. data/lib/active_support/core_ext/time/calculations.rb +290 -0
  138. data/lib/active_support/core_ext/time/compatibility.rb +14 -0
  139. data/lib/active_support/core_ext/time/conversions.rb +67 -0
  140. data/lib/active_support/core_ext/time/marshal.rb +3 -0
  141. data/lib/active_support/core_ext/time/zones.rb +111 -0
  142. data/lib/active_support/core_ext/uri.rb +24 -0
  143. data/lib/active_support/dependencies.rb +755 -0
  144. data/lib/active_support/dependencies/autoload.rb +77 -0
  145. data/lib/active_support/dependencies/interlock.rb +55 -0
  146. data/lib/active_support/deprecation.rb +43 -0
  147. data/lib/active_support/deprecation/behaviors.rb +90 -0
  148. data/lib/active_support/deprecation/instance_delegator.rb +37 -0
  149. data/lib/active_support/deprecation/method_wrappers.rb +70 -0
  150. data/lib/active_support/deprecation/proxy_wrappers.rb +149 -0
  151. data/lib/active_support/deprecation/reporting.rb +112 -0
  152. data/lib/active_support/descendants_tracker.rb +60 -0
  153. data/lib/active_support/duration.rb +235 -0
  154. data/lib/active_support/duration/iso8601_parser.rb +122 -0
  155. data/lib/active_support/duration/iso8601_serializer.rb +51 -0
  156. data/lib/active_support/evented_file_update_checker.rb +199 -0
  157. data/lib/active_support/execution_wrapper.rb +126 -0
  158. data/lib/active_support/executor.rb +6 -0
  159. data/lib/active_support/file_update_checker.rb +157 -0
  160. data/lib/active_support/gem_version.rb +15 -0
  161. data/lib/active_support/gzip.rb +36 -0
  162. data/lib/active_support/hash_with_indifferent_access.rb +329 -0
  163. data/lib/active_support/i18n.rb +13 -0
  164. data/lib/active_support/i18n_railtie.rb +115 -0
  165. data/lib/active_support/inflections.rb +70 -0
  166. data/lib/active_support/inflector.rb +7 -0
  167. data/lib/active_support/inflector/inflections.rb +242 -0
  168. data/lib/active_support/inflector/methods.rb +390 -0
  169. data/lib/active_support/inflector/transliterate.rb +112 -0
  170. data/lib/active_support/json.rb +2 -0
  171. data/lib/active_support/json/decoding.rb +74 -0
  172. data/lib/active_support/json/encoding.rb +127 -0
  173. data/lib/active_support/key_generator.rb +71 -0
  174. data/lib/active_support/lazy_load_hooks.rb +76 -0
  175. data/lib/active_support/locale/en.yml +135 -0
  176. data/lib/active_support/log_subscriber.rb +109 -0
  177. data/lib/active_support/log_subscriber/test_helper.rb +104 -0
  178. data/lib/active_support/logger.rb +106 -0
  179. data/lib/active_support/logger_silence.rb +28 -0
  180. data/lib/active_support/logger_thread_safe_level.rb +31 -0
  181. data/lib/active_support/message_encryptor.rb +114 -0
  182. data/lib/active_support/message_verifier.rb +134 -0
  183. data/lib/active_support/multibyte.rb +21 -0
  184. data/lib/active_support/multibyte/chars.rb +231 -0
  185. data/lib/active_support/multibyte/unicode.rb +413 -0
  186. data/lib/active_support/notifications.rb +212 -0
  187. data/lib/active_support/notifications/fanout.rb +157 -0
  188. data/lib/active_support/notifications/instrumenter.rb +91 -0
  189. data/lib/active_support/number_helper.rb +368 -0
  190. data/lib/active_support/number_helper/number_converter.rb +182 -0
  191. data/lib/active_support/number_helper/number_to_currency_converter.rb +44 -0
  192. data/lib/active_support/number_helper/number_to_delimited_converter.rb +28 -0
  193. data/lib/active_support/number_helper/number_to_human_converter.rb +68 -0
  194. data/lib/active_support/number_helper/number_to_human_size_converter.rb +62 -0
  195. data/lib/active_support/number_helper/number_to_percentage_converter.rb +12 -0
  196. data/lib/active_support/number_helper/number_to_phone_converter.rb +58 -0
  197. data/lib/active_support/number_helper/number_to_rounded_converter.rb +92 -0
  198. data/lib/active_support/option_merger.rb +25 -0
  199. data/lib/active_support/ordered_hash.rb +48 -0
  200. data/lib/active_support/ordered_options.rb +81 -0
  201. data/lib/active_support/per_thread_registry.rb +58 -0
  202. data/lib/active_support/proxy_object.rb +13 -0
  203. data/lib/active_support/rails.rb +27 -0
  204. data/lib/active_support/railtie.rb +51 -0
  205. data/lib/active_support/reloader.rb +129 -0
  206. data/lib/active_support/rescuable.rb +173 -0
  207. data/lib/active_support/security_utils.rb +27 -0
  208. data/lib/active_support/string_inquirer.rb +26 -0
  209. data/lib/active_support/subscriber.rb +120 -0
  210. data/lib/active_support/tagged_logging.rb +77 -0
  211. data/lib/active_support/test_case.rb +88 -0
  212. data/lib/active_support/testing/assertions.rb +99 -0
  213. data/lib/active_support/testing/autorun.rb +5 -0
  214. data/lib/active_support/testing/constant_lookup.rb +50 -0
  215. data/lib/active_support/testing/declarative.rb +26 -0
  216. data/lib/active_support/testing/deprecation.rb +36 -0
  217. data/lib/active_support/testing/file_fixtures.rb +34 -0
  218. data/lib/active_support/testing/isolation.rb +115 -0
  219. data/lib/active_support/testing/method_call_assertions.rb +41 -0
  220. data/lib/active_support/testing/setup_and_teardown.rb +50 -0
  221. data/lib/active_support/testing/stream.rb +42 -0
  222. data/lib/active_support/testing/tagged_logging.rb +25 -0
  223. data/lib/active_support/testing/time_helpers.rb +136 -0
  224. data/lib/active_support/time.rb +18 -0
  225. data/lib/active_support/time_with_zone.rb +511 -0
  226. data/lib/active_support/values/time_zone.rb +484 -0
  227. data/lib/active_support/values/unicode_tables.dat +0 -0
  228. data/lib/active_support/version.rb +8 -0
  229. data/lib/active_support/xml_mini.rb +209 -0
  230. data/lib/active_support/xml_mini/jdom.rb +181 -0
  231. data/lib/active_support/xml_mini/libxml.rb +77 -0
  232. data/lib/active_support/xml_mini/libxmlsax.rb +82 -0
  233. data/lib/active_support/xml_mini/nokogiri.rb +81 -0
  234. data/lib/active_support/xml_mini/nokogirisax.rb +85 -0
  235. data/lib/active_support/xml_mini/rexml.rb +128 -0
  236. metadata +349 -0
@@ -0,0 +1,484 @@
1
+ require 'tzinfo'
2
+ require 'concurrent/map'
3
+ require 'active_support/core_ext/object/blank'
4
+
5
+ module ActiveSupport
6
+ # The TimeZone class serves as a wrapper around TZInfo::Timezone instances.
7
+ # It allows us to do the following:
8
+ #
9
+ # * Limit the set of zones provided by TZInfo to a meaningful subset of 146
10
+ # zones.
11
+ # * Retrieve and display zones with a friendlier name
12
+ # (e.g., "Eastern Time (US & Canada)" instead of "America/New_York").
13
+ # * Lazily load TZInfo::Timezone instances only when they're needed.
14
+ # * Create ActiveSupport::TimeWithZone instances via TimeZone's +local+,
15
+ # +parse+, +at+ and +now+ methods.
16
+ #
17
+ # If you set <tt>config.time_zone</tt> in the Rails Application, you can
18
+ # access this TimeZone object via <tt>Time.zone</tt>:
19
+ #
20
+ # # application.rb:
21
+ # class Application < Rails::Application
22
+ # config.time_zone = 'Eastern Time (US & Canada)'
23
+ # end
24
+ #
25
+ # Time.zone # => #<ActiveSupport::TimeZone:0x514834...>
26
+ # Time.zone.name # => "Eastern Time (US & Canada)"
27
+ # Time.zone.now # => Sun, 18 May 2008 14:30:44 EDT -04:00
28
+ class TimeZone
29
+ # Keys are Rails TimeZone names, values are TZInfo identifiers.
30
+ MAPPING = {
31
+ "International Date Line West" => "Pacific/Midway",
32
+ "Midway Island" => "Pacific/Midway",
33
+ "American Samoa" => "Pacific/Pago_Pago",
34
+ "Hawaii" => "Pacific/Honolulu",
35
+ "Alaska" => "America/Juneau",
36
+ "Pacific Time (US & Canada)" => "America/Los_Angeles",
37
+ "Tijuana" => "America/Tijuana",
38
+ "Mountain Time (US & Canada)" => "America/Denver",
39
+ "Arizona" => "America/Phoenix",
40
+ "Chihuahua" => "America/Chihuahua",
41
+ "Mazatlan" => "America/Mazatlan",
42
+ "Central Time (US & Canada)" => "America/Chicago",
43
+ "Saskatchewan" => "America/Regina",
44
+ "Guadalajara" => "America/Mexico_City",
45
+ "Mexico City" => "America/Mexico_City",
46
+ "Monterrey" => "America/Monterrey",
47
+ "Central America" => "America/Guatemala",
48
+ "Eastern Time (US & Canada)" => "America/New_York",
49
+ "Indiana (East)" => "America/Indiana/Indianapolis",
50
+ "Bogota" => "America/Bogota",
51
+ "Lima" => "America/Lima",
52
+ "Quito" => "America/Lima",
53
+ "Atlantic Time (Canada)" => "America/Halifax",
54
+ "Caracas" => "America/Caracas",
55
+ "La Paz" => "America/La_Paz",
56
+ "Santiago" => "America/Santiago",
57
+ "Newfoundland" => "America/St_Johns",
58
+ "Brasilia" => "America/Sao_Paulo",
59
+ "Buenos Aires" => "America/Argentina/Buenos_Aires",
60
+ "Montevideo" => "America/Montevideo",
61
+ "Georgetown" => "America/Guyana",
62
+ "Greenland" => "America/Godthab",
63
+ "Mid-Atlantic" => "Atlantic/South_Georgia",
64
+ "Azores" => "Atlantic/Azores",
65
+ "Cape Verde Is." => "Atlantic/Cape_Verde",
66
+ "Dublin" => "Europe/Dublin",
67
+ "Edinburgh" => "Europe/London",
68
+ "Lisbon" => "Europe/Lisbon",
69
+ "London" => "Europe/London",
70
+ "Casablanca" => "Africa/Casablanca",
71
+ "Monrovia" => "Africa/Monrovia",
72
+ "UTC" => "Etc/UTC",
73
+ "Belgrade" => "Europe/Belgrade",
74
+ "Bratislava" => "Europe/Bratislava",
75
+ "Budapest" => "Europe/Budapest",
76
+ "Ljubljana" => "Europe/Ljubljana",
77
+ "Prague" => "Europe/Prague",
78
+ "Sarajevo" => "Europe/Sarajevo",
79
+ "Skopje" => "Europe/Skopje",
80
+ "Warsaw" => "Europe/Warsaw",
81
+ "Zagreb" => "Europe/Zagreb",
82
+ "Brussels" => "Europe/Brussels",
83
+ "Copenhagen" => "Europe/Copenhagen",
84
+ "Madrid" => "Europe/Madrid",
85
+ "Paris" => "Europe/Paris",
86
+ "Amsterdam" => "Europe/Amsterdam",
87
+ "Berlin" => "Europe/Berlin",
88
+ "Bern" => "Europe/Zurich",
89
+ "Zurich" => "Europe/Zurich",
90
+ "Rome" => "Europe/Rome",
91
+ "Stockholm" => "Europe/Stockholm",
92
+ "Vienna" => "Europe/Vienna",
93
+ "West Central Africa" => "Africa/Algiers",
94
+ "Bucharest" => "Europe/Bucharest",
95
+ "Cairo" => "Africa/Cairo",
96
+ "Helsinki" => "Europe/Helsinki",
97
+ "Kyiv" => "Europe/Kiev",
98
+ "Riga" => "Europe/Riga",
99
+ "Sofia" => "Europe/Sofia",
100
+ "Tallinn" => "Europe/Tallinn",
101
+ "Vilnius" => "Europe/Vilnius",
102
+ "Athens" => "Europe/Athens",
103
+ "Istanbul" => "Europe/Istanbul",
104
+ "Minsk" => "Europe/Minsk",
105
+ "Jerusalem" => "Asia/Jerusalem",
106
+ "Harare" => "Africa/Harare",
107
+ "Pretoria" => "Africa/Johannesburg",
108
+ "Kaliningrad" => "Europe/Kaliningrad",
109
+ "Moscow" => "Europe/Moscow",
110
+ "St. Petersburg" => "Europe/Moscow",
111
+ "Volgograd" => "Europe/Volgograd",
112
+ "Samara" => "Europe/Samara",
113
+ "Kuwait" => "Asia/Kuwait",
114
+ "Riyadh" => "Asia/Riyadh",
115
+ "Nairobi" => "Africa/Nairobi",
116
+ "Baghdad" => "Asia/Baghdad",
117
+ "Tehran" => "Asia/Tehran",
118
+ "Abu Dhabi" => "Asia/Muscat",
119
+ "Muscat" => "Asia/Muscat",
120
+ "Baku" => "Asia/Baku",
121
+ "Tbilisi" => "Asia/Tbilisi",
122
+ "Yerevan" => "Asia/Yerevan",
123
+ "Kabul" => "Asia/Kabul",
124
+ "Ekaterinburg" => "Asia/Yekaterinburg",
125
+ "Islamabad" => "Asia/Karachi",
126
+ "Karachi" => "Asia/Karachi",
127
+ "Tashkent" => "Asia/Tashkent",
128
+ "Chennai" => "Asia/Kolkata",
129
+ "Kolkata" => "Asia/Kolkata",
130
+ "Mumbai" => "Asia/Kolkata",
131
+ "New Delhi" => "Asia/Kolkata",
132
+ "Kathmandu" => "Asia/Kathmandu",
133
+ "Astana" => "Asia/Dhaka",
134
+ "Dhaka" => "Asia/Dhaka",
135
+ "Sri Jayawardenepura" => "Asia/Colombo",
136
+ "Almaty" => "Asia/Almaty",
137
+ "Novosibirsk" => "Asia/Novosibirsk",
138
+ "Rangoon" => "Asia/Rangoon",
139
+ "Bangkok" => "Asia/Bangkok",
140
+ "Hanoi" => "Asia/Bangkok",
141
+ "Jakarta" => "Asia/Jakarta",
142
+ "Krasnoyarsk" => "Asia/Krasnoyarsk",
143
+ "Beijing" => "Asia/Shanghai",
144
+ "Chongqing" => "Asia/Chongqing",
145
+ "Hong Kong" => "Asia/Hong_Kong",
146
+ "Urumqi" => "Asia/Urumqi",
147
+ "Kuala Lumpur" => "Asia/Kuala_Lumpur",
148
+ "Singapore" => "Asia/Singapore",
149
+ "Taipei" => "Asia/Taipei",
150
+ "Perth" => "Australia/Perth",
151
+ "Irkutsk" => "Asia/Irkutsk",
152
+ "Ulaanbaatar" => "Asia/Ulaanbaatar",
153
+ "Seoul" => "Asia/Seoul",
154
+ "Osaka" => "Asia/Tokyo",
155
+ "Sapporo" => "Asia/Tokyo",
156
+ "Tokyo" => "Asia/Tokyo",
157
+ "Yakutsk" => "Asia/Yakutsk",
158
+ "Darwin" => "Australia/Darwin",
159
+ "Adelaide" => "Australia/Adelaide",
160
+ "Canberra" => "Australia/Melbourne",
161
+ "Melbourne" => "Australia/Melbourne",
162
+ "Sydney" => "Australia/Sydney",
163
+ "Brisbane" => "Australia/Brisbane",
164
+ "Hobart" => "Australia/Hobart",
165
+ "Vladivostok" => "Asia/Vladivostok",
166
+ "Guam" => "Pacific/Guam",
167
+ "Port Moresby" => "Pacific/Port_Moresby",
168
+ "Magadan" => "Asia/Magadan",
169
+ "Srednekolymsk" => "Asia/Srednekolymsk",
170
+ "Solomon Is." => "Pacific/Guadalcanal",
171
+ "New Caledonia" => "Pacific/Noumea",
172
+ "Fiji" => "Pacific/Fiji",
173
+ "Kamchatka" => "Asia/Kamchatka",
174
+ "Marshall Is." => "Pacific/Majuro",
175
+ "Auckland" => "Pacific/Auckland",
176
+ "Wellington" => "Pacific/Auckland",
177
+ "Nuku'alofa" => "Pacific/Tongatapu",
178
+ "Tokelau Is." => "Pacific/Fakaofo",
179
+ "Chatham Is." => "Pacific/Chatham",
180
+ "Samoa" => "Pacific/Apia"
181
+ }
182
+
183
+ UTC_OFFSET_WITH_COLON = '%s%02d:%02d'
184
+ UTC_OFFSET_WITHOUT_COLON = UTC_OFFSET_WITH_COLON.tr(':', '')
185
+
186
+ @lazy_zones_map = Concurrent::Map.new
187
+ @country_zones = Concurrent::Map.new
188
+
189
+ class << self
190
+ # Assumes self represents an offset from UTC in seconds (as returned from
191
+ # Time#utc_offset) and turns this into an +HH:MM formatted string.
192
+ #
193
+ # ActiveSupport::TimeZone.seconds_to_utc_offset(-21_600) # => "-06:00"
194
+ def seconds_to_utc_offset(seconds, colon = true)
195
+ format = colon ? UTC_OFFSET_WITH_COLON : UTC_OFFSET_WITHOUT_COLON
196
+ sign = (seconds < 0 ? '-' : '+')
197
+ hours = seconds.abs / 3600
198
+ minutes = (seconds.abs % 3600) / 60
199
+ format % [sign, hours, minutes]
200
+ end
201
+
202
+ def find_tzinfo(name)
203
+ TZInfo::Timezone.new(MAPPING[name] || name)
204
+ end
205
+
206
+ alias_method :create, :new
207
+
208
+ # Returns a TimeZone instance with the given name, or +nil+ if no
209
+ # such TimeZone instance exists. (This exists to support the use of
210
+ # this class with the +composed_of+ macro.)
211
+ def new(name)
212
+ self[name]
213
+ end
214
+
215
+ # Returns an array of all TimeZone objects. There are multiple
216
+ # TimeZone objects per time zone, in many cases, to make it easier
217
+ # for users to find their own time zone.
218
+ def all
219
+ @zones ||= zones_map.values.sort
220
+ end
221
+
222
+ # Locate a specific time zone object. If the argument is a string, it
223
+ # is interpreted to mean the name of the timezone to locate. If it is a
224
+ # numeric value it is either the hour offset, or the second offset, of the
225
+ # timezone to find. (The first one with that offset will be returned.)
226
+ # Returns +nil+ if no such time zone is known to the system.
227
+ def [](arg)
228
+ case arg
229
+ when String
230
+ begin
231
+ @lazy_zones_map[arg] ||= create(arg)
232
+ rescue TZInfo::InvalidTimezoneIdentifier
233
+ nil
234
+ end
235
+ when Numeric, ActiveSupport::Duration
236
+ arg *= 3600 if arg.abs <= 13
237
+ all.find { |z| z.utc_offset == arg.to_i }
238
+ else
239
+ raise ArgumentError, "invalid argument to TimeZone[]: #{arg.inspect}"
240
+ end
241
+ end
242
+
243
+ # A convenience method for returning a collection of TimeZone objects
244
+ # for time zones in the USA.
245
+ def us_zones
246
+ country_zones(:us)
247
+ end
248
+
249
+ # A convenience method for returning a collection of TimeZone objects
250
+ # for time zones in the country specified by its ISO 3166-1 Alpha2 code.
251
+ def country_zones(country_code)
252
+ code = country_code.to_s.upcase
253
+ @country_zones[code] ||= load_country_zones(code)
254
+ end
255
+
256
+ private
257
+ def load_country_zones(code)
258
+ country = TZInfo::Country.get(code)
259
+ country.zone_identifiers.map do |tz_id|
260
+ if MAPPING.value?(tz_id)
261
+ MAPPING.inject([]) do |memo, (key, value)|
262
+ memo << self[key] if value == tz_id
263
+ memo
264
+ end
265
+ else
266
+ create(tz_id, nil, TZInfo::Timezone.new(tz_id))
267
+ end
268
+ end.flatten(1).sort!
269
+ end
270
+
271
+ def zones_map
272
+ @zones_map ||= begin
273
+ MAPPING.each_key {|place| self[place]} # load all the zones
274
+ @lazy_zones_map
275
+ end
276
+ end
277
+ end
278
+
279
+ include Comparable
280
+ attr_reader :name
281
+ attr_reader :tzinfo
282
+
283
+ # Create a new TimeZone object with the given name and offset. The
284
+ # offset is the number of seconds that this time zone is offset from UTC
285
+ # (GMT). Seconds were chosen as the offset unit because that is the unit
286
+ # that Ruby uses to represent time zone offsets (see Time#utc_offset).
287
+ def initialize(name, utc_offset = nil, tzinfo = nil)
288
+ @name = name
289
+ @utc_offset = utc_offset
290
+ @tzinfo = tzinfo || TimeZone.find_tzinfo(name)
291
+ end
292
+
293
+ # Returns the offset of this time zone from UTC in seconds.
294
+ def utc_offset
295
+ if @utc_offset
296
+ @utc_offset
297
+ else
298
+ tzinfo.current_period.utc_offset if tzinfo && tzinfo.current_period
299
+ end
300
+ end
301
+
302
+ # Returns a formatted string of the offset from UTC, or an alternative
303
+ # string if the time zone is already UTC.
304
+ #
305
+ # zone = ActiveSupport::TimeZone['Central Time (US & Canada)']
306
+ # zone.formatted_offset # => "-06:00"
307
+ # zone.formatted_offset(false) # => "-0600"
308
+ def formatted_offset(colon=true, alternate_utc_string = nil)
309
+ utc_offset == 0 && alternate_utc_string || self.class.seconds_to_utc_offset(utc_offset, colon)
310
+ end
311
+
312
+ # Compare this time zone to the parameter. The two are compared first on
313
+ # their offsets, and then by name.
314
+ def <=>(zone)
315
+ return unless zone.respond_to? :utc_offset
316
+ result = (utc_offset <=> zone.utc_offset)
317
+ result = (name <=> zone.name) if result == 0
318
+ result
319
+ end
320
+
321
+ # Compare #name and TZInfo identifier to a supplied regexp, returning +true+
322
+ # if a match is found.
323
+ def =~(re)
324
+ re === name || re === MAPPING[name]
325
+ end
326
+
327
+ # Returns a textual representation of this time zone.
328
+ def to_s
329
+ "(GMT#{formatted_offset}) #{name}"
330
+ end
331
+
332
+ # Method for creating new ActiveSupport::TimeWithZone instance in time zone
333
+ # of +self+ from given values.
334
+ #
335
+ # Time.zone = 'Hawaii' # => "Hawaii"
336
+ # Time.zone.local(2007, 2, 1, 15, 30, 45) # => Thu, 01 Feb 2007 15:30:45 HST -10:00
337
+ def local(*args)
338
+ time = Time.utc(*args)
339
+ ActiveSupport::TimeWithZone.new(nil, self, time)
340
+ end
341
+
342
+ # Method for creating new ActiveSupport::TimeWithZone instance in time zone
343
+ # of +self+ from number of seconds since the Unix epoch.
344
+ #
345
+ # Time.zone = 'Hawaii' # => "Hawaii"
346
+ # Time.utc(2000).to_f # => 946684800.0
347
+ # Time.zone.at(946684800.0) # => Fri, 31 Dec 1999 14:00:00 HST -10:00
348
+ def at(secs)
349
+ Time.at(secs).utc.in_time_zone(self)
350
+ end
351
+
352
+ # Method for creating new ActiveSupport::TimeWithZone instance in time zone
353
+ # of +self+ from parsed string.
354
+ #
355
+ # Time.zone = 'Hawaii' # => "Hawaii"
356
+ # Time.zone.parse('1999-12-31 14:00:00') # => Fri, 31 Dec 1999 14:00:00 HST -10:00
357
+ #
358
+ # If upper components are missing from the string, they are supplied from
359
+ # TimeZone#now:
360
+ #
361
+ # Time.zone.now # => Fri, 31 Dec 1999 14:00:00 HST -10:00
362
+ # Time.zone.parse('22:30:00') # => Fri, 31 Dec 1999 22:30:00 HST -10:00
363
+ #
364
+ # However, if the date component is not provided, but any other upper
365
+ # components are supplied, then the day of the month defaults to 1:
366
+ #
367
+ # Time.zone.parse('Mar 2000') # => Wed, 01 Mar 2000 00:00:00 HST -10:00
368
+ def parse(str, now=now())
369
+ parts_to_time(Date._parse(str, false), now)
370
+ end
371
+
372
+ # Parses +str+ according to +format+ and returns an ActiveSupport::TimeWithZone.
373
+ #
374
+ # Assumes that +str+ is a time in the time zone +self+,
375
+ # unless +format+ includes an explicit time zone.
376
+ # (This is the same behavior as +parse+.)
377
+ # In either case, the returned TimeWithZone has the timezone of +self+.
378
+ #
379
+ # Time.zone = 'Hawaii' # => "Hawaii"
380
+ # 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
381
+ #
382
+ # If upper components are missing from the string, they are supplied from
383
+ # TimeZone#now:
384
+ #
385
+ # Time.zone.now # => Fri, 31 Dec 1999 14:00:00 HST -10:00
386
+ # Time.zone.strptime('22:30:00', '%H:%M:%S') # => Fri, 31 Dec 1999 22:30:00 HST -10:00
387
+ #
388
+ # However, if the date component is not provided, but any other upper
389
+ # components are supplied, then the day of the month defaults to 1:
390
+ #
391
+ # Time.zone.strptime('Mar 2000', '%b %Y') # => Wed, 01 Mar 2000 00:00:00 HST -10:00
392
+ def strptime(str, format, now=now())
393
+ parts_to_time(DateTime._strptime(str, format), now)
394
+ end
395
+
396
+ # Returns an ActiveSupport::TimeWithZone instance representing the current
397
+ # time in the time zone represented by +self+.
398
+ #
399
+ # Time.zone = 'Hawaii' # => "Hawaii"
400
+ # Time.zone.now # => Wed, 23 Jan 2008 20:24:27 HST -10:00
401
+ def now
402
+ time_now.utc.in_time_zone(self)
403
+ end
404
+
405
+ # Returns the current date in this time zone.
406
+ def today
407
+ tzinfo.now.to_date
408
+ end
409
+
410
+ # Returns the next date in this time zone.
411
+ def tomorrow
412
+ today + 1
413
+ end
414
+
415
+ # Returns the previous date in this time zone.
416
+ def yesterday
417
+ today - 1
418
+ end
419
+
420
+ # Adjust the given time to the simultaneous time in the time zone
421
+ # represented by +self+. Returns a Time.utc() instance -- if you want an
422
+ # ActiveSupport::TimeWithZone instance, use Time#in_time_zone() instead.
423
+ def utc_to_local(time)
424
+ tzinfo.utc_to_local(time)
425
+ end
426
+
427
+ # Adjust the given time to the simultaneous time in UTC. Returns a
428
+ # Time.utc() instance.
429
+ def local_to_utc(time, dst=true)
430
+ tzinfo.local_to_utc(time, dst)
431
+ end
432
+
433
+ # Available so that TimeZone instances respond like TZInfo::Timezone
434
+ # instances.
435
+ def period_for_utc(time)
436
+ tzinfo.period_for_utc(time)
437
+ end
438
+
439
+ # Available so that TimeZone instances respond like TZInfo::Timezone
440
+ # instances.
441
+ def period_for_local(time, dst=true)
442
+ tzinfo.period_for_local(time, dst)
443
+ end
444
+
445
+ def periods_for_local(time) #:nodoc:
446
+ tzinfo.periods_for_local(time)
447
+ end
448
+
449
+ def init_with(coder) #:nodoc:
450
+ initialize(coder['name'])
451
+ end
452
+
453
+ def encode_with(coder) #:nodoc:
454
+ coder.tag ="!ruby/object:#{self.class}"
455
+ coder.map = { 'name' => tzinfo.name }
456
+ end
457
+
458
+ private
459
+ def parts_to_time(parts, now)
460
+ raise ArgumentError, "invalid date" if parts.nil?
461
+ return if parts.empty?
462
+
463
+ time = Time.new(
464
+ parts.fetch(:year, now.year),
465
+ parts.fetch(:mon, now.month),
466
+ parts.fetch(:mday, parts[:year] || parts[:mon] ? 1 : now.day),
467
+ parts.fetch(:hour, 0),
468
+ parts.fetch(:min, 0),
469
+ parts.fetch(:sec, 0) + parts.fetch(:sec_fraction, 0),
470
+ parts.fetch(:offset, 0)
471
+ )
472
+
473
+ if parts[:offset]
474
+ TimeWithZone.new(time.utc, self)
475
+ else
476
+ TimeWithZone.new(nil, self, time)
477
+ end
478
+ end
479
+
480
+ def time_now
481
+ Time.now
482
+ end
483
+ end
484
+ end