/* COPYRIGHT Esri - https://js.arcgis.com/5.1.2/LICENSE.txt */
import{resolveTimeZone as e}from"../intl/date.js";import{system as t}from"../time/constants.js";import{IANAZone as s,DateTime as n}from"luxon";const r=/^(?<hh>([01][0-9])|(2[0-3])):(?<mm>[0-5][0-9])(:(?<ss>[0-5][0-9]))?(\.(?<ms>\d+))?$/,o={milliseconds:1,seconds:1e3,minutes:6e4,hours:36e5,days:864e5,weeks:6048e5,months:26784e5,years:31536e6,decades:31536e7,centuries:31536e8,unknown:NaN};function u(r,o,u,c=t){if("unknown"===u)return r;const a=new s(e(c)),i=n.fromJSDate(r,{zone:a}),l="decades"===u||"centuries"===u?"year":y(u);return"decades"===u&&(o*=10),"centuries"===u&&(o*=100),i.plus({[l]:o}).toJSDate()}function c(e,t,s="milliseconds"){const n=e.getTime(),r=h(t,s,"milliseconds");return new Date(n+r)}function a(r,o,u=t){const{unit:c}=o,a=Math.max(1,Math.trunc(o.value)),i=new s(e(u)),l=n.fromJSDate(r,{zone:i}),m=e=>e-e%a;switch(c){case"milliseconds":return l.startOf("second").plus({millisecond:m(l.millisecond)}).toJSDate();case"seconds":return l.startOf("minute").plus({second:m(l.second)}).toJSDate();case"minutes":return l.startOf("hour").plus({minute:m(l.minute)}).toJSDate();case"hours":return l.startOf("day").plus({hour:m(l.hour)}).toJSDate();case"days":return l.startOf("month").plus({day:m(l.day-1)}).toJSDate();case"weeks":{const e=l.startOf("year").startOf("week"),t=Math.floor(l.diff(e,"weeks").weeks);return e.plus({week:m(t)}).toJSDate()}case"months":return l.startOf("year").plus({month:m(l.month-1)}).toJSDate();case"years":case"decades":case"centuries":{const e=l.startOf("year"),t=("decades"===c?10:"centuries"===c?100:1)*a,s=e.year-e.year%t;return e.set({year:s}).toJSDate()}case"unknown":return r}}function i(e,t,s="milliseconds"){const n=new Date(h(t,s,"milliseconds"));return n.setUTCFullYear(e.getUTCFullYear(),e.getUTCMonth(),e.getUTCDate()),n}function l(e,t="milliseconds"){const s=h(e.getUTCHours(),"hours","milliseconds"),n=h(e.getUTCMinutes(),"minutes","milliseconds"),r=h(e.getUTCSeconds(),"seconds","milliseconds");return h(s+n+r+e.getUTCMilliseconds(),"milliseconds",t)}function m(e){const t=r.exec(e);if(!t)return null;const{hh:s,mm:n,ss:u,ms:c}=t.groups;return Number(s)*o.hours+Number(n)*o.minutes+Number(u||0)*o.seconds+Number(c||0)}function d(e,t){const s=new Date(e);return s.setUTCFullYear(t.getFullYear(),t.getMonth(),t.getDate()),s}function f(e){const t=new Date(0);return t.setHours(0),t.setMinutes(0),t.setSeconds(0),t.setMilliseconds(0),t.setFullYear(e.getUTCFullYear(),e.getUTCMonth(),e.getUTCDate()),t}function h(e,t,s){if(0===e)return 0;return e*o[t]/o[s]}function w(e){return e instanceof Date&&!isNaN(e.getTime())}function D(e,t){return e&&t?e.intersection(t):e||t}function y(e){switch(e){case"milliseconds":return"millisecond";case"seconds":return"second";case"minutes":return"minute";case"hours":return"hour";case"days":return"day";case"weeks":return"week";case"months":return"month";case"years":return"year"}}export{h as convertTime,w as isValidDate,i as makeUTCTime,D as mergeTimeExtents,o as millisecondsPerTimeUnit,u as offsetDate,c as offsetDateUTC,d as resetUTCDate,m as timeOnlyToMilliseconds,l as timeSinceUTCMidnight,a as truncateDate,f as truncateLocalTime};
