|
152 | 152 |
|
153 | 153 | /* |
154 | 154 | * 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 |
157 | 157 | */ |
158 | 158 | setDefaults: function(settings) { |
159 | 159 | extendRemove(this._defaults, settings || {}); |
|
1823 | 1823 | return String(hour); |
1824 | 1824 | }; |
1825 | 1825 |
|
| 1826 | + var computeEffectiveSetting = function (settings, property) { |
| 1827 | + return settings && settings[property] ? settings[property] : $.timepicker._defaults[property]; |
| 1828 | + }; |
| 1829 | + |
1826 | 1830 | /* |
1827 | 1831 | * Splits datetime string into date and time substrings. |
1828 | 1832 | * Throws exception when date can't be parsed |
1829 | | - * Returns [dateString, timeString] |
| 1833 | + * Returns {dateString: dateString, timeString: timeString} |
1830 | 1834 | */ |
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 | + }; |
1869 | 1850 | } |
1870 | | - return [dateTimeString, '']; |
| 1851 | + |
| 1852 | + return { |
| 1853 | + dateString: dateTimeString, |
| 1854 | + timeString: '' |
| 1855 | + }; |
1871 | 1856 | }; |
1872 | 1857 |
|
1873 | 1858 | /* |
|
1877 | 1862 | * timeObj = {hour: , minute: , second: , millisec: , microsec: } - parsed time. Optional |
1878 | 1863 | */ |
1879 | 1864 | 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 === '') { |
1895 | 1873 | return { |
1896 | 1874 | date: date |
1897 | 1875 | }; |
1898 | 1876 | } |
| 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 | + }; |
1899 | 1888 | }; |
1900 | 1889 |
|
1901 | 1890 | /* |
|
1915 | 1904 |
|
1916 | 1905 | /** |
1917 | 1906 | * 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 |
1919 | 1908 | * @param {boolean} iso8601 if true formats in accordance to iso8601 "+12:45" |
1920 | 1909 | * @return {string} |
1921 | 1910 | */ |
1922 | 1911 | $.timepicker.timezoneOffsetString = function(tzMinutes, iso8601) { |
1923 | | - if(isNaN(tzMinutes) || tzMinutes > 840){ |
| 1912 | + if(isNaN(tzMinutes) || tzMinutes > 840 || tzMinutes < -720){ |
1924 | 1913 | return tzMinutes; |
1925 | 1914 | } |
1926 | 1915 |
|
1927 | 1916 | var off = tzMinutes, |
1928 | 1917 | minutes = off % 60, |
1929 | 1918 | hours = (off - minutes) / 60, |
1930 | 1919 | 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); |
1932 | 1921 |
|
1933 | 1922 | if(tz == '+00:00'){ |
1934 | 1923 | return 'Z'; |
|
1938 | 1927 |
|
1939 | 1928 | /** |
1940 | 1929 | * 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 |
1943 | 1932 | */ |
1944 | 1933 | $.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" |
1946 | 1935 |
|
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 |
1948 | 1937 | return 0; |
1949 | 1938 | } |
1950 | 1939 |
|
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 |
1952 | 1941 | return tzString; |
1953 | 1942 | } |
1954 | 1943 |
|
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 |
1958 | 1947 | }; |
1959 | 1948 |
|
1960 | 1949 | /** |
|
1966 | 1955 | $.timepicker.timezoneAdjust = function(date, toTimezone) { |
1967 | 1956 | var toTz = $.timepicker.timezoneOffsetNumber(toTimezone); |
1968 | 1957 | if(!isNaN(toTz)){ |
1969 | | - date.setMinutes(date.getMinutes()*1 + (date.getTimezoneOffset()*-1 - toTz*1) ); |
| 1958 | + date.setMinutes(date.getMinutes() + -date.getTimezoneOffset() - toTz); |
1970 | 1959 | } |
1971 | 1960 | return date; |
1972 | 1961 | }; |
|
1999 | 1988 | }; |
2000 | 1989 |
|
2001 | 1990 | /** |
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 |
2003 | 1992 | * enforce date range limits. |
2004 | 1993 | * @param {Element} startTime |
2005 | 1994 | * @param {Element} endTime |
|
2113 | 2102 | _isEmptyObject: isEmptyObject, |
2114 | 2103 | _convert24to12: convert24to12, |
2115 | 2104 | _detectSupport: detectSupport, |
2116 | | - _selectLocalTimezone: selectLocalTimezone |
| 2105 | + _selectLocalTimezone: selectLocalTimezone, |
| 2106 | + _computeEffectiveSetting: computeEffectiveSetting, |
| 2107 | + _splitDateTime: splitDateTime, |
| 2108 | + _parseDateTimeInternal: parseDateTimeInternal |
2117 | 2109 | }; |
2118 | 2110 |
|
2119 | 2111 | /* |
|
0 commit comments