timezone 0.3.5 → 0.3.6

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.
data/CHANGES.markdown CHANGED
@@ -1,6 +1,10 @@
1
- # 0.3.5+
1
+ # 0.3.6
2
2
 
3
- * `0.3.5` - Updated with tzdata-2014j-1. (panthomakos)
3
+ * Added `Timezone::Zone#time_with_offset` functionality. (panthomakos)
4
+
5
+ # 0.3.5
6
+
7
+ * Updated with tzdata-2014j-1. (panthomakos)
4
8
 
5
9
  # 0.3.4
6
10
 
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
- timezone.time Time.now
29
+
30
+ timezone.time(Time.now)
30
31
  => 2011-02-11 17:29:05 UTC
31
- timezone.time Time.utc(2010, 1, 1, 0, 0, 0)
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 is always returned in the UTC timezone, but it accurately reflects the
35
- actual time in the specified timezone. The reason for this is that this function
36
- also takes into account daylight savings time, which can alter the timezone
37
- offset and hence put Ruby in the wrong timezone.
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:
@@ -1,3 +1,3 @@
1
1
  module Timezone
2
- VERSION = "0.3.5"
2
+ VERSION = "0.3.6"
3
3
  end
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 reference
71
- reference.utc + rule_for_reference(reference)[OFFSET_BIT]
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 reference=Time.now
99
+ def utc_offset(reference=Time.now)
83
100
  rule_for_reference(reference)[OFFSET_BIT]
84
101
  end
85
102
 
86
- def <=> zone #:nodoc:
103
+ def <=>(zone) #:nodoc:
87
104
  utc_offset <=> zone.utc_offset
88
105
  end
89
106
 
@@ -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.5
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-05 00:00:00.000000000 Z
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.5
724
+ summary: timezone-0.3.6
725
725
  test_files:
726
726
  - test/data/Helsinki_rules_without_timestamps.json
727
727
  - test/data/asia