date-fns-tz
Advanced tools
Comparing version 2.0.0 to 2.0.1
@@ -20,3 +20,3 @@ "use strict"; | ||
timezoneHH: /^([+-]\d{2})$/, | ||
timezoneHHMM: /^([+-]\d{2}):?(\d{2})$/ | ||
timezoneHHMM: /^([+-])(\d{2}):?(\d{2})$/ | ||
}; // Parse various time zone offset formats to an offset in milliseconds | ||
@@ -57,4 +57,4 @@ | ||
if (token) { | ||
hours = parseInt(token[1], 10); | ||
var minutes = parseInt(token[2], 10); | ||
hours = parseInt(token[2], 10); | ||
var minutes = parseInt(token[3], 10); | ||
@@ -66,3 +66,3 @@ if (!validateTimezone(hours, minutes)) { | ||
absoluteOffset = Math.abs(hours) * MILLISECONDS_IN_HOUR + minutes * MILLISECONDS_IN_MINUTE; | ||
return hours > 0 ? -absoluteOffset : absoluteOffset; | ||
return token[1] === '+' ? -absoluteOffset : absoluteOffset; | ||
} // IANA time zone | ||
@@ -69,0 +69,0 @@ |
@@ -50,3 +50,3 @@ "use strict"; | ||
function hackyOffset(dtf, date) { | ||
var formatted = dtf.format(date).replace(/\u200E/g, ''); | ||
var formatted = dtf.format(date); | ||
var parsed = /(\d+)\/(\d+)\/(\d+),? (\d+):(\d+):(\d+)/.exec(formatted); // var [, fMonth, fDay, fYear, fHour, fMinute, fSecond] = parsed | ||
@@ -67,6 +67,6 @@ // return [fYear, fMonth, fDay, fHour, fMinute, fSecond] | ||
var testDateFormatted = new Intl.DateTimeFormat('en-US', { | ||
hour12: false, | ||
hourCycle: 'h23', | ||
timeZone: 'America/New_York', | ||
year: 'numeric', | ||
month: 'numeric', | ||
month: '2-digit', | ||
day: '2-digit', | ||
@@ -79,3 +79,3 @@ hour: '2-digit', | ||
dtfCache[timeZone] = hourCycleSupported ? new Intl.DateTimeFormat('en-US', { | ||
hour12: false, | ||
hourCycle: 'h23', | ||
timeZone: timeZone, | ||
@@ -89,3 +89,3 @@ year: 'numeric', | ||
}) : new Intl.DateTimeFormat('en-US', { | ||
hourCycle: 'h23', | ||
hour12: false, | ||
timeZone: timeZone, | ||
@@ -92,0 +92,0 @@ year: 'numeric', |
@@ -22,2 +22,4 @@ /** | ||
* Used by all functions that take String as Date-like argument. | ||
* @property {Date|Number} [originalDate] - used to pick the correct IANA time zone of a date. | ||
* Used by `format` function. | ||
* @property {Locale} [locale=defaultLocale] - the locale object. | ||
@@ -24,0 +26,0 @@ * Used by `formatDistance`, `formatDistanceStrict`, `format` and `parse`. |
@@ -11,3 +11,3 @@ import tzTokenizeDate from '../tzTokenizeDate/index.js' | ||
timezoneHH: /^([+-]\d{2})$/, | ||
timezoneHHMM: /^([+-]\d{2}):?(\d{2})$/, | ||
timezoneHHMM: /^([+-])(\d{2}):?(\d{2})$/, | ||
} | ||
@@ -48,4 +48,4 @@ | ||
if (token) { | ||
hours = parseInt(token[1], 10) | ||
var minutes = parseInt(token[2], 10) | ||
hours = parseInt(token[2], 10) | ||
var minutes = parseInt(token[3], 10) | ||
@@ -57,3 +57,3 @@ if (!validateTimezone(hours, minutes)) { | ||
absoluteOffset = Math.abs(hours) * MILLISECONDS_IN_HOUR + minutes * MILLISECONDS_IN_MINUTE | ||
return hours > 0 ? -absoluteOffset : absoluteOffset | ||
return token[1] === '+' ? -absoluteOffset : absoluteOffset | ||
} | ||
@@ -60,0 +60,0 @@ |
@@ -40,3 +40,3 @@ /** | ||
function hackyOffset(dtf, date) { | ||
var formatted = dtf.format(date).replace(/\u200E/g, '') | ||
var formatted = dtf.format(date) | ||
var parsed = /(\d+)\/(\d+)\/(\d+),? (\d+):(\d+):(\d+)/.exec(formatted) | ||
@@ -56,6 +56,6 @@ // var [, fMonth, fDay, fYear, fHour, fMinute, fSecond] = parsed | ||
var testDateFormatted = new Intl.DateTimeFormat('en-US', { | ||
hour12: false, | ||
hourCycle: 'h23', | ||
timeZone: 'America/New_York', | ||
year: 'numeric', | ||
month: 'numeric', | ||
month: '2-digit', | ||
day: '2-digit', | ||
@@ -72,3 +72,3 @@ hour: '2-digit', | ||
? new Intl.DateTimeFormat('en-US', { | ||
hour12: false, | ||
hourCycle: 'h23', | ||
timeZone: timeZone, | ||
@@ -83,3 +83,3 @@ year: 'numeric', | ||
: new Intl.DateTimeFormat('en-US', { | ||
hourCycle: 'h23', | ||
hour12: false, | ||
timeZone: timeZone, | ||
@@ -86,0 +86,0 @@ year: 'numeric', |
@@ -9,3 +9,3 @@ import tzIntlTimeZoneName from '../../_lib/tzIntlTimeZoneName/index.js' | ||
X: function (date, token, localize, options) { | ||
var timezoneOffset = getTimeZoneOffset(options.timeZone, options._originalDate || date) | ||
var timezoneOffset = getTimeZoneOffset(options.timeZone, date) | ||
@@ -40,3 +40,3 @@ if (timezoneOffset === 0) { | ||
x: function (date, token, localize, options) { | ||
var timezoneOffset = getTimeZoneOffset(options.timeZone, options._originalDate || date) | ||
var timezoneOffset = getTimeZoneOffset(options.timeZone, date) | ||
@@ -67,3 +67,3 @@ switch (token) { | ||
O: function (date, token, localize, options) { | ||
var timezoneOffset = getTimeZoneOffset(options.timeZone, options._originalDate || date) | ||
var timezoneOffset = getTimeZoneOffset(options.timeZone, date) | ||
@@ -85,4 +85,2 @@ switch (token) { | ||
z: function (date, token, localize, options) { | ||
var originalDate = options._originalDate || date | ||
switch (token) { | ||
@@ -93,7 +91,7 @@ // Short | ||
case 'zzz': | ||
return tzIntlTimeZoneName('short', originalDate, options) | ||
return tzIntlTimeZoneName('short', date, options) | ||
// Long | ||
case 'zzzz': | ||
default: | ||
return tzIntlTimeZoneName('long', originalDate, options) | ||
return tzIntlTimeZoneName('long', date, options) | ||
} | ||
@@ -139,3 +137,3 @@ }, | ||
function formatTimezoneShort(offset, dirtyDelimeter) { | ||
function formatTimezoneShort(offset, dirtyDelimiter) { | ||
var sign = offset > 0 ? '-' : '+' | ||
@@ -148,6 +146,6 @@ var absOffset = Math.abs(offset) | ||
} | ||
var delimeter = dirtyDelimeter || '' | ||
return sign + String(hours) + delimeter + addLeadingZeros(minutes, 2) | ||
var delimiter = dirtyDelimiter || '' | ||
return sign + String(hours) + delimiter + addLeadingZeros(minutes, 2) | ||
} | ||
export default formatters |
@@ -289,2 +289,4 @@ import dateFnsFormat from 'date-fns/format/index.js' | ||
* @param {String} [options.timeZone=''] - used to specify the IANA time zone offset of a date String. | ||
* @param {Date|Number} [options.originalDate] - can be used to pass the original unmodified date to `format` to | ||
* improve correctness of the replaced timezone token close to the DST threshold. | ||
* @returns {String} the formatted date string | ||
@@ -324,3 +326,3 @@ * @throws {TypeError} 2 arguments required | ||
if (matches) { | ||
var date = toDate(dirtyDate, options) | ||
var date = toDate(options.originalDate || dirtyDate, options) | ||
// Work through each match and replace the tz token in the format string with the quoted | ||
@@ -327,0 +329,0 @@ // formatted time zone so the remaining tokens can be filled in by date-fns#format. |
@@ -30,3 +30,4 @@ import cloneObject from 'date-fns/_lib/cloneObject/index.js' | ||
extendedOptions.timeZone = timeZone | ||
extendedOptions.originalDate = date | ||
return format(utcToZonedTime(date, timeZone), formatStr, extendedOptions) | ||
} |
@@ -18,3 +18,3 @@ "use strict"; | ||
X: function (date, token, localize, options) { | ||
var timezoneOffset = getTimeZoneOffset(options.timeZone, options._originalDate || date); | ||
var timezoneOffset = getTimeZoneOffset(options.timeZone, date); | ||
@@ -50,3 +50,3 @@ if (timezoneOffset === 0) { | ||
x: function (date, token, localize, options) { | ||
var timezoneOffset = getTimeZoneOffset(options.timeZone, options._originalDate || date); | ||
var timezoneOffset = getTimeZoneOffset(options.timeZone, date); | ||
@@ -78,3 +78,3 @@ switch (token) { | ||
O: function (date, token, localize, options) { | ||
var timezoneOffset = getTimeZoneOffset(options.timeZone, options._originalDate || date); | ||
var timezoneOffset = getTimeZoneOffset(options.timeZone, date); | ||
@@ -96,4 +96,2 @@ switch (token) { | ||
z: function (date, token, localize, options) { | ||
var originalDate = options._originalDate || date; | ||
switch (token) { | ||
@@ -104,3 +102,3 @@ // Short | ||
case 'zzz': | ||
return (0, _index.default)('short', originalDate, options); | ||
return (0, _index.default)('short', date, options); | ||
// Long | ||
@@ -110,3 +108,3 @@ | ||
default: | ||
return (0, _index.default)('long', originalDate, options); | ||
return (0, _index.default)('long', date, options); | ||
} | ||
@@ -155,3 +153,3 @@ } | ||
function formatTimezoneShort(offset, dirtyDelimeter) { | ||
function formatTimezoneShort(offset, dirtyDelimiter) { | ||
var sign = offset > 0 ? '-' : '+'; | ||
@@ -166,4 +164,4 @@ var absOffset = Math.abs(offset); | ||
var delimeter = dirtyDelimeter || ''; | ||
return sign + String(hours) + delimeter + addLeadingZeros(minutes, 2); | ||
var delimiter = dirtyDelimiter || ''; | ||
return sign + String(hours) + delimiter + addLeadingZeros(minutes, 2); | ||
} | ||
@@ -170,0 +168,0 @@ |
@@ -299,2 +299,4 @@ "use strict"; | ||
* @param {String} [options.timeZone=''] - used to specify the IANA time zone offset of a date String. | ||
* @param {Date|Number} [options.originalDate] - can be used to pass the original unmodified date to `format` to | ||
* improve correctness of the replaced timezone token close to the DST threshold. | ||
* @returns {String} the formatted date string | ||
@@ -335,3 +337,3 @@ * @throws {TypeError} 2 arguments required | ||
if (matches) { | ||
var date = (0, _index3.default)(dirtyDate, options); // Work through each match and replace the tz token in the format string with the quoted | ||
var date = (0, _index3.default)(options.originalDate || dirtyDate, options); // Work through each match and replace the tz token in the format string with the quoted | ||
// formatted time zone so the remaining tokens can be filled in by date-fns#format. | ||
@@ -338,0 +340,0 @@ |
@@ -41,2 +41,3 @@ "use strict"; | ||
extendedOptions.timeZone = timeZone; | ||
extendedOptions.originalDate = date; | ||
return (0, _index2.default)((0, _index3.default)(date, timeZone), formatStr, extendedOptions); | ||
@@ -43,0 +44,0 @@ } |
{ | ||
"name": "date-fns-tz", | ||
"version": "2.0.0", | ||
"version": "2.0.1", | ||
"sideEffects": false, | ||
@@ -123,3 +123,3 @@ "description": "Time zone support for date-fns v2 with the Intl API", | ||
"peerDependencies": { | ||
"date-fns": ">=2.0.0" | ||
"date-fns": "2.x" | ||
}, | ||
@@ -126,0 +126,0 @@ "devDependencies": { |
@@ -93,3 +93,3 @@ # date-fns-tz | ||
formatInTimeZone(date, 'America/New_York', 'yyyy-MM-dd HH:mm:ss zzz') // 2014-10-25 06:46:20 EST | ||
formatInTimeZone(date, 'Europe/Paris', 'yyyy-MM-dd HH:mm:ss zzz') // 2014-10-25 10:46:20 GMT+2 | ||
formatInTimeZone(date, 'Europe/Paris', 'yyyy-MM-dd HH:mm:ss zzz') // 2014-10-25 12:46:20 GMT+2 | ||
@@ -96,0 +96,0 @@ // The time zone name is generated by the Intl API which works best when a locale is also provided |
@@ -35,2 +35,3 @@ // This file is generated automatically by `scripts/build/typings.js`. Please, don't change it. | ||
timeZone?: string | ||
originalDate?: Date | number | ||
locale?: Locale | ||
@@ -37,0 +38,0 @@ includeSeconds?: boolean |
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
214674
3473