timezone 0.6.0 → 0.99.0

Sign up to get free protection for your applications and to get access to all the features.
Files changed (43) hide show
  1. checksums.yaml +4 -4
  2. data/.gitignore +1 -0
  3. data/.rubocop.yml +99 -1
  4. data/.travis.yml +6 -3
  5. data/CHANGES.markdown +10 -0
  6. data/Gemfile +1 -1
  7. data/README.markdown +129 -89
  8. data/Rakefile +7 -4
  9. data/benchmark.rb +13 -13
  10. data/lib/timezone/active_support.rb +147 -134
  11. data/lib/timezone/configure.rb +131 -110
  12. data/lib/timezone/deprecate.rb +32 -0
  13. data/lib/timezone/error.rb +16 -5
  14. data/lib/timezone/loader.rb +19 -16
  15. data/lib/timezone/lookup/basic.rb +24 -2
  16. data/lib/timezone/lookup/geonames.rb +9 -5
  17. data/lib/timezone/lookup/google.rb +24 -15
  18. data/lib/timezone/lookup/test.rb +8 -8
  19. data/lib/timezone/lookup.rb +68 -0
  20. data/lib/timezone/net_http_client.rb +23 -9
  21. data/lib/timezone/nil_zone.rb +32 -0
  22. data/lib/timezone/parser.rb +10 -5
  23. data/lib/timezone/version.rb +2 -1
  24. data/lib/timezone/zone.rb +230 -99
  25. data/lib/timezone.rb +75 -1
  26. data/test/basic_lookup_test.rb +2 -2
  27. data/test/geonames_lookup_test.rb +13 -6
  28. data/test/google_lookup_test.rb +34 -24
  29. data/test/http_test_client.rb +7 -6
  30. data/test/test_lookup_test.rb +3 -1
  31. data/test/test_timezone.rb +59 -0
  32. data/test/timezone/lookup/test_geonames.rb +59 -0
  33. data/test/timezone/lookup/test_google.rb +94 -0
  34. data/test/timezone/lookup/test_test.rb +24 -0
  35. data/test/timezone/test_deprecate.rb +20 -0
  36. data/test/timezone/test_loader.rb +32 -0
  37. data/test/timezone/test_lookup.rb +53 -0
  38. data/test/timezone/test_nil_zone.rb +26 -0
  39. data/test/timezone/test_zone.rb +49 -0
  40. data/test/timezone_test.rb +64 -63
  41. data/timezone.gemspec +16 -15
  42. metadata +39 -38
  43. data/.rubocop_todo.yml +0 -235
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA1:
3
- metadata.gz: dbebfe56aa5f9abc965cac59978c0eb469dcbdf6
4
- data.tar.gz: b28d0929501921621e59d1c725bf5657b3270b83
3
+ metadata.gz: b0f558f0857fb3cfaada212b4d8c8c16d4731958
4
+ data.tar.gz: 3cf2c98225f1e5d7dfca723740ff43a8a0d421fb
5
5
  SHA512:
6
- metadata.gz: 2b4a6c3b19996972245082417b7d02901597cc0182a5dbf8a474b325db97ac4808579ad3bec984beebe95a69fcb6b23bb0d84ead5f485aa3e0e88a01126de9f9
7
- data.tar.gz: ccf22671f1a066998d229c4e4dc72281b4ec4fc4ce7d10889edcc0e387286a4e4bb085764c5adb0710fd328214296425be0216343a75efc4f92a6de0a5e3c482
6
+ metadata.gz: 405363941d45705e9d3a9955edc82737a0a05df509a076f9734fd5ccf560e6777a61f70da35babcb1a5f98bd4b93a9fd4595e5cb7448007efdaef52dfc7e30b6
7
+ data.tar.gz: 5d644aec9dc3e920ae20bbf5af57b54f30337af6d28bba626aaff8524c71fe91ed9df99bfc0e0d372f6bf95ec62613d61156154fe166070d8d5bef336f4718b7
data/.gitignore CHANGED
@@ -1,4 +1,5 @@
1
1
  *.gem
2
+ doc/
2
3
  .DS_Store
