Skip to content

Commit a7d83b1

Browse files
change timezone values to minute numbers for easier formatting and js Date compat
1 parent 99f250b commit a7d83b1

File tree

2 files changed

+48
-61
lines changed

2 files changed

+48
-61
lines changed

index.html

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -212,7 +212,7 @@ <h3>Timezone Options</h3>
212212
<dl class="defs">
213213

214214
<dt>timezoneList</dt>
215-
<dd><em>Default: [generated timezones]</em> - An array of timezones used to populate the timezone select. Can be an array of values or an array of objects: { label: "EST", value: "+0400" }</dd>
215+
<dd><em>Default: [generated timezones]</em> - An array of timezones used to populate the timezone select. Can be an array of values or an array of objects: { label: "EDT", value: -240 }. The value should be the offset number in minutes. So "-0400" which is the format "-hhmm", would equate to -240 minutes.</dd>
216216
</dl>
217217

218218
<h3>Time Field Options</h3>
@@ -285,7 +285,7 @@ <h3>Time Field Options</h3>
285285
<dd><em>Default: 0</em> - Initial microsecond set. Note: Javascript's native Date object does not natively support microseconds. Timepicker adds ability to simply Date.setMicroseconds(m) and Date.getMicroseconds(). Date comparisons will not acknowledge microseconds. Use this only for display purposes.</dd>
286286

287287
<dt>timezone</dt>
288-
<dd><em>Default: 0</em> - Initial timezone set.</dd>
288+
<dd><em>Default: null</em> - Initial timezone set. This is the offset in minutes. If null the browser's local timezone will be used. If you're timezone is "-0400" you would use -240. For backwards compatibility you may pass "-0400", however the timezone is stored in minutes and more reliable.</dd>
289289

290290
<dt>hourMin</dt>
291291
<dd><em>Default: 0</em> - The minimum hour allowed for all dates.</dd>
@@ -575,10 +575,10 @@ <h3 id="timezone_examples">Using Timezones</h3>
575575
$('#timezone_example_2').datetimepicker({
576576
timeFormat: 'HH:mm z',
577577
timezoneList: [
578-
{ value: '-0500', label: 'Eastern'},
579-
{ value: '-0600', label: 'Central' },
580-
{ value: '-0700', label: 'Mountain' },
581-
{ value: '-0800', label: 'Pacific' }
578+
{ value: -300, label: 'Eastern'},
579+
{ value: -360, label: 'Central' },
580+
{ value: -420, label: 'Mountain' },
581+
{ value: -480, label: 'Pacific' }
582582
]
583583
});
584584
</pre>

jquery-ui-timepicker-addon.js

Lines changed: 42 additions & 55 deletions
Original file line numberDiff line numberDiff line change
@@ -239,21 +239,24 @@
239239
tp_inst.control = tp_inst._defaults.controlType;
240240
}
241241

