timezone 0.6.0 → 0.99.0
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +4 -4
- data/.gitignore +1 -0
- data/.rubocop.yml +99 -1
- data/.travis.yml +6 -3
- data/CHANGES.markdown +10 -0
- data/Gemfile +1 -1
- data/README.markdown +129 -89
- data/Rakefile +7 -4
- data/benchmark.rb +13 -13
- data/lib/timezone/active_support.rb +147 -134
- data/lib/timezone/configure.rb +131 -110
- data/lib/timezone/deprecate.rb +32 -0
- data/lib/timezone/error.rb +16 -5
- data/lib/timezone/loader.rb +19 -16
- data/lib/timezone/lookup/basic.rb +24 -2
- data/lib/timezone/lookup/geonames.rb +9 -5
- data/lib/timezone/lookup/google.rb +24 -15
- data/lib/timezone/lookup/test.rb +8 -8
- data/lib/timezone/lookup.rb +68 -0
- data/lib/timezone/net_http_client.rb +23 -9
- data/lib/timezone/nil_zone.rb +32 -0
- data/lib/timezone/parser.rb +10 -5
- data/lib/timezone/version.rb +2 -1
- data/lib/timezone/zone.rb +230 -99
- data/lib/timezone.rb +75 -1
- data/test/basic_lookup_test.rb +2 -2
- data/test/geonames_lookup_test.rb +13 -6
- data/test/google_lookup_test.rb +34 -24
- data/test/http_test_client.rb +7 -6
- data/test/test_lookup_test.rb +3 -1
- data/test/test_timezone.rb +59 -0
- data/test/timezone/lookup/test_geonames.rb +59 -0
- data/test/timezone/lookup/test_google.rb +94 -0
- data/test/timezone/lookup/test_test.rb +24 -0
- data/test/timezone/test_deprecate.rb +20 -0
- data/test/timezone/test_loader.rb +32 -0
- data/test/timezone/test_lookup.rb +53 -0
- data/test/timezone/test_nil_zone.rb +26 -0
- data/test/timezone/test_zone.rb +49 -0
- data/test/timezone_test.rb +64 -63
- data/timezone.gemspec +16 -15
- metadata +39 -38
- data/.rubocop_todo.yml +0 -235
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA1:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: b0f558f0857fb3cfaada212b4d8c8c16d4731958
|
4
|
+
data.tar.gz: 3cf2c98225f1e5d7dfca723740ff43a8a0d421fb
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: 405363941d45705e9d3a9955edc82737a0a05df509a076f9734fd5ccf560e6777a61f70da35babcb1a5f98bd4b93a9fd4595e5cb7448007efdaef52dfc7e30b6
|
7
|
+
data.tar.gz: 5d644aec9dc3e920ae20bbf5af57b54f30337af6d28bba626aaff8524c71fe91ed9df99bfc0e0d372f6bf95ec62613d61156154fe166070d8d5bef336f4718b7
|
data/.rubocop.yml
CHANGED
@@ -1,4 +1,102 @@
|
|
1
|
-
|
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
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
data/README.markdown
CHANGED
@@ -1,10 +1,9 @@
|
|
1
1
|
# Timezone
|
2
2
|
|
3
|
-
|
4
|
-
|
5
|
-
[tz
|
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
|
-
|
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
|
-
|
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
|
-
|
24
|
+
Timezone::Deprecate.callback = lambda do |klass, method, message|
|
25
|
+
MyLogger.log("[#{klass} : #{method}] #{message}")
|
26
|
+
end
|
27
|
+
|
28
|
+
## RubyDocs
|
20
29
|
|
21
|
-
|
30
|
+
Complete documentation for this gem can be found on [RubyDoc](http://www.rubydoc.info/gems/timezone).
|
22
31
|
|
23
|
-
##
|
32
|
+
## Simple Timezone Queries
|
24
33
|
|
25
|
-
|
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
|
36
|
+
timezone = Timezone['America/Los_Angeles']
|
37
|
+
=> #<Timezone::Zone name: "America/Los_Angeles">
|
38
|
+
|
39
|
+
timezone.valid?
|
40
|
+
=> true
|
29
41
|
|
30
|
-
timezone.
|
42
|
+
timezone.utc_to_local(Time.now)
|
31
43
|
=> 2011-02-11 17:29:05 UTC
|
32
44
|
|
33
|
-
timezone.
|
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
|
-
|
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
|
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
|
-
|
76
|
+
For more information on the `::Timezone::Zone` object, see the [RubyDocs](http://www.rubydoc.info/gems/timezone/Timezone/Zone).
|
66
77
|
|
67
|
-
|
78
|
+
## Finding Timezones Based on Latitude and Longitude
|
68
79
|
|
69
|
-
|
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
|
-
|
72
|
-
c.username = 'your_geonames_username_goes_here'
|
73
|
-
end
|
82
|
+
### Lookup Configuration with Geonames
|
74
83
|
|
75
|
-
|
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
|
-
|
88
|
+
Timezone::Lookup.config(:geonames) do |c|
|
89
|
+
c.username = 'your_geonames_username_goes_here'
|
90
|
+
end
|
78
91
|
|
79
|
-
|
92
|
+
### Lookup Configuration with Google
|
80
93
|
|
81
|
-
|
82
|
-
|
83
|
-
|
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
|
-
|
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
|
-
|
89
|
-
|
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
|
-
|
112
|
+
|
113
|
+
timezone.utc_to_local(Time.now)
|
92
114
|
=> 2011-02-12 12:02:13 UTC
|
93
115
|
|
94
|
-
##
|
116
|
+
## Error States and Nil Objects
|
95
117
|
|
96
|
-
|
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
|
-
|
120
|
+
Timezone.fetch('foobar')
|
121
|
+
=> Timezone::Error::InvalidZone
|
101
122
|
|
102
|
-
|
123
|
+
Timezone::Error::InvalidZone < Timezone::Error::Base
|
124
|
+
=> true
|
103
125
|
|
104
|
-
|
105
|
-
=>
|
126
|
+
Timezone.fetch('foobar', Timezone['America/Los_Angeles'])
|
127
|
+
=> #<Timezone::Zone name: "America/Los_Angeles">
|
106
128
|
|
107
|
-
|
129
|
+
Timezone.fetch('foobar'){ |name| "#{name} is invalid" }
|
130
|
+
=> "foobar is invalid"
|
108
131
|
|
109
|
-
|
132
|
+
zone = Timezone['foo/bar']
|
133
|
+
=> #<Timezone::NilZone>
|
110
134
|
|
111
|
-
|
112
|
-
|
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
|
-
|
138
|
+
For more information on errors, check [`::Timezone::Error`](http://www.rubydoc.info/gems/timezone/Timezone/Error).
|
122
139
|
|
123
|
-
|
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
|
-
|
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
|
130
|
-
|
131
|
-
end
|
144
|
+
Timezone.lookup(10, 10)
|
145
|
+
=> Timezone::Error::Geonames: api limit reached
|
132
146
|
|
133
|
-
|
147
|
+
Timezone.lookup(10, 100000)
|
148
|
+
=> Timezone::Error::InvalidZone
|
134
149
|
|
135
|
-
Timezone
|
136
|
-
|
137
|
-
|
138
|
-
|
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
|
-
|
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
|
-
|
146
|
-
|
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(
|
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
|
-
|
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
|
-
|
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
|
-
|
201
|
+
::Timezone::Lookup.config(:test)
|
202
|
+
=> #<Timezone::Lookup::Test:... @stubs={}>
|
165
203
|
|
166
|
-
::Timezone::
|
204
|
+
::Timezone::Lookup.lookup.stub(-10, 10, 'America/Los_Angeles')
|
205
|
+
=> "America/Los_Angeles"
|
167
206
|
|
168
|
-
::Timezone
|
207
|
+
::Timezone.lookup(-10, 10).name
|
208
|
+
=> 'America/Los_Angeles'
|
169
209
|
|
170
|
-
::Timezone
|
171
|
-
|
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 <<
|
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
|
-
|
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
|
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
|
27
|
-
x.report('la'){ LOCAL_ITERATIONS.times{ calc_local(timezone) } }
|
28
|
-
timezone = Timezone
|
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
|
41
|
-
x.report('la'){ UTC_ITERATIONS.times{ calc_utc(timezone) } }
|
42
|
-
timezone = Timezone
|
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
|