3
4
  .rvmrc
4
5
  .bundle
data/.rubocop.yml CHANGED
@@ -1,4 +1,102 @@
1
- inherit_from: .rubocop_todo.yml
1
+ Lint/DuplicateMethods:
2
+ Enabled: true
3
+
4
+ Lint/UnusedMethodArgument:
5
+ Enabled: true
6
+
7
+
8
+ Metrics/AbcSize:
9
+ Enabled: false
2
10
 
3
11
  Metrics/ClassLength:
4
12
  Enabled: false
13
+
14
+ # Offense count: 13
15
+ # Configuration parameters: AllowHeredoc, AllowURI, URISchemes.
16
+ # URISchemes: http, https
17
+ Metrics/LineLength:
18
+ Max: 80
19
+
20
+ Metrics/MethodLength:
21
+ Enabled: false
22
+
23
+ # Offense count: 1
24
+ Metrics/PerceivedComplexity:
25
+ Max: 8
26
+
27
+
28
+ Performance/RedundantBlockCall:
29
+ Enabled: true
30
+
31
+
32
+ Style/AlignParameters:
33
+ EnforcedStyle: with_fixed_indentation
34
+
35
+ Style/ClassVars:
36
+ Enabled: true
37
+
38
+ Style/DeprecatedHashMethods:
39
+ Enabled: true
40
+
41
+ Style/Documentation:
42
+ Enabled: true
43
+
44
+ Style/DoubleNegation:
45
+ Enabled: true
46
+
47
+ Style/EmptyLineBetweenDefs:
48
+ Enabled: true
49
+
50
+ Style/EmptyLiteral:
51
+ Enabled: true
52
+
53
+ Style/GuardClause:
54
+ Enabled: true
55
+
56
+ Style/HashSyntax:
57
+ Enabled: true
58
+
59
+ Style/MethodName:
60
+ Enabled: true
61
+
62
+ Style/MultilineMethodCallIndentation:
63
+ EnforcedStyle: indented
64
+
65
+ Style/NumericLiterals:
66
+ MinDigits: 5
67
+
68
+ Style/OpMethod:
69
+ Enabled: true
70
+
71
+ Style/RedundantReturn:
72
+ Enabled: true
73
+
74
+ Style/RedundantSelf:
75
+ Enabled: true
76
+
77
+ Style/SingleLineMethods:
78
+ Enabled: false
79
+
80
+ Style/SpaceAfterComma:
81
+ Enabled: true
82
+
83
+ Style/SpaceAroundEqualsInParameterDefault:
84
+ Enabled: true
85
+
86
+ Style/SpaceAroundOperators:
87
+ Enabled: true
88
+
89
+ Style/SpaceBeforeSemicolon:
90
+ Enabled: true
91
+
92
+ Style/SpecialGlobalVars:
93
+ EnforcedStyle: use_perl_names
94
+
95
+ Style/StringLiterals:
96
+ Enabled: true
97
+
98
+ Style/TrivialAccessors:
99
+ Enabled: true
100
+
101
+ Style/UnneededPercentQ:
102
+ Enabled: true
data/.travis.yml CHANGED
@@ -1,8 +1,11 @@
1
1
  language: ruby
2
+ branches:
3
+ only:
4
+ - master
2
5
  script:
3
- - bundle exec rake test
4
- - bundle exec rubocop
6
+ - rake
5
7
  rvm:
6
8
  - 2.0.0-p648
7
- - 2.1.7
9
+ - 2.1.8
8
10
  - 2.2.4
11
+ - 2.3.0
data/CHANGES.markdown CHANGED
@@ -1,3 +1,13 @@
1
+ # master (unreleased)
2
+
3
+ # 0.99.0
4
+
5
+ * Added nice `to_s` and `inspect` methods for `::Timezone::Zone`. (panthomakos)
6
+ * Added deprecation warnings for `0.99` and `1.0` release. (panthomakos)
7
+ * Upgraded existing objects for `0.99` and `1.0` release. (panthomakos)
8
+ * Upgraded configuration for `0.99` and `1.0` release. (panthomakos)
9
+ * Added new objects for `0.99` and `1.0` release. (panthomakos)
10
+
1
11
  # 0.6.0