242-
if (tp_inst._defaults.timezoneList === null) {
243-
var timezoneList = ['-1200', '-1100', '-1000', '-0930', '-0900', '-0800', '-0700', '-0600', '-0500', '-0430', '-0400', '-0330', '-0300', '-0200', '-0100', '+0000',
244-
'+0100', '+0200', '+0300', '+0330', '+0400', '+0430', '+0500', '+0530', '+0545', '+0600', '+0630', '+0700', '+0800', '+0845', '+0900', '+0930',
245-
'+1000', '+1030', '+1100', '+1130', '+1200', '+1245', '+1300', '+1400'];
246-
247-
if (tp_inst.support.iso8601) {
248-
timezoneList = $.map(timezoneList, function(val) {
249-
return val == '+0000' ? 'Z' : (val.substring(0, 3) + ':' + val.substring(3));
250-
});
242+
// prep the timezone options
243+
var timezoneList = [-720,-660,-600,-570,-540,-480,-420,-360,-300,-270,-240,-210,-180,-120,-60,
244+
0,60,120,180,210,240,270,300,330,345,360,390,420,480,525,540,570,600,630,660,690,720,765,780,840];
245+
if (tp_inst._defaults.timezoneList !== null) {
246+
timezoneList = tp_inst._defaults.timezoneList;
247+
}
248+
var tzl=timezoneList.length,tzi=0,tzv=null;
249+
if (tzl > 0 && typeof timezoneList[0] !== 'object') {
250+
for(; tzi<tzl; tzi++){
251+
tzv = timezoneList[tzi];
252+
timezoneList[tzi] = { value: tzv, label: $.timepicker.timezoneOffsetString(tzv, tp_inst.support.iso8601) };
251253
}
252-
tp_inst._defaults.timezoneList = timezoneList;
253254
}
255+
tp_inst._defaults.timezoneList = timezoneList;
254256

255-
tp_inst.timezone = tp_inst._defaults.timezone !== null? tp_inst._defaults.timezone :
256-
$.timepicker.timezoneOffsetString((new Date()).getTimezoneOffset(), tp_inst.support.iso8601);
257+
// set the default units
258+
tp_inst.timezone = tp_inst._defaults.timezone !== null? $.timepicker.timezoneOffsetNumber(tp_inst._defaults.timezone) :
259+
((new Date()).getTimezoneOffset()*-1);
257260
tp_inst.hour = tp_inst._defaults.hour < tp_inst._defaults.hourMin? tp_inst._defaults.hourMin :
258261
tp_inst._defaults.hour > tp_inst._defaults.hourMax? tp_inst._defaults.hourMax : tp_inst._defaults.hour;
259262
tp_inst.minute = tp_inst._defaults.minute < tp_inst._defaults.minuteMin? tp_inst._defaults.minuteMin :
@@ -476,8 +479,7 @@
476479
return $("<option />").val(typeof val == "object" ? val.value : val).text(typeof val == "object" ? val.label : val);
477480
}));
478481
if (typeof(this.timezone) != "undefined" && this.timezone !== null && this.timezone !== "") {
479-
var local_date = new Date(this.inst.selectedYear, this.inst.selectedMonth, this.inst.selectedDay, 12);
480-
var local_timezone = $.timepicker.timezoneOffsetString(local_date.getTimezoneOffset(), this.support.iso8601);
482+
var local_timezone = (new Date(this.inst.selectedYear, this.inst.selectedMonth, this.inst.selectedDay, 12)).getTimezoneOffset()*-1;
481483
if (local_timezone == this.timezone) {
482484
selectLocalTimezone(tp_inst);
483485
} else {
@@ -1193,31 +1195,7 @@
11931195
resTime.microsec = Number(treg[order.c]);
11941196
}
11951197
if (order.z !== -1 && treg[order.z] !== undefined) {
1196-
var tz = treg[order.z].toUpperCase();
1197-
1198-
switch (tz.length) {
1199-
case 1:
1200-
// Z
1201-
tz = iso8601 ? 'Z' : '+0000';
1202-
break;
1203-
case 5:
1204-
// +hhmm
1205-
if (iso8601) {
1206-
tz = tz.substring(1) == '0000' ? 'Z' : tz.substring(0, 3) + ':' + tz.substring(3);
1207-
}
1208-
break;
1209-
case 6:
1210-
// +hh:mm
1211-
if (!iso8601) {
1212-
tz = tz == 'Z' || tz.substring(1) == '00:00' ? '+0000' : tz.replace(/:/, '');
1213-
} else {
1214-
if (tz.substring(1) == '00:00') {
1215-
tz = 'Z';
1216-
}
1217-
}
1218-
break;
1219-
}
1220-
resTime.timezone = tz;
1198+
resTime.timezone = $.timepicker.timezoneOffsetNumber(treg[order.z]);
12211199
}
12221200

12231201

@@ -1246,7 +1224,7 @@
12461224
second: d.getSeconds(),
12471225
millisec: d.getMilliseconds(),
12481226
microsec: d.getMicroseconds(),
1249-
timezone: $.timepicker.timezoneOffsetString(d.getTimezoneOffset(), iso8601)
1227+
timezone: d.getTimezoneOffset()*-1
12501228
};
12511229
}
12521230
catch(err){
@@ -1283,7 +1261,7 @@
12831261
minute: 0,
12841262
second: 0,
12851263
millisec: 0,
1286-
timezone: '+0000'
1264+
timezone: 0
12871265
}, time);
12881266

