qrono 1.4.0 → 1.5.1

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/dist/qrono.cjs CHANGED
@@ -1,2 +1,641 @@
1
- "use strict";var st=Object.defineProperty;var Q=Object.getOwnPropertySymbols;var rt=Object.prototype.hasOwnProperty,at=Object.prototype.propertyIsEnumerable;var G=(t,e,n)=>e in t?st(t,e,{enumerable:!0,configurable:!0,writable:!0,value:n}):t[e]=n,C=(t,e)=>{for(var n in e||(e={}))rt.call(e,n)&&G(t,n,e[n]);if(Q)for(var n of Q(e))at.call(e,n)&&G(t,n,e[n]);return t};Object.defineProperty(exports,Symbol.toStringTag,{value:"Module"});const ut=new Date(1915,0,1,12,0,0,0),w=7,K=24,M=60,I=M*K,V=60,ct=V*M,ft=ct*K,J=1e3,lt=V*J,X=ft*J;function g(t,...e){return e.flat().some(t.hasOwnProperty,t)}function ht(t){return Object.entries(t).filter(([,e])=>!dt(e)).map(([e])=>e)}function Y(t){return t!==void 0}function dt(t){return t instanceof Function}function mt(t){return typeof t=="string"||t instanceof String}function T(t){return t!==null&&typeof t=="object"&&t.constructor===Object}function _(t){return!Number.isNaN(t.getTime())}function S(t){return g(t,["year","month","day","hour","minute","second","millisecond"])}const $=s;s.date=u;const v={localtime:!1,disambiguation:"compatible"};s.context=function(t){if(Y(t)){for(const e of ht(v))g(t,e)&&(v[e]=t[e]);return this}return C({},v)};const yt=1,pt=2,tt=3,b=4,Dt=5,gt=6,Ot=7;Object.assign(s,{monday:yt,tuesday:pt,wednesday:tt,thursday:b,friday:Dt,saturday:gt,sunday:Ot});const f=Symbol();function s(...t){var r;if(!new.target)return new s(...t);const e={nativeDate:null,localtime:!1,disambiguation:"compatible",set:Mt,parse:St,valid:bt,context:wt,getNative:vt};if(this[f]=e,e.context(v),t[0]instanceof s){const i=t.shift();e.nativeDate=i.nativeDate(),e.context(i.context())}T(t[0])&&!S(t[0])&&e.context(t.shift());const n=t[0],a=t[1];if(n==null)(r=e.nativeDate)!=null||(e.nativeDate=new Date);else if(n instanceof Date)e.nativeDate=new Date(n.getTime());else if(mt(n))e.parse(n);else if(T(n)){if(!S(n))throw RangeError("Missing time field (year, minute, day, hour, minute, second or millisecond)");e.set(n)}else if(Number.isFinite(n)&&!Number.isFinite(a))e.nativeDate=new Date(n);else if(Number.isFinite(n)||Array.isArray(n)){const i=t.flat(),o=i.filter(h=>Number.isSafeInteger(h));if(o.length!==i.length)throw RangeError("Should be safe integers");if(o.length>7)throw RangeError("Too many numbers");e.set({year:o[0],month:o[1],day:o[2],hour:o[3],minute:o[4],second:o[5],millisecond:o[6]})}else throw TypeError(`Invalid argument ${t}`);return this}function bt(){return _(this.nativeDate)}function wt(t){return t?("localtime"in t&&(this.localtime=t.localtime),"disambiguation"in t&&(this.disambiguation=t.disambiguation),this):{localtime:this.localtime,disambiguation:this.disambiguation}}function vt(t){return this.nativeDate[`get${this.localtime?"":"UTC"}${t}`]()}function Mt(t){var N,k,H,U,E,A,P,R,j,W,x,L,q,z,B,Z;const e=C({},t);if(e.month=e.month&&e.month-1,!this.localtime){const D=(N=this.nativeDate)!=null?N:new Date(0),F=new Date(0);return F.setUTCFullYear((k=e.year)!=null?k:D.getUTCFullYear(),(H=e.month)!=null?H:D.getUTCMonth(),(U=e.day)!=null?U:D.getUTCDate()),F.setUTCHours((E=e.hour)!=null?E:D.getUTCHours(),(A=e.minute)!=null?A:D.getUTCMinutes(),(P=e.second)!=null?P:D.getUTCSeconds(),(R=e.millisecond)!=null?R:D.getUTCMilliseconds()),this.nativeDate=F,this}const n=!g(t,"hour","minute","second","millisecond"),a=n?"later":this.disambiguation,r=(j=this.nativeDate)!=null?j:new Date(0,0),i=new Date(ut.getTime()),o={year:(W=e.year)!=null?W:r.getFullYear(),month:(x=e.month)!=null?x:r.getMonth(),day:(L=e.day)!=null?L:r.getDate(),hour:(q=e.hour)!=null?q:n?0:r.getHours(),minute:(z=e.minute)!=null?z:n?0:r.getMinutes(),second:(B=e.second)!=null?B:n?0:r.getSeconds(),millisecond:(Z=e.millisecond)!=null?Z:n?0:r.getMilliseconds()};i.setFullYear(o.year,o.month,o.day),i.setHours(o.hour,o.minute,o.second,o.millisecond);const h=i.getTime(),d=new Date(h);d.setDate(i.getDate()+1);const l=new Date(h);l.setDate(i.getDate()-1);const c=d.getTimezoneOffset()-l.getTimezoneOffset();if(a==="compatible"||c===0)return this.nativeDate=i,this;const p=o.year*1e8+o.month*1e6+o.day*1e4+o.hour*100+o.minute<i.getFullYear()*1e8+i.getMonth()*1e6+i.getDate()*1e4+i.getHours()*100+i.getMinutes(),O=new Date(new Date(h).setUTCMinutes(i.getUTCMinutes()+c)),ot=O.getHours()===i.getHours()&&O.getMinutes()===i.getMinutes();if(!p&&!ot)return this.nativeDate=i,this;if(a==="reject")throw new RangeError(`Requested local time ${o} is ambiguous.`);return this.nativeDate=a==="later"?i:O,this}const Tt=new RegExp("^(\\d{4})(?:[-/]?([0-2]?\\d)(?:[-/]?([0-3]?\\d))?)?(?:[T\\s]([0-2]?\\d)(?::([0-5]?\\d)?(?::([0-6]?\\d)?(?:[.:](\\d{1,3})?\\d*)?)?)?)?(Z|[-+]\\d{2}:?\\d{2})?$");function St(t){var l;const e=t.trim().toUpperCase(),n=e.match(Tt);if(!n)throw RangeError(`Failed to parse '${t}'. Should be yyyy[[-|/]MM[[-|/]DD]][(T| )HH:mm[:ss[(.|:)SSS]]][Z|(+|-)hh:mm]`);const a=n[4]!==void 0,[r,i,o,h]=[+n[1],+n[2]||1,+n[3]||1,n[8]],d=new Date(e);if(!_(d))throw RangeError(`Failed to parse '${t}' by Date. Should be yyyy[[-|/]MM[[-|/]DD]][(T| )HH:mm[:ss[(.|:)SSS]]][Z|(+|-)hh:mm]`);return h?this.nativeDate=d:a?this.set({year:r,month:i,day:o,hour:+n[4]||0,minute:+n[5]||0,second:+n[6]||0,millisecond:+((l=n[7])==null?void 0:l.padStart(3,"0"))||0}):this.set({year:r,month:i,day:o}),this}const y=(t,e)=>String(t).padStart(e,"0");s.prototype.toString=function(){if(this[f].localtime){const t=this[f].nativeDate,e=-t.getTimezoneOffset(),n=Math.abs(e);return`${y(t.getFullYear(),4)}-${y(t.getMonth()+1,2)}-${y(t.getDate(),2)}T${y(t.getHours(),2)}:${y(t.getMinutes(),2)}:${y(t.getSeconds(),2)}.${y(t.getMilliseconds(),3)}${e>=0?"+":"-"}${y(Math.trunc(n/M),2)}:${y(n%M,2)}`}return this[f].nativeDate.toISOString()};s.prototype.valueOf=function(){return this[f].nativeDate.valueOf()};s.prototype.clone=function(...t){return new s(this,...t)};s.prototype.context=function(t){return Y(t)?this.clone(t):{localtime:this[f].localtime,disambiguation:this[f].disambiguation}};s.prototype.nativeDate=function(){return new Date(this[f].nativeDate.getTime())};s.prototype.offset=function(){return this[f].localtime?-this[f].nativeDate.getTimezoneOffset():0};s.prototype.valid=function(){return this[f].valid()};s.prototype.toObject=function(){return{year:this.year(),month:this.month(),day:this.day(),hour:this.hour(),minute:this.minute(),second:this.second(),millisecond:this.millisecond()}};s.prototype.toArray=function(){return[this.year(),this.month(),this.day(),this.hour(),this.minute(),this.second(),this.millisecond()]};s.prototype.toDate=function(...t){return new u(this.clone(...t))};for(const[t,e,n]of[["year","FullYear",0],["month","Month",1],["day","Date",0],["hour","Hours",0],["minute","Minutes",0],["second","Seconds",0],["millisecond","Milliseconds",0]])s.prototype[t]=function(a){return Y(a)?this.clone({[t]:a}):this[f].getNative(e)+n};s.prototype.dayOfWeek=function(){return 1+(this[f].getNative("Day")-1+w)%w};s.prototype.dayOfYear=function(){const t=this.toDate();return 1+t-t.startOfYear()};s.prototype.weekOfYear=function(){const t=this.toDate(),e=t.day(t.day()-t.dayOfWeek()+b),n=e.startOfYear(),a=n.dayOfWeek()===b?n:n.day(1+(b-n.dayOfWeek()+w)%w);return 1+Math.ceil((e-a)/w)};s.prototype.yearOfWeek=function(){const t=this.toDate();return t.day(t.day()-t.dayOfWeek()+b).year()};s.prototype.isLeapYear=function(){const t=this.year();return t%4===0&&(t%100!==0||t%400===0)};s.prototype.hasOffsetChangeInYear=function(){if(!this[f].localtime)return!1;const t=this.offset();return[3,6,9,12].map(e=>this.month(e).offset()).some(e=>e!==t)};s.prototype.isInDst=function(){if(!this[f].localtime)return!1;const t=this.offset();let e=!1,n=!1;for(let a=1;a<=5;a+=2)if(this.month(-a).offset()<t&&(e=!0),this.month(a).offset()<t&&(n=!0),e&&n)return!0;return!1};s.prototype.hasOffsetChangeInDay=function(){return this[f].localtime?this.minutesInDay()!==I:!1};s.prototype.minutesInDay=function(){if(!this[f].localtime)return I;const t=this.context({disambiguation:"later"}).startOfDay(),e=t.plus({day:1}).startOfDay();return t.day()===e.day()?I:(e-t)/lt};s.prototype.daysInMonth=function(){const t=[31,28,31,30,31,30,31,31,30,31,30,31],e=this.month();return t[e-1]+(this.isLeapYear()&&e===2?1:0)};s.prototype.daysInYear=function(){return this.isLeapYear()?366:365};s.prototype.weeksInYear=function(){const t=this.toDate({month:12,day:31}),e=t.minus({year:1});return t.dayOfWeek()===b||e.dayOfWeek()===tt?53:52};for(const[t,e]of[["Year",{month:1,day:1,hour:0,minute:0,second:0,millisecond:0}],["Month",{day:1,hour:0,minute:0,second:0,millisecond:0}],["Hour",{minute:0,second:0,millisecond:0}],["Minute",{second:0,millisecond:0}],["Second",{millisecond:0}]])s.prototype[`startOf${t}`]=function(){return this.clone(e)};s.prototype.startOfDay=function(){const t=this.clone({disambiguation:"later"},{hour:0,minute:0,second:0,millisecond:0}).valueOf();return this.clone(t)};function et(t){Object.assign(t,{isSame(e){return+this==+e},isBefore(e){return this<e},isAfter(e){return this>e},isSameOrBefore(e){return this<=e},isSameOrAfter(e){return this>=e},isBetween(e,n){return e<=this&&this<=n||n<=this&&this<=e}})}et(s.prototype);s.prototype.plus=function(...t){return nt.call(this,1,...t)};s.prototype.minus=function(...t){return nt.call(this,-1,...t)};function nt(t,...e){var h,d;const n=e[0],a=e[1];if(Number.isFinite(n)&&!Number.isFinite(a))return this.clone(this.valueOf()+n);let r=null;if(T(n)){if(!S(n))throw RangeError("Missing time field (year, minute, day, hour, minute, second or millisecond)");r=n}else if(Number.isFinite(n)||Array.isArray(n)){const l=e.flat(),c=l.filter(p=>Number.isSafeInteger(p));if(c.length!==l.length)throw RangeError("Should be safe integers");if(c.length>7)throw RangeError("Too many numbers");r={year:c[0],month:c[1],day:c[2],hour:c[3],minute:c[4],second:c[5],millisecond:c[6]}}else throw TypeError();const i=this.nativeDate(),o=this[f].localtime?"":"UTC";if(g(r,"year")||g(r,"month")){const l=this.year()+t*((h=r.year)!=null?h:0),c=this.month()+t*((d=r.month)!=null?d:0),p=new Date(i.getTime());p[`set${o}FullYear`](l,c,0);const O=p[`get${o}Date`]();O<this.day()?i[`set${o}FullYear`](l,p[`get${o}Month`](),O):i[`set${o}FullYear`](l,c-1)}g(r,"day")&&i[`set${o}Date`](i[`get${o}Date`]()+t*r.day);for(const[l,c]of[["hour","Hours"],["minute","Minutes"],["second","Seconds"],["millisecond","Milliseconds"]])!g(r,l)||r[l]==null||i[`setUTC${c}`](i[`getUTC${c}`]()+t*r[l]);return this.clone(i)}const m=Symbol();function u(...t){if(!new.target)return new u(...t);const e={datetime:null};this[m]=e;let n=null;t[0]instanceof u?n=t.shift().toDatetime():t[0]instanceof s&&(n=t.shift());const a=t[0],r=t[1];return Number.isFinite(a)&&!Number.isFinite(r)&&(t[0]=Math.floor(a)*X),n?n=n.clone(...t):n=$(...t),e.datetime=n.startOfDay(),this}u.prototype.toString=function(){return this[m].datetime.toString().substring(0,10)};u.prototype.valueOf=function(){return this[m].datetime/X};u.prototype.valid=function(){return this[m].datetime.valid()};u.prototype.clone=function(...t){return new u(this,...t)};u.prototype.toDatetime=function(){return $(this[m].datetime.toArray())};u.prototype.toObject=function(){return{year:this.year(),month:this.month(),day:this.day()}};u.prototype.toArray=function(){return[this.year(),this.month(),this.day()]};for(const t of["Year","Month"])u.prototype[`startOf${t}`]=function(){return new u(this[m].datetime[`startOf${t}`]())};for(const t of["year","month","day"])u.prototype[t]=function(e){return Y(e)?new u(this[m].datetime[t](e)):this[m].datetime[t]()};for(const t of["dayOfWeek","dayOfYear","weekOfYear","yearOfWeek","isLeapYear","daysInMonth","daysInYear","weeksInYear"])u.prototype[t]=function(){return this[m].datetime[t]()};for(const t of["minutesInDay","hasOffsetChangeInYear","hasOffsetChangeInDay"])u.prototype[t]=function(){return $({disambiguation:"later"},this[m].datetime.toArray().slice(0,3))[t]()};u.prototype.endOfYear=function(){return this.clone({month:12,day:31})};u.prototype.endOfMonth=function(){return this.clone({day:this.daysInMonth()})};et(u.prototype);u.prototype.plus=function(...t){return it.call(this,1,...t)};u.prototype.minus=function(...t){return it.call(this,-1,...t)};function it(t,...e){var o,h,d;const n=e[0],a=e[1],r=this[m].datetime;if(Number.isFinite(n)&&!Number.isFinite(a))return r.plus({day:t*n}).toDate();let i=null;if(T(n)&&S(n))i={year:t*((o=n.year)!=null?o:0),month:t*((h=n.month)!=null?h:0),day:t*((d=n.day)!=null?d:0)};else if(Number.isFinite(n)){if(e.length>3)throw RangeError("Too many arguments");i={year:t*n,month:t*a,day:t*arg2}}else if(Array.isArray(n)){if(n.length>3)throw RangeError("Too many elements");i={year:t*n[0],month:t*n[1],day:t*n[2]}}else throw TypeError();return r.plus(i).toDate()}exports.qrono=$;
2
- //# sourceMappingURL=qrono.cjs.map
1
+ Object.defineProperty(exports, Symbol.toStringTag, { value: "Module" });
2
+ //#region src/helpers.js
3
+ /**
4
+ * When creating or updating a local time, both `Date.setFullYear` and `Date.setHours` are used.
5
+ * Noon is used as the initial reference point to avoid the time after `setFullYear`
6
+ * from falling into an ambiguous DST period. Historically, DST transitions in all countries
7
+ * have been scheduled around midnight, and it should be the same in the future.
8
+ * The reason for selecting 1915 as the initial value is as follows.
9
+ * Since DST was first established in 1916, the initial value should be set to a year prior to that.
10
+ * If a year too far in the past is chosen, it may correspond to a period when time zones were not yet
11
+ * precisely defined in some regions, which could result in unexpected timezone offsets.
12
+ */
13
+ var initialSafeDate = new Date(1915, 0, 1, 12, 0, 0, 0);
14
+ var minutesPerDay = 1440;
15
+ minutesPerDay * 7;
16
+ var secondsPerHour = 3600;
17
+ var secondsPerDay = secondsPerHour * 24;
18
+ var secondsPerWeek = secondsPerDay * 7;
19
+ var millisecondsPerSecond = 1e3;
20
+ var millisecondsPerMinute = 60 * millisecondsPerSecond;
21
+ secondsPerHour * millisecondsPerSecond;
22
+ var millisecondsPerDay = secondsPerDay * millisecondsPerSecond;
23
+ secondsPerWeek * millisecondsPerSecond;
24
+ function has(object, ...keys) {
25
+ return keys.flat().some(object.hasOwnProperty, object);
26
+ }
27
+ function fields(object) {
28
+ return Object.entries(object).filter(([, value]) => !isFunction(value)).map(([key]) => key);
29
+ }
30
+ function given(arg) {
31
+ return arg !== void 0;
32
+ }
33
+ function isFunction(a) {
34
+ return a instanceof Function;
35
+ }
36
+ function isString(a) {
37
+ return typeof a === "string" || a instanceof String;
38
+ }
39
+ function isObject(a) {
40
+ return a !== null && typeof a === "object" && a.constructor === Object;
41
+ }
42
+ function isValidDate(date) {
43
+ return !Number.isNaN(date.getTime());
44
+ }
45
+ function hasDatetimeField(object) {
46
+ return has(object, [
47
+ "year",
48
+ "month",
49
+ "day",
50
+ "hour",
51
+ "minute",
52
+ "second",
53
+ "millisecond"
54
+ ]);
55
+ }
56
+ //#endregion
57
+ //#region src/qrono.js
58
+ var qrono = Qrono;
59
+ Qrono.date = QronoDate;
60
+ var defaultContext = {
61
+ localtime: false,
62
+ disambiguation: "compatible"
63
+ };
64
+ Qrono.context = function(context) {
65
+ if (given(context)) {
66
+ for (const key of fields(defaultContext)) {
67
+ if (!has(context, key)) continue;
68
+ defaultContext[key] = context[key];
69
+ }
70
+ return this;
71
+ }
72
+ return { ...defaultContext };
73
+ };
74
+ var monday = 1;
75
+ var tuesday = 2;
76
+ var wednesday = 3;
77
+ var thursday = 4;
78
+ Object.assign(Qrono, {
79
+ monday,
80
+ tuesday,
81
+ wednesday,
82
+ thursday,
83
+ friday: 5,
84
+ saturday: 6,
85
+ sunday: 7
86
+ });
87
+ var internal = Symbol();
88
+ function Qrono(...args) {
89
+ if (!new.target) return new Qrono(...args);
90
+ const self = {
91
+ nativeDate: null,
92
+ localtime: false,
93
+ disambiguation: "compatible",
94
+ set,
95
+ parse,
96
+ valid,
97
+ context,
98
+ getNative
99
+ };
100
+ this[internal] = self;
101
+ self.context(defaultContext);
102
+ if (args[0] instanceof Qrono) {
103
+ const source = args.shift();
104
+ self.nativeDate = source.nativeDate();
105
+ self.context(source.context());
106
+ }
107
+ if (isObject(args[0]) && !hasDatetimeField(args[0])) self.context(args.shift());
108
+ const first = args[0];
109
+ const second = args[1];
110
+ if (first == null) self.nativeDate ??= /* @__PURE__ */ new Date();
111
+ else if (first instanceof Date) self.nativeDate = new Date(first.getTime());
112
+ else if (isString(first)) self.parse(first);
113
+ else if (isObject(first)) {
114
+ if (!hasDatetimeField(first)) throw RangeError("Missing time field (year, minute, day, hour, minute, second or millisecond)");
115
+ self.set(first);
116
+ } else if (Number.isFinite(first) && !Number.isFinite(second)) self.nativeDate = new Date(first);
117
+ else if (Number.isFinite(first) || Array.isArray(first)) {
118
+ const flat = args.flat();
119
+ const values = flat.filter((v) => Number.isSafeInteger(v));
120
+ if (values.length !== flat.length) throw RangeError("Should be safe integers");
121
+ if (values.length > 7) throw RangeError("Too many numbers");
122
+ self.set({
123
+ year: values[0],
124
+ month: values[1],
125
+ day: values[2],
126
+ hour: values[3],
127
+ minute: values[4],
128
+ second: values[5],
129
+ millisecond: values[6]
130
+ });
131
+ } else throw TypeError(`Invalid argument ${args}`);
132
+ return this;
133
+ }
134
+ function valid() {
135
+ return isValidDate(this.nativeDate);
136
+ }
137
+ function context(arg) {
138
+ if (!arg) return {
139
+ localtime: this.localtime,
140
+ disambiguation: this.disambiguation
141
+ };
142
+ if ("localtime" in arg) this.localtime = arg.localtime;
143
+ if ("disambiguation" in arg) this.disambiguation = arg.disambiguation;
144
+ return this;
145
+ }
146
+ function getNative(name) {
147
+ return this.nativeDate[`get${this.localtime ? "" : "UTC"}${name}`]();
148
+ }
149
+ function set(values) {
150
+ const args = { ...values };
151
+ args.month = args.month && args.month - 1;
152
+ if (!this.localtime) {
153
+ const baseDate = this.nativeDate ?? /* @__PURE__ */ new Date(0);
154
+ const newDate = /* @__PURE__ */ new Date(0);
155
+ newDate.setUTCFullYear(args.year ?? baseDate.getUTCFullYear(), args.month ?? baseDate.getUTCMonth(), args.day ?? baseDate.getUTCDate());
156
+ newDate.setUTCHours(args.hour ?? baseDate.getUTCHours(), args.minute ?? baseDate.getUTCMinutes(), args.second ?? baseDate.getUTCSeconds(), args.millisecond ?? baseDate.getUTCMilliseconds());
157
+ this.nativeDate = newDate;
158
+ return this;
159
+ }
160
+ const dateOnly = !has(values, "hour", "minute", "second", "millisecond");
161
+ const disambig = dateOnly ? "later" : this.disambiguation;
162
+ const baseDate = this.nativeDate ?? new Date(0, 0);
163
+ const newDate = new Date(initialSafeDate.getTime());
164
+ const requested = {
165
+ year: args.year ?? baseDate.getFullYear(),
166
+ month: args.month ?? baseDate.getMonth(),
167
+ day: args.day ?? baseDate.getDate(),
168
+ hour: args.hour ?? (dateOnly ? 0 : baseDate.getHours()),
169
+ minute: args.minute ?? (dateOnly ? 0 : baseDate.getMinutes()),
170
+ second: args.second ?? (dateOnly ? 0 : baseDate.getSeconds()),
171
+ millisecond: args.millisecond ?? (dateOnly ? 0 : baseDate.getMilliseconds())
172
+ };
173
+ newDate.setFullYear(requested.year, requested.month, requested.day);
174
+ newDate.setHours(requested.hour, requested.minute, requested.second, requested.millisecond);
175
+ const numeric = newDate.getTime();
176
+ const nextDay = new Date(numeric);
177
+ nextDay.setDate(newDate.getDate() + 1);
178
+ const prevDay = new Date(numeric);
179
+ prevDay.setDate(newDate.getDate() - 1);
180
+ const adjust = nextDay.getTimezoneOffset() - prevDay.getTimezoneOffset();
181
+ if (disambig === "compatible" || adjust === 0) {
182
+ this.nativeDate = newDate;
183
+ return this;
184
+ }
185
+ const isGap = requested.year * 1e8 + requested.month * 1e6 + requested.day * 1e4 + requested.hour * 100 + requested.minute < newDate.getFullYear() * 1e8 + newDate.getMonth() * 1e6 + newDate.getDate() * 1e4 + newDate.getHours() * 100 + newDate.getMinutes();
186
+ const adjustedUTC = new Date(new Date(numeric).setUTCMinutes(newDate.getUTCMinutes() + adjust));
187
+ const isOverlap = adjustedUTC.getHours() === newDate.getHours() && adjustedUTC.getMinutes() === newDate.getMinutes();
188
+ if (!isGap && !isOverlap) {
189
+ this.nativeDate = newDate;
190
+ return this;
191
+ }
192
+ if (disambig === "reject") throw new RangeError(`Requested local time ${requested} is ambiguous.`);
193
+ this.nativeDate = disambig === "later" ? newDate : adjustedUTC;
194
+ return this;
195
+ }
196
+ var parsePattern = /* @__PURE__ */ new RegExp("^(\\d{4})(?:[-/]?([0-2]?\\d)(?:[-/]?([0-3]?\\d))?)?(?:[T\\s]([0-2]?\\d)(?::([0-5]?\\d)?(?::([0-6]?\\d)?(?:[.:](\\d{1,3})?\\d*)?)?)?)?(Z|[-+]\\d{2}:?\\d{2})?$");
197
+ function parse(str) {
198
+ const text = str.trim().toUpperCase();
199
+ const values = text.match(parsePattern);
200
+ if (!values) throw RangeError(`Failed to parse '${str}'. Should be yyyy[[-|/]MM[[-|/]DD]][(T| )HH:mm[:ss[(.|:)SSS]]][Z|(+|-)hh:mm]`);
201
+ const hasTime = values[4] !== void 0;
202
+ const [year, month, day, offset] = [
203
+ +values[1],
204
+ +values[2] || 1,
205
+ +values[3] || 1,
206
+ values[8]
207
+ ];
208
+ const native = new Date(text);
209
+ if (!isValidDate(native)) throw RangeError(`Failed to parse '${str}' by Date. Should be yyyy[[-|/]MM[[-|/]DD]][(T| )HH:mm[:ss[(.|:)SSS]]][Z|(+|-)hh:mm]`);
210
+ if (offset) this.nativeDate = native;
211
+ else if (hasTime) this.set({
212
+ year,
213
+ month,
214
+ day,
215
+ hour: +values[4] || 0,
216
+ minute: +values[5] || 0,
217
+ second: +values[6] || 0,
218
+ millisecond: +values[7]?.padStart(3, "0") || 0
219
+ });
220
+ else this.set({
221
+ year,
222
+ month,
223
+ day
224
+ });
225
+ return this;
226
+ }
227
+ var pad0 = (v, n) => String(v).padStart(n, "0");
228
+ Qrono.prototype.toString = function() {
229
+ if (this[internal].localtime) {
230
+ const t = this[internal].nativeDate;
231
+ const offset = -t.getTimezoneOffset();
232
+ const offsetAbs = Math.abs(offset);
233
+ return `${pad0(t.getFullYear(), 4)}-${pad0(t.getMonth() + 1, 2)}-${pad0(t.getDate(), 2)}T${pad0(t.getHours(), 2)}:${pad0(t.getMinutes(), 2)}:${pad0(t.getSeconds(), 2)}.${pad0(t.getMilliseconds(), 3)}${offset >= 0 ? "+" : "-"}${pad0(Math.trunc(offsetAbs / 60), 2)}:${pad0(offsetAbs % 60, 2)}`;
234
+ }
235
+ return this[internal].nativeDate.toISOString();
236
+ };
237
+ Qrono.prototype.valueOf = function() {
238
+ return this[internal].nativeDate.valueOf();
239
+ };
240
+ Qrono.prototype.clone = function(...args) {
241
+ return new Qrono(this, ...args);
242
+ };
243
+ Qrono.prototype.context = function(context) {
244
+ return given(context) ? this.clone(context) : {
245
+ localtime: this[internal].localtime,
246
+ disambiguation: this[internal].disambiguation
247
+ };
248
+ };
249
+ Qrono.prototype.nativeDate = function() {
250
+ return new Date(this[internal].nativeDate.getTime());
251
+ };
252
+ Qrono.prototype.offset = function() {
253
+ return this[internal].localtime ? -this[internal].nativeDate.getTimezoneOffset() : 0;
254
+ };
255
+ Qrono.prototype.valid = function() {
256
+ return this[internal].valid();
257
+ };
258
+ Qrono.prototype.toObject = function() {
259
+ return {
260
+ year: this.year(),
261
+ month: this.month(),
262
+ day: this.day(),
263
+ hour: this.hour(),
264
+ minute: this.minute(),
265
+ second: this.second(),
266
+ millisecond: this.millisecond()
267
+ };
268
+ };
269
+ Qrono.prototype.toArray = function() {
270
+ return [
271
+ this.year(),
272
+ this.month(),
273
+ this.day(),
274
+ this.hour(),
275
+ this.minute(),
276
+ this.second(),
277
+ this.millisecond()
278
+ ];
279
+ };
280
+ Qrono.prototype.toDate = function(...args) {
281
+ return new QronoDate(this.clone(...args));
282
+ };
283
+ for (const [field, native, base] of [
284
+ [
285
+ "year",
286
+ "FullYear",
287
+ 0
288
+ ],
289
+ [
290
+ "month",
291
+ "Month",
292
+ 1
293
+ ],
294
+ [
295
+ "day",
296
+ "Date",
297
+ 0
298
+ ],
299
+ [
300
+ "hour",
301
+ "Hours",
302
+ 0
303
+ ],
304
+ [
305
+ "minute",
306
+ "Minutes",
307
+ 0
308
+ ],
309
+ [
310
+ "second",
311
+ "Seconds",
312
+ 0
313
+ ],
314
+ [
315
+ "millisecond",
316
+ "Milliseconds",
317
+ 0
318
+ ]
319
+ ]) Qrono.prototype[field] = function(value) {
320
+ return given(value) ? this.clone({ [field]: value }) : this[internal].getNative(native) + base;
321
+ };
322
+ Qrono.prototype.dayOfWeek = function() {
323
+ return 1 + (this[internal].getNative("Day") - 1 + 7) % 7;
324
+ };
325
+ Qrono.prototype.dayOfYear = function() {
326
+ const date = this.toDate();
327
+ return 1 + date - date.startOfYear();
328
+ };
329
+ Qrono.prototype.weekOfYear = function() {
330
+ const date = this.toDate();
331
+ const theThursday = date.day(date.day() - date.dayOfWeek() + thursday);
332
+ const startOfYear = theThursday.startOfYear();
333
+ const firstThursday = startOfYear.dayOfWeek() === thursday ? startOfYear : startOfYear.day(1 + (thursday - startOfYear.dayOfWeek() + 7) % 7);
334
+ return 1 + Math.ceil((theThursday - firstThursday) / 7);
335
+ };
336
+ Qrono.prototype.yearOfWeek = function() {
337
+ const date = this.toDate();
338
+ return date.day(date.day() - date.dayOfWeek() + thursday).year();
339
+ };
340
+ Qrono.prototype.isLeapYear = function() {
341
+ const year = this.year();
342
+ return year % 4 === 0 && (year % 100 !== 0 || year % 400 === 0);
343
+ };
344
+ Qrono.prototype.hasOffsetChangeInYear = function() {
345
+ if (!this[internal].localtime) return false;
346
+ const currentOffset = this.offset();
347
+ return [
348
+ 3,
349
+ 6,
350
+ 9,
351
+ 12
352
+ ].map((month) => this.month(month).offset()).some((offset) => offset !== currentOffset);
353
+ };
354
+ Qrono.prototype.isInDst = function() {
355
+ if (!this[internal].localtime) return false;
356
+ const offset = this.offset();
357
+ let past = false, future = false;
358
+ for (let i = 1; i <= 5; i += 2) {
359
+ if (this.month(-i).offset() < offset) past = true;
360
+ if (this.month(i).offset() < offset) future = true;
361
+ if (past && future) return true;
362
+ }
363
+ return false;
364
+ };
365
+ Qrono.prototype.hasOffsetChangeInDay = function() {
366
+ if (!this[internal].localtime) return false;
367
+ return this.minutesInDay() !== minutesPerDay;
368
+ };
369
+ Qrono.prototype.minutesInDay = function() {
370
+ if (!this[internal].localtime) return minutesPerDay;
371
+ const startOfDay = this.context({ disambiguation: "later" }).startOfDay();
372
+ const nextDay = startOfDay.plus({ day: 1 }).startOfDay();
373
+ if (startOfDay.day() === nextDay.day()) return minutesPerDay;
374
+ return (nextDay - startOfDay) / millisecondsPerMinute;
375
+ };
376
+ Qrono.prototype.daysInMonth = function() {
377
+ const days = [
378
+ 31,
379
+ 28,
380
+ 31,
381
+ 30,
382
+ 31,
383
+ 30,
384
+ 31,
385
+ 31,
386
+ 30,
387
+ 31,
388
+ 30,
389
+ 31
390
+ ];
391
+ const month = this.month();
392
+ return days[month - 1] + (this.isLeapYear() && month === 2 ? 1 : 0);
393
+ };
394
+ Qrono.prototype.daysInYear = function() {
395
+ return this.isLeapYear() ? 366 : 365;
396
+ };
397
+ Qrono.prototype.weeksInYear = function() {
398
+ const endOfYear = this.toDate({
399
+ month: 12,
400
+ day: 31
401
+ });
402
+ const endOfLastYear = endOfYear.minus({ year: 1 });
403
+ if (endOfYear.dayOfWeek() === thursday || endOfLastYear.dayOfWeek() === wednesday) return 53;
404
+ return 52;
405
+ };
406
+ for (const [name, cloneArg] of [
407
+ ["Year", {
408
+ month: 1,
409
+ day: 1,
410
+ hour: 0,
411
+ minute: 0,
412
+ second: 0,
413
+ millisecond: 0
414
+ }],
415
+ ["Month", {
416
+ day: 1,
417
+ hour: 0,
418
+ minute: 0,
419
+ second: 0,
420
+ millisecond: 0
421
+ }],
422
+ ["Hour", {
423
+ minute: 0,
424
+ second: 0,
425
+ millisecond: 0
426
+ }],
427
+ ["Minute", {
428
+ second: 0,
429
+ millisecond: 0
430
+ }],
431
+ ["Second", { millisecond: 0 }]
432
+ ]) Qrono.prototype[`startOf${name}`] = function() {
433
+ return this.clone(cloneArg);
434
+ };
435
+ Qrono.prototype.startOfDay = function() {
436
+ const timestamp = this.clone({ disambiguation: "later" }, {
437
+ hour: 0,
438
+ minute: 0,
439
+ second: 0,
440
+ millisecond: 0
441
+ }).valueOf();
442
+ return this.clone(timestamp);
443
+ };
444
+ function implementComparison(prototype) {
445
+ Object.assign(prototype, {
446
+ isSame(another) {
447
+ return +this === +another;
448
+ },
449
+ isBefore(another) {
450
+ return this < another;
451
+ },
452
+ isAfter(another) {
453
+ return this > another;
454
+ },
455
+ isSameOrBefore(another) {
456
+ return this <= another;
457
+ },
458
+ isSameOrAfter(another) {
459
+ return this >= another;
460
+ },
461
+ isBetween(a, b) {
462
+ return a <= this && this <= b || b <= this && this <= a;
463
+ }
464
+ });
465
+ }
466
+ implementComparison(Qrono.prototype);
467
+ Qrono.prototype.plus = function(...args) {
468
+ return plus.call(this, 1, ...args);
469
+ };
470
+ Qrono.prototype.minus = function(...args) {
471
+ return plus.call(this, -1, ...args);
472
+ };
473
+ function plus(sign, ...args) {
474
+ const arg0 = args[0];
475
+ const arg1 = args[1];
476
+ if (Number.isFinite(arg0) && !Number.isFinite(arg1)) return this.clone(this.valueOf() + arg0);
477
+ let timeFields = null;
478
+ if (isObject(arg0)) {
479
+ if (!hasDatetimeField(arg0)) throw RangeError("Missing time field (year, minute, day, hour, minute, second or millisecond)");
480
+ timeFields = arg0;
481
+ } else if (Number.isFinite(arg0) || Array.isArray(arg0)) {
482
+ const flat = args.flat();
483
+ const values = flat.filter((v) => Number.isSafeInteger(v));
484
+ if (values.length !== flat.length) throw RangeError("Should be safe integers");
485
+ if (values.length > 7) throw RangeError("Too many numbers");
486
+ timeFields = {
487
+ year: values[0],
488
+ month: values[1],
489
+ day: values[2],
490
+ hour: values[3],
491
+ minute: values[4],
492
+ second: values[5],
493
+ millisecond: values[6]
494
+ };
495
+ } else throw TypeError();
496
+ const date = this.nativeDate();
497
+ const utc = this[internal].localtime ? "" : "UTC";
498
+ if (has(timeFields, "year") || has(timeFields, "month")) {
499
+ const year = this.year() + sign * (timeFields.year ?? 0);
500
+ const month = this.month() + sign * (timeFields.month ?? 0);
501
+ const endOfMonth = new Date(date.getTime());
502
+ endOfMonth[`set${utc}FullYear`](year, month, 0);
503
+ const lastDay = endOfMonth[`get${utc}Date`]();
504
+ if (lastDay < this.day()) date[`set${utc}FullYear`](year, endOfMonth[`get${utc}Month`](), lastDay);
505
+ else date[`set${utc}FullYear`](year, month - 1);
506
+ }
507
+ if (has(timeFields, "day")) date[`set${utc}Date`](date[`get${utc}Date`]() + sign * timeFields.day);
508
+ for (const [key, nativeKey] of [
509
+ ["hour", "Hours"],
510
+ ["minute", "Minutes"],
511
+ ["second", "Seconds"],
512
+ ["millisecond", "Milliseconds"]
513
+ ]) {
514
+ if (!has(timeFields, key) || timeFields[key] == null) continue;
515
+ date[`setUTC${nativeKey}`](date[`getUTC${nativeKey}`]() + sign * timeFields[key]);
516
+ }
517
+ return this.clone(date);
518
+ }
519
+ var internalDate = Symbol();
520
+ function QronoDate(...args) {
521
+ if (!new.target) return new QronoDate(...args);
522
+ const self = { datetime: null };
523
+ this[internalDate] = self;
524
+ let source = null;
525
+ if (args[0] instanceof QronoDate) source = args.shift().toDatetime();
526
+ else if (args[0] instanceof Qrono) source = args.shift();
527
+ const first = args[0];
528
+ const second = args[1];
529
+ if (Number.isFinite(first) && !Number.isFinite(second)) args[0] = Math.floor(first) * millisecondsPerDay;
530
+ if (source) source = source.clone(...args);
531
+ else source = qrono(...args);
532
+ self.datetime = source.startOfDay();
533
+ return this;
534
+ }
535
+ QronoDate.prototype.toString = function() {
536
+ return this[internalDate].datetime.toString().substring(0, 10);
537
+ };
538
+ QronoDate.prototype.valueOf = function() {
539
+ return this[internalDate].datetime / millisecondsPerDay;
540
+ };
541
+ QronoDate.prototype.valid = function() {
542
+ return this[internalDate].datetime.valid();
543
+ };
544
+ QronoDate.prototype.clone = function(...args) {
545
+ return new QronoDate(this, ...args);
546
+ };
547
+ QronoDate.prototype.toDatetime = function() {
548
+ return qrono(this[internalDate].datetime.toArray());
549
+ };
550
+ QronoDate.prototype.toObject = function() {
551
+ return {
552
+ year: this.year(),
553
+ month: this.month(),
554
+ day: this.day()
555
+ };
556
+ };
557
+ QronoDate.prototype.toArray = function() {
558
+ return [
559
+ this.year(),
560
+ this.month(),
561
+ this.day()
562
+ ];
563
+ };
564
+ for (const name of ["Year", "Month"]) QronoDate.prototype[`startOf${name}`] = function() {
565
+ return new QronoDate(this[internalDate].datetime[`startOf${name}`]());
566
+ };
567
+ for (const field of [
568
+ "year",
569
+ "month",
570
+ "day"
571
+ ]) QronoDate.prototype[field] = function(value) {
572
+ if (given(value)) return new QronoDate(this[internalDate].datetime[field](value));
573
+ return this[internalDate].datetime[field]();
574
+ };
575
+ for (const method of [
576
+ "dayOfWeek",
577
+ "dayOfYear",
578
+ "weekOfYear",
579
+ "yearOfWeek",
580
+ "isLeapYear",
581
+ "daysInMonth",
582
+ "daysInYear",
583
+ "weeksInYear"
584
+ ]) QronoDate.prototype[method] = function() {
585
+ return this[internalDate].datetime[method]();
586
+ };
587
+ for (const method of [
588
+ "minutesInDay",
589
+ "hasOffsetChangeInYear",
590
+ "hasOffsetChangeInDay"
591
+ ]) QronoDate.prototype[method] = function() {
592
+ return qrono({ disambiguation: "later" }, this[internalDate].datetime.toArray().slice(0, 3))[method]();
593
+ };
594
+ QronoDate.prototype.endOfYear = function() {
595
+ return this.clone({
596
+ month: 12,
597
+ day: 31
598
+ });
599
+ };
600
+ QronoDate.prototype.endOfMonth = function() {
601
+ return this.clone({ day: this.daysInMonth() });
602
+ };
603
+ implementComparison(QronoDate.prototype);
604
+ QronoDate.prototype.plus = function(...args) {
605
+ return plusDate.call(this, 1, ...args);
606
+ };
607
+ QronoDate.prototype.minus = function(...args) {
608
+ return plusDate.call(this, -1, ...args);
609
+ };
610
+ function plusDate(sign, ...args) {
611
+ const arg0 = args[0];
612
+ const arg1 = args[1];
613
+ const datetime = this[internalDate].datetime;
614
+ if (Number.isFinite(arg0) && !Number.isFinite(arg1)) return datetime.plus({ day: sign * arg0 }).toDate();
615
+ let timeFields = null;
616
+ if (isObject(arg0) && hasDatetimeField(arg0)) timeFields = {
617
+ year: sign * (arg0.year ?? 0),
618
+ month: sign * (arg0.month ?? 0),
619
+ day: sign * (arg0.day ?? 0)
620
+ };
621
+ else if (Number.isFinite(arg0)) {
622
+ if (args.length > 3) throw RangeError("Too many arguments");
623
+ timeFields = {
624
+ year: sign * arg0,
625
+ month: sign * arg1,
626
+ day: sign * arg2
627
+ };
628
+ } else if (Array.isArray(arg0)) {
629
+ if (arg0.length > 3) throw RangeError("Too many elements");
630
+ timeFields = {
631
+ year: sign * arg0[0],
632
+ month: sign * arg0[1],
633
+ day: sign * arg0[2]
634
+ };
635
+ } else throw TypeError();
636
+ return datetime.plus(timeFields).toDate();
637
+ }
638
+ //#endregion
639
+ exports.qrono = qrono;
640
+
641
+ //# sourceMappingURL=qrono.cjs.map