datevolt 1.0.0 → 1.0.2

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/src/tempox.js DELETED
@@ -1,456 +0,0 @@
1
- 'use strict';
2
-
3
- var parseModule = require('./parse');
4
- var localeModule = require('./locale');
5
- var Duration = require('./duration');
6
-
7
- var UNIT_MAP = {
8
- year:'year',years:'year',y:'year',
9
- month:'month',months:'month',M:'month',
10
- week:'week',weeks:'week',w:'week',
11
- day:'day',days:'day',d:'day',
12
- hour:'hour',hours:'hour',h:'hour',
13
- minute:'minute',minutes:'minute',m:'minute',
14
- second:'second',seconds:'second',s:'second',
15
- millisecond:'millisecond',milliseconds:'millisecond',ms:'millisecond'
16
- };
17
-
18
- function normalizeUnit(u) { return UNIT_MAP[u] || u; }
19
-
20
- // ─────────────────────────────────────────────────────────────────────────────
21
- // Tempox constructor
22
- // ─────────────────────────────────────────────────────────────────────────────
23
- function Tempox(input, fmt, strict, utcMode, tzOffset) {
24
- if (!(this instanceof Tempox)) return new Tempox(input, fmt, strict, utcMode, tzOffset);
25
- this.__isTempox = true;
26
-
27
- var parsed = parseModule.parseInput(input, fmt, strict, utcMode, tzOffset);
28
- this._d = parsed.d;
29
- this._utc = parsed.utc || false;
30
- this._offset = parsed.offset; // fixed UTC offset in minutes (for utcOffset())
31
- this._locale = localeModule.getGlobalLocale();
32
- this._isValid = null; // lazy
33
- this._pf = { unusedInput: [], unusedTokens: [], overflow: -1 };
34
- }
35
-
36
- // ─── Private helpers ─────────────────────────────────────────────────────────
37
- Tempox.prototype._get = function(method) {
38
- return this._utc ? this._d['getUTC'+method]() : this._d['get'+method]();
39
- };
40
-
41
- Tempox.prototype._set = function(method, val) {
42
- if (this._utc) this._d['setUTC'+method](val);
43
- else this._d['set'+method](val);
44
- return this;
45
- };
46
-
47
- // ─── Validation ──────────────────────────────────────────────────────────────
48
- Tempox.prototype.isValid = function() {
49
- if (this._isValid === null) this._isValid = !isNaN(this._d.getTime());
50
- return this._isValid;
51
- };
52
-
53
- Tempox.prototype.invalidAt = function() { return this._pf.overflow; };
54
- Tempox.prototype.parsingFlags = function() { return this._pf; };
55
- Tempox.prototype.creationData = function() {
56
- return { input: undefined, format: undefined, locale: this._locale, isUTC: this._utc, strict: false };
57
- };
58
-
59
- // ─── Getters / Setters ───────────────────────────────────────────────────────
60
- Tempox.prototype.millisecond = Tempox.prototype.milliseconds = function(v) {
61
- if (v === undefined) return this._get('Milliseconds');
62
- this._set('Milliseconds', v); return this;
63
- };
64
- Tempox.prototype.second = Tempox.prototype.seconds = function(v) {
65
- if (v === undefined) return this._get('Seconds');
66
- this._set('Seconds', v); return this;
67
- };
68
- Tempox.prototype.minute = Tempox.prototype.minutes = function(v) {
69
- if (v === undefined) return this._get('Minutes');
70
- this._set('Minutes', v); return this;
71
- };
72
- Tempox.prototype.hour = Tempox.prototype.hours = function(v) {
73
- if (v === undefined) return this._get('Hours');
74
- this._set('Hours', v); return this;
75
- };
76
- Tempox.prototype.date = function(v) {
77
- if (v === undefined) return this._get('Date');
78
- this._set('Date', v); return this;
79
- };
80
- Tempox.prototype.day = Tempox.prototype.days = function(v) {
81
- if (v === undefined) return this._get('Day');
82
- var current = this._get('Day');
83
- this._set('Date', this._get('Date') + (v - current));
84
- return this;
85
- };
86
- Tempox.prototype.weekday = function(v) {
87
- var dow = localeModule.getLocale(this._locale).week.dow;
88
- if (v === undefined) return (this._get('Day') + 7 - dow) % 7;
89
- var current = this.weekday();
90
- this._set('Date', this._get('Date') + (v - current));
91
- return this;
92
- };
93
- Tempox.prototype.isoWeekday = function(v) {
94
- if (v === undefined) return this._get('Day') || 7;
95
- var d = this._get('Day') || 7;
96
- this._set('Date', this._get('Date') + (v - d));
97
- return this;
98
- };
99
- Tempox.prototype.month = Tempox.prototype.months = function(v) {
100
- if (v === undefined) return this._get('Month');
101
- this._set('Month', v); return this;
102
- };
103
- Tempox.prototype.year = Tempox.prototype.years = function(v) {
104
- if (v === undefined) return this._get('FullYear');
105
- this._set('FullYear', v); return this;
106
- };
107
- Tempox.prototype.quarter = Tempox.prototype.quarters = function(v) {
108
- if (v === undefined) return Math.ceil((this._get('Month')+1)/3);
109
- var targetMonth = (v-1)*3;
110
- this._set('Month', targetMonth);
111
- return this;
112
- };
113
- Tempox.prototype.dayOfYear = function(v) {
114
- var start = new Date(this._get('FullYear'), 0, 1);
115
- var doy = Math.round((this._d - start) / 86400000) + 1;
116
- if (v === undefined) return doy;
117
- this._set('Date', this._get('Date') + (v - doy));
118
- return this;
119
- };
120
- Tempox.prototype.week = Tempox.prototype.weeks = function() {
121
- return parseModule.weekOfYear(this._d);
122
- };
123
- Tempox.prototype.isoWeek = Tempox.prototype.isoWeeks = function() {
124
- return parseModule.isoWeekOfYear(this._d);
125
- };
126
- Tempox.prototype.weeksInYear = function() {
127
- return parseModule.isoWeekOfYear(new Date(this._get('FullYear'), 11, 28));
128
- };
129
- Tempox.prototype.isoWeeksInYear = Tempox.prototype.weeksInYear;
130
-
131
- // Generic get/set
132
- Tempox.prototype.get = function(unit) {
133
- var u = normalizeUnit(unit);
134
- return this[u] ? this[u]() : undefined;
135
- };
136
- Tempox.prototype.set = function(unit, val) {
137
- var u = normalizeUnit(unit);
138
- if (this[u]) this[u](val);
139
- return this;
140
- };
141
-
142
- // ─── Manipulation ─────────────────────────────────────────────────────────────
143
- Tempox.prototype.add = function(amount, unit) {
144
- return _manipulate(this, amount, unit, 1);
145
- };
146
- Tempox.prototype.subtract = function(amount, unit) {
147
- return _manipulate(this, amount, unit, -1);
148
- };
149
-
150
- function _manipulate(inst, amount, unit, sign) {
151
- if (amount instanceof Duration) {
152
- var dur = amount;
153
- inst._d.setFullYear(inst._d.getFullYear() + sign*dur._years);
154
- inst._d.setMonth(inst._d.getMonth() + sign*dur._months);
155
- inst._d.setDate(inst._d.getDate() + sign*(dur._weeks*7 + dur._days));
156
- inst._d.setTime(inst._d.getTime() + sign*(dur._hours*3600000 + dur._minutes*60000 + dur._seconds*1000 + dur._milliseconds));
157
- return inst;
158
- }
159
- if (typeof amount === 'object' && amount !== null) {
160
- var ORDER = ['year','month','week','day','hour','minute','second','millisecond'];
161
- for (var i=0;i<ORDER.length;i++) {
162
- var u = ORDER[i];
163
- var v = amount[u] !== undefined ? amount[u]
164
- : amount[u+'s'] !== undefined ? amount[u+'s']
165
- : amount[UNIT_MAP[u[0]]] !== undefined ? amount[UNIT_MAP[u[0]]]
166
- : undefined;
167
- if (v !== undefined) _manipulate(inst, v, u, sign);
168
- }
169
- return inst;
170
- }
171
- var resolved = normalizeUnit(unit);
172
- var val = amount * sign;
173
- switch(resolved) {
174
- case 'year': inst._d.setFullYear(inst._d.getFullYear()+val); break;
175
- case 'month': inst._d.setMonth(inst._d.getMonth()+val); break;
176
- case 'week': inst._d.setDate(inst._d.getDate()+val*7); break;
177
- case 'day': inst._d.setDate(inst._d.getDate()+val); break;
178
- case 'hour': inst._d.setTime(inst._d.getTime()+val*3600000); break;
179
- case 'minute': inst._d.setTime(inst._d.getTime()+val*60000); break;
180
- case 'second': inst._d.setTime(inst._d.getTime()+val*1000); break;
181
- case 'millisecond': inst._d.setTime(inst._d.getTime()+val); break;
182
- }
183
- return inst;
184
- }
185
-
186
- // ─── startOf / endOf ─────────────────────────────────────────────────────────
187
- Tempox.prototype.startOf = function(unit) {
188
- var u = normalizeUnit(unit);
189
- var d = this._d;
190
- switch(u) {
191
- case 'year': d.setMonth(0); /* fall through */
192
- case 'month': d.setDate(1); /* fall through */
193
- case 'day': d.setHours(0); /* fall through */
194
- case 'hour': d.setMinutes(0); /* fall through */
195
- case 'minute': d.setSeconds(0); /* fall through */
196
- case 'second': d.setMilliseconds(0); break;
197
- case 'week':
198
- d.setDate(d.getDate() - d.getDay() + localeModule.getLocale(this._locale).week.dow);
199
- d.setHours(0,0,0,0); break;
200
- case 'isoWeek':
201
- var dow = d.getDay()||7;
202
- d.setDate(d.getDate()-(dow-1)); d.setHours(0,0,0,0); break;
203
- case 'quarter':
204
- d.setMonth(Math.floor(d.getMonth()/3)*3,1); d.setHours(0,0,0,0); break;
205
- }
206
- return this;
207
- };
208
-
209
- Tempox.prototype.endOf = function(unit) {
210
- return this.startOf(unit).add(1, unit).subtract(1, 'millisecond');
211
- };
212
-
213
- // ─── UTC / Local / Offset ─────────────────────────────────────────────────────
214
- Tempox.prototype.utc = function() {
215
- this._utc = true; return this;
216
- };
217
- Tempox.prototype.local = function() {
218
- this._utc = false; return this;
219
- };
220
- Tempox.prototype.isUTC = function() { return this._utc; };
221
- Tempox.prototype.isLocal = function() { return !this._utc; };
222
-
223
- Tempox.prototype.utcOffset = function(offset, keepLocalTime) {
224
- if (offset === undefined) {
225
- return this._offset !== undefined ? this._offset : -this._d.getTimezoneOffset();
226
- }
227
- var minutes = typeof offset === 'string' ? parseOffsetString(offset) : offset;
228
- if (keepLocalTime) {
229
- var localDiff = (minutes - this.utcOffset()) * 60000;
230
- this._d.setTime(this._d.getTime() + localDiff);
231
- }
232
- this._offset = minutes;
233
- this._utc = (minutes === 0);
234
- return this;
235
- };
236
-
237
- Tempox.prototype.utcOffset.prototype = null;
238
-
239
- function parseOffsetString(str) {
240
- var match = str.match(/([+-])(\d{2}):?(\d{2})/);
241
- if (!match) return 0;
242
- var sign = match[1]==='+' ? 1 : -1;
243
- return sign * (parseInt(match[2],10)*60 + parseInt(match[3],10));
244
- }
245
-
246
- Tempox.prototype.parseZone = function() {
247
- return this;
248
- };
249
-
250
- Tempox.prototype.isDST = function() {
251
- var jan = new Date(this._d.getFullYear(),0,1);
252
- var jul = new Date(this._d.getFullYear(),6,1);
253
- return this._d.getTimezoneOffset() < Math.max(jan.getTimezoneOffset(), jul.getTimezoneOffset());
254
- };
255
-
256
- Tempox.prototype.zoneAbbr = function() { return this._utc ? 'UTC' : ''; };
257
- Tempox.prototype.zoneName = function() { return this._utc ? 'Coordinated Universal Time' : ''; };
258
-
259
- // ─── Format ───────────────────────────────────────────────────────────────────
260
- Tempox.prototype.format = function(fmt) {
261
- if (!this.isValid()) return localeModule.getLocale(this._locale).invalidDate || 'Invalid date';
262
- var f = fmt || localeModule.getLocale(this._locale).longDateFormat.LT;
263
- var loc = localeModule.getLocale(this._locale);
264
- // Expand long date format tokens: L, LL, LLL, LLLL, LT, LTS
265
- f = f.replace(/\bLLLL\b/g, loc.longDateFormat.LLLL)
266
- .replace(/\bLLL\b/g, loc.longDateFormat.LLL)
267
- .replace(/\bLL\b/g, loc.longDateFormat.LL)
268
- .replace(/\bL\b/g, loc.longDateFormat.L)
269
- .replace(/\bLTS\b/g, loc.longDateFormat.LTS)
270
- .replace(/\bLT\b/g, loc.longDateFormat.LT);
271
-
272
- var FT = parseModule.FORMAT_TOKENS;
273
- var d = this._d;
274
- return f.replace(parseModule.TOKEN_REGEX, function(match) {
275
- // Escaped literal: [text] -> text
276
- if (match[0] === '[' && match[match.length-1] === ']') return match.slice(1,-1);
277
- if (FT[match]) return FT[match](d, loc);
278
- return match;
279
- });
280
- };
281
-
282
- // ─── Relative time ────────────────────────────────────────────────────────────
283
- function relativeTime(diffMs, thresholds, rt) {
284
- var s = Math.abs(diffMs)/1000;
285
- var m = s/60, h = m/60, d = h/24, w = d/7, M = d/30.4375, y = d/365.25;
286
- var str;
287
- if (s < 45) str = rt.s;
288
- else if (s < 90) str = rt.m;
289
- else if (m < 45) str = rt.mm.replace('%d', Math.round(m));
290
- else if (m < 90) str = rt.h;
291
- else if (h < 22) str = rt.hh.replace('%d', Math.round(h));
292
- else if (h < 36) str = rt.d;
293
- else if (d < 26) str = rt.dd.replace('%d', Math.round(d));
294
- else if (d < 46) str = rt.M;
295
- else if (d < 320) str = rt.MM.replace('%d', Math.round(M));
296
- else if (d < 548) str = rt.y;
297
- else str = rt.yy.replace('%d', Math.round(y));
298
- return str;
299
- }
300
-
301
- Tempox.prototype.fromNow = function(withoutSuffix) { return this.from(new Tempox(), withoutSuffix); };
302
- Tempox.prototype.toNow = function(withoutSuffix) { return this.to(new Tempox(), withoutSuffix); };
303
-
304
- Tempox.prototype.from = function(other, withoutSuffix) {
305
- var loc = localeModule.getLocale(this._locale);
306
- var diff = this._d.getTime() - _toDate(other).getTime();
307
- var str = relativeTime(diff, null, loc.relativeTime);
308
- if (withoutSuffix) return str;
309
- return diff < 0 ? loc.relativeTime.past.replace('%s',str) : loc.relativeTime.future.replace('%s',str);
310
- };
311
-
312
- Tempox.prototype.to = function(other, withoutSuffix) {
313
- var loc = localeModule.getLocale(this._locale);
314
- var diff = _toDate(other).getTime() - this._d.getTime();
315
- var str = relativeTime(diff, null, loc.relativeTime);
316
- if (withoutSuffix) return str;
317
- return diff < 0 ? loc.relativeTime.past.replace('%s',str) : loc.relativeTime.future.replace('%s',str);
318
- };
319
-
320
- // ─── Calendar ────────────────────────────────────────────────────────────────
321
- Tempox.prototype.calendar = function(refTime, formats) {
322
- var loc = localeModule.getLocale(this._locale);
323
- var ref = refTime ? (refTime instanceof Tempox ? refTime : new Tempox(refTime)) : new Tempox();
324
- var cal = formats || loc.calendar;
325
-
326
- var startOfRef = ref.clone().startOf('day');
327
- var startOfThis = this.clone().startOf('day');
328
- var diffDays = Math.round((startOfThis._d - startOfRef._d) / 86400000);
329
-
330
- var key;
331
- if (diffDays === 0) key = 'sameDay';
332
- else if (diffDays === 1) key = 'nextDay';
333
- else if (diffDays > 1 && diffDays < 7) key = 'nextWeek';
334
- else if (diffDays === -1) key = 'lastDay';
335
- else if (diffDays < 0 && diffDays > -7) key = 'lastWeek';
336
- else key = 'sameElse';
337
-
338
- var fmtStr = typeof cal[key] === 'function' ? cal[key].call(this, ref) : cal[key];
339
- return this.format(fmtStr);
340
- };
341
-
342
- // ─── Diff ────────────────────────────────────────────────────────────────────
343
- Tempox.prototype.diff = function(other, unit, precise) {
344
- var b = _toDate(other);
345
- var diffMs = this._d.getTime() - b.getTime();
346
- var u = normalizeUnit(unit||'millisecond');
347
- var result;
348
- switch(u) {
349
- case 'year':
350
- result = (this.year() - new Tempox(b).year()) +
351
- (this.month() - new Tempox(b).month())/12 +
352
- (this.date() - new Tempox(b).date())/(12*30.4375);
353
- break;
354
- case 'month':
355
- result = (this.year()-new Tempox(b).year())*12 + (this.month()-new Tempox(b).month()) +
356
- (this.date()-new Tempox(b).date())/30.4375;
357
- break;
358
- case 'week': result = diffMs/604800000; break;
359
- case 'day': result = diffMs/86400000; break;
360
- case 'hour': result = diffMs/3600000; break;
361
- case 'minute': result = diffMs/60000; break;
362
- case 'second': result = diffMs/1000; break;
363
- default: result = diffMs; break;
364
- }
365
- return precise ? result : (result<0 ? Math.ceil(result) : Math.floor(result));
366
- };
367
-
368
- // ─── Query methods ───────────────────────────────────────────────────────────
369
- function _toDate(input) {
370
- if (input instanceof Tempox) return input._d;
371
- if (input instanceof Date) return input;
372
- return new Tempox(input)._d;
373
- }
374
-
375
- Tempox.prototype.isBefore = function(other, unit) {
376
- if (!unit) return this._d.getTime() < _toDate(other).getTime();
377
- return this.clone().endOf(unit)._d < new Tempox(other).startOf(unit)._d;
378
- };
379
- Tempox.prototype.isAfter = function(other, unit) {
380
- if (!unit) return this._d.getTime() > _toDate(other).getTime();
381
- return this.clone().startOf(unit)._d > new Tempox(other).endOf(unit)._d;
382
- };
383
- Tempox.prototype.isSame = function(other, unit) {
384
- if (!unit) return this._d.getTime() === _toDate(other).getTime();
385
- return this.clone().startOf(unit)._d.getTime() === new Tempox(other).startOf(unit)._d.getTime();
386
- };
387
- Tempox.prototype.isSameOrBefore = function(other, unit) {
388
- return this.isSame(other,unit) || this.isBefore(other,unit);
389
- };
390
- Tempox.prototype.isSameOrAfter = function(other, unit) {
391
- return this.isSame(other,unit) || this.isAfter(other,unit);
392
- };
393
- Tempox.prototype.isBetween = function(start, end, unit, incl) {
394
- incl = incl || '()';
395
- var afterStart = incl[0]==='[' ? this.isSameOrAfter(start,unit) : this.isAfter(start,unit);
396
- var beforeEnd = incl[1]===']' ? this.isSameOrBefore(end,unit) : this.isBefore(end,unit);
397
- return afterStart && beforeEnd;
398
- };
399
- Tempox.prototype.isDST = function() {
400
- var jan = new Date(this._d.getFullYear(),0,1);
401
- var jul = new Date(this._d.getFullYear(),6,1);
402
- return this._d.getTimezoneOffset() < Math.max(jan.getTimezoneOffset(),jul.getTimezoneOffset());
403
- };
404
- Tempox.prototype.isLeapYear = function() {
405
- var y = this.year();
406
- return (y%4===0 && y%100!==0) || y%400===0;
407
- };
408
- Tempox.prototype.isMoment = Tempox.prototype.isTempox = function() { return true; };
409
-
410
- // ─── Output helpers ──────────────────────────────────────────────────────────
411
- Tempox.prototype.valueOf = function() { return this._d.getTime(); };
412
- Tempox.prototype.unix = function() { return Math.floor(this._d.getTime()/1000); };
413
- Tempox.prototype.toDate = function() { return new Date(this._d.getTime()); };
414
- Tempox.prototype.toArray = function() {
415
- return [this.year(),this.month(),this.date(),this.hour(),this.minute(),this.second(),this.millisecond()];
416
- };
417
- Tempox.prototype.toObject = function() {
418
- return { years:this.year(), months:this.month(), date:this.date(), hours:this.hour(),
419
- minutes:this.minute(), seconds:this.second(), milliseconds:this.millisecond() };
420
- };
421
- Tempox.prototype.toISOString = function() {
422
- return this.isValid() ? this._d.toISOString() : null;
423
- };
424
- Tempox.prototype.toJSON = Tempox.prototype.toISOString;
425
- Tempox.prototype.toString = function() {
426
- return this.format('ddd MMM DD YYYY HH:mm:ss [GMT]ZZ');
427
- };
428
- Tempox.prototype.inspect = function() {
429
- return 'tempox("'+this.format()+'")';
430
- };
431
- Tempox.prototype.daysInMonth = function() {
432
- return new Date(this.year(), this.month()+1, 0).getDate();
433
- };
434
-
435
- // ─── Locale ───────────────────────────────────────────────────────────────────
436
- Tempox.prototype.locale = function(loc) {
437
- if (loc === undefined) return this._locale;
438
- this._locale = loc;
439
- return this;
440
- };
441
- Tempox.prototype.lang = Tempox.prototype.locale;
442
-
443
- Tempox.prototype.localeData = function() {
444
- return localeModule.getLocale(this._locale);
445
- };
446
-
447
- // ─── Clone ────────────────────────────────────────────────────────────────────
448
- Tempox.prototype.clone = function() {
449
- var c = new Tempox(this._d);
450
- c._utc = this._utc;
451
- c._offset = this._offset;
452
- c._locale = this._locale;
453
- return c;
454
- };
455
-
456
- module.exports = Tempox;