time_crisis 0.1.8 → 0.2.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 (145) hide show
  1. data/VERSION.yml +2 -2
  2. data/lib/time_crisis.rb +5 -12
  3. data/lib/time_crisis/ext.rb +60 -0
  4. data/lib/time_crisis/support/conversions.rb +4 -0
  5. data/lib/time_crisis/tzinfo.rb +3 -0
  6. data/lib/time_crisis/tzinfo/LICENSE +21 -0
  7. data/lib/time_crisis/tzinfo/data_timezone.rb +27 -0
  8. data/lib/time_crisis/tzinfo/data_timezone_info.rb +208 -0
  9. data/lib/time_crisis/tzinfo/definitions/Africa/Algiers.rb +55 -0
  10. data/lib/time_crisis/tzinfo/definitions/Africa/Cairo.rb +219 -0
  11. data/lib/time_crisis/tzinfo/definitions/Africa/Casablanca.rb +42 -0
  12. data/lib/time_crisis/tzinfo/definitions/Africa/Harare.rb +18 -0
  13. data/lib/time_crisis/tzinfo/definitions/Africa/Johannesburg.rb +25 -0
  14. data/lib/time_crisis/tzinfo/definitions/Africa/Monrovia.rb +22 -0
  15. data/lib/time_crisis/tzinfo/definitions/Africa/Nairobi.rb +23 -0
  16. data/lib/time_crisis/tzinfo/definitions/America/Argentina/Buenos_Aires.rb +84 -0
  17. data/lib/time_crisis/tzinfo/definitions/America/Argentina/San_Juan.rb +86 -0
  18. data/lib/time_crisis/tzinfo/definitions/America/Bogota.rb +23 -0
  19. data/lib/time_crisis/tzinfo/definitions/America/Caracas.rb +23 -0
  20. data/lib/time_crisis/tzinfo/definitions/America/Chicago.rb +283 -0
  21. data/lib/time_crisis/tzinfo/definitions/America/Chihuahua.rb +136 -0
  22. data/lib/time_crisis/tzinfo/definitions/America/Denver.rb +204 -0
  23. data/lib/time_crisis/tzinfo/definitions/America/Godthab.rb +161 -0
  24. data/lib/time_crisis/tzinfo/definitions/America/Guatemala.rb +27 -0
  25. data/lib/time_crisis/tzinfo/definitions/America/Halifax.rb +274 -0
  26. data/lib/time_crisis/tzinfo/definitions/America/Indiana/Indianapolis.rb +149 -0
  27. data/lib/time_crisis/tzinfo/definitions/America/Juneau.rb +194 -0
  28. data/lib/time_crisis/tzinfo/definitions/America/La_Paz.rb +22 -0
  29. data/lib/time_crisis/tzinfo/definitions/America/Lima.rb +35 -0
  30. data/lib/time_crisis/tzinfo/definitions/America/Los_Angeles.rb +232 -0
  31. data/lib/time_crisis/tzinfo/definitions/America/Mazatlan.rb +139 -0
  32. data/lib/time_crisis/tzinfo/definitions/America/Mexico_City.rb +144 -0
  33. data/lib/time_crisis/tzinfo/definitions/America/Monterrey.rb +131 -0
  34. data/lib/time_crisis/tzinfo/definitions/America/New_York.rb +282 -0
  35. data/lib/time_crisis/tzinfo/definitions/America/Phoenix.rb +30 -0
  36. data/lib/time_crisis/tzinfo/definitions/America/Regina.rb +74 -0
  37. data/lib/time_crisis/tzinfo/definitions/America/Santiago.rb +205 -0
  38. data/lib/time_crisis/tzinfo/definitions/America/Sao_Paulo.rb +171 -0
  39. data/lib/time_crisis/tzinfo/definitions/America/St_Johns.rb +288 -0
  40. data/lib/time_crisis/tzinfo/definitions/America/Tijuana.rb +196 -0
  41. data/lib/time_crisis/tzinfo/definitions/Asia/Almaty.rb +67 -0
  42. data/lib/time_crisis/tzinfo/definitions/Asia/Baghdad.rb +73 -0
  43. data/lib/time_crisis/tzinfo/definitions/Asia/Baku.rb +161 -0
  44. data/lib/time_crisis/tzinfo/definitions/Asia/Bangkok.rb +20 -0
  45. data/lib/time_crisis/tzinfo/definitions/Asia/Chongqing.rb +33 -0
  46. data/lib/time_crisis/tzinfo/definitions/Asia/Colombo.rb +30 -0
  47. data/lib/time_crisis/tzinfo/definitions/Asia/Dhaka.rb +29 -0
  48. data/lib/time_crisis/tzinfo/definitions/Asia/Hong_Kong.rb +87 -0
  49. data/lib/time_crisis/tzinfo/definitions/Asia/Irkutsk.rb +165 -0
  50. data/lib/time_crisis/tzinfo/definitions/Asia/Jakarta.rb +30 -0
  51. data/lib/time_crisis/tzinfo/definitions/Asia/Jerusalem.rb +163 -0
  52. data/lib/time_crisis/tzinfo/definitions/Asia/Kabul.rb +20 -0
  53. data/lib/time_crisis/tzinfo/definitions/Asia/Kamchatka.rb +163 -0
  54. data/lib/time_crisis/tzinfo/definitions/Asia/Karachi.rb +114 -0
  55. data/lib/time_crisis/tzinfo/definitions/Asia/Kathmandu.rb +20 -0
  56. data/lib/time_crisis/tzinfo/definitions/Asia/Kolkata.rb +25 -0
  57. data/lib/time_crisis/tzinfo/definitions/Asia/Krasnoyarsk.rb +163 -0
  58. data/lib/time_crisis/tzinfo/definitions/Asia/Kuala_Lumpur.rb +31 -0
  59. data/lib/time_crisis/tzinfo/definitions/Asia/Kuwait.rb +18 -0
  60. data/lib/time_crisis/tzinfo/definitions/Asia/Magadan.rb +163 -0
  61. data/lib/time_crisis/tzinfo/definitions/Asia/Muscat.rb +18 -0
  62. data/lib/time_crisis/tzinfo/definitions/Asia/Novosibirsk.rb +164 -0
  63. data/lib/time_crisis/tzinfo/definitions/Asia/Rangoon.rb +24 -0
  64. data/lib/time_crisis/tzinfo/definitions/Asia/Riyadh.rb +18 -0
  65. data/lib/time_crisis/tzinfo/definitions/Asia/Seoul.rb +34 -0
  66. data/lib/time_crisis/tzinfo/definitions/Asia/Shanghai.rb +35 -0
  67. data/lib/time_crisis/tzinfo/definitions/Asia/Singapore.rb +33 -0
  68. data/lib/time_crisis/tzinfo/definitions/Asia/Taipei.rb +59 -0
  69. data/lib/time_crisis/tzinfo/definitions/Asia/Tashkent.rb +47 -0
  70. data/lib/time_crisis/tzinfo/definitions/Asia/Tbilisi.rb +78 -0
  71. data/lib/time_crisis/tzinfo/definitions/Asia/Tehran.rb +121 -0
  72. data/lib/time_crisis/tzinfo/definitions/Asia/Tokyo.rb +30 -0
  73. data/lib/time_crisis/tzinfo/definitions/Asia/Ulaanbaatar.rb +65 -0
  74. data/lib/time_crisis/tzinfo/definitions/Asia/Urumqi.rb +33 -0
  75. data/lib/time_crisis/tzinfo/definitions/Asia/Vladivostok.rb +164 -0
  76. data/lib/time_crisis/tzinfo/definitions/Asia/Yakutsk.rb +163 -0
  77. data/lib/time_crisis/tzinfo/definitions/Asia/Yekaterinburg.rb +165 -0
  78. data/lib/time_crisis/tzinfo/definitions/Asia/Yerevan.rb +165 -0
  79. data/lib/time_crisis/tzinfo/definitions/Atlantic/Azores.rb +270 -0
  80. data/lib/time_crisis/tzinfo/definitions/Atlantic/Cape_Verde.rb +23 -0
  81. data/lib/time_crisis/tzinfo/definitions/Atlantic/South_Georgia.rb +18 -0
  82. data/lib/time_crisis/tzinfo/definitions/Australia/Adelaide.rb +187 -0
  83. data/lib/time_crisis/tzinfo/definitions/Australia/Brisbane.rb +35 -0
  84. data/lib/time_crisis/tzinfo/definitions/Australia/Darwin.rb +29 -0
  85. data/lib/time_crisis/tzinfo/definitions/Australia/Hobart.rb +193 -0
  86. data/lib/time_crisis/tzinfo/definitions/Australia/Melbourne.rb +185 -0
  87. data/lib/time_crisis/tzinfo/definitions/Australia/Perth.rb +37 -0
  88. data/lib/time_crisis/tzinfo/definitions/Australia/Sydney.rb +185 -0
  89. data/lib/time_crisis/tzinfo/definitions/Etc/UTC.rb +16 -0
  90. data/lib/time_crisis/tzinfo/definitions/Europe/Amsterdam.rb +228 -0
  91. data/lib/time_crisis/tzinfo/definitions/Europe/Athens.rb +185 -0
  92. data/lib/time_crisis/tzinfo/definitions/Europe/Belgrade.rb +163 -0
  93. data/lib/time_crisis/tzinfo/definitions/Europe/Berlin.rb +188 -0
  94. data/lib/time_crisis/tzinfo/definitions/Europe/Bratislava.rb +13 -0
  95. data/lib/time_crisis/tzinfo/definitions/Europe/Brussels.rb +232 -0
  96. data/lib/time_crisis/tzinfo/definitions/Europe/Bucharest.rb +181 -0
  97. data/lib/time_crisis/tzinfo/definitions/Europe/Budapest.rb +197 -0
  98. data/lib/time_crisis/tzinfo/definitions/Europe/Copenhagen.rb +179 -0
  99. data/lib/time_crisis/tzinfo/definitions/Europe/Dublin.rb +276 -0
  100. data/lib/time_crisis/tzinfo/definitions/Europe/Helsinki.rb +163 -0
  101. data/lib/time_crisis/tzinfo/definitions/Europe/Istanbul.rb +218 -0
  102. data/lib/time_crisis/tzinfo/definitions/Europe/Kiev.rb +168 -0
  103. data/lib/time_crisis/tzinfo/definitions/Europe/Lisbon.rb +268 -0
  104. data/lib/time_crisis/tzinfo/definitions/Europe/Ljubljana.rb +13 -0
  105. data/lib/time_crisis/tzinfo/definitions/Europe/London.rb +288 -0
  106. data/lib/time_crisis/tzinfo/definitions/Europe/Madrid.rb +211 -0
  107. data/lib/time_crisis/tzinfo/definitions/Europe/Minsk.rb +170 -0
  108. data/lib/time_crisis/tzinfo/definitions/Europe/Moscow.rb +181 -0
  109. data/lib/time_crisis/tzinfo/definitions/Europe/Paris.rb +232 -0
  110. data/lib/time_crisis/tzinfo/definitions/Europe/Prague.rb +187 -0
  111. data/lib/time_crisis/tzinfo/definitions/Europe/Riga.rb +176 -0
  112. data/lib/time_crisis/tzinfo/definitions/Europe/Rome.rb +215 -0
  113. data/lib/time_crisis/tzinfo/definitions/Europe/Sarajevo.rb +13 -0
  114. data/lib/time_crisis/tzinfo/definitions/Europe/Skopje.rb +13 -0
  115. data/lib/time_crisis/tzinfo/definitions/Europe/Sofia.rb +173 -0
  116. data/lib/time_crisis/tzinfo/definitions/Europe/Stockholm.rb +165 -0
  117. data/lib/time_crisis/tzinfo/definitions/Europe/Tallinn.rb +172 -0
  118. data/lib/time_crisis/tzinfo/definitions/Europe/Vienna.rb +183 -0
  119. data/lib/time_crisis/tzinfo/definitions/Europe/Vilnius.rb +170 -0
  120. data/lib/time_crisis/tzinfo/definitions/Europe/Warsaw.rb +212 -0
  121. data/lib/time_crisis/tzinfo/definitions/Europe/Zagreb.rb +13 -0
  122. data/lib/time_crisis/tzinfo/definitions/Pacific/Auckland.rb +202 -0
  123. data/lib/time_crisis/tzinfo/definitions/Pacific/Fiji.rb +23 -0
  124. data/lib/time_crisis/tzinfo/definitions/Pacific/Guam.rb +22 -0
  125. data/lib/time_crisis/tzinfo/definitions/Pacific/Honolulu.rb +28 -0
  126. data/lib/time_crisis/tzinfo/definitions/Pacific/Majuro.rb +20 -0
  127. data/lib/time_crisis/tzinfo/definitions/Pacific/Midway.rb +25 -0
  128. data/lib/time_crisis/tzinfo/definitions/Pacific/Noumea.rb +25 -0
  129. data/lib/time_crisis/tzinfo/definitions/Pacific/Pago_Pago.rb +26 -0
  130. data/lib/time_crisis/tzinfo/definitions/Pacific/Port_Moresby.rb +20 -0
  131. data/lib/time_crisis/tzinfo/definitions/Pacific/Tongatapu.rb +27 -0
  132. data/lib/time_crisis/tzinfo/info_timezone.rb +32 -0
  133. data/lib/time_crisis/tzinfo/linked_timezone.rb +32 -0
  134. data/lib/time_crisis/tzinfo/linked_timezone_info.rb +24 -0
  135. data/lib/time_crisis/tzinfo/offset_rationals.rb +79 -0
  136. data/lib/time_crisis/tzinfo/ruby_core_support.rb +33 -0
  137. data/lib/time_crisis/tzinfo/time_or_datetime.rb +309 -0
  138. data/lib/time_crisis/tzinfo/timezone.rb +486 -0
  139. data/lib/time_crisis/tzinfo/timezone_definition.rb +36 -0
  140. data/lib/time_crisis/tzinfo/timezone_info.rb +20 -0
  141. data/lib/time_crisis/tzinfo/timezone_offset_info.rb +75 -0
  142. data/lib/time_crisis/tzinfo/timezone_period.rb +179 -0
  143. data/lib/time_crisis/tzinfo/timezone_transition_info.rb +109 -0
  144. data/time_crisis.gemspec +142 -2
  145. metadata +142 -2
