timezone 0.1.1 → 0.1.2

Sign up to get free protection for your applications and to get access to all the features.
data/README.markdown CHANGED
@@ -7,7 +7,7 @@ A simple way to get accurate current and historical timezone information based o
7
7
  Add the following to your Gemfile:
8
8
 
9
9
  gem 'timezone'
10
-
10
+
11
11
  Then install your bundle.
12
12
 
13
13
  bundle install
@@ -16,14 +16,14 @@ Then install your bundle.
16
16
 
17
17
  Getting the current time or any historical time in any timezone, with daylight savings time taken into consideration, is easy:
18
18
 
19
- timezone = Timezone.new :zone => 'America/Los_Angeles'
19
+ timezone = Timezone::Zone.new :zone => 'America/Los_Angeles'
20
20
  timezone.time Time.now
21
21
  => 2011-02-11 17:29:05 UTC
22
22
  timezone.time Time.utc(2010, 1, 1, 0, 0, 0)
23
23
  => 2009-12-31 16:00:00 UTC
24
-
24
+
25
25
  Time is always returned in the UTC timezone, but it accurately reflects the actual time in the specified timezone. The reason for this is that this function also takes into account daylight savings time, which can alter the timezone offset and hence put Ruby in the wrong timezone.
26
-
26
+
27
27
  ## Getting the timezone for a specific latitude and longitude
28
28
 
29
29
  First, make sure you have a geonames username. It's free and easy to setup, you can do so [here](http://www.geonames.org/login).
@@ -33,11 +33,11 @@ Second, add the following to your application.rb file, or before you perform a c
33
33
  Timezone::Configure.begin do |c|
34
34
  c.username = 'your_geonames_username_goes_here'
35
35
  end
36
-
36
+
37
37
  Finally, pass the coordinates to your timezone initialization function.
38
38
 
39
- timezone = Timezone.new :latlon => [-34.92771808058, 138.477041423321]
39
+ timezone = Timezone::Zone.new :latlon => [-34.92771808058, 138.477041423321]
40
40
  timezone.zone
41
41
  => "Australia/Adelaide"
42
42
  timezone.time Time.now
43
- => 2011-02-12 12:02:13 UTC
43
+ => 2011-02-12 12:02:13 UTC
@@ -1,3 +1,3 @@
1
1
  module Timezone
2
- VERSION = "0.1.1"
2
+ VERSION = "0.1.2"
3
3
  end
data/lib/timezone/zone.rb CHANGED
@@ -9,9 +9,9 @@ module Timezone
9
9
  class Zone
10
10
  include Comparable
11
11
  attr_accessor :rules, :zone
12
-
12
+
13
13
  # Create a new Timezone object.
14
- #
14
+ #
15
15
  # Timezone.new(options)
16
16
  #
17
17
  # :zone - The actual name of the zone. For example, Australia/Sydney or Americas/Los_Angeles.
@@ -48,24 +48,27 @@ module Timezone
48
48
  # offset in the timezone rules. Once the offset has been found that offset is added to the reference UTC time
49
49
  # to calculate the reference time in the timezone.
50
50
  def time reference
51
- reference = reference.utc
52
- rule = rules.detect{ |rule| _parsetime(rule['_from']) <= reference && _parsetime(rule['_to']) >= reference }
53
- reference + rule['offset']
51
+ reference.utc + rule_for_reference(reference)['offset']
54
52
  end
55
-
53
+
56
54
  # Get the current UTC offset in seconds for this timezone.
57
55
  #
58
- # timezone.utc_offset
59
- def utc_offset
60
- @rules.last['offset']-(@rules.last['dst'] ? 3600 : 0)
56
+ # timezone.utc_offset(reference)
57
+ def utc_offset reference=Time.now
58
+ rule_for_reference(reference)['offset']
61
59
  end
62
-
60
+
63
61
  def <=> zone #:nodoc:
64
62
  utc_offset <=> zone.utc_offset
65
63
  end
66
-
64
+
67
65
  private
68
66
 