12891267
var tmptime = format,
@@ -1317,9 +1295,9 @@
13171295
case 'c':
13181296
return ('00' + time.microsec).slice(-3);
13191297
case 'z':
1320-
return time.timezone === null? options.timezone : time.timezone;
1298+
return $.timepicker.timezoneOffsetString(time.timezone === null? options.timezone : time.timezone, false);
13211299
case 'Z':
1322-
return time.timezone === null? options.timezone : time.timezone;
1300+
return $.timepicker.timezoneOffsetString(time.timezone === null? options.timezone : time.timezone, true);
13231301
case 'T':
13241302
return ampmName.charAt(0).toUpperCase();
13251303
case 'TT':
@@ -1902,8 +1880,7 @@
19021880
var selectLocalTimezone = function(tp_inst, date) {
19031881
if (tp_inst && tp_inst.timezone_select) {
19041882
var now = typeof date !== 'undefined' ? date : new Date();
1905-
var tzoffset = $.timepicker.timezoneOffsetString(now.getTimezoneOffset(), tp_inst.support.iso8601);
1906-
tp_inst.timezone_select.val(tzoffset);
1883+
tp_inst.timezone_select.val(now.getTimezoneOffset()*-1);
19071884
}
19081885
};
19091886

@@ -1914,12 +1891,16 @@
19141891

19151892
/**
19161893
* Get the timezone offset as string from a date object (eg '+0530' for UTC+5.5)
1917-
* @param date
1894+
* @param number if not a number this value is returned
19181895
* @param boolean if true formats in accordance to iso8601 "+12:45"
19191896
* @return string
19201897
*/
19211898
$.timepicker.timezoneOffsetString = function(tzMinutes, iso8601) {
1922-
var off = tzMinutes * -1,
1899+
if(isNaN(tzMinutes) || tzMinutes > 840){
1900+
return tzMinutes;
1901+
}
1902+
1903+
var off = tzMinutes,
19231904
minutes = off % 60,
19241905
hours = (off - minutes) / 60,
19251906
iso = iso8601? ':':'',
@@ -1937,11 +1918,16 @@
19371918
* @return number
19381919
*/
19391920
$.timepicker.timezoneOffsetNumber = function(tzString) {
1940-
tzString = tzString.replace(/(\:|z)/gi,''); // excuse any iso8601, end up with "+1245"
1941-
1942-
if(!/^(\-|\+)\d{4}$/.test(tzString)){
1921+
tzString = tzString.toString().replace(':',''); // excuse any iso8601, end up with "+1245"
1922+
1923+
if(tzString.toUpperCase() === 'Z'){ // if iso8601 with Z, its 0 minute offset
19431924
return 0;
19441925
}
1926+
1927+
if(!/^(\-|\+)\d{4}$/.test(tzString)){ // possibly a user defined tz, so just give it back
1928+
return tzString;
1929+
}
1930+
19451931
return ((tzString.substr(0,1) =='-'? -1 : 1) * // plus or minus
19461932
((parseInt(tzString.substr(1,2),10)*60) + // hours (converted to minutes)
19471933
parseInt(tzString.substr(3,2),10))); // minutes
@@ -1954,12 +1940,13 @@
19541940
* @return date
19551941
*/
19561942
$.timepicker.timezoneAdjust = function(date, toTimezone) {
1957-
var currTz = date.getTimezoneOffset(),
1958-
toTz = $.timepicker.timezoneOffsetNumber(toTimezone)*-1,
1959-
diff = currTz - toTz; // difference in minutes
1960-
1961-
date.setMinutes(date.getMinutes()+diff);
1943+
var toTz = $.timepicker.timezoneOffsetNumber(toTimezone);
1944+
if(!isNaN(toTz)){
1945+
var currTz = date.getTimezoneOffset()*-1,
1946+
diff = currTz - toTz; // difference in minutes
19621947

1948+
date.setMinutes(date.getMinutes()+diff);
1949+
}
19631950
return date;
19641951
};
19651952

0 commit comments

Comments
 (0)