2
12
 
3
13
  * Added `::Timezone::Lookup::Test`, which provides lookup stubs for testing frameworks. (panthomakos)
data/Gemfile CHANGED
@@ -1,4 +1,4 @@
1
- source "http://rubygems.org"
1
+ source 'https://rubygems.org'
2
2
 
3
3
  # Specify your gem's dependencies in timezone.gemspec
4
4
  gemspec
data/README.markdown CHANGED
@@ -1,10 +1,9 @@
1
1
  # Timezone
2
2
 
3
- A simple way to get accurate current and historical timezone information based
4
- on zone or latitude and longitude coordinates. This gem uses the
5
- [tz database][tz-database] for historical timezone information. It also uses the
6
- [geonames API][geonames-api] or the [Google Timezone API][google-api] for
7
- timezone latitude and longitude lookup.
3
+ Accurate current and history timezones for Ruby.
4
+
5
+ * Uses [tz-database][tz-database] for up-to-date historical timezone calculations.
6
+ * Uses the [geonames API][geonames-api] or the [Google Timezone API][google-api] for timezone latitude and longitude lookup.
8
7
 
9
8
  [tz-database]: http://www.twinsun.com/tz/tz-link.htm
10
9
  [geonames-api]: http://www.geonames.org/export/web-services.html
@@ -12,163 +11,204 @@ timezone latitude and longitude lookup.
12
11
 
13
12
  ## Installation
14
13
 
