Compare commits

..

2 Commits

Author SHA1 Message Date
AJ ONeal 417346ad3e 1.0.2 2021-05-29 01:42:12 -06:00
AJ ONeal d18b0ad3de trivial workarounds for iOS 2021-05-29 01:41:54 -06:00
3 changed files with 6 additions and 5 deletions

View File

@ -1,6 +1,6 @@
{ {
"name": "xtz", "name": "xtz",
"version": "1.0.1", "version": "1.0.2",
"description": "A fast, lightweight, zero-dependency library to translate between Time Zones and UTC with native Intl.DateTimeFormat in ~100 LoC. For Node.js & Browsers.", "description": "A fast, lightweight, zero-dependency library to translate between Time Zones and UTC with native Intl.DateTimeFormat in ~100 LoC. For Node.js & Browsers.",
"main": "xtz.js", "main": "xtz.js",
"scripts": { "scripts": {

7
xtz.js
View File

@ -24,7 +24,8 @@ var XTZ;
var formater = new Intl.DateTimeFormat("default", tzOptions); var formater = new Intl.DateTimeFormat("default", tzOptions);
var parts = formater.formatToParts(date); var parts = formater.formatToParts(date);
var whole = {}; // millisecond is explicitly 0 for iOS' lack of fractionalSecond support
var whole = { millisecond: 0 };
parts.forEach(function (part) { parts.forEach(function (part) {
var val = part.value; var val = part.value;
switch (part.type) { switch (part.type) {
@ -70,7 +71,7 @@ var XTZ;
function getOffset(utcDate, tzD2) { function getOffset(utcDate, tzD2) {
var tzDate = new Date(toOffsetISOString(tzD2)); var tzDate = new Date(toOffsetISOString(tzD2));
var diff = Math.round(tzDate.valueOf() - utcDate.valueOf()) / (60 * 1000); var diff = Math.round((tzDate.valueOf() - utcDate.valueOf()) / (60 * 1000));
return diff; return diff;
} }
@ -126,7 +127,7 @@ var XTZ;
var utcDate = new Date(dt); var utcDate = new Date(dt);
var tzD2 = toTimeZone(utcDate, tz); var tzD2 = toTimeZone(utcDate, tz);
var offset = tzD2.offset; var offset = tzD2.offset;
tzD2.offset = ""; tzD2.offset = 0;
var deltaDate = new Date(utcDate); var deltaDate = new Date(utcDate);
deltaDate.setUTCMinutes(deltaDate.getUTCMinutes() - offset); deltaDate.setUTCMinutes(deltaDate.getUTCMinutes() - offset);

2
xtz.min.js vendored
View File

@ -1 +1 @@
var XTZ;(function(){"use strict";function toTimeZone(date,timeZone){date=new Date(date);var options={timeZone:timeZone,year:"numeric",month:"numeric",day:"numeric",hour12:false,hour:"numeric",minute:"numeric",second:"numeric",fractionalSecondDigits:3};var tzOptions=Object.assign({timeZoneName:"long"},options);var formater=new Intl.DateTimeFormat("default",tzOptions);var parts=formater.formatToParts(date);var whole={};parts.forEach(function(part){var val=part.value;switch(part.type){case"literal":return;case"timeZoneName":break;case"month":val=parseInt(val,10)-1;break;case"hour":val=parseInt(val,10)%24;break;case"fractionalSecond":whole.millisecond=parseInt(val,10);return;default:val=parseInt(val,10)}whole[part.type]=val});whole.timeZone=timeZone;whole.offset=getOffset(date,whole);whole.toISOString=_toOffsetISOString;return whole}function toTimeZoneISOString(date,timeZone){var whole=toTimeZone(date,timeZone);return toOffsetISOString(whole)}function _toOffsetISOString(){return toOffsetISOString(this)}function getOffset(utcDate,tzD2){var tzDate=new Date(toOffsetISOString(tzD2));var diff=Math.round(tzDate.valueOf()-utcDate.valueOf())/(60*1e3);return diff}function p2(x){return String(x).padStart(2,"0")}function p3(x){return String(x).padStart(3,"0")}function formatOffset(minutes){if(!minutes){return"Z"}var h=Math.floor(Math.abs(minutes)/60);var m=Math.abs(minutes)%60;var offset="";if(minutes>0){offset="+"}else if(minutes<0){offset="-"}return offset+h.toString().padStart(2,"0")+m.toString().padStart(2,"0")}function toOffsetISOString(d){var offset=formatOffset(d.offset);return`${d.year}-${p2(d.month+1)}-${p2(d.day)}`+`T${p2(d.hour)}:${p2(d.minute)}:${p2(d.second)}.${p3(d.millisecond)}${offset}`}function toUTC(dt,tz){if("string"===typeof dt){dt=dt.replace("T"," ").replace("Z","").replace(" ","T").replace(/$/,"Z")}var utcDate=new Date(dt);var tzD2=toTimeZone(utcDate,tz);var offset=tzD2.offset;tzD2.offset="";var deltaDate=new Date(utcDate);deltaDate.setUTCMinutes(deltaDate.getUTCMinutes()-offset);var tzD3=toTimeZone(deltaDate,tz);if(tzD3.hour===utcDate.getUTCHours()&&tzD3.minute===utcDate.getUTCMinutes()){return tzD3}var diff=tzD3.offset-offset;var h=Math.floor(Math.abs(diff)/60);var m=Math.abs(diff)%60;var sign=Math.abs(diff)/diff;tzD3.hour-=h*sign;tzD3.minute-=m*sign;return tzD3}function toUTCISOString(date,timeZone){var whole=toUTC(date,timeZone);return toOffsetISOString(whole)}XTZ={toOffsetISOString:toOffsetISOString,formatOffset:formatOffset,toTimeZone:toTimeZone,toTimeZoneISOString:toTimeZoneISOString,toUTC:toUTC,toUTCISOString:toUTCISOString};if("undefined"!=typeof module&&module.exports){module.exports=XTZ}})(); var XTZ;(function(){"use strict";function toTimeZone(date,timeZone){date=new Date(date);var options={timeZone:timeZone,year:"numeric",month:"numeric",day:"numeric",hour12:false,hour:"numeric",minute:"numeric",second:"numeric",fractionalSecondDigits:3};var tzOptions=Object.assign({timeZoneName:"long"},options);var formater=new Intl.DateTimeFormat("default",tzOptions);var parts=formater.formatToParts(date);var whole={millisecond:0};parts.forEach(function(part){var val=part.value;switch(part.type){case"literal":return;case"timeZoneName":break;case"month":val=parseInt(val,10)-1;break;case"hour":val=parseInt(val,10)%24;break;case"fractionalSecond":whole.millisecond=parseInt(val,10);return;default:val=parseInt(val,10)}whole[part.type]=val});whole.timeZone=timeZone;whole.offset=getOffset(date,whole);whole.toISOString=_toOffsetISOString;return whole}function toTimeZoneISOString(date,timeZone){var whole=toTimeZone(date,timeZone);return toOffsetISOString(whole)}function _toOffsetISOString(){return toOffsetISOString(this)}function getOffset(utcDate,tzD2){var tzDate=new Date(toOffsetISOString(tzD2));var diff=Math.round((tzDate.valueOf()-utcDate.valueOf())/(60*1e3));return diff}function p2(x){return String(x).padStart(2,"0")}function p3(x){return String(x).padStart(3,"0")}function formatOffset(minutes){if(!minutes){return"Z"}var h=Math.floor(Math.abs(minutes)/60);var m=Math.abs(minutes)%60;var offset="";if(minutes>0){offset="+"}else if(minutes<0){offset="-"}return offset+h.toString().padStart(2,"0")+m.toString().padStart(2,"0")}function toOffsetISOString(d){var offset=formatOffset(d.offset);return`${d.year}-${p2(d.month+1)}-${p2(d.day)}`+`T${p2(d.hour)}:${p2(d.minute)}:${p2(d.second)}.${p3(d.millisecond)}${offset}`}function toUTC(dt,tz){if("string"===typeof dt){dt=dt.replace("T"," ").replace("Z","").replace(" ","T").replace(/$/,"Z")}var utcDate=new Date(dt);var tzD2=toTimeZone(utcDate,tz);var offset=tzD2.offset;tzD2.offset=0;var deltaDate=new Date(utcDate);deltaDate.setUTCMinutes(deltaDate.getUTCMinutes()-offset);var tzD3=toTimeZone(deltaDate,tz);if(tzD3.hour===utcDate.getUTCHours()&&tzD3.minute===utcDate.getUTCMinutes()){return tzD3}var diff=tzD3.offset-offset;var h=Math.floor(Math.abs(diff)/60);var m=Math.abs(diff)%60;var sign=Math.abs(diff)/diff;tzD3.hour-=h*sign;tzD3.minute-=m*sign;return tzD3}function toUTCISOString(date,timeZone){var whole=toUTC(date,timeZone);return toOffsetISOString(whole)}XTZ={toOffsetISOString:toOffsetISOString,formatOffset:formatOffset,toTimeZone:toTimeZone,toTimeZoneISOString:toTimeZoneISOString,toUTC:toUTC,toUTCISOString:toUTCISOString};if("undefined"!=typeof module&&module.exports){module.exports=XTZ}})();