67
+ def rule_for_reference reference
68
+ reference = reference.utc
69
+ rules.detect{ |rule| _parsetime(rule['_from']) <= reference && _parsetime(rule['_to']) > reference }
70
+ end
71
+
69
72
  def timezone_id lat, lon #:nodoc:
70
73
  begin
71
74
  response = Net::HTTP.get('ws.geonames.org', "/timezoneJSON?lat=#{lat}&lng=#{lon}&username=#{Timezone::Configure.username}")
@@ -83,4 +86,4 @@ module Timezone
83
86
  end
84
87
  end
85
88
  end
86
- end
89
+ end
@@ -9,19 +9,19 @@ class TimezoneTest < Test::Unit::TestCase
9
9
  Timezone::Zone.new :zone => 'Australia/Sydney'
10
10
  end
11
11
  end
12
-
12
+
13
13
  def test_nil_timezone
14
14
  assert_raise Timezone::Error::NilZone do
15
15
  Timezone::Zone.new :zone => nil
16
16
  end
17
17
  end
18
-
18
+
19
19
  def test_invalid_timezone
20
20
  assert_raise Timezone::Error::InvalidZone do
21
21
  Timezone::Zone.new :zone => 'Foo/Bar'
22
22
  end
23
23
  end
24
-
24
+
25
25
  def time_timezone_equivalence
26
26
  gmt = Timezone::Zone.new :zone => 'GMT'
27
27
  australia = Timezone::Zone.new :zone => 'Australia/Sydney'
@@ -29,42 +29,45 @@ class TimezoneTest < Test::Unit::TestCase
29
29
  assert gmt <= australia
30
30
  assert gmt < australia
31
31
  end
32
-
32
+
33
33
  def test_getting_utc_offset
34
- assert_equal 36000, Timezone::Zone.new(:zone => 'Australia/Sydney').utc_offset
35
- assert_equal -28800, Timezone::Zone.new(:zone => 'America/Los_Angeles').utc_offset
36
- assert_equal 20700, Timezone::Zone.new(:zone => 'Asia/Kathmandu').utc_offset
34
+ assert_equal 36000, Timezone::Zone.new(:zone => 'Australia/Sydney').utc_offset(Time.parse('2011-06-05'))
35
+ assert_equal -25200, Timezone::Zone.new(:zone => 'America/Los_Angeles').utc_offset(Time.parse('2011-06-05'))
36
+ assert_equal 20700, Timezone::Zone.new(:zone => 'Asia/Kathmandu').utc_offset(Time.parse('2011-06-05'))
37
+
38
+ assert_equal -18000, Timezone::Zone.new(:zone => 'America/New_York').utc_offset(Time.parse('2011-01-11'))
39
+ assert_equal -14400, Timezone::Zone.new(:zone => 'America/New_York').utc_offset(Time.parse('2011-06-11'))
37
40
  end
38
-
41
+
39
42
  def test_loading_GMT_timezone
40
43
  timezone = Timezone::Zone.new :zone => 'GMT'
41
44
  assert_equal Time.now.utc.to_i, timezone.time(Time.now).to_i
42
45
  end
43
-
46
+
44
47
  def test_loading_historical_time
45
48
  timezone = Timezone::Zone.new :zone => 'America/Los_Angeles'
46
49
  local = Time.strptime('2011-02-11T13:20:00Z', '%Y-%m-%dT%H:%M:%SZ')
47
50
  utc = Time.strptime('2011-02-11T21:20:00Z', '%Y-%m-%dT%H:%M:%SZ')
48
51
  assert_equal local.to_i, timezone.time(utc).to_i
49
52
  end
50
-
53
+
51
54
  def test_loading_half_hour_timezone
52
55
  timezone = Timezone::Zone.new :zone => 'Asia/Kathmandu'
53
56
  utc = Time.utc(2011, 1, 4, 3, 51, 29)
54
57
  local = Time.utc(2011, 1, 4, 9, 36, 29)
55
58
  assert_equal local.to_i, timezone.time(utc).to_i
56
59
  end
57
-
60
+
58
61
  def test_using_lat_lon_coordinates
