timezone 0.1.3 → 0.1.4

Sign up to get free protection for your applications and to get access to all the features.
data/.gitignore CHANGED
@@ -2,5 +2,6 @@
2
2
  .DS_Store
3
3
  .rvmrc
4
4
  .bundle
5
+ .idea/*
5
6
  Gemfile.lock
6
7
  pkg/*
data/README.markdown CHANGED
@@ -47,4 +47,37 @@ Finally, pass the coordinates to your timezone initialization function.
47
47
  Retrieving the complete list of timezones is quite simple:
48
48
 
49
49
  timezones = Timezone::Zone.names
50
- => ["Africa/Abidjan", "Africa/Accra", "Africa/Addis_Ababa", "Africa/Algiers", ...]
50
+ => ["Africa/Abidjan", "Africa/Accra", "Africa/Addis_Ababa", "Africa/Algiers", ...]
51
+
52
+ ## Listing current information from specific timezones
53
+
54
+ If you need information from a specific set of timezones rather than a complete list or one at a time, this can be accomplished with the following:
55
+
56
+ zone_list = Timezone::Zone.list "America/Chicago", "America/New_York", "America/Boise"
57
+ # This will return an array of information hashes in the following format:
58
+ # {
59
+ # :zone => "America/Chicago",
60
+ # :title => "America/Chicago", # this can be customized to your needs
61
+ # :offset => -18000, # UTC offset in seconds
62
+ # :utc_offset => -5, # UTC offset in hours
63
+ # :dst => false
64
+ # }
65
+
66
+ You can customize what is placed in the `:title` key in the configuration block. This would be useful in the case of an HTML select list that you would like to display different values than the default name. For example, the following configuration will set the `:title` key in the list hash to "Chicago" rather than "America/Chicago".
67
+
68
+ Timezone::Configure.build do |c|
69
+ c.replace "America/Chicago", with: "Chicago"
70
+ end
71
+
72
+ Also, if you make numerous calls to the **Zone#list** method in your software, but you would like to avoid duplicating which timezones to retrieve, you can set a default in the configuration:
73
+
74
+ Timezone::Configure.begin do |c|
75
+ c.default_for_list = "America/Chicago", "America/New_York", "Australia/Sydney"
76
+ end
77
+
78
+ Finally, by default the **Zone#list** method will order the results by the timezone's UTC offset. You can customize this behavior this way:
79
+
80
+ Timezone::Configure.begin do |c|
81
+ # this can equal any hash key returned by the Zone#list method
82
+ c.order_list_by = :title
83
+ end
@@ -9,16 +9,50 @@ module Timezone
9
9
  # your application for latitude and longitude based timezone searches. If you aren't going to
10
10
  # initialize timezone objects based on latitude and longitude then this configuration is not necessary.
11
11
  class Configure
12
+ def self.url
13
+ @@url ||= 'ws.geonames.org'
14
+ end
15
+
16
+ def self.url= url
17
+ @@url = url
18
+ end
19
+
12
20
  def self.username
13
21
  @@username
14
22
  end
15
-
23
+
16
24
  def self.username= username
17
25
  @@username = username
18
26
  end
19
-
27
+
20
28
  def self.begin
21
29
  yield self
22
30
  end
31
+
32
+ def self.replace(what, with = Hash.new)
33
+ replacements # instantiate @@replacements
34
+ @@replacements[what] = with[:with]
35
+ end
36
+
37
+ def self.replacements
38
+ @@replacements ||= {}
39
+ end
40
+
41
+ def self.default_for_list
42
+ @@default_list ||= nil
43
+ end
44
+
45
+ def self.default_for_list=(*list)
46
+ @@default_list = list.flatten!
47
+ end
48
+
49
+ def self.order_list_by
50
+ @@order_by ||= :utc_offset
51
+ end
52
+
53
+ def self.order_list_by=(order)
54
+ @@order_by = order
55
+ end
56
+
23
57
  end
24
- end
58
+ end
@@ -1,3 +1,3 @@
1
1
  module Timezone
2
- VERSION = "0.1.3"
2
+ VERSION = "0.1.4"
3
3
  end
data/lib/timezone/zone.rb CHANGED
@@ -38,28 +38,6 @@ module Timezone
38
38
  @zone = data['_zone'] || options[:zone]
39
39
  end
40
40
 
41
-
42
- class << self
43
-
44
- # Retrieve the data from a particular time zone
45
- def get_zone_data(zone)
46
- file = File.join(ZONE_FILE_PATH, "#{zone}.json")
47
- begin
48
- return JSON.parse(open(file).read)
49
- rescue
50
- raise Timezone::Error::InvalidZone, "'#{zone}' is not a valid zone."
51
- end
52
- end
53
-
54
- # Instantly grab all possible time zone names.
55
- def names
56
- Dir[File.join(ZONE_FILE_PATH, "**/**/*.json")].collect do |file|
57
- file.gsub("#{ZONE_FILE_PATH}/", '').gsub(/\.json/, '')
58
- end
59
- end
60
-
61
- end
62
-
63
41
  # Determine the time in the timezone.
64
42
  #
65
43
  # timezone.time(reference)
@@ -84,16 +62,64 @@ module Timezone
84
62
  utc_offset <=> zone.utc_offset
85
63
  end
86
64
 
87
- private
65
+ class << self
66
+
67
+ # Retrieve the data from a particular time zone
68
+ def get_zone_data(zone)
69
+ file = File.join(ZONE_FILE_PATH, "#{zone}.json")
70
+ begin
71
+ return JSON.parse(open(file).read)
72
+ rescue
73
+ raise Timezone::Error::InvalidZone, "'#{zone}' is not a valid zone."
74
+ end
75
+ end
76
+
77
+ # Instantly grab all possible time zone names.
78
+ def names
79
+ @@names ||= Dir[File.join(ZONE_FILE_PATH, "**/**/*.json")].collect do |file|
80
+ file.gsub("#{ZONE_FILE_PATH}/", '').gsub(".json", '')
81
+ end
82
+ end
83
+
84
+ # Get a list of specified timezones and the basic information accompanying that zone
85
+ #
86
+ # zones = Timezone::Zone.infos(zones)
87
+ #
88
+ # zones - An array of timezone names. (i.e. Timezone::Zones.infos("America/Chicago", "Australia/Sydney"))
89
+ #
90
+ # The result is a Hash of timezones with their title, offset in seconds, UTC offset, and if it uses DST.
91
+ #
92
+ def list(*args)
93
+ args = nil if args.empty? # set to nil if no args are provided
94
+ zones = args || Configure.default_for_list || self.names # get default list
95
+ list = self.names.select { |name| zones.include? name } # only select zones if they exist
96
+
97
+ @zones = []
98
+ list.each do |zone|
99
+ item = Zone.new(zone: zone)
100
+ @zones << {
101
+ :zone => item.zone,
102
+ :title => Configure.replacements[item.zone] || item.zone,
103
+ :offset => item.utc_offset,
104
+ :utc_offset => (item.utc_offset/(60*60)),
105
+ :dst => item.time(Time.now).dst?
106
+ }
107
+ end
108
+ @zones.sort_by! { |zone| zone[Configure.order_list_by] }
109
+ end
110
+
111
+ end
112
+
113
+ private
88
114
 
89
115
  def rule_for_reference reference
90
116
  reference = reference.utc
91
- rules.detect{ |rule| _parsetime(rule['_from']) <= reference && _parsetime(rule['_to']) > reference }
117
+ @rules.detect{ |rule| _parsetime(rule['_from']) <= reference && _parsetime(rule['_to']) > reference }
92
118
  end
93
119
 
94
120
  def timezone_id lat, lon #:nodoc:
95
121
  begin
96
- response = Net::HTTP.get('ws.geonames.org', "/timezoneJSON?lat=#{lat}&lng=#{lon}&username=#{Timezone::Configure.username}")
122
+ response = Net::HTTP.get(Timezone::Configure.url, "/timezoneJSON?lat=#{lat}&lng=#{lon}&username=#{Timezone::Configure.username}")
97
123
  JSON.parse(response)['timezoneId']
98
124
  rescue Exception => e
99
125
  raise Timezone::Error::GeoNames, e.message
@@ -22,6 +22,30 @@ class TimezoneTest < Test::Unit::TestCase
22
22
  end
23
23
  end
24
24
 
25
+ def test_timezone_list
26
+ list = Timezone::Zone.list "Australia/Sydney", "America/Chicago"
27
+ assert list.is_a?(Array)
28
+ assert list.count == 2
29
+ assert list.first.is_a?(Hash)
30
+ assert list.first[:zone] == "Australia/Sydney"
31
+ end
32
+
33
+ def test_timezone_custom_list_order
34
+ Timezone::Configure.order_list_by = :title
35
+ Timezone::Configure.replace "America/Chicago", with: "Chicago"
36
+ list = Timezone::Zone.list "Australia/Sydney", "America/Chicago"
37
+ assert list.first[:title] == "Australia/Sydney"
38
+ assert list.last[:title] == "Chicago"
39
+ assert list.last[:zone] == "America/Chicago"
40
+ end
41
+
42
+ def test_timezone_default_list
43
+ Timezone::Configure.default_for_list = "America/Chicago", "Australia/Sydney"
44
+ list = Timezone::Zone.list
45
+ assert list.count == 2
46
+ assert list.first.has_value? "Australia/Sydney"
47
+ end
48
+
25
49
  def test_timezone_names
26
50
  zones = Timezone::Zone.names
27
51
  assert zones.is_a?(Array)
@@ -77,4 +101,16 @@ class TimezoneTest < Test::Unit::TestCase
77
101
  local = Time.utc(2010, 12, 24, 6, 7, 15)
78
102
  assert_equal local.to_i, timezone.time(utc).to_i
79
103
  end
104
+
105
+ def test_configure_url_default
106
+ assert_equal 'ws.geonames.org', Timezone::Configure.url
107
+ end
108
+
109
+ def test_configure_url_custom
110
+ Timezone::Configure.begin { |c| c.url = 'www.newtimezoneserver.com' }
111
+ assert_equal 'www.newtimezoneserver.com', Timezone::Configure.url
112
+ # clean up url after test
113
+ Timezone::Configure.begin { |c| c.url = nil }
114
+ end
115
+
80
116
  end
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.1.3
4
+ version: 0.1.4
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: 2011-10-21 00:00:00.000000000Z
12
+ date: 2012-01-11 00:00:00.000000000Z
13
13
  dependencies: []
14
14
  description: A simple way to get accurate current and historical timezone information
15
15
  based on zone or latitude and longitude coordinates. This gem uses the tz database
@@ -515,9 +515,9 @@ required_rubygems_version: !ruby/object:Gem::Requirement
515
515
  version: '0'
516
516
  requirements: []
517
517
  rubyforge_project: timezone
518
- rubygems_version: 1.8.6
518
+ rubygems_version: 1.8.10
519
519
  signing_key:
520
520
  specification_version: 3
521
- summary: timezone-0.1.3
521
+ summary: timezone-0.1.4
522
522
  test_files:
523
523
  - test/timezone_test.rb