detect_timezone_rails 0.0.1

Sign up to get free protection for your applications and to get access to all the features.
@@ -0,0 +1,3 @@
1
+ *.gem
2
+ .bundle
3
+ pkg/*
@@ -0,0 +1 @@
1
+ detect_timezone_rails global
data/Gemfile ADDED
@@ -0,0 +1,4 @@
1
+ source "http://rubygems.org"
2
+
3
+ # Specify your gem's dependencies in detect_timezone_rails.gemspec
4
+ gemspec
@@ -0,0 +1,14 @@
1
+ PATH
2
+ remote: .
3
+ specs:
4
+ detect_timezone_rails (0.0.1)
5
+
6
+ GEM
7
+ remote: http://rubygems.org/
8
+ specs:
9
+
10
+ PLATFORMS
11
+ ruby
12
+
13
+ DEPENDENCIES
14
+ detect_timezone_rails!
@@ -0,0 +1 @@
1
+ require "bundler/gem_tasks"
@@ -0,0 +1,24 @@
1
+ # -*- encoding: utf-8 -*-
2
+ $:.push File.expand_path("../lib", __FILE__)
3
+ require "detect_timezone_rails/version"
4
+
5
+ Gem::Specification.new do |s|
6
+ s.name = "detect_timezone_rails"
7
+ s.version = DetectTimezoneRails::VERSION
8
+ s.authors = ["Scott Watermasysk"]
9
+ s.email = ["scottwater@gmail.com"]
10
+ s.homepage = "http://www.scottw.com"
11
+ s.summary = %q{Simple javascript timezone detection}
12
+ s.description = %q{Simple javascript timezone detection}
13
+
14
+ s.rubyforge_project = "detect_timezone_rails"
15
+
16
+ s.files = `git ls-files`.split("\n")
17
+ s.test_files = `git ls-files -- {test,spec,features}/*`.split("\n")
18
+ s.executables = `git ls-files -- bin/*`.split("\n").map{ |f| File.basename(f) }
19
+ s.require_paths = ["lib"]
20
+
21
+ # specify any dependencies here; for example:
22
+ # s.add_development_dependency "rspec"
23
+ # s.add_runtime_dependency "rest-client"
24
+ end
@@ -0,0 +1,5 @@
1
+ require "detect_timezone_rails/version"
2
+
3
+ module DetectTimezoneRails
4
+ # Your code goes here...
5
+ end
@@ -0,0 +1,3 @@
1
+ module DetectTimezoneRails
2
+ VERSION = "0.0.1"
3
+ end
@@ -0,0 +1,316 @@
1
+ /*
2
+ * Original script by Josh Fraser (http://www.onlineaspect.com)
3
+ * Continued and maintained by Jon Nylander at https://bitbucket.org/pellepim/jstimezonedetect
4
+ *
5
+ * Provided under the Do Whatever You Want With This Code License.
6
+ */
7
+ /**
8
+ * Namespace to hold all the code for timezone detection.
9
+ */
10
+ var jstz = {};
11
+ jstz.HEMISPHERE_SOUTH = 'SOUTH';
12
+ jstz.HEMISPHERE_NORTH = 'NORTH';
13
+ jstz.HEMISPHERE_UNKNOWN = 'N/A';
14
+ jstz.olson = {};
15
+
16
+ /**
17
+ * A simple object containing information of utc_offset, which olson timezone key to use,
18
+ * and if the timezone cares about daylight savings or not.
19
+ *
20
+ * @constructor
21
+ * @param {string} offset - for example '-11:00'
22
+ * @param {string} olson_tz - the olson Identifier, such as "America/Denver"
23
+ * @param {boolean} uses_dst - flag for whether the time zone somehow cares about daylight savings.
24
+ */
25
+ jstz.TimeZone = function (offset, olson_tz, uses_dst) {
26
+ this.utc_offset = offset;
27
+ this.olson_tz = olson_tz;
28
+ this.uses_dst = uses_dst;
29
+ };
30
+
31
+ /**
32
+ * Prints out the result.
33
+ * But before it does that, it calls this.ambiguity_check.
34
+ */
35
+ jstz.TimeZone.prototype.display = function () {
36
+ this.ambiguity_check();
37
+ var response_text = '<b>UTC-offset</b>: ' + this.utc_offset + '<br/>';
38
+ response_text += '<b>Zoneinfo key</b>: ' + this.olson_tz + '<br/>';
39
+ response_text += '<b>Zone uses DST</b>: ' + (this.uses_dst ? 'yes' : 'no') + '<br/>';
40
+
41
+ return response_text;
42
+ };
43
+
44
+ /**
45
+ * Checks if a timezone has possible ambiguities. I.e timezones that are similar.
46
+ *
47
+ * If the preliminary scan determines that we're in America/Denver. We double check
48
+ * here that we're really there and not in America/Mazatlan.
49
+ *
50
+ * This is done by checking known dates for when daylight savings start for different
51
+ * timezones.
52
+ */
53
+ jstz.TimeZone.prototype.ambiguity_check = function () {
54
+ var ambiguity_list, length, i, tz;
55
+ ambiguity_list = jstz.olson.ambiguity_list[this.olson_tz];
56
+
57
+ if (typeof (ambiguity_list) === 'undefined') {
58
+ return;
59
+ }
60
+
61
+ length = ambiguity_list.length;
62
+ i = 0;
63
+
64
+ for (; i < length; i += 1) {
65
+ tz = ambiguity_list[i];
66
+
67
+ if (jstz.date_is_dst(jstz.olson.dst_start_dates[tz])) {
68
+ this.olson_tz = tz;
69
+ return;
70
+ }
71
+ }
72
+ };
73
+
74
+ /**
75
+ * Checks whether a given date is in daylight savings time.
76
+ *
77
+ * If the date supplied is after june, we assume that we're checking
78
+ * for southern hemisphere DST.
79
+ *
80
+ * @param {Date} date
81
+ * @returns {boolean}
82
+ */
83
+ jstz.date_is_dst = function (date) {
84
+ var date_offset, base_offset;
85
+ base_offset = ((date.getMonth() > 5 ? jstz.get_june_offset()
86
+ : jstz.get_january_offset()));
87
+
88
+ date_offset = jstz.get_date_offset(date);
89
+
90
+ return (base_offset - date_offset) !== 0;
91
+ };
92
+
93
+ /**
94
+ * Gets the offset in minutes from UTC for a certain date.
95
+ *
96
+ * @param date
97
+ * @returns {number}
98
+ */
99
+ jstz.get_date_offset = function (date) {
100
+ return -date.getTimezoneOffset();
101
+ };
102
+
103
+ /**
104
+ * This function does some basic calculations to create information about
105
+ * the user's timezone.
106
+ *
107
+ * Returns a primitive object on the format
108
+ * {'utc_offset' : -9, 'dst': 1, hemisphere' : 'north'}
109
+ * where dst is 1 if the region uses daylight savings.
110
+ *
111
+ * @returns {Object}
112
+ */
113
+ jstz.get_timezone_info = function () {
114
+ var january_offset, june_offset, diff;
115
+ january_offset = jstz.get_january_offset();
116
+ june_offset = jstz.get_june_offset();
117
+ diff = january_offset - june_offset;
118
+
119
+ if (diff < 0) {
120
+ return {
121
+ 'utc_offset' : january_offset,
122
+ 'dst': 1,
123
+ 'hemisphere' : jstz.HEMISPHERE_NORTH
124
+ };
125
+ } else if (diff > 0) {
126
+ return {
127
+ 'utc_offset' : june_offset,
128
+ 'dst' : 1,
129
+ 'hemisphere' : jstz.HEMISPHERE_SOUTH
130
+ };
131
+ }
132
+
133
+ return {
134
+ 'utc_offset' : january_offset,
135
+ 'dst': 0,
136
+ 'hemisphere' : jstz.HEMISPHERE_UNKNOWN
137
+ };
138
+ };
139
+
140
+ jstz.get_january_offset = function () {
141
+ return jstz.get_date_offset(new Date(2011, 0, 1, 0, 0, 0, 0));
142
+ };
143
+
144
+ jstz.get_june_offset = function () {
145
+ return jstz.get_date_offset(new Date(2011, 5, 1, 0, 0, 0, 0));
146
+ };
147
+
148
+ /**
149
+ * Uses get_timezone_info() to formulate a key to use in the olson.timezones dictionary.
150
+ *
151
+ * Returns a primitive object on the format:
152
+ * {'timezone': TimeZone, 'key' : 'the key used to find the TimeZone object'}
153
+ *
154
+ * @returns Object
155
+ */
156
+ jstz.determine_timezone = function () {
157
+ var timezone_key_info, hemisphere_suffix, tz_key;
158
+ timezone_key_info = jstz.get_timezone_info();
159
+ hemisphere_suffix = '';
160
+
161
+ if (timezone_key_info.hemisphere === jstz.HEMISPHERE_SOUTH) {
162
+ hemisphere_suffix = ',s';
163
+ }
164
+
165
+ tz_key = timezone_key_info.utc_offset + ',' + timezone_key_info.dst + hemisphere_suffix;
166
+
167
+ return {'timezone' : jstz.olson.timezones[tz_key], 'key' : tz_key};
168
+ };
169
+
170
+ /**
171
+ * The keys in this dictionary are comma separated as such:
172
+ *
173
+ * First the offset compared to UTC time in minutes.
174
+ *
175
+ * Then a flag which is 0 if the timezone does not take daylight savings into account and 1 if it does.
176
+ *
177
+ * Thirdly an optional 's' signifies that the timezone is in the southern hemisphere, only interesting for timezones with DST.
178
+ *
179
+ * The values of the dictionary are TimeZone objects.
180
+ */
181
+ jstz.olson.timezones = {
182
+ '-720,0' : new jstz.TimeZone('-12:00', 'Etc/GMT+12', false),
183
+ '-660,0' : new jstz.TimeZone('-11:00', 'Pacific/Pago_Pago', false),
184
+ '-600,1' : new jstz.TimeZone('-11:00', 'America/Adak', true),
185
+ '-660,1,s' : new jstz.TimeZone('-11:00', 'Pacific/Apia', true),
186
+ '-600,0' : new jstz.TimeZone('-10:00', 'Pacific/Honolulu', false),
187
+ '-570,0' : new jstz.TimeZone('-10:30', 'Pacific/Marquesas', false),
188
+ '-540,0' : new jstz.TimeZone('-09:00', 'Pacific/Gambier', false),
189
+ '-540,1' : new jstz.TimeZone('-09:00', 'America/Anchorage', true),
190
+ '-480,1' : new jstz.TimeZone('-08:00', 'America/Los_Angeles', true),
191
+ '-480,0' : new jstz.TimeZone('-08:00', 'Pacific/Pitcairn', false),
192
+ '-420,0' : new jstz.TimeZone('-07:00', 'America/Phoenix', false),
193
+ '-420,1' : new jstz.TimeZone('-07:00', 'America/Denver', true),
194
+ '-360,0' : new jstz.TimeZone('-06:00', 'America/Guatemala', false),
195
+ '-360,1' : new jstz.TimeZone('-06:00', 'America/Chicago', true),
196
+ '-360,1,s' : new jstz.TimeZone('-06:00', 'Pacific/Easter', true),
197
+ '-300,0' : new jstz.TimeZone('-05:00', 'America/Bogota', false),
198
+ '-300,1' : new jstz.TimeZone('-05:00', 'America/New_York', true),
199
+ '-270,0' : new jstz.TimeZone('-04:30', 'America/Caracas', false),
200
+ '-240,1' : new jstz.TimeZone('-04:00', 'America/Halifax', true),
201
+ '-240,0' : new jstz.TimeZone('-04:00', 'America/Santo_Domingo', false),
202
+ '-240,1,s' : new jstz.TimeZone('-04:00', 'America/Asuncion', true),
203
+ '-210,1' : new jstz.TimeZone('-03:30', 'America/St_Johns', true),
204
+ '-180,1' : new jstz.TimeZone('-03:00', 'America/Godthab', true),
205
+ '-180,0' : new jstz.TimeZone('-03:00', 'America/Argentina/Buenos_Aires', false),
206
+ '-180,1,s' : new jstz.TimeZone('-03:00', 'America/Montevideo', true),
207
+ '-120,0' : new jstz.TimeZone('-02:00', 'America/Noronha', false),
208
+ '-120,1' : new jstz.TimeZone('-02:00', 'Etc/GMT+2', true),
209
+ '-60,1' : new jstz.TimeZone('-01:00', 'Atlantic/Azores', true),
210
+ '-60,0' : new jstz.TimeZone('-01:00', 'Atlantic/Cape_Verde', false),
211
+ '0,0' : new jstz.TimeZone('00:00', 'Etc/UTC', false),
212
+ '0,1' : new jstz.TimeZone('00:00', 'Europe/London', true),
213
+ '60,1' : new jstz.TimeZone('+01:00', 'Europe/Berlin', true),
214
+ '60,0' : new jstz.TimeZone('+01:00', 'Africa/Lagos', false),
215
+ '60,1,s' : new jstz.TimeZone('+01:00', 'Africa/Windhoek', true),
216
+ '120,1' : new jstz.TimeZone('+02:00', 'Asia/Beirut', true),
217
+ '120,0' : new jstz.TimeZone('+02:00', 'Africa/Johannesburg', false),
218
+ '180,1' : new jstz.TimeZone('+03:00', 'Europe/Moscow', true),
219
+ '180,0' : new jstz.TimeZone('+03:00', 'Asia/Baghdad', false),
220
+ '210,1' : new jstz.TimeZone('+03:30', 'Asia/Tehran', true),
221
+ '240,0' : new jstz.TimeZone('+04:00', 'Asia/Dubai', false),
222
+ '240,1' : new jstz.TimeZone('+04:00', 'Asia/Yerevan', true),
223
+ '270,0' : new jstz.TimeZone('+04:30', 'Asia/Kabul', false),
224
+ '300,1' : new jstz.TimeZone('+05:00', 'Asia/Yekaterinburg', true),
225
+ '300,0' : new jstz.TimeZone('+05:00', 'Asia/Karachi', false),
226
+ '330,0' : new jstz.TimeZone('+05:30', 'Asia/Kolkata', false),
227
+ '345,0' : new jstz.TimeZone('+05:45', 'Asia/Kathmandu', false),
228
+ '360,0' : new jstz.TimeZone('+06:00', 'Asia/Dhaka', false),
229
+ '360,1' : new jstz.TimeZone('+06:00', 'Asia/Omsk', true),
230
+ '390,0' : new jstz.TimeZone('+06:30', 'Asia/Rangoon', false),
231
+ '420,1' : new jstz.TimeZone('+07:00', 'Asia/Krasnoyarsk', true),
232
+ '420,0' : new jstz.TimeZone('+07:00', 'Asia/Jakarta', false),
233
+ '480,0' : new jstz.TimeZone('+08:00', 'Asia/Shanghai', false),
234
+ '480,1' : new jstz.TimeZone('+08:00', 'Asia/Irkutsk', true),
235
+ '525,0' : new jstz.TimeZone('+08:45', 'Australia/Eucla', true),
236
+ '525,1,s' : new jstz.TimeZone('+08:45', 'Australia/Eucla', true),
237
+ '540,1' : new jstz.TimeZone('+09:00', 'Asia/Yakutsk', true),
238
+ '540,0' : new jstz.TimeZone('+09:00', 'Asia/Tokyo', false),
239
+ '570,0' : new jstz.TimeZone('+09:30', 'Australia/Darwin', false),
240
+ '570,1,s' : new jstz.TimeZone('+09:30', 'Australia/Adelaide', true),
241
+ '600,0' : new jstz.TimeZone('+10:00', 'Australia/Brisbane', false),
242
+ '600,1' : new jstz.TimeZone('+10:00', 'Asia/Vladivostok', true),
243
+ '600,1,s' : new jstz.TimeZone('+10:00', 'Australia/Sydney', true),
244
+ '630,1,s' : new jstz.TimeZone('+10:30', 'Australia/Lord_Howe', true),
245
+ '660,1' : new jstz.TimeZone('+11:00', 'Asia/Kamchatka', true),
246
+ '660,0' : new jstz.TimeZone('+11:00', 'Pacific/Noumea', false),
247
+ '690,0' : new jstz.TimeZone('+11:30', 'Pacific/Norfolk', false),
248
+ '720,1,s' : new jstz.TimeZone('+12:00', 'Pacific/Auckland', true),
249
+ '720,0' : new jstz.TimeZone('+12:00', 'Pacific/Tarawa', false),
250
+ '765,1,s' : new jstz.TimeZone('+12:45', 'Pacific/Chatham', true),
251
+ '780,0' : new jstz.TimeZone('+13:00', 'Pacific/Tongatapu', false),
252
+ '840,0' : new jstz.TimeZone('+14:00', 'Pacific/Kiritimati', false)
253
+ };
254
+
255
+ /**
256
+ * This object contains information on when daylight savings starts for
257
+ * different timezones.
258
+ *
259
+ * The list is short for a reason. Often we do not have to be very specific
260
+ * to single out the correct timezone. But when we do, this list comes in
261
+ * handy.
262
+ *
263
+ * Each value is a date denoting when daylight savings starts for that timezone.
264
+ */
265
+ jstz.olson.dst_start_dates = {
266
+ 'America/Denver' : new Date(2011, 2, 13, 3, 0, 0, 0),
267
+ 'America/Mazatlan' : new Date(2011, 3, 3, 3, 0, 0, 0),
268
+ 'America/Chicago' : new Date(2011, 2, 13, 3, 0, 0, 0),
269
+ 'America/Mexico_City' : new Date(2011, 3, 3, 3, 0, 0, 0),
270
+ 'Atlantic/Stanley' : new Date(2011, 8, 4, 7, 0, 0, 0),
271
+ 'America/Asuncion' : new Date(2011, 9, 2, 3, 0, 0, 0),
272
+ 'America/Santiago' : new Date(2011, 9, 9, 3, 0, 0, 0),
273
+ 'America/Campo_Grande' : new Date(2011, 9, 16, 5, 0, 0, 0),
274
+ 'America/Montevideo' : new Date(2011, 9, 2, 3, 0, 0, 0),
275
+ 'America/Sao_Paulo' : new Date(2011, 9, 16, 5, 0, 0, 0),
276
+ 'America/Los_Angeles' : new Date(2011, 2, 13, 8, 0, 0, 0),
277
+ 'America/Santa_Isabel' : new Date(2011, 3, 5, 8, 0, 0, 0),
278
+ 'America/Havana' : new Date(2011, 2, 13, 2, 0, 0, 0),
279
+ 'America/New_York' : new Date(2011, 2, 13, 7, 0, 0, 0),
280
+ 'Asia/Gaza' : new Date(2011, 2, 26, 23, 0, 0, 0),
281
+ 'Asia/Beirut' : new Date(2011, 2, 27, 1, 0, 0, 0),
282
+ 'Europe/Minsk' : new Date(2011, 2, 27, 3, 0, 0, 0),
283
+ 'Europe/Istanbul' : new Date(2011, 2, 27, 7, 0, 0, 0),
284
+ 'Asia/Damascus' : new Date(2011, 3, 1, 2, 0, 0, 0),
285
+ 'Asia/Jerusalem' : new Date(2011, 3, 1, 6, 0, 0, 0),
286
+ 'Africa/Cairo' : new Date(2011, 3, 29, 4, 0, 0, 0),
287
+ 'Asia/Yerevan' : new Date(2011, 2, 27, 4, 0, 0, 0),
288
+ 'Asia/Baku' : new Date(2011, 2, 27, 8, 0, 0, 0),
289
+ 'Pacific/Auckland' : new Date(2011, 8, 26, 7, 0, 0, 0),
290
+ 'Pacific/Fiji' : new Date(2010, 11, 29, 23, 0, 0, 0),
291
+ 'America/Halifax' : new Date(2011, 2, 13, 6, 0, 0, 0),
292
+ 'America/Goose_Bay' : new Date(2011, 2, 13, 2, 1, 0, 0),
293
+ 'America/Miquelon' : new Date(2011, 2, 13, 5, 0, 0, 0),
294
+ 'America/Godthab' : new Date(2011, 2, 27, 1, 0, 0, 0)
295
+ };
296
+
297
+ /**
298
+ * The keys in this object are timezones that we know may be ambiguous after
299
+ * a preliminary scan through the olson_tz object.
300
+ *
301
+ * The array of timezones to compare must be in the order that daylight savings
302
+ * starts for the regions.
303
+ */
304
+ jstz.olson.ambiguity_list = {
305
+ 'America/Denver' : ['America/Denver', 'America/Mazatlan'],
306
+ 'America/Chicago' : ['America/Chicago', 'America/Mexico_City'],
307
+ 'America/Asuncion' : ['Atlantic/Stanley', 'America/Asuncion', 'America/Santiago', 'America/Campo_Grande'],
308
+ 'America/Montevideo' : ['America/Montevideo', 'America/Sao_Paulo'],
309
+ 'Asia/Beirut' : ['Asia/Gaza', 'Asia/Beirut', 'Europe/Minsk', 'Europe/Istanbul', 'Asia/Damascus', 'Asia/Jerusalem', 'Africa/Cairo'],
310
+ 'Asia/Yerevan' : ['Asia/Yerevan', 'Asia/Baku'],
311
+ 'Pacific/Auckland' : ['Pacific/Auckland', 'Pacific/Fiji'],
312
+ 'America/Los_Angeles' : ['America/Los_Angeles', 'America/Santa_Isabel'],
313
+ 'America/New_York' : ['America/Havana', 'America/New_York'],
314
+ 'America/Halifax' : ['America/Goose_Bay', 'America/Halifax'],
315
+ 'America/Godthab' : ['America/Miquelon', 'America/Godthab']
316
+ };
@@ -0,0 +1,41 @@
1
+ /**
2
+ * jQuery Detect Timezone plugin
3
+ *
4
+ * Copyright (c) 2011 Scott Watermasysk (scottwater@gmail.com)
5
+ * Provided under the Do Whatever You Want With This Code License. (same as detect_timezone).
6
+ *
7
+ */
8
+
9
+ (function( $ ){
10
+
11
+ $.fn.set_timezone = function(options) {
12
+
13
+ this.val(this.get_timezone(options));
14
+ return this;
15
+ };
16
+
17
+ $.fn.get_timezone = function(options) {
18
+
19
+ var settings = {
20
+ 'debug' : false,
21
+ 'default' : 'America/New_York'
22
+ };
23
+
24
+ if(options) {
25
+ $.extend( settings, options );
26
+ }
27
+
28
+ var tz_info = jstz.determine_timezone();
29
+ var timezone = tz_info.timezone;
30
+ if (timezone != 'undefined') {
31
+ timezone.ambiguity_check();
32
+ return timezone.olson_tz;
33
+ } else {
34
+ if(settings.debug) {
35
+ alert('no timezone to be found. using default.')
36
+ }
37
+ return settings.default
38
+ }
39
+ };
40
+
41
+ })( jQuery );
metadata ADDED
@@ -0,0 +1,55 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: detect_timezone_rails
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.0.1
5
+ prerelease:
6
+ platform: ruby
7
+ authors:
8
+ - Scott Watermasysk
9
+ autorequire:
10
+ bindir: bin
11
+ cert_chain: []
12
+ date: 2011-09-04 00:00:00.000000000Z
13
+ dependencies: []
14
+ description: Simple javascript timezone detection
15
+ email:
16
+ - scottwater@gmail.com
17
+ executables: []
18
+ extensions: []
19
+ extra_rdoc_files: []
20
+ files:
21
+ - .gitignore
22
+ - .rbenv-gemsets
23
+ - Gemfile
24
+ - Gemfile.lock
25
+ - Rakefile
26
+ - detect_timezone_rails.gemspec
27
+ - lib/detect_timezone_rails.rb
28
+ - lib/detect_timezone_rails/version.rb
29
+ - vendor/assets/javascripts/detect_timezone.js
30
+ - vendor/assets/javascripts/jquery.detect_timezone.js
31
+ homepage: http://www.scottw.com
32
+ licenses: []
33
+ post_install_message:
34
+ rdoc_options: []
35
+ require_paths:
36
+ - lib
37
+ required_ruby_version: !ruby/object:Gem::Requirement
38
+ none: false
39
+ requirements:
40
+ - - ! '>='
41
+ - !ruby/object:Gem::Version
42
+ version: '0'
43
+ required_rubygems_version: !ruby/object:Gem::Requirement
44
+ none: false
45
+ requirements:
46
+ - - ! '>='
47
+ - !ruby/object:Gem::Version
48
+ version: '0'
49
+ requirements: []
50
+ rubyforge_project: detect_timezone_rails
51
+ rubygems_version: 1.8.7
52
+ signing_key:
53
+ specification_version: 3
54
+ summary: Simple javascript timezone detection
55
+ test_files: []