59
62
  Timezone::Configure.begin { |c| c.username = 'timezone' }
60
63
  timezone = Timezone::Zone.new :latlon => [-34.92771808058, 138.477041423321]
61
64
  assert_equal 'Australia/Adelaide', timezone.zone
62
65
  end
63
-
66
+
64
67
  def test_australian_timezone_with_dst
65
68
  timezone = Timezone::Zone.new :zone => 'Australia/Adelaide'
66
69
  utc = Time.utc(2010, 12, 23, 19, 37, 15)
67
70
  local = Time.utc(2010, 12, 24, 6, 7, 15)
68
71
  assert_equal local.to_i, timezone.time(utc).to_i
69
72
  end
70
- end
73
+ end
metadata CHANGED
@@ -1,30 +1,29 @@
1
- --- !ruby/object:Gem::Specification
1
+ --- !ruby/object:Gem::Specification
2
2
  name: timezone
3
- version: !ruby/object:Gem::Version
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.1.2
4
5
  prerelease:
5
- version: 0.1.1
6
6
  platform: ruby
7
- authors:
7
+ authors:
8
8
  - Pan Thomakos
9
9
  autorequire:
10
10
  bindir: bin
11
11
  cert_chain: []
12
-
13
- date: 2011-02-22 00:00:00 -08:00
12
+ date: 2011-06-06 00:00:00.000000000 -07:00
14
13
  default_executable:
15
14
  dependencies: []
16
-
17
- description: A simple way to get accurate current and historical timezone information based on zone or latitude and longitude coordinates. This gem uses the tz database (http://www.twinsun.com/tz/tz-link.htm) for historical timezone information. It also uses the geonames API for timezone latitude and longitude lookup (http://www.geonames.org/export/web-services.html).
18
- email:
15
+ description: A simple way to get accurate current and historical timezone information
16
+ based on zone or latitude and longitude coordinates. This gem uses the tz database
17
+ (http://www.twinsun.com/tz/tz-link.htm) for historical timezone information. It
18
+ also uses the geonames API for timezone latitude and longitude lookup (http://www.geonames.org/export/web-services.html).
19
+ email:
19
20
  - pan.thomakos@gmail.com
20
21
  executables: []
21
-
22
22
  extensions: []
23
-
24
- extra_rdoc_files:
23
+ extra_rdoc_files:
25
24
  - README.markdown
26
25
  - License.txt
27
- files:
26
+ files:
28
27
  - .gitignore
29
28
  - Gemfile
30
29
  - License.txt
@@ -499,30 +498,28 @@ files:
499
498
  has_rdoc: true
500
499
  homepage: http://github.com/panthomakos/timezone
501
500
  licenses: []
502
-
503
501
  post_install_message:
504
- rdoc_options:
502
+ rdoc_options:
505
503
  - --charset=UTF-8
506
- require_paths:
504
+ require_paths:
507
505
  - lib
508
- required_ruby_version: !ruby/object:Gem::Requirement
506
+ required_ruby_version: !ruby/object:Gem::Requirement
509
507
  none: false
510
- requirements:
511
- - - ">="
512
- - !ruby/object:Gem::Version
513
- version: "0"
514
- required_rubygems_version: !ruby/object:Gem::Requirement
508
+ requirements:
509
+ - - ! '>='
510
+ - !ruby/object:Gem::Version
511
+ version: '0'
512
+ required_rubygems_version: !ruby/object:Gem::Requirement
515
513
  none: false
516
- requirements:
517
- - - ">="
518
- - !ruby/object:Gem::Version
519
- version: "0"
514
+ requirements:
515
+ - - ! '>='
516
+ - !ruby/object:Gem::Version
517
+ version: '0'
520
518
  requirements: []
521
-
522
519
  rubyforge_project: timezone
523
- rubygems_version: 1.5.2
520
+ rubygems_version: 1.6.2
524
521
  signing_key:
525
522
  specification_version: 3
526
- summary: timezone-0.1.1
527
- test_files:
523
+ summary: timezone-0.1.2
524
+ test_files:
528
525
  - test/timezone_test.rb