date-fns-tz
Advanced tools
Comparing version 1.1.1 to 1.1.2
@@ -12,2 +12,14 @@ "use strict"; | ||
function _slicedToArray(arr, i) { return _arrayWithHoles(arr) || _iterableToArrayLimit(arr, i) || _unsupportedIterableToArray(arr, i) || _nonIterableRest(); } | ||
function _nonIterableRest() { throw new TypeError("Invalid attempt to destructure non-iterable instance.\nIn order to be iterable, non-array objects must have a [Symbol.iterator]() method."); } | ||
function _unsupportedIterableToArray(o, minLen) { if (!o) return; if (typeof o === "string") return _arrayLikeToArray(o, minLen); var n = Object.prototype.toString.call(o).slice(8, -1); if (n === "Object" && o.constructor) n = o.constructor.name; if (n === "Map" || n === "Set") return Array.from(o); if (n === "Arguments" || /^(?:Ui|I)nt(?:8|16|32)(?:Clamped)?Array$/.test(n)) return _arrayLikeToArray(o, minLen); } | ||
function _arrayLikeToArray(arr, len) { if (len == null || len > arr.length) len = arr.length; for (var i = 0, arr2 = new Array(len); i < len; i++) { arr2[i] = arr[i]; } return arr2; } | ||
function _iterableToArrayLimit(arr, i) { if (typeof Symbol === "undefined" || !(Symbol.iterator in Object(arr))) return; var _arr = []; var _n = true; var _d = false; var _e = undefined; try { for (var _i = arr[Symbol.iterator](), _s; !(_n = (_s = _i.next()).done); _n = true) { _arr.push(_s.value); if (i && _arr.length === i) break; } } catch (err) { _d = true; _e = err; } finally { try { if (!_n && _i["return"] != null) _i["return"](); } finally { if (_d) throw _e; } } return _arr; } | ||
function _arrayWithHoles(arr) { if (Array.isArray(arr)) return arr; } | ||
var MILLISECONDS_IN_HOUR = 3600000; | ||
@@ -23,3 +35,3 @@ var MILLISECONDS_IN_MINUTE = 60000; | ||
function tzParseTimezone(timezoneString, date) { | ||
function tzParseTimezone(timezoneString, date, isUtcDate) { | ||
var token; | ||
@@ -69,7 +81,6 @@ var absoluteOffset; // Z | ||
date = new Date(date || Date.now()); | ||
date.setMilliseconds(0); // var [fYear, fMonth, fDay, fHour, fMinute, fSecond] = tzTokenizeDate(date, timezoneString) | ||
var tokens = (0, _index.default)(date, timezoneString); | ||
var asUTC = Date.UTC(tokens[0], tokens[1] - 1, tokens[2], tokens[3], tokens[4], tokens[5]); | ||
return -(asUTC - date.getTime()); | ||
var utcDate = isUtcDate ? date : toUtcDate(date); | ||
var offset = calcOffset(utcDate, timezoneString); | ||
var fixedOffset = isUtcDate ? offset : fixOffset(date, offset, timezoneString); | ||
return -fixedOffset; | ||
} | ||
@@ -80,2 +91,47 @@ | ||
function toUtcDate(date) { | ||
return new Date(Date.UTC(date.getFullYear(), date.getMonth(), date.getDate(), date.getHours(), date.getMinutes(), date.getSeconds(), date.getMilliseconds())); | ||
} | ||
function calcOffset(date, timezoneString) { | ||
var _tzTokenizeDate = (0, _index.default)(date, timezoneString), | ||
_tzTokenizeDate2 = _slicedToArray(_tzTokenizeDate, 6), | ||
year = _tzTokenizeDate2[0], | ||
month = _tzTokenizeDate2[1], | ||
day = _tzTokenizeDate2[2], | ||
hour = _tzTokenizeDate2[3], | ||
minute = _tzTokenizeDate2[4], | ||
second = _tzTokenizeDate2[5]; | ||
var asUTC = Date.UTC(year, month - 1, day, hour % 24, minute, second); | ||
var asTS = date.getTime(); | ||
var over = asTS % 1000; | ||
asTS -= over >= 0 ? over : 1000 + over; | ||
return asUTC - asTS; | ||
} | ||
function fixOffset(date, offset, timezoneString) { | ||
var localTS = date.getTime(); // Our UTC time is just a guess because our offset is just a guess | ||
var utcGuess = localTS - offset; // Test whether the zone matches the offset for this ts | ||
var o2 = calcOffset(new Date(utcGuess), timezoneString); // If so, offset didn't change and we're done | ||
if (offset === o2) { | ||
return offset; | ||
} // If not, change the ts by the difference in the offset | ||
utcGuess -= o2 - offset; // If that gives us the local time we want, we're done | ||
var o3 = calcOffset(new Date(utcGuess), timezoneString); | ||
if (o2 === o3) { | ||
return o2; | ||
} // If it's different, we're in a hole time. The offset has changed, but the we don't adjust the time | ||
return Math.max(o2, o3); | ||
} | ||
function validateTimezone(hours, minutes) { | ||
@@ -82,0 +138,0 @@ if (minutes != null && (minutes < 0 || minutes > 59)) { |
@@ -15,3 +15,3 @@ import tzTokenizeDate from '../tzTokenizeDate/index.js' | ||
// Parse various time zone offset formats to an offset in milliseconds | ||
export default function tzParseTimezone(timezoneString, date) { | ||
export default function tzParseTimezone(timezoneString, date, isUtcDate) { | ||
var token | ||
@@ -59,7 +59,9 @@ var absoluteOffset | ||
date = new Date(date || Date.now()) | ||
date.setMilliseconds(0) | ||
// var [fYear, fMonth, fDay, fHour, fMinute, fSecond] = tzTokenizeDate(date, timezoneString) | ||
var tokens = tzTokenizeDate(date, timezoneString) | ||
var asUTC = Date.UTC(tokens[0], tokens[1] - 1, tokens[2], tokens[3], tokens[4], tokens[5]) | ||
return -(asUTC - date.getTime()) | ||
var utcDate = isUtcDate ? date : toUtcDate(date) | ||
var offset = calcOffset(utcDate, timezoneString) | ||
var fixedOffset = isUtcDate ? offset : fixOffset(date, offset, timezoneString) | ||
return -fixedOffset | ||
} | ||
@@ -70,2 +72,54 @@ | ||
function toUtcDate(date) { | ||
return new Date( | ||
Date.UTC( | ||
date.getFullYear(), | ||
date.getMonth(), | ||
date.getDate(), | ||
date.getHours(), | ||
date.getMinutes(), | ||
date.getSeconds(), | ||
date.getMilliseconds() | ||
) | ||
) | ||
} | ||
function calcOffset(date, timezoneString) { | ||
var [year, month, day, hour, minute, second] = tzTokenizeDate(date, timezoneString) | ||
var asUTC = Date.UTC(year, month - 1, day, hour % 24, minute, second) | ||
var asTS = date.getTime() | ||
var over = asTS % 1000 | ||
asTS -= over >= 0 ? over : 1000 + over | ||
return asUTC - asTS | ||
} | ||
function fixOffset(date, offset, timezoneString) { | ||
var localTS = date.getTime() | ||
// Our UTC time is just a guess because our offset is just a guess | ||
var utcGuess = localTS - offset | ||
// Test whether the zone matches the offset for this ts | ||
var o2 = calcOffset(new Date(utcGuess), timezoneString) | ||
// If so, offset didn't change and we're done | ||
if (offset === o2) { | ||
return offset | ||
} | ||
// If not, change the ts by the difference in the offset | ||
utcGuess -= o2 - offset | ||
// If that gives us the local time we want, we're done | ||
var o3 = calcOffset(new Date(utcGuess), timezoneString) | ||
if (o2 === o3) { | ||
return o2 | ||
} | ||
// If it's different, we're in a hole time. The offset has changed, but the we don't adjust the time | ||
return Math.max(o2, o3) | ||
} | ||
function validateTimezone(hours, minutes) { | ||
@@ -72,0 +126,0 @@ if (minutes != null && (minutes < 0 || minutes > 59)) { |
@@ -153,9 +153,2 @@ import toInteger from 'date-fns/esm/_lib/toInteger/index.js' | ||
} | ||
offset = tzParseTimezone( | ||
dateStrings.timezone || options.timeZone, | ||
new Date(timestamp + time + offset) | ||
) | ||
if (isNaN(offset)) { | ||
return new Date(NaN) | ||
} | ||
} else { | ||
@@ -162,0 +155,0 @@ // get offset accurate to hour in timezones that change offset |
import tzParseTimezone from '../_lib/tzParseTimezone' | ||
import subMilliseconds from 'date-fns/esm/subMilliseconds' | ||
import toDate from '../toDate' | ||
@@ -32,16 +31,17 @@ | ||
// This date has the UTC time values of the input date at the system time zone | ||
var utcDate = new Date( | ||
date.getUTCFullYear(), | ||
date.getUTCMonth(), | ||
date.getUTCDate(), | ||
date.getUTCHours(), | ||
date.getUTCMinutes(), | ||
date.getUTCSeconds(), | ||
date.getUTCMilliseconds() | ||
var offsetMilliseconds = tzParseTimezone(timeZone, date, true) || 0 | ||
var d = new Date(date.getTime() - offsetMilliseconds) | ||
var zonedTime = new Date( | ||
d.getUTCFullYear(), | ||
d.getUTCMonth(), | ||
d.getUTCDate(), | ||
d.getUTCHours(), | ||
d.getUTCMinutes(), | ||
d.getUTCSeconds(), | ||
d.getUTCMilliseconds() | ||
) | ||
// We just need to apply the offset indicated by the time zone to this localized date | ||
var offsetMilliseconds = tzParseTimezone(timeZone, utcDate) | ||
return offsetMilliseconds ? subMilliseconds(utcDate, offsetMilliseconds) : utcDate | ||
return zonedTime | ||
} |
{ | ||
"name": "date-fns-tz", | ||
"version": "1.1.1", | ||
"version": "1.1.2", | ||
"sideEffects": false, | ||
@@ -5,0 +5,0 @@ "description": "Time zone support for date-fns v2 with the browser Intl API", |
@@ -143,8 +143,2 @@ "use strict"; | ||
} | ||
offset = (0, _tzParseTimezone.default)(dateStrings.timezone || options.timeZone, new Date(timestamp + time + offset)); | ||
if (isNaN(offset)) { | ||
return new Date(NaN); | ||
} | ||
} else { | ||
@@ -151,0 +145,0 @@ // get offset accurate to hour in timezones that change offset |
@@ -10,4 +10,2 @@ "use strict"; | ||
var _subMilliseconds = _interopRequireDefault(require("date-fns/subMilliseconds")); | ||
var _toDate = _interopRequireDefault(require("../toDate")); | ||
@@ -42,10 +40,9 @@ | ||
function utcToZonedTime(dirtyDate, timeZone, options) { | ||
var date = (0, _toDate.default)(dirtyDate, options); // This date has the UTC time values of the input date at the system time zone | ||
var utcDate = new Date(date.getUTCFullYear(), date.getUTCMonth(), date.getUTCDate(), date.getUTCHours(), date.getUTCMinutes(), date.getUTCSeconds(), date.getUTCMilliseconds()); // We just need to apply the offset indicated by the time zone to this localized date | ||
var offsetMilliseconds = (0, _tzParseTimezone.default)(timeZone, utcDate); | ||
return offsetMilliseconds ? (0, _subMilliseconds.default)(utcDate, offsetMilliseconds) : utcDate; | ||
var date = (0, _toDate.default)(dirtyDate, options); | ||
var offsetMilliseconds = (0, _tzParseTimezone.default)(timeZone, date, true) || 0; | ||
var d = new Date(date.getTime() - offsetMilliseconds); | ||
var zonedTime = new Date(d.getUTCFullYear(), d.getUTCMonth(), d.getUTCDate(), d.getUTCHours(), d.getUTCMinutes(), d.getUTCSeconds(), d.getUTCMilliseconds()); | ||
return zonedTime; | ||
} | ||
module.exports = exports.default; |
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
183066
3115