timezone 0.3.5 → 0.3.6
Sign up to get free protection for your applications and to get access to all the features.
- data/CHANGES.markdown +6 -2
- data/README.markdown +13 -6
- data/lib/timezone/version.rb +1 -1
- data/lib/timezone/zone.rb +21 -4
- data/test/timezone_test.rb +19 -0
- metadata +3 -3
data/CHANGES.markdown
CHANGED
data/README.markdown
CHANGED
@@ -26,15 +26,22 @@ Getting the current time or any historical time in any timezone, with daylight
|
|
26
26
|
savings time taken into consideration, is easy:
|
27
27
|
|
28
28
|
timezone = Timezone::Zone.new :zone => 'America/Los_Angeles'
|
29
|
-
|
29
|
+
|
30
|
+
timezone.time(Time.now)
|
30
31
|
=> 2011-02-11 17:29:05 UTC
|
31
|
-
|
32
|
+
|
33
|
+
timezone.time(Time.utc(2010, 1, 1, 0, 0, 0))
|
32
34
|
=> 2009-12-31 16:00:00 UTC
|
33
35
|
|
34
|
-
Time
|
35
|
-
|
36
|
-
|
37
|
-
|
36
|
+
timezone.time_with_offset(Time.utc(2010,1,1,0,0,0))
|
37
|
+
=> 2009-12-31 16:00:00 -0800
|
38
|
+
|
39
|
+
Time is always returned in the UTC timezone when using the `time` function, but
|
40
|
+
it accurately reflects the actual time in the specified timezone. The reason for
|
41
|
+
this is that this function also takes into account daylight savings time and
|
42
|
+
historical changes in timezone, which can alter the offset. If you want a time
|
43
|
+
with the appropriate offset at the given time, then use the `time_with_offset`
|
44
|
+
function as shown above.
|
38
45
|
|
39
46
|
You can also query a `Timezone::Zone` object to determine if it was in Daylight
|
40
47
|
Savings Time:
|
data/lib/timezone/version.rb
CHANGED
data/lib/timezone/zone.rb
CHANGED
@@ -67,8 +67,25 @@ module Timezone
|
|
67
67
|
# The reference is converted to a UTC equivalent. That UTC equivalent is then used to lookup the appropriate
|
68
68
|
# offset in the timezone rules. Once the offset has been found that offset is added to the reference UTC time
|
69
69
|
# to calculate the reference time in the timezone.
|
70
|
-
def time
|
71
|
-
reference.utc +
|
70
|
+
def time(reference)
|
71
|
+
reference.utc + utc_offset(reference)
|
72
|
+
end
|
73
|
+
|
74
|
+
# Determine the time in the timezone w/ the appropriate offset.
|
75
|
+
#
|
76
|
+
# timezone.time_with_offset(reference)
|
77
|
+
#
|
78
|
+
# reference - the `Time` you want to convert.
|
79
|
+
#
|
80
|
+
# The reference is converted to a UTC equivalent. That UTC equivalent is
|
81
|
+
# then used to lookup the appropriate offset in the timezone rules. Once the
|
82
|
+
# offset has been found, that offset is added to the reference UTC time
|
83
|
+
# to calculate the reference time in the timezone. The offset is then
|
84
|
+
# appended to put the time object into the proper offset.
|
85
|
+
def time_with_offset(reference)
|
86
|
+
utc = time(reference)
|
87
|
+
offset = utc_offset(reference)
|
88
|
+
Time.new(utc.year, utc.month, utc.day, utc.hour, utc.min, utc.sec, offset)
|
72
89
|
end
|
73
90
|
|
74
91
|
# Whether or not the time in the timezone is in DST.
|
@@ -79,11 +96,11 @@ module Timezone
|
|
79
96
|
# Get the current UTC offset in seconds for this timezone.
|
80
97
|
#
|
81
98
|
# timezone.utc_offset(reference)
|
82
|
-
def utc_offset
|
99
|
+
def utc_offset(reference=Time.now)
|
83
100
|
rule_for_reference(reference)[OFFSET_BIT]
|
84
101
|
end
|
85
102
|
|
86
|
-
def <=>
|
103
|
+
def <=>(zone) #:nodoc:
|
87
104
|
utc_offset <=> zone.utc_offset
|
88
105
|
end
|
89
106
|
|
data/test/timezone_test.rb
CHANGED
@@ -147,6 +147,25 @@ class TimezoneTest < Test::Unit::TestCase
|
|
147
147
|
assert_equal local.to_i, timezone.time(utc).to_i
|
148
148
|
end
|
149
149
|
|
150
|
+
# Testing is done with strings since two times can be equivalent even if
|
151
|
+
# their offsets do not match, and we want to test that the time and offsets
|
152
|
+
# are equivalent.
|
153
|
+
def test_time_with_offset
|
154
|
+
timezone = Timezone::Zone.new :zone => 'Asia/Kathmandu'
|
155
|
+
utc = Time.utc(2011, 1, 4, 3, 51, 29)
|
156
|
+
local = Time.new(2011, 1, 4, 9, 36, 29, 20700)
|
157
|
+
assert_equal local.to_s, timezone.time_with_offset(utc).to_s
|
158
|
+
|
159
|
+
zone = Timezone::Zone.new(:zone => 'America/Los_Angeles')
|
160
|
+
utc = Time.utc(2014,12,15,22,00,00)
|
161
|
+
local = Time.new(2014,12,15,14,00,00,'-08:00')
|
162
|
+
assert_equal local.to_s, zone.time_with_offset(utc).to_s
|
163
|
+
|
164
|
+
utc = Time.utc(2014,4,5,22,00,00)
|
165
|
+
local = Time.new(2014,4,5,15,00,00,'-07:00')
|
166
|
+
assert_equal local.to_s, zone.time_with_offset(utc).to_s
|
167
|
+
end
|
168
|
+
|
150
169
|
class HTTPTestClient
|
151
170
|
class << self ; attr_accessor :body ; end
|
152
171
|
|
metadata
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: timezone
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 0.3.
|
4
|
+
version: 0.3.6
|
5
5
|
prerelease:
|
6
6
|
platform: ruby
|
7
7
|
authors:
|
@@ -9,7 +9,7 @@ authors:
|
|
9
9
|
autorequire:
|
10
10
|
bindir: bin
|
11
11
|
cert_chain: []
|
12
|
-
date: 2014-12-
|
12
|
+
date: 2014-12-16 00:00:00.000000000 Z
|
13
13
|
dependencies:
|
14
14
|
- !ruby/object:Gem::Dependency
|
15
15
|
name: rake
|
@@ -721,7 +721,7 @@ rubyforge_project: timezone
|
|
721
721
|
rubygems_version: 1.8.23.2
|
722
722
|
signing_key:
|
723
723
|
specification_version: 3
|
724
|
-
summary: timezone-0.3.
|
724
|
+
summary: timezone-0.3.6
|
725
725
|
test_files:
|
726
726
|
- test/data/Helsinki_rules_without_timestamps.json
|
727
727
|
- test/data/asia
|