UNPKG

@alessiofrittoli/date-utils

Version:

Lightweight TypeScript date utility functions library

2 lines (1 loc) 2.12 kB
"use strict";Object.defineProperty(exports, "__esModule", {value: true}); function _optionalChain(ops) { let lastAccessLHS = undefined; let value = ops[0]; let i = 1; while (i < ops.length) { const op = ops[i]; const fn = ops[i + 1]; i += 2; if ((op === 'optionalAccess' || op === 'optionalCall') && value == null) { return undefined; } if (op === 'access' || op === 'optionalAccess') { lastAccessLHS = value; value = fn(value); } else if (op === 'call' || op === 'optionalCall') { value = fn((...args) => value.call(lastAccessLHS, ...args)); lastAccessLHS = undefined; } } return value; }var _mathutils = require('@alessiofrittoli/math-utils');var T=()=>Intl.DateTimeFormat().resolvedOptions().timeZone,f= exports.b =(e=new Date)=>-(new Date(e).getTimezoneOffset()/60),o= exports.c =e=>{if(e.endsWith("Z"))return 0;let t=e.replace(/:/g,"").match(/GMT([+-]\d{4})/);if(!_optionalChain([t, 'optionalAccess', _ => _[1]]))return f(e);let n=t[1],r=parseInt(n.slice(0,3),10),m=parseInt(n.slice(3),10);return(r*60*60+m*60)/60/60},s= exports.d =(e,t)=>t?t==="UTC"?0:typeof e=="string"?o(e)||o(i({date:e,timeZone:t,timeZoneName:"longOffset"})):o(i({date:e,timeZone:t,timeZoneName:"longOffset"})):typeof e=="string"?o(e):f(e),D= exports.e =(e,t,n=":")=>{let r=-(s(e,t)*60);return(r<=0?"+":"-")+_mathutils.padStart.call(void 0, parseInt(String(Math.abs(r/60))),2)+(n||"")+_mathutils.padStart.call(void 0, Math.abs(r%60),2)},i= exports.f =e=>Intl.DateTimeFormat(_optionalChain([e, 'optionalAccess', _2 => _2.locale]),{timeZoneName:_optionalChain([e, 'optionalAccess', _3 => _3.timeZoneName])||"shortOffset",timeZone:_optionalChain([e, 'optionalAccess', _4 => _4.timeZone])}).formatToParts(_optionalChain([e, 'optionalAccess', _5 => _5.date])?new Date(e.date):void 0).find(t=>t.type==="timeZoneName").value,c= exports.g =(e=new Date,t)=>{let n=new Date(e),r=new Date(n.getFullYear(),0,1),m=new Date(n.getFullYear(),6,1);return Math.max(s(r,t)*-60,s(m,t)*-60)},p= exports.h =(e=new Date,t)=>s(e,t)*-60<c(e,t);exports.a = T; exports.b = f; exports.c = o; exports.d = s; exports.e = D; exports.f = i; exports.g = c; exports.h = p;