Skip to content

Commit a1e67c5

Browse files
Merge pull request trentrichardson#606 from srvance/dev
More tests added, cleanup
2 parents af2f050 + 032adf1 commit a1e67c5

File tree

2 files changed

+328
-71
lines changed

2 files changed

+328
-71
lines changed

jquery-ui-timepicker-addon.js

Lines changed: 63 additions & 71 deletions
Original file line numberDiff line numberDiff line change
@@ -152,8 +152,8 @@
152152

153153
/*
154154
* Override the default settings for all instances of the time picker.
155-
* @param settings object - the new settings to use as defaults (anonymous object)
156-
* @return the manager object
155+
* @param {Object} settings object - the new settings to use as defaults (anonymous object)
156+
* @return {Object} the manager object
157157
*/
158158
setDefaults: function(settings) {
159159
extendRemove(this._defaults, settings || {});
@@ -1823,51 +1823,36 @@
18231823
return String(hour);
18241824
};
18251825

1826+
var computeEffectiveSetting = function (settings, property) {
1827+
return settings && settings[property] ? settings[property] : $.timepicker._defaults[property];
1828+
};
1829+
18261830
/*
18271831
* Splits datetime string into date and time substrings.
18281832
* Throws exception when date can't be parsed
1829-
* Returns [dateString, timeString]
1833+
* Returns {dateString: dateString, timeString: timeString}
18301834
*/
1831-
var splitDateTime = function(dateFormat, dateTimeString, dateSettings, timeSettings) {
1832-
try {
1833-
// The idea is to get the number separator occurrences in datetime and the time format requested (since time has
1834-
// fewer unknowns, mostly numbers and am/pm). We will use the time pattern to split.
1835-
var separator = timeSettings && timeSettings.separator ? timeSettings.separator : $.timepicker._defaults.separator,
1836-
format = timeSettings && timeSettings.timeFormat ? timeSettings.timeFormat : $.timepicker._defaults.timeFormat,
1837-
timeParts = format.split(separator), // how many occurrences of separator may be in our format?
1838-
timePartsLen = timeParts.length,
1839-
allParts = dateTimeString.split(separator),
1840-
allPartsLen = allParts.length;
1841-
1842-
if (allPartsLen > 1) {
1843-
return [
1844-
allParts.splice(0,allPartsLen-timePartsLen).join(separator),
1845-
allParts.splice(0,timePartsLen).join(separator)
1846-
];
1847-
}
1848-
1849-
} catch (err) {
1850-
$.timepicker.log('Could not split the date from the time. Please check the following datetimepicker options' +
1851-
"\nthrown error: " + err +
1852-
"\ndateTimeString" + dateTimeString +
1853-
"\ndateFormat = " + dateFormat +
1854-
"\nseparator = " + timeSettings.separator +
1855-
"\ntimeFormat = " + timeSettings.timeFormat);
1856-
1857-
if (err.indexOf(":") >= 0) {
1858-
// Hack! The error message ends with a colon, a space, and
1859-
// the "extra" characters. We rely on that instead of
1860-
// attempting to perfectly reproduce the parsing algorithm.
1861-
var dateStringLength = dateTimeString.length - (err.length - err.indexOf(':') - 2),
1862-
timeString = dateTimeString.substring(dateStringLength);
1863-
1864-
return [$.trim(dateTimeString.substring(0, dateStringLength)), $.trim(dateTimeString.substring(dateStringLength))];
1865-
1866-
} else {
1867-
throw err;
1868-
}
1835+
var splitDateTime = function(dateTimeString, timeSettings) {
1836+
// The idea is to get the number separator occurrences in datetime and the time format requested (since time has
1837+
// fewer unknowns, mostly numbers and am/pm). We will use the time pattern to split.
1838+
var separator = computeEffectiveSetting(timeSettings, 'separator'),
1839+
format = computeEffectiveSetting(timeSettings, 'timeFormat'),
1840+
timeParts = format.split(separator), // how many occurrences of separator may be in our format?
1841+
timePartsLen = timeParts.length,
1842+
allParts = dateTimeString.split(separator),
1843+
allPartsLen = allParts.length;
1844+
1845+
if (allPartsLen > 1) {
1846+
return {
1847+
dateString: allParts.splice(0,allPartsLen-timePartsLen).join(separator),
1848+
timeString: allParts.splice(0,timePartsLen).join(separator)
1849+
};
18691850
}
1870-
return [dateTimeString, ''];
1851+
1852+
return {
1853+
dateString: dateTimeString,
1854+
timeString: ''
1855+
};
18711856
};
18721857

18731858
/*
@@ -1877,25 +1862,29 @@
18771862
* timeObj = {hour: , minute: , second: , millisec: , microsec: } - parsed time. Optional
18781863
*/
18791864
var parseDateTimeInternal = function(dateFormat, timeFormat, dateTimeString, dateSettings, timeSettings) {
1880-
var date;
1881-
var splitRes = splitDateTime(dateFormat, dateTimeString, dateSettings, timeSettings);
1882-
date = $.datepicker._base_parseDate(dateFormat, splitRes[0], dateSettings);
1883-
if (splitRes[1] !== '') {
1884-
var timeString = splitRes[1],
1885-
parsedTime = $.datepicker.parseTime(timeFormat, timeString, timeSettings);
1886-
1887-
if (parsedTime === null) {
1888-
throw 'Wrong time format';
1889-
}
1890-
return {
1891-
date: date,
1892-
timeObj: parsedTime
1893-
};
1894-
} else {
1865+
var date,
1866+
parts,
1867+
parsedTime;
1868+
1869+
parts = splitDateTime(dateTimeString, timeSettings);
1870+
date = $.datepicker._base_parseDate(dateFormat, parts.dateString, dateSettings);
1871+
1872+
if (parts.timeString === '') {
18951873
return {
18961874
date: date
18971875
};
18981876
}
1877+
1878+
parsedTime = $.datepicker.parseTime(timeFormat, parts.timeString, timeSettings);
1879+
1880+
if (!parsedTime) {
1881+
throw 'Wrong time format';
1882+
}
1883+
1884+
return {
1885+
date: date,
1886+
timeObj: parsedTime
1887+
};
18991888
};
19001889

19011890
/*
@@ -1915,20 +1904,20 @@
19151904

19161905
/**
19171906
* Get the timezone offset as string from a date object (eg '+0530' for UTC+5.5)
1918-
* @param {number} tzMinutes if not a number this value is returned
1907+
* @param {number} tzMinutes if not a number, less than -720 (-1200), or greater than 840 (+1400) this value is returned
19191908
* @param {boolean} iso8601 if true formats in accordance to iso8601 "+12:45"
19201909
* @return {string}
19211910
*/
19221911
$.timepicker.timezoneOffsetString = function(tzMinutes, iso8601) {
1923-
if(isNaN(tzMinutes) || tzMinutes > 840){
1912+
if(isNaN(tzMinutes) || tzMinutes > 840 || tzMinutes < -720){
19241913
return tzMinutes;
19251914
}
19261915

19271916
var off = tzMinutes,
19281917
minutes = off % 60,
19291918
hours = (off - minutes) / 60,
19301919
iso = iso8601? ':':'',
1931-
tz = (off >= 0 ? '+' : '-') + ('0' + (hours * 101).toString()).slice(-2) + iso + ('0' + (minutes * 101).toString()).slice(-2);
1920+
tz = (off >= 0 ? '+' : '-') + ('0' + Math.abs(hours)).slice(-2) + iso + ('0' + Math.abs(minutes)).slice(-2);
19321921

19331922
if(tz == '+00:00'){
19341923
return 'Z';
@@ -1938,23 +1927,23 @@
19381927

19391928
/**
19401929
* Get the number in minutes that represents a timezone string
1941-
* @param {string} tzString formatted like "+0500", "-1245"
1942-
* @return {number}
1930+
* @param {string} tzString formatted like "+0500", "-1245", "Z"
1931+
* @return {number} the offset minutes or the original string if it doesn't match expectations
19431932
*/
19441933
$.timepicker.timezoneOffsetNumber = function(tzString) {
1945-
tzString = tzString.toString().replace(':',''); // excuse any iso8601, end up with "+1245"
1934+
var normalized = tzString.toString().replace(':',''); // excuse any iso8601, end up with "+1245"
19461935

1947-
if(tzString.toUpperCase() === 'Z'){ // if iso8601 with Z, its 0 minute offset
1936+
if(normalized.toUpperCase() === 'Z'){ // if iso8601 with Z, its 0 minute offset
19481937
return 0;
19491938
}
19501939

1951-
if(!/^(\-|\+)\d{4}$/.test(tzString)){ // possibly a user defined tz, so just give it back
1940+
if(!/^(\-|\+)\d{4}$/.test(normalized)){ // possibly a user defined tz, so just give it back
19521941
return tzString;
19531942
}
19541943

1955-
return ((tzString.substr(0,1) =='-'? -1 : 1) * // plus or minus
1956-
((parseInt(tzString.substr(1,2),10)*60) + // hours (converted to minutes)
1957-
parseInt(tzString.substr(3,2),10))); // minutes
1944+
return ((normalized.substr(0,1) =='-'? -1 : 1) * // plus or minus
1945+
((parseInt(normalized.substr(1,2),10)*60) + // hours (converted to minutes)
1946+
parseInt(normalized.substr(3,2),10))); // minutes
19581947
};
19591948

19601949
/**
@@ -1966,7 +1955,7 @@
19661955
$.timepicker.timezoneAdjust = function(date, toTimezone) {
19671956
var toTz = $.timepicker.timezoneOffsetNumber(toTimezone);
19681957
if(!isNaN(toTz)){
1969-
date.setMinutes(date.getMinutes()*1 + (date.getTimezoneOffset()*-1 - toTz*1) );
1958+
date.setMinutes(date.getMinutes() + -date.getTimezoneOffset() - toTz);
19701959
}
19711960
return date;
19721961
};
@@ -1999,7 +1988,7 @@
19991988
};
20001989

20011990
/**
2002-
* Calls `method` on the `startTime` and `endTime` elements, and configures them to
1991+
* Calls `datepicker` on the `startTime` and `endTime` elements, and configures them to
20031992
* enforce date range limits.
20041993
* @param {Element} startTime
20051994
* @param {Element} endTime
@@ -2113,7 +2102,10 @@
21132102
_isEmptyObject: isEmptyObject,
21142103
_convert24to12: convert24to12,
21152104
_detectSupport: detectSupport,
2116-
_selectLocalTimezone: selectLocalTimezone
2105+
_selectLocalTimezone: selectLocalTimezone,
2106+
_computeEffectiveSetting: computeEffectiveSetting,
2107+
_splitDateTime: splitDateTime,
2108+
_parseDateTimeInternal: parseDateTimeInternal
21172109
};
21182110

21192111
/*

0 commit comments

Comments
 (0)