15
- Add the following to your Gemfile:
14
+ Use the [`timezone`](https://rubygems.org/gems/timezone) gem - available on RubyGems. Semantic versioning is used, so if you would like to remain up-to-date and avoid any backwards-incompatible changes, use the following in your `Gemfile`:
15
+
16
+ gem 'timezone', '~> 0.99'
17
+
18
+ ## NOTE: v1.0.0 Release and Upgrade
19
+
20
+ Version `1.0.0` of `timezone` will be released in the coming months. The `0.99.*` releases are backwards-forwards-compatible preparatory releases for `1.0.0`. Once `1.0.0` has been released, previous major versions will no longer be updated to include new timezone data. Any method that will be removed in `1.0.0` has been deprecated and warnings are included when deprecated methods are used. There have been some configuration changes - deprecation warnings and upgrade instructions are provided for those as well.
16
21
 
17
- gem 'timezone'
22
+ Additionally, if you would like to provide your own deprecation logging, you can use `Timezone::Deprecate.callback`. For instance, to log to an external logger, you might use:
18
23
 
19
- Then install your bundle.
24
+ Timezone::Deprecate.callback = lambda do |klass, method, message|
25
+ MyLogger.log("[#{klass} : #{method}] #{message}")
26
+ end
27
+
28
+ ## RubyDocs
20
29
 
21
- bundle install
30
+ Complete documentation for this gem can be found on [RubyDoc](http://www.rubydoc.info/gems/timezone).
22
31
 
23
- ## Getting Started
32
+ ## Simple Timezone Queries
24
33
 
25
- Getting the current time or any historical time in any timezone, with daylight
26
- savings time taken into consideration, is easy:
34
+ Simple querying of time, in any timezone, is accomplished by first retrieving a `Timezone::Zone` object and then calling methods on that object.
27
35
 
28
- timezone = Timezone::Zone.new :zone => 'America/Los_Angeles'
36
+ timezone = Timezone['America/Los_Angeles']
37
+ => #<Timezone::Zone name: "America/Los_Angeles">
38
+
39
+ timezone.valid?
40
+ => true
29
41
 
30
- timezone.time(Time.now)
42
+ timezone.utc_to_local(Time.now)
31
43
  => 2011-02-11 17:29:05 UTC
32
44
 
33
- timezone.time(Time.utc(2010, 1, 1, 0, 0, 0))
45
+ timezone.utc_to_local(Time.utc(2010, 1, 1, 0, 0, 0))
34
46
  => 2009-12-31 16:00:00 UTC
35
47
 
36
- timezone.time_with_offset(Time.utc(2010,1,1,0,0,0))
48
+ timezone.time_with_offset(Time.utc(2010, 1, 1, 0, 0, 0))
37
49
  => 2009-12-31 16:00:00 -0800
38
50
 
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.
51
+ NOTE: time is always returned in the UTC timezone when using the `utc_to_local` function, 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 and historical changes in timezone, which can alter the offset. If you want a time with the appropriate offset at the given time, then use the `time_with_offset` function as shown above.
45
52
 
46
53
  You can use the timezone object to convert local times into the best UTC
47
54
  estimate. The reason this is an estimate is that some local times do not
48
55
  actually map to UTC times (for example when time jumps forward) and some
49
56
  local times map to multiple UTC times (for example when time falls back).
50
57
 
51
- timezone = Timezone::Zone.new :zone => 'America/Los_Angeles'
58
+ timezone = Timezone.fetch('America/Los_Angeles')
59
+ => #<Timezone::Zone name: "America/Los_Angeles">
52
60
 
53
61
  timezone.local_to_utc(Time.utc(2015,11,1,1,50,0))
54
62
  => 2015-11-01 08:50:00 UTC
55
63
 
56
64
  You can also query a `Timezone::Zone` object to determine if it was in Daylight
57
- Savings Time:
65
+ Savings Time.
66
+
67
+ timezone = Timezone['America/Los_Angeles']
68
+ => #<Timezone::Zone name: "America/Los_Angeles">
58
69
 
59
- timezone = Timezone::Zone.new :zone => 'America/Los_Angeles'
60
70
  timezone.dst?(Time.now)
61
71
  => true
72
+
62
73
  timezone.dst?(Time.utc(2010, 1, 1, 0, 0, 0))
63
74
  => false
64
75
 
65
- ## Getting the timezone for a specific latitude and longitude
76
+ For more information on the `::Timezone::Zone` object, see the [RubyDocs](http://www.rubydoc.info/gems/timezone/Timezone/Zone).
66
77
 
67
- 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). Once you have created an account, make sure that you have enabled web services [here](http://www.geonames.org/enablefreewebservice).
78
+ ## Finding Timezones Based on Latitude and Longitude
68
79
 
69
- Second, add the following to your application.rb file, or before you perform a coordinate lookup.
80
+ `timezone` has the capacity to query Geonames and Google for timezones based on latitude and longitude. Before querying a timezone API you'll need to configure the API you want to use.
70
81
 
71
- Timezone::Configure.begin do |c|
72
- c.username = 'your_geonames_username_goes_here'
73
- end
82
+ ### Lookup Configuration with Geonames
74
83
 
75
- Alternatively, timezone can be used with a Google api key, which you can get [here](https://code.google.com/apis/console/).
84
+ 1. Ensure you have a Geonames username. It's free and easy to setup, you can do so [here](http://www.geonames.org/login).
85
+ 1. Ensure you have enabled web services [here](http://www.geonames.org/enablefreewebservice).
86
+ 1. Configure your lookup. NOTE: in Rails it is recommended that you add this code to an initializer.
76
87
 
77
- Enable the Google Maps Time Zone API to do this.
88
+ Timezone::Lookup.config(:geonames) do |c|
89
+ c.username = 'your_geonames_username_goes_here'
90
+ end
78
91
 
79
- Next, add the following to your application.rb file, or before you perform a coordinate lookup.
92
+ ### Lookup Configuration with Google
80
93
 
81
- Timezone::Configure.begin do |c|
82
- c.google_api_key = 'your_google_api_key_goes_here'
83
- c.google_client_id = 'your_google_client_id' # only if using 'Google for Work'
84
- end
94
+ 1. Ensure you have a Google API Key, which you can get [here](https://code.google.com/apis/console/).
95
+ 1. Enable the Google Maps Time Zone API.
96
+ 1. Configure your lookup. NOTE: in Rails it is recommended that you add this code to an initializer.
85
97
 
86
- Finally, for either geonames or Google implementation, pass the coordinates to your timezone initialization function.
98
+ Timezone::Lookup.config(:google) do |c|
99
+ c.api_key = 'your_google_api_key_goes_here'
100
+ c.client_id = 'your_google_client_id' # if using 'Google for Work'
101
+ end
87
102
 
88
- timezone = Timezone::Zone.new :latlon => [-34.92771808058, 138.477041423321]
89
- timezone.zone
103
+ ### Performing Latitude - Longitude Lookups
104
+
105
+ After configuring the API of your choice, pass the lookup coordinates to `Timezone::lookup`.
106
+
107
+ timezone = Timezone.lookup(-34.92771808058, 138.477041423321)
108
+ => #<Timezone::Zone name: "Australia/Adelaide">
109
+
110
+ timezone.name
90
111
  => "Australia/Adelaide"
91
- timezone.time Time.now
112
+
113
+ timezone.utc_to_local(Time.now)
92
114
  => 2011-02-12 12:02:13 UTC
93
115
 
94
- ## Displaying a timezone's name in a Rails/ActiveSupport compatible format
116
+ ## Error States and Nil Objects
95
117
 
96
- timezone = Timezone::Zone.new :latlon => [-34.92771808058, 138.477041423321]
97
- timezone.active_support_time_zone
98
- => "Eastern Time (US & Canada)"
118
+ All exceptions raised by the `timezone` gem are subclasses of `::Timezone::Error::Base`. `timezone` also provides a default `nil` timezone object that behaves like a `Timezone::Zone` except that it is invalid.
99
119
 
100
- ## Getting the complete list of timezones.
120
+ Timezone.fetch('foobar')
121
+ => Timezone::Error::InvalidZone
101
122
 
102
- Retrieving the complete list of timezones is quite simple:
123
+ Timezone::Error::InvalidZone < Timezone::Error::Base
124
+ => true
103
125
 
104
- timezones = Timezone::Zone.names
105
- => ["Africa/Abidjan", "Africa/Accra", "Africa/Addis_Ababa", "Africa/Algiers", ...]
126
+ Timezone.fetch('foobar', Timezone['America/Los_Angeles'])
127
+ => #<Timezone::Zone name: "America/Los_Angeles">
106
128
 
107
- ## Listing current information from specific timezones
129
+ Timezone.fetch('foobar'){ |name| "#{name} is invalid" }
130
+ => "foobar is invalid"
108
131
 
109
- 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:
132
+ zone = Timezone['foo/bar']
133
+ => #<Timezone::NilZone>
110
134
 
111
- zone_list = Timezone::Zone.list "America/Chicago", "America/New_York", "America/Boise"
112
- # This will return an array of information hashes in the following format:
113
- # {
114
- # :zone => "America/Chicago",
115
- # :title => "America/Chicago", # this can be customized to your needs
116
- # :offset => -18000, # UTC offset in seconds
117
- # :utc_offset => -5, # UTC offset in hours
118
- # :dst => false
119
- # }
135
+ zone.valid?
136
+ => false
120
137
 
121
- 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".
138
+ For more information on errors, check [`::Timezone::Error`](http://www.rubydoc.info/gems/timezone/Timezone/Error).
122
139
 
123
- Timezone::Configure.build do |c|
124
- c.replace "America/Chicago", with: "Chicago"
125
- end
140
+ For more information on the `nil` object, check [`::Timezone::NilZone`](http://www.rubydoc.info/gems/timezone/Timezone/NilZone).
126
141
 
127
- 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:
142
+ Latitude - longitude lookups can raise `::Timezone::Error::Lookup` exceptions when issues occur with the remote API request. For example, if an API limit is reached. If the request is valid but the result does not return a valid timezone, then an `::Timezone::Error::InvalidZone` exception will be raised, or a default value will be returned if you have provided one.
128
143
 
129
- Timezone::Configure.begin do |c|
130
- c.default_for_list = "America/Chicago", "America/New_York", "Australia/Sydney"
131
- end
144
+ Timezone.lookup(10, 10)
145
+ => Timezone::Error::Geonames: api limit reached
132
146
 
133
- Finally, by default the **Zone#list** method will order the results by the timezone's UTC offset. You can customize this behavior this way:
147
+ Timezone.lookup(10, 100000)
148
+ => Timezone::Error::InvalidZone
134
149
 
135
- Timezone::Configure.begin do |c|
136
- # this can equal any hash key returned by the Zone#list method
137
- c.order_list_by = :title
138
- end
150
+ Timezone.lookup(10, 100000, Timezone::NilZone.new)
151
+ => #<Timezone::NilZone>
152
+
153
+ Timezone.lookup(10, 100000){ |name| "#{name} is invalid" }
154
+ => " is invalid"
155
+
156
+ ## Listing Timezones
157
+
158
+ Retrieving the complete list of timezones can be accomplished using the `::Timezone::names` function. NOTE: the list is not ordered.
139
159
 
140
- ## Using Your Own HTTP Client
160
+ Timezone.names
161
+ => ["EST", "Indian/Comoro", "Indian/Christmas", "Indian/Cocos", ...]
141
162
 
142
- If you have non-standard http request needs or want to have more control over API calls to Geonames and Google, you can write your own very simple http client wrapper instead of using the built-in default.
143
- Be aware that the Google timezone API uses `https` protocol.
144
163
 
145
- class MyHTTPClient
146
- def initialize(protocol, host)
164
+ ## Using Your Own HTTP Request Handler
165
+
166
+ If you have non-standard http request needs or want to have more control over API calls to Geonames and Google, you can write your own http request handler instead of using the built-in client.
167
+
168
+ Here is a sample request handler that uses `open-uri` to perform requests.
169
+
170
+ require 'open-uri'
171
+
172
+ class MyRequestHandler
173
+ def initialize(config)
174
+ @protocol = config.protocol
175
+ @url = config.url
147
176
  end
148
177
 
178
+ Response = Struct.new(:body, :code)
179
+
149
180
  # Return a response object that responds to #body and #code
150
- def get(url)
181
+ def get(path)
182
+ response = open("#{@protocol}://#{@url}#{path}")
183
+
184
+ Response.new(response.read, response.status.first)
185
+ rescue OpenURI::HTTPError
186
+ Response.new(nil, '500')
151
187
  end
152
188
  end
153
189
 
154
- Timezone::Configure.begin do |c|
155
- c.http_client = MyHTTPClient
156
- end
190
+ This custom request handler can be configured for Google or Geonames. For example, to configure with Geonames you would do the following:
157
191
 
158
- For an example, see `Timezone::NetHTTPClient` which uses the standard `Net::HTTP` library to perform API calls.
192
+ Timezone::Lookup.config(:geonames) do |c|
193
+ c.username = 'foobar'
194
+ c.request_handler = MyRequestHandler
195
+ end
159
196
 
160
197
  ## Testing Timezone Lookups
161
198
 
162
199
  You can provide your own lookup stubs using the built in `::Timezone::Lookup::Test` class.
163
200
 
164
- require 'timezone/lookup/test'
201
+ ::Timezone::Lookup.config(:test)
202
+ => #<Timezone::Lookup::Test:... @stubs={}>
165
203
 
166
- ::Timezone::Configure.begin{ |c| c.lookup = ::Timezone::Lookup::Test }
204
+ ::Timezone::Lookup.lookup.stub(-10, 10, 'America/Los_Angeles')
205
+ => "America/Los_Angeles"
167
206
 
168
- ::Timezone::Configure.lookup.stub(-10, 10, 'America/Los_Angeles')
207
+ ::Timezone.lookup(-10, 10).name
208
+ => 'America/Los_Angeles'
169
209
 
170
- ::Timezone::Zone.new(lat: -10, lon: 10).zone #=> 'America/Los_Angeles'
171
- ::Timezone::Zone.new(lat: -11, lon: 11) #=> raises ::Timezone::Error::Test
210
+ ::Timezone.lookup(-11, 11)
211
+ => Timezone::Error::Test: missing stub
172
212
 
173
213
  ## Build Status [![Build Status](https://secure.travis-ci.org/panthomakos/timezone.png?branch=master)](http://travis-ci.org/panthomakos/timezone)
174
214
 
data/Rakefile CHANGED
@@ -1,15 +1,18 @@
1
- require 'bundler'
1
+ require 'bundler/setup'
2
2
  Bundler::GemHelper.install_tasks
3
3
 
4
4
  require 'rake/testtask'
5
+ require 'rubocop/rake_task'
5
6
 
6
7
  Rake::TestTask.new do |t|
7
- t.libs << "test"
8
- t.test_files = FileList['test/**/*_test.rb']
8
+ t.libs << 'test'
9
+ t.test_files = FileList['test/**/*_test.rb', 'test/**/test_*.rb']
9
10
  t.verbose = true
10
11
  end
11
12
 
12
- task :default => :test
13
+ RuboCop::RakeTask.new
14
+
15
+ task default: [:test, :rubocop]
13
16
 
14
17
  task :parse do
15
18
  require 'timezone/parser'
data/benchmark.rb CHANGED
@@ -4,41 +4,41 @@ require 'benchmark'
4
4
  require 'timezone'
5
5
 
6
6
  def load_tz(timezone)
7
- Timezone::Zone.new(zone: timezone)
7
+ Timezone.fetch(timezone)
8
8
  end
9
9
 
10
10
  puts 'Loading timezones'
11
11
 
12
12
  LOAD_ITERATIONS = 1_000
13
13
  Benchmark.bm do |x|
14
- x.report('la'){ LOAD_ITERATIONS.times{ load_tz('America/Los_Angeles') } }
15
- x.report('hk'){ LOAD_ITERATIONS.times{ load_tz('Asia/Hong_Kong') } }
14
+ x.report('la') { LOAD_ITERATIONS.times { load_tz('America/Los_Angeles') } }
15
+ x.report('hk') { LOAD_ITERATIONS.times { load_tz('Asia/Hong_Kong') } }
16
16
  end
17
17
 
18
18
  def calc_local(timezone)
19
- timezone.time(Time.utc(3000,1,1))
19
+ timezone.time(Time.utc(3000, 1, 1))
20
20
  end
21
21
 
22
22
  puts 'Calculating LOCAL'
23
23
 
24
24
  LOCAL_ITERATIONS = 10_000
25
25
  Benchmark.bm do |x|
26
- timezone = Timezone::Zone.new(zone: 'America/Los_Angeles')
27
- x.report('la'){ LOCAL_ITERATIONS.times{ calc_local(timezone) } }
28
- timezone = Timezone::Zone.new(zone: 'Asia/Hong_Kong')
29
- x.report('hk'){ LOCAL_ITERATIONS.times{ calc_local(timezone) } }
26
+ timezone = Timezone.fetch('America/Los_Angeles')
27
+ x.report('la') { LOCAL_ITERATIONS.times { calc_local(timezone) } }
28
+ timezone = Timezone.fetch('Asia/Hong_Kong')
29
+ x.report('hk') { LOCAL_ITERATIONS.times { calc_local(timezone) } }
30
30
  end
31
31
 
32
32
  def calc_utc(timezone)
33
- timezone.local_to_utc(Time.utc(3000,1,1))
33
+ timezone.local_to_utc(Time.utc(3000, 1, 1))
34
34
  end
35
35
 
36
36
  puts 'Calculating UTC'
37
37
 
38
38
  UTC_ITERATIONS = 10_000
39
39
  Benchmark.bm do |x|
40
- timezone = Timezone::Zone.new(zone: 'America/Los_Angeles')
41
- x.report('la'){ UTC_ITERATIONS.times{ calc_utc(timezone) } }
42
- timezone = Timezone::Zone.new(zone: 'Asia/Hong_Kong')
43
- x.report('hk'){ UTC_ITERATIONS.times{ calc_utc(timezone) } }
40
+ timezone = Timezone.fetch('America/Los_Angeles')
41
+ x.report('la') { UTC_ITERATIONS.times { calc_utc(timezone) } }
42
+ timezone = Timezone.fetch('Asia/Hong_Kong')
43
+ x.report('hk') { UTC_ITERATIONS.times { calc_utc(timezone) } }
44
44
  end