@@ -0,0 +1,22 @@
1
+ require 'time_crisis/tzinfo/timezone_definition'
2
+
3
+ module TimeCrisis::TZInfo
4
+ module Definitions
5
+ module Pacific
6
+ module Guam
7
+ include TimezoneDefinition
8
+
9
+ timezone 'Pacific/Guam' do |tz|
10
+ tz.offset :o0, -51660, 0, :LMT
11
+ tz.offset :o1, 34740, 0, :LMT
12
+ tz.offset :o2, 36000, 0, :GST
13
+ tz.offset :o3, 36000, 0, :ChST
14
+
15
+ tz.transition 1844, 12, :o1, 1149567407, 480
16
+ tz.transition 1900, 12, :o2, 1159384847, 480
17
+ tz.transition 2000, 12, :o3, 977493600
18
+ end
19
+ end
20
+ end
21
+ end
22
+ end
@@ -0,0 +1,28 @@
1
+ require 'time_crisis/tzinfo/timezone_definition'
2
+
3
+ module TimeCrisis::TZInfo
4
+ module Definitions
5
+ module Pacific
6
+ module Honolulu
7
+ include TimezoneDefinition
8
+
9
+ timezone 'Pacific/Honolulu' do |tz|
10
+ tz.offset :o0, -37886, 0, :LMT
11
+ tz.offset :o1, -37800, 0, :HST
12
+ tz.offset :o2, -37800, 3600, :HDT
13
+ tz.offset :o3, -37800, 3600, :HWT
14
+ tz.offset :o4, -37800, 3600, :HPT
15
+ tz.offset :o5, -36000, 0, :HST
16
+
17
+ tz.transition 1900, 1, :o1, 104328926143, 43200
18
+ tz.transition 1933, 4, :o2, 116505265, 48
19
+ tz.transition 1933, 5, :o1, 116506271, 48
20
+ tz.transition 1942, 2, :o3, 116659201, 48
21
+ tz.transition 1945, 8, :o4, 58360379, 24
22
+ tz.transition 1945, 9, :o1, 116722991, 48
23
+ tz.transition 1947, 6, :o5, 116752561, 48
24
+ end
25
+ end
26
+ end
27
+ end
28
+ end
@@ -0,0 +1,20 @@
1
+ require 'time_crisis/tzinfo/timezone_definition'
2
+
3
+ module TimeCrisis::TZInfo
4
+ module Definitions
5
+ module Pacific
6
+ module Majuro
7
+ include TimezoneDefinition
8
+
9
+ timezone 'Pacific/Majuro' do |tz|
10
+ tz.offset :o0, 41088, 0, :LMT
11
+ tz.offset :o1, 39600, 0, :MHT
12
+ tz.offset :o2, 43200, 0, :MHT
13
+
14
+ tz.transition 1900, 12, :o1, 1086923261, 450
15
+ tz.transition 1969, 9, :o2, 58571881, 24
16
+ end
17
+ end
18
+ end
19
+ end
20
+ end
@@ -0,0 +1,25 @@
1
+ require 'time_crisis/tzinfo/timezone_definition'
2
+
3
+ module TimeCrisis::TZInfo
4
+ module Definitions
5
+ module Pacific
6
+ module Midway
7
+ include TimezoneDefinition
8
+
9
+ timezone 'Pacific/Midway' do |tz|
10
+ tz.offset :o0, -42568, 0, :LMT
11
+ tz.offset :o1, -39600, 0, :NST
12
+ tz.offset :o2, -39600, 3600, :NDT
13
+ tz.offset :o3, -39600, 0, :BST
14
+ tz.offset :o4, -39600, 0, :SST
15
+
16
+ tz.transition 1901, 1, :o1, 26086168721, 10800
17
+ tz.transition 1956, 6, :o2, 58455071, 24
18
+ tz.transition 1956, 9, :o1, 29228627, 12
19
+ tz.transition 1967, 4, :o3, 58549967, 24
20
+ tz.transition 1983, 11, :o4, 439038000
21
+ end
22
+ end
23
+ end
24
+ end
25
+ end
@@ -0,0 +1,25 @@
1
+ require 'time_crisis/tzinfo/timezone_definition'
2
+
3
+ module TimeCrisis::TZInfo
4
+ module Definitions
5
+ module Pacific
6
+ module Noumea
7
+ include TimezoneDefinition
8
+
9
+ timezone 'Pacific/Noumea' do |tz|
10
+ tz.offset :o0, 39948, 0, :LMT
11
+ tz.offset :o1, 39600, 0, :NCT
12
+ tz.offset :o2, 39600, 3600, :NCST
13
+
14
+ tz.transition 1912, 1, :o1, 17419781071, 7200
15
+ tz.transition 1977, 12, :o2, 250002000
16
+ tz.transition 1978, 2, :o1, 257342400
17
+ tz.transition 1978, 12, :o2, 281451600
18
+ tz.transition 1979, 2, :o1, 288878400
19
+ tz.transition 1996, 11, :o2, 849366000
20
+ tz.transition 1997, 3, :o1, 857228400
21
+ end
22
+ end
23
+ end
24
+ end
25
+ end
@@ -0,0 +1,26 @@
1
+ require 'time_crisis/tzinfo/timezone_definition'
2
+
3
+ module TimeCrisis::TZInfo
4
+ module Definitions
5
+ module Pacific
6
+ module Pago_Pago
7
+ include TimezoneDefinition
8
+
9
+ timezone 'Pacific/Pago_Pago' do |tz|
10
+ tz.offset :o0, 45432, 0, :LMT
11
+ tz.offset :o1, -40968, 0, :LMT
12
+ tz.offset :o2, -41400, 0, :SAMT
13
+ tz.offset :o3, -39600, 0, :NST
14
+ tz.offset :o4, -39600, 0, :BST
15
+ tz.offset :o5, -39600, 0, :SST
16
+
17
+ tz.transition 1879, 7, :o1, 2889041969, 1200
18
+ tz.transition 1911, 1, :o2, 2902845569, 1200
19
+ tz.transition 1950, 1, :o3, 116797583, 48
20
+ tz.transition 1967, 4, :o4, 58549967, 24
21
+ tz.transition 1983, 11, :o5, 439038000
22
+ end
23
+ end
24
+ end
25
+ end
26
+ end
@@ -0,0 +1,20 @@
1
+ require 'time_crisis/tzinfo/timezone_definition'
2
+
3
+ module TimeCrisis::TZInfo
4
+ module Definitions
5
+ module Pacific
6
+ module Port_Moresby
7
+ include TimezoneDefinition
8
+
9
+ timezone 'Pacific/Port_Moresby' do |tz|
10
+ tz.offset :o0, 35320, 0, :LMT
11
+ tz.offset :o1, 35312, 0, :PMMT
12
+ tz.offset :o2, 36000, 0, :PGT
13
+
14
+ tz.transition 1879, 12, :o1, 5200664597, 2160
15
+ tz.transition 1894, 12, :o2, 13031248093, 5400
16
+ end
17
+ end
18
+ end
19
+ end
20
+ end
@@ -0,0 +1,27 @@
1
+ require 'time_crisis/tzinfo/timezone_definition'
2
+
3
+ module TimeCrisis::TZInfo
4
+ module Definitions
5
+ module Pacific
6
+ module Tongatapu
7
+ include TimezoneDefinition
8
+
9
+ timezone 'Pacific/Tongatapu' do |tz|
10
+ tz.offset :o0, 44360, 0, :LMT
11
+ tz.offset :o1, 44400, 0, :TOT
12
+ tz.offset :o2, 46800, 0, :TOT
13
+ tz.offset :o3, 46800, 3600, :TOST
14
+
15
+ tz.transition 1900, 12, :o1, 5217231571, 2160
16
+ tz.transition 1940, 12, :o2, 174959639, 72
17
+ tz.transition 1999, 10, :o3, 939214800
18
+ tz.transition 2000, 3, :o2, 953384400
19
+ tz.transition 2000, 11, :o3, 973342800
20
+ tz.transition 2001, 1, :o2, 980596800
21
+ tz.transition 2001, 11, :o3, 1004792400
22
+ tz.transition 2002, 1, :o2, 1012046400
23
+ end
24
+ end
25
+ end
26
+ end
27
+ end
@@ -0,0 +1,32 @@
1
+ require 'time_crisis/tzinfo/timezone'
2
+
3
+ module TimeCrisis
4
+ module TZInfo
5
+
6
+ # A Timezone based on a TimezoneInfo.
7
+ class InfoTimezone < Timezone #:nodoc:
8
+
9
+ # Constructs a new InfoTimezone with a TimezoneInfo instance.
10
+ def self.new(info)
11
+ tz = super()
12
+ tz.send(:setup, info)
13
+ tz
14
+ end
15
+
16
+ # The identifier of the timezone, e.g. "Europe/Paris".
17
+ def identifier
18
+ @info.identifier
19
+ end
20
+
21
+ protected
22
+ # The TimezoneInfo for this Timezone.
23
+ def info
24
+ @info
25
+ end
26
+
27
+ def setup(info)
28
+ @info = info
29
+ end
30
+ end
31
+ end
32
+ end
@@ -0,0 +1,32 @@
1
+ require 'time_crisis/tzinfo/info_timezone'
2
+
3
+ module TimeCrisis
4
+ module TZInfo
5
+
6
+ class LinkedTimezone < InfoTimezone #:nodoc:
7
+ # Returns the TimezonePeriod for the given UTC time. utc can either be
8
+ # a DateTime, Time or integer timestamp (Time.to_i). Any timezone
9
+ # information in utc is ignored (it is treated as a UTC time).
10
+ #
11
+ # If no TimezonePeriod could be found, PeriodNotFound is raised.
12
+ def period_for_utc(utc)
13
+ @linked_timezone.period_for_utc(utc)
14
+ end
15
+
16
+ # Returns the set of TimezonePeriod instances that are valid for the given
17
+ # local time as an array. If you just want a single period, use
18
+ # period_for_local instead and specify how abiguities should be resolved.
19
+ # Raises PeriodNotFound if no periods are found for the given time.
20
+ def periods_for_local(local)
21
+ @linked_timezone.periods_for_local(local)
22
+ end
23
+
24
+ protected
25
+
26
+ def setup(info)
27
+ super(info)
28
+ @linked_timezone = Timezone.get(info.link_to_identifier)
29
+ end
30
+ end
31
+ end
32
+ end
@@ -0,0 +1,24 @@
1
+ require 'time_crisis/tzinfo/timezone_info'
2
+
3
+ module TimeCrisis
4
+ module TZInfo
5
+ # Represents a linked timezone defined in a data module.
6
+ class LinkedTimezoneInfo < TimezoneInfo #:nodoc:
7
+
8
+ # The zone that provides the data (that this zone is an alias for).
9
+ attr_reader :link_to_identifier
10
+
11
+ # Constructs a new TimezoneInfo with an identifier and the identifier
12
+ # of the zone linked to.
13
+ def initialize(identifier, link_to_identifier)
14
+ super(identifier)
15
+ @link_to_identifier = link_to_identifier
16
+ end
17
+
18
+ # Returns internal object state as a programmer-readable string.
19
+ def inspect
20
+ "#<#{self.class}: #@identifier,#@link_to_identifier>"
21
+ end
22
+ end
23
+ end
24
+ end
@@ -0,0 +1,79 @@
1
+ require 'rational'
2
+ require 'time_crisis/tzinfo/ruby_core_support'
3
+
4
+ module TimeCrisis
5
+ module TZInfo
6
+
7
+ # Provides a method for getting Rationals for a timezone offset in seconds.
8
+ # Pre-reduced rationals are returned for all the half-hour intervals between
9
+ # -14 and +14 hours to avoid having to call gcd at runtime.
10
+ module OffsetRationals #:nodoc:
11
+ @@rational_cache = {
12
+ -50400 => RubyCoreSupport.rational_new!(-7, 12),
13
+ -48600 => RubyCoreSupport.rational_new!(-9, 16),
14
+ -46800 => RubyCoreSupport.rational_new!(-13, 24),
15
+ -45000 => RubyCoreSupport.rational_new!(-25, 48),
16
+ -43200 => RubyCoreSupport.rational_new!(-1, 2),
17
+ -41400 => RubyCoreSupport.rational_new!(-23, 48),
18
+ -39600 => RubyCoreSupport.rational_new!(-11, 24),
19
+ -37800 => RubyCoreSupport.rational_new!(-7, 16),
20
+ -36000 => RubyCoreSupport.rational_new!(-5, 12),
21
+ -34200 => RubyCoreSupport.rational_new!(-19, 48),
22
+ -32400 => RubyCoreSupport.rational_new!(-3, 8),
23
+ -30600 => RubyCoreSupport.rational_new!(-17, 48),
24
+ -28800 => RubyCoreSupport.rational_new!(-1, 3),
25
+ -27000 => RubyCoreSupport.rational_new!(-5, 16),
26
+ -25200 => RubyCoreSupport.rational_new!(-7, 24),
27
+ -23400 => RubyCoreSupport.rational_new!(-13, 48),
28
+ -21600 => RubyCoreSupport.rational_new!(-1, 4),
29
+ -19800 => RubyCoreSupport.rational_new!(-11, 48),
30
+ -18000 => RubyCoreSupport.rational_new!(-5, 24),
31
+ -16200 => RubyCoreSupport.rational_new!(-3, 16),
32
+ -14400 => RubyCoreSupport.rational_new!(-1, 6),
33
+ -12600 => RubyCoreSupport.rational_new!(-7, 48),
34
+ -10800 => RubyCoreSupport.rational_new!(-1, 8),
35
+ -9000 => RubyCoreSupport.rational_new!(-5, 48),
36
+ -7200 => RubyCoreSupport.rational_new!(-1, 12),
37
+ -5400 => RubyCoreSupport.rational_new!(-1, 16),
38
+ -3600 => RubyCoreSupport.rational_new!(-1, 24),
39
+ -1800 => RubyCoreSupport.rational_new!(-1, 48),
40
+ 0 => RubyCoreSupport.rational_new!(0, 1),
41
+ 1800 => RubyCoreSupport.rational_new!(1, 48),
42
+ 3600 => RubyCoreSupport.rational_new!(1, 24),
43
+ 5400 => RubyCoreSupport.rational_new!(1, 16),
44
+ 7200 => RubyCoreSupport.rational_new!(1, 12),
45
+ 9000 => RubyCoreSupport.rational_new!(5, 48),
46
+ 10800 => RubyCoreSupport.rational_new!(1, 8),
47
+ 12600 => RubyCoreSupport.rational_new!(7, 48),
48
+ 14400 => RubyCoreSupport.rational_new!(1, 6),
49
+ 16200 => RubyCoreSupport.rational_new!(3, 16),
50
+ 18000 => RubyCoreSupport.rational_new!(5, 24),
51
+ 19800 => RubyCoreSupport.rational_new!(11, 48),
52
+ 21600 => RubyCoreSupport.rational_new!(1, 4),
53
+ 23400 => RubyCoreSupport.rational_new!(13, 48),
54
+ 25200 => RubyCoreSupport.rational_new!(7, 24),
55
+ 27000 => RubyCoreSupport.rational_new!(5, 16),
56
+ 28800 => RubyCoreSupport.rational_new!(1, 3),
57
+ 30600 => RubyCoreSupport.rational_new!(17, 48),
58
+ 32400 => RubyCoreSupport.rational_new!(3, 8),
59
+ 34200 => RubyCoreSupport.rational_new!(19, 48),
60
+ 36000 => RubyCoreSupport.rational_new!(5, 12),
61
+ 37800 => RubyCoreSupport.rational_new!(7, 16),
62
+ 39600 => RubyCoreSupport.rational_new!(11, 24),
63
+ 41400 => RubyCoreSupport.rational_new!(23, 48),
64
+ 43200 => RubyCoreSupport.rational_new!(1, 2),
65
+ 45000 => RubyCoreSupport.rational_new!(25, 48),
66
+ 46800 => RubyCoreSupport.rational_new!(13, 24),
67
+ 48600 => RubyCoreSupport.rational_new!(9, 16),
68
+ 50400 => RubyCoreSupport.rational_new!(7, 12)}
69
+
70
+ # Returns a Rational expressing the fraction of a day that offset in
71
+ # seconds represents (i.e. equivalent to Rational(offset, 86400)).
72
+ def rational_for_offset(offset)
73
+ @@rational_cache[offset] || Rational(offset, 86400)
74
+ end
75
+
76
+ module_function :rational_for_offset
77
+ end
78
+ end
79
+ end
@@ -0,0 +1,33 @@
1
+ require 'rational'
2
+
3
+ module TimeCrisis
4
+ module TZInfo
5
+
6
+ # Methods to support different versions of Ruby.
7
+ module RubyCoreSupport #:nodoc:
8
+
9
+ # Use Rational.new! for performance reasons in Ruby 1.8.
10
+ # This has been removed from 1.9, but Rational performs better.
11
+ if Rational.respond_to? :new!
12
+ def self.rational_new!(numerator, denominator = 1)
13
+ Rational.new!(numerator, denominator)
14
+ end
15
+ else
16
+ def self.rational_new!(numerator, denominator = 1)
17
+ Rational(numerator, denominator)
18
+ end
19
+ end
20
+
21
+ def self.datetime_new!(ajd = 0, offset = 0, sg = nil)
22
+ if offset < 0
23
+ offset = 1 + offset
24
+ end
25
+
26
+ jd, fract = (ajd + offset + Rational(1, 2)).divmod(1)
27
+
28
+ ::TimeCrisis::DateTime.jd_fract(jd.to_i, fract.to_f)
29
+ end
30
+
31
+ end
32
+ end
33
+ end
@@ -0,0 +1,309 @@
1
+ require 'time_crisis/tzinfo/offset_rationals'
2
+
3
+ module TimeCrisis
4
+ module TZInfo
5
+ # Used by TZInfo internally to represent either a Time, DateTime or integer
6
+ # timestamp (seconds since 1970-01-01 00:00:00).
7
+ class TimeOrDateTime #:nodoc:
8
+ include Comparable
9
+
10
+ # Constructs a new TimeOrDateTime. timeOrDateTime can be a Time, DateTime
11
+ # or an integer. If using a Time or DateTime, any time zone information is
12
+ # ignored.
13
+ def initialize(timeOrDateTime)
14
+ @time = nil
15
+ @datetime = nil
16
+ @timestamp = nil
17
+
18
+ if timeOrDateTime.is_a?(Time)
19
+ @time = timeOrDateTime
20
+ @time = Time.utc(@time.year, @time.mon, @time.mday, @time.hour, @time.min, @time.sec) unless @time.zone == 'UTC'
21
+ @orig = @time
22
+ elsif timeOrDateTime.is_a?(::TimeCrisis::DateTime)
23
+ @tcdatetime = timeOrDateTime
24
+ @tcdatetime.offset = 0
25
+ @orig = @tcdatetime
26
+ elsif timeOrDateTime.is_a?(::DateTime)
27
+ @datetime = timeOrDateTime
28
+ @datetime = @datetime.new_offset(0) unless @datetime.offset == 0
29
+ @orig = @datetime
30
+ else
31
+ @timestamp = timeOrDateTime.to_i
32
+ @orig = @timestamp
33
+ end
34
+ end
35
+
36
+ # Returns the time as a Time.
37
+ def to_time
38
+ unless @time
39
+ if @timestamp
40
+ @time = Time.at(@timestamp).utc
41
+ else
42
+ @time = Time.utc(year, mon, mday, hour, min, sec)
43
+ end
44
+ end
45
+
46
+ @time
47
+ end
48
+
49
+ # Returns the time as a DateTime.
50
+ def to_datetime
51
+ unless @datetime
52
+ @datetime = ::DateTime.civil(year, mon, mday, hour, min, sec)
53
+ end
54
+
55
+ @datetime
56
+ end
57
+
58
+ # returns the time as a TimeCrisis::DateTime
59
+ def to_tc_datetime
60
+ unless @tcdatetime
61
+ @tcdatetime = ::TimeCrisis::DateTime.civil(year, mon, mday, hour, min, sec)
62
+ end
63
+
64
+ @tcdatetime
65
+ end
66
+
67
+ # Returns the time as an integer timestamp.
68
+ def to_i
69
+ unless @timestamp
70
+ @timestamp = to_time.to_i
71
+ end
72
+
73
+ @timestamp
74
+ end
75
+
76
+ # Returns the time as the original time passed to new.
77
+ def to_orig
78
+ @orig
79
+ end
80
+
81
+ # Returns a string representation of the TimeOrDateTime.
82
+ def to_s
83
+ if @orig.is_a?(Time)
84
+ "Time: #{@orig.to_s}"
85
+ elsif @orig.is_a?(::TimeCrisis::DateTime)
86
+ "TimeCrisis::DateTime: #{@orig.to_s}"
87
+ elsif @orig.is_a?(::DateTime)
88
+ "DateTime: #{@orig.to_s}"
89
+ else
90
+ "Timestamp: #{@orig.to_s}"
91
+ end
92
+ end
93
+
94
+ # Returns internal object state as a programmer-readable string.
95
+ def inspect
96
+ "#<#{self.class}: #{@orig.inspect}>"
97
+ end
98
+
99
+ # Returns the year.
100
+ def year
101
+ if @time
102
+ @time.year
103
+ elsif @datetime
104
+ @datetime.year
105
+ elsif @tcdatetime
106
+ @tcdatetime.year
107
+ else
108
+ to_time.year
109
+ end
110
+ end
111
+
112
+ # Returns the month of the year (1..12).
113
+ def mon
114
+ if @time
115
+ @time.mon
116
+ elsif @datetime
117
+ @datetime.mon
118
+ elsif @tcdatetime
119
+ @tcdatetime.mon
120
+ else
121
+ to_time.mon
122
+ end
123
+ end
124
+
125
+ alias :month :mon
126
+
127
+ # Returns the day of the month (1..n).
128
+ def mday
129
+ if @time
130
+ @time.mday
131
+ elsif @datetime
132
+ @datetime.mday
133
+ elsif @tcdatetime
134
+ @tcdatetime.mday
135
+ else
136
+ to_time.mday
137
+ end
138
+ end
139
+
140
+ alias :day :mday
141
+
142
+ # Returns the hour of the day (0..23).
143
+ def hour
144
+ if @time
145
+ @time.hour
146
+ elsif @datetime
147
+ @datetime.hour
148
+ elsif @tcdatetime
149
+ @tcdatetime.hour
150
+ else
151
+ to_time.hour
152
+ end
153
+ end
154
+
155
+ # Returns the minute of the hour (0..59).
156
+ def min
157
+ if @time
158
+ @time.min
159
+ elsif @datetime
160
+ @datetime.min
161
+ elsif @tcdatetime
162
+ @tcdatetime.min
163
+ else
164
+ to_time.min
165
+ end
166
+ end
167
+
168
+ # Returns the second of the minute (0..60). (60 for a leap second).
169
+ def sec
170
+ if @time
171
+ @time.sec
172
+ elsif @datetime
173
+ @datetime.sec
174
+ elsif @tcdatetime
175
+ @tcdatetime.sec
176
+ else
177
+ to_time.sec
178
+ end
179
+ end
180
+
181
+ # Compares this TimeOrDateTime with another Time, DateTime, integer
182
+ # timestamp or TimeOrDateTime. Returns -1, 0 or +1 depending whether the
183
+ # receiver is less than, equal to, or greater than timeOrDateTime.
184
+ #
185
+ # Milliseconds and smaller units are ignored in the comparison.
186
+ def <=>(timeOrDateTime)
187
+ if timeOrDateTime.is_a?(TimeOrDateTime)
188
+ orig = timeOrDateTime.to_orig
189
+
190
+ if @orig.is_a?(::TimeCrisis::DateTime) || orig.is_a?(::TimeCrisis::DateTime)
191
+ to_tc_datetime <=> timeOrDateTime.to_tc_datetime
192
+ elsif @orig.is_a?(::DateTime) || orig.is_a?(::DateTime)
193
+ # If either is a DateTime, assume it is there for a reason
194
+ # (i.e. for range).
195
+ to_datetime <=> timeOrDateTime.to_datetime
196
+ elsif orig.is_a?(Time)
197
+ to_time <=> timeOrDateTime.to_time
198
+ else
199
+ to_i <=> timeOrDateTime.to_i
200
+ end
201
+ elsif @orig.is_a?(::TimeCrisis::DateTime) || timeOrDateTime.is_a?(::TimeCrisis::DateTime)
202
+ to_tc_datetime <=> TimeOrDateTime.wrap(timeOrDateTime).to_tc_datetime
203
+ elsif @orig.is_a?(::DateTime) || timeOrDateTime.is_a?(::DateTime)
204
+ # If either is a DateTime, assume it is there for a reason
205
+ # (i.e. for range).
206
+ to_datetime <=> TimeOrDateTime.wrap(timeOrDateTime).to_datetime
207
+ elsif timeOrDateTime.is_a?(Time)
208
+ to_time <=> timeOrDateTime
209
+ else
210
+ to_i <=> timeOrDateTime.to_i
211
+ end
212
+ end
213
+
214
+ # Adds a number of seconds to the TimeOrDateTime. Returns a new
215
+ # TimeOrDateTime, preserving what the original constructed type was.
216
+ # If the original type is a Time and the resulting calculation goes out of
217
+ # range for Times, then an exception will be raised by the Time class.
218
+ def +(seconds)
219
+ if seconds == 0
220
+ self
221
+ else
222
+ if @orig.is_a?(::TimeCrisis::DateTime)
223
+ TimeOrDateTime.new(@orig + OffsetRationals.rational_for_offset(seconds).to_f)
224
+ elsif @orig.is_a?(::DateTime)
225
+ TimeOrDateTime.new(@orig + OffsetRationals.rational_for_offset(seconds))
226
+ else
227
+ # + defined for Time and integer timestamps
228
+ TimeOrDateTime.new(@orig + seconds)
229
+ end
230
+ end
231
+ end
232
+
233
+ # Subtracts a number of seconds from the TimeOrDateTime. Returns a new
234
+ # TimeOrDateTime, preserving what the original constructed type was.
235
+ # If the original type is a Time and the resulting calculation goes out of
236
+ # range for Times, then an exception will be raised by the Time class.
237
+ def -(seconds)
238
+ self + (-seconds)
239
+ end
240
+
241
+ # Similar to the + operator, but for cases where adding would cause a
242
+ # timestamp or time to go out of the allowed range, converts to a DateTime
243
+ # based TimeOrDateTime.
244
+ def add_with_convert(seconds)
245
+ if seconds == 0
246
+ self
247
+ else
248
+ if @orig.is_a?(::TimeCrisis::DateTime)
249
+ TimeOrDateTime.new(@orig + OffsetRationals.rational_for_offset(seconds).to_f)
250
+ elsif @orig.is_a?(::DateTime)
251
+ TimeOrDateTime.new(@orig + OffsetRationals.rational_for_offset(seconds))
252
+ else
253
+ # A Time or timestamp.
254
+ result = to_i + seconds
255
+
256
+ if result < 0 || result > 2147483647
257
+ result = TimeOrDateTime.new(to_tc_datetime + OffsetRationals.rational_for_offset(seconds).to_f)
258
+ else
259
+ result = TimeOrDateTime.new(@orig + seconds)
260
+ end
261
+ end
262
+ end
263
+ end
264
+
265
+ # Returns true if todt represents the same time and was originally
266
+ # constructed with the same type (DateTime, Time or timestamp) as this
267
+ # TimeOrDateTime.
268
+ def eql?(todt)
269
+ todt.respond_to?(:to_orig) && to_orig.eql?(todt.to_orig)
270
+ end
271
+
272
+ # Returns a hash of this TimeOrDateTime.
273
+ def hash
274
+ @orig.hash
275
+ end
276
+
277
+ # If no block is given, returns a TimeOrDateTime wrapping the given
278
+ # timeOrDateTime. If a block is specified, a TimeOrDateTime is constructed
279
+ # and passed to the block. The result of the block must be a TimeOrDateTime.
280
+ # to_orig will be called on the result and the result of to_orig will be
281
+ # returned.
282
+ #
283
+ # timeOrDateTime can be a Time, DateTime, integer timestamp or TimeOrDateTime.
284
+ # If a TimeOrDateTime is passed in, no new TimeOrDateTime will be constructed,
285
+ # the passed in value will be used.
286
+ def self.wrap(timeOrDateTime)
287
+ t = timeOrDateTime.is_a?(TimeOrDateTime) ? timeOrDateTime : TimeOrDateTime.new(timeOrDateTime)
288
+
289
+ if block_given?
290
+ t = yield t
291
+
292
+ if timeOrDateTime.is_a?(TimeOrDateTime)
293
+ t
294
+ elsif timeOrDateTime.is_a?(Time)
295
+ t.to_time
296
+ elsif timeOrDateTime.is_a?(::TimeCrisis::DateTime)
297
+ t.to_tc_datetime
298
+ elsif timeOrDateTime.is_a?(::DateTime)
299
+ t.to_datetime
300
+ else
301
+ t.to_i
302
+ end
303
+ else
304
+ t
305
+ end
306
+ end
307
+ end
308
+ end
309
+ end