datevolt 1.0.1 → 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/package.json +2 -2
- package/src/duration.js +0 -188
- package/src/locale.js +0 -179
- package/src/parse.js +0 -335
- package/src/tempox.js +0 -456
package/package.json
CHANGED
|
@@ -1,11 +1,11 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "datevolt",
|
|
3
|
-
"version": "1.0.
|
|
3
|
+
"version": "1.0.2",
|
|
4
4
|
"description": "A complete, lightweight, zero-dependency date library with a moment.js-compatible API. Built for React Native CLI (Android & iOS), Node.js, and browsers.",
|
|
5
5
|
"main": "src/index.js",
|
|
6
6
|
"react-native": "src/index.js",
|
|
7
7
|
"files": [
|
|
8
|
-
"
|
|
8
|
+
"dist/",
|
|
9
9
|
"README.md",
|
|
10
10
|
"LICENSE"
|
|
11
11
|
],
|
package/src/duration.js
DELETED
|
@@ -1,188 +0,0 @@
|
|
|
1
|
-
'use strict';
|
|
2
|
-
|
|
3
|
-
var localeModule = require('./locale');
|
|
4
|
-
|
|
5
|
-
var MS = {
|
|
6
|
-
ms: 1, millisecond: 1, milliseconds: 1,
|
|
7
|
-
s: 1000, second: 1000, seconds: 1000,
|
|
8
|
-
m: 60000, minute: 60000, minutes: 60000,
|
|
9
|
-
h: 3600000, hour: 3600000, hours: 3600000,
|
|
10
|
-
d: 86400000, day: 86400000, days: 86400000,
|
|
11
|
-
w: 604800000, week: 604800000, weeks: 604800000,
|
|
12
|
-
M: 2629800000, month: 2629800000, months: 2629800000, // 30.4375 days
|
|
13
|
-
y: 31557600000, year: 31557600000, years: 31557600000 // 365.25 days
|
|
14
|
-
};
|
|
15
|
-
|
|
16
|
-
// ISO 8601 duration string: P1Y2M3DT4H5M6S
|
|
17
|
-
var ISO_DURATION_REGEX = /^(-|\+)?P(?:([-+]?[0-9,.]*)Y)?(?:([-+]?[0-9,.]*)M)?(?:([-+]?[0-9,.]*)W)?(?:([-+]?[0-9,.]*)D)?(?:T(?:([-+]?[0-9,.]*)H)?(?:([-+]?[0-9,.]*)M)?(?:([-+]?[0-9,.]*)S)?)?$/;
|
|
18
|
-
|
|
19
|
-
function Duration(input, unit) {
|
|
20
|
-
this.__isDuration = true;
|
|
21
|
-
this._locale = localeModule.getGlobalLocale();
|
|
22
|
-
|
|
23
|
-
if (typeof input === 'string') {
|
|
24
|
-
var isoMatch = input.match(ISO_DURATION_REGEX);
|
|
25
|
-
if (isoMatch) {
|
|
26
|
-
var sign = isoMatch[1] === '-' ? -1 : 1;
|
|
27
|
-
this._years = sign * parseFloat(isoMatch[2]||0);
|
|
28
|
-
this._months = sign * parseFloat(isoMatch[3]||0);
|
|
29
|
-
this._weeks = sign * parseFloat(isoMatch[4]||0);
|
|
30
|
-
this._days = sign * parseFloat(isoMatch[5]||0);
|
|
31
|
-
this._hours = sign * parseFloat(isoMatch[6]||0);
|
|
32
|
-
this._minutes = sign * parseFloat(isoMatch[7]||0);
|
|
33
|
-
this._seconds = sign * parseFloat(isoMatch[8]||0);
|
|
34
|
-
this._ms = 0;
|
|
35
|
-
this._milliseconds = 0;
|
|
36
|
-
} else {
|
|
37
|
-
this._ms = 0;
|
|
38
|
-
this._years=this._months=this._weeks=this._days=this._hours=this._minutes=this._seconds=this._milliseconds=0;
|
|
39
|
-
}
|
|
40
|
-
} else if (typeof input === 'number') {
|
|
41
|
-
var resolvedUnit = unit || 'ms';
|
|
42
|
-
var multiplier = MS[resolvedUnit] || 1;
|
|
43
|
-
this._ms = input * multiplier;
|
|
44
|
-
this._years = 0; this._months = 0; this._weeks = 0; this._days = 0;
|
|
45
|
-
this._hours = 0; this._minutes = 0; this._seconds = 0; this._milliseconds = 0;
|
|
46
|
-
this._normalizeFromMs();
|
|
47
|
-
} else if (typeof input === 'object' && input !== null) {
|
|
48
|
-
this._years = input.years || input.year || input.y || 0;
|
|
49
|
-
this._months = input.months || input.month || input.M || 0;
|
|
50
|
-
this._weeks = input.weeks || input.week || input.w || 0;
|
|
51
|
-
this._days = input.days || input.day || input.d || 0;
|
|
52
|
-
this._hours = input.hours || input.hour || input.h || 0;
|
|
53
|
-
this._minutes = input.minutes || input.minute || input.m || 0;
|
|
54
|
-
this._seconds = input.seconds || input.second || input.s || 0;
|
|
55
|
-
this._milliseconds= input.milliseconds|| input.millisecond||input.ms||0;
|
|
56
|
-
this._ms = this._toMs();
|
|
57
|
-
} else {
|
|
58
|
-
this._ms = 0;
|
|
59
|
-
this._years=this._months=this._weeks=this._days=this._hours=this._minutes=this._seconds=this._milliseconds=0;
|
|
60
|
-
}
|
|
61
|
-
}
|
|
62
|
-
|
|
63
|
-
Duration.prototype._toMs = function() {
|
|
64
|
-
return this._years * MS.y + this._months * MS.M + this._weeks * MS.w +
|
|
65
|
-
this._days * MS.d + this._hours * MS.h + this._minutes * MS.m +
|
|
66
|
-
this._seconds * MS.s + this._milliseconds;
|
|
67
|
-
};
|
|
68
|
-
|
|
69
|
-
Duration.prototype._normalizeFromMs = function() {
|
|
70
|
-
var remaining = Math.abs(this._ms);
|
|
71
|
-
var sign = this._ms < 0 ? -1 : 1;
|
|
72
|
-
this._years = sign * Math.floor(remaining / MS.y); remaining %= MS.y;
|
|
73
|
-
this._months = sign * Math.floor(remaining / MS.M); remaining %= MS.M;
|
|
74
|
-
this._days = sign * Math.floor(remaining / MS.d); remaining %= MS.d;
|
|
75
|
-
this._hours = sign * Math.floor(remaining / MS.h); remaining %= MS.h;
|
|
76
|
-
this._minutes = sign * Math.floor(remaining / MS.m); remaining %= MS.m;
|
|
77
|
-
this._seconds = sign * Math.floor(remaining / MS.s); remaining %= MS.s;
|
|
78
|
-
this._milliseconds = sign * remaining;
|
|
79
|
-
this._weeks = 0;
|
|
80
|
-
};
|
|
81
|
-
|
|
82
|
-
// ── Getters ───────────────────────────────────────────────────────────────────
|
|
83
|
-
Duration.prototype.years = function() { return this._years; };
|
|
84
|
-
Duration.prototype.months = function() { return this._months; };
|
|
85
|
-
Duration.prototype.weeks = function() { return Math.floor(this._days/7); };
|
|
86
|
-
Duration.prototype.days = function() { return this._days % 7; };
|
|
87
|
-
Duration.prototype.hours = function() { return this._hours; };
|
|
88
|
-
Duration.prototype.minutes = function() { return this._minutes; };
|
|
89
|
-
Duration.prototype.seconds = function() { return this._seconds; };
|
|
90
|
-
Duration.prototype.milliseconds = function() { return this._milliseconds; };
|
|
91
|
-
|
|
92
|
-
// ── as() ─────────────────────────────────────────────────────────────────────
|
|
93
|
-
Duration.prototype.as = function(unit) {
|
|
94
|
-
var totalMs = this._toMs();
|
|
95
|
-
var multiplier = MS[unit] || 1;
|
|
96
|
-
return totalMs / multiplier;
|
|
97
|
-
};
|
|
98
|
-
|
|
99
|
-
Duration.prototype.asMilliseconds = function() { return this._toMs(); };
|
|
100
|
-
Duration.prototype.asSeconds = function() { return this._toMs()/1000; };
|
|
101
|
-
Duration.prototype.asMinutes = function() { return this._toMs()/60000; };
|
|
102
|
-
Duration.prototype.asHours = function() { return this._toMs()/3600000; };
|
|
103
|
-
Duration.prototype.asDays = function() { return this._toMs()/86400000; };
|
|
104
|
-
Duration.prototype.asWeeks = function() { return this._toMs()/604800000; };
|
|
105
|
-
Duration.prototype.asMonths = function() { return this._toMs()/2629800000; };
|
|
106
|
-
Duration.prototype.asYears = function() { return this._toMs()/31557600000; };
|
|
107
|
-
|
|
108
|
-
Duration.prototype.get = function(unit) {
|
|
109
|
-
return this.as(unit);
|
|
110
|
-
};
|
|
111
|
-
|
|
112
|
-
// ── Add / Subtract ────────────────────────────────────────────────────────────
|
|
113
|
-
Duration.prototype.add = function(input, unit) {
|
|
114
|
-
var other = input instanceof Duration ? input : new Duration(input, unit);
|
|
115
|
-
this._years += other._years;
|
|
116
|
-
this._months += other._months;
|
|
117
|
-
this._weeks += other._weeks;
|
|
118
|
-
this._days += other._days;
|
|
119
|
-
this._hours += other._hours;
|
|
120
|
-
this._minutes += other._minutes;
|
|
121
|
-
this._seconds += other._seconds;
|
|
122
|
-
this._milliseconds+= other._milliseconds;
|
|
123
|
-
this._ms = this._toMs();
|
|
124
|
-
return this;
|
|
125
|
-
};
|
|
126
|
-
|
|
127
|
-
Duration.prototype.subtract = function(input, unit) {
|
|
128
|
-
var other = input instanceof Duration ? input : new Duration(input, unit);
|
|
129
|
-
this._years -= other._years;
|
|
130
|
-
this._months -= other._months;
|
|
131
|
-
this._weeks -= other._weeks;
|
|
132
|
-
this._days -= other._days;
|
|
133
|
-
this._hours -= other._hours;
|
|
134
|
-
this._minutes -= other._minutes;
|
|
135
|
-
this._seconds -= other._seconds;
|
|
136
|
-
this._milliseconds-= other._milliseconds;
|
|
137
|
-
this._ms = this._toMs();
|
|
138
|
-
return this;
|
|
139
|
-
};
|
|
140
|
-
|
|
141
|
-
// ── Humanize ─────────────────────────────────────────────────────────────────
|
|
142
|
-
Duration.prototype.humanize = function(withSuffix) {
|
|
143
|
-
var ms = Math.abs(this._toMs());
|
|
144
|
-
var loc = localeModule.getLocale(this._locale);
|
|
145
|
-
var rt = loc.relativeTime;
|
|
146
|
-
var str;
|
|
147
|
-
|
|
148
|
-
var secs = ms/1000, mins = ms/60000, hrs = ms/3600000,
|
|
149
|
-
days = ms/86400000, weeks = ms/604800000,
|
|
150
|
-
months = ms/2629800000, years = ms/31557600000;
|
|
151
|
-
|
|
152
|
-
if (ms < 45000) str = rt.s;
|
|
153
|
-
else if (ms < 90000) str = rt.m;
|
|
154
|
-
else if (ms < 2700000) str = rt.mm.replace('%d', Math.round(mins));
|
|
155
|
-
else if (ms < 5400000) str = rt.h;
|
|
156
|
-
else if (ms < 79200000) str = rt.hh.replace('%d', Math.round(hrs));
|
|
157
|
-
else if (ms < 129600000)str = rt.d;
|
|
158
|
-
else if (ms < 2160000000)str = rt.dd.replace('%d', Math.round(days));
|
|
159
|
-
else if (ms < 3888000000)str = rt.M;
|
|
160
|
-
else if (ms < 31536000000)str = rt.MM.replace('%d', Math.round(months));
|
|
161
|
-
else if (ms < 47260800000)str = rt.y;
|
|
162
|
-
else str = rt.yy.replace('%d', Math.round(years));
|
|
163
|
-
|
|
164
|
-
if (withSuffix) {
|
|
165
|
-
if (this._toMs() >= 0) return rt.future.replace('%s', str);
|
|
166
|
-
return rt.past.replace('%s', str);
|
|
167
|
-
}
|
|
168
|
-
return str;
|
|
169
|
-
};
|
|
170
|
-
|
|
171
|
-
Duration.prototype.locale = function(loc) {
|
|
172
|
-
this._locale = loc;
|
|
173
|
-
return this;
|
|
174
|
-
};
|
|
175
|
-
|
|
176
|
-
// ── ISO string ────────────────────────────────────────────────────────────────
|
|
177
|
-
Duration.prototype.toISOString = function() {
|
|
178
|
-
var Y = this._years, M = this._months, W = this._weeks;
|
|
179
|
-
var D = this._days, H = this._hours, m = this._minutes, S = this._seconds;
|
|
180
|
-
return 'P' + (Y?Y+'Y':'') + (M?M+'M':'') + (W?W+'W':'') + (D?D+'D':'') +
|
|
181
|
-
((H||m||S)?('T'+(H?H+'H':'')+(m?m+'M':'')+(S?S+'S':'')):'') || 'P0D';
|
|
182
|
-
};
|
|
183
|
-
|
|
184
|
-
Duration.prototype.toJSON = Duration.prototype.toISOString;
|
|
185
|
-
Duration.prototype.valueOf = function() { return this._toMs(); };
|
|
186
|
-
Duration.prototype.clone = function() { return new Duration(this._toMs()); };
|
|
187
|
-
|
|
188
|
-
module.exports = Duration;
|
package/src/locale.js
DELETED
|
@@ -1,179 +0,0 @@
|
|
|
1
|
-
'use strict';
|
|
2
|
-
|
|
3
|
-
// ─── Default English Locale ───────────────────────────────────────────────────
|
|
4
|
-
var en = {
|
|
5
|
-
name: 'en',
|
|
6
|
-
months: ['January','February','March','April','May','June','July','August','September','October','November','December'],
|
|
7
|
-
monthsShort: ['Jan','Feb','Mar','Apr','May','Jun','Jul','Aug','Sep','Oct','Nov','Dec'],
|
|
8
|
-
weekdays: ['Sunday','Monday','Tuesday','Wednesday','Thursday','Friday','Saturday'],
|
|
9
|
-
weekdaysShort: ['Sun','Mon','Tue','Wed','Thu','Fri','Sat'],
|
|
10
|
-
weekdaysMin: ['Su','Mo','Tu','We','Th','Fr','Sa'],
|
|
11
|
-
longDateFormat: {
|
|
12
|
-
LT: 'h:mm A', LTS: 'h:mm:ss A',
|
|
13
|
-
L: 'MM/DD/YYYY', LL: 'MMMM D, YYYY',
|
|
14
|
-
LLL: 'MMMM D, YYYY h:mm A', LLLL: 'dddd, MMMM D, YYYY h:mm A'
|
|
15
|
-
},
|
|
16
|
-
calendar: {
|
|
17
|
-
sameDay: '[Today at] LT',
|
|
18
|
-
nextDay: '[Tomorrow at] LT',
|
|
19
|
-
nextWeek: 'dddd [at] LT',
|
|
20
|
-
lastDay: '[Yesterday at] LT',
|
|
21
|
-
lastWeek: '[Last] dddd [at] LT',
|
|
22
|
-
sameElse: 'L'
|
|
23
|
-
},
|
|
24
|
-
relativeTime: {
|
|
25
|
-
future: 'in %s',
|
|
26
|
-
past: '%s ago',
|
|
27
|
-
s: 'a few seconds', ss: '%d seconds',
|
|
28
|
-
m: 'a minute', mm: '%d minutes',
|
|
29
|
-
h: 'an hour', hh: '%d hours',
|
|
30
|
-
d: 'a day', dd: '%d days',
|
|
31
|
-
w: 'a week', ww: '%d weeks',
|
|
32
|
-
M: 'a month', MM: '%d months',
|
|
33
|
-
y: 'a year', yy: '%d years'
|
|
34
|
-
},
|
|
35
|
-
ordinal: function(n) {
|
|
36
|
-
var s=['th','st','nd','rd'], v=n%100;
|
|
37
|
-
return n+(s[(v-20)%10]||s[v]||s[0]);
|
|
38
|
-
},
|
|
39
|
-
week: { dow: 0, doy: 6 },
|
|
40
|
-
meridiem: function(h, m, isLower) {
|
|
41
|
-
return h < 12 ? (isLower ? 'am' : 'AM') : (isLower ? 'pm' : 'PM');
|
|
42
|
-
}
|
|
43
|
-
};
|
|
44
|
-
|
|
45
|
-
// ─── Hindi Locale ─────────────────────────────────────────────────────────────
|
|
46
|
-
var hi = {
|
|
47
|
-
name: 'hi',
|
|
48
|
-
months: ['जनवरी','फ़रवरी','मार्च','अप्रैल','मई','जून','जुलाई','अगस्त','सितंबर','अक्टूबर','नवंबर','दिसंबर'],
|
|
49
|
-
monthsShort: ['जन.','फ़र.','मार्च','अप्रै.','मई','जून','जुल.','अग.','सित.','अक्टू.','नव.','दिस.'],
|
|
50
|
-
weekdays: ['रविवार','सोमवार','मंगलवार','बुधवार','गुरूवार','शुक्रवार','शनिवार'],
|
|
51
|
-
weekdaysShort: ['रवि','सोम','मंगल','बुध','गुरू','शुक्र','शनि'],
|
|
52
|
-
weekdaysMin: ['र','सो','मं','बु','गु','शु','श'],
|
|
53
|
-
longDateFormat: { LT:'A h:mm बजे', LTS:'A h:mm:ss बजे', L:'DD/MM/YYYY', LL:'D MMMM YYYY', LLL:'D MMMM YYYY, A h:mm बजे', LLLL:'dddd, D MMMM YYYY, A h:mm बजे' },
|
|
54
|
-
calendar: { sameDay:'[आज] LT', nextDay:'[कल] LT', nextWeek:'dddd[,] LT', lastDay:'[कल] LT', lastWeek:'[पिछले] dddd[,] LT', sameElse:'L' },
|
|
55
|
-
relativeTime: { future:'%s में', past:'%s पहले', s:'कुछ ही क्षण', ss:'%d सेकंड', m:'एक मिनट', mm:'%d मिनट', h:'एक घंटा', hh:'%d घंटे', d:'एक दिन', dd:'%d दिन', w:'एक सप्ताह', ww:'%d सप्ताह', M:'एक महीने', MM:'%d महीने', y:'एक वर्ष', yy:'%d वर्ष' },
|
|
56
|
-
ordinal: function(n) { return n; },
|
|
57
|
-
week: { dow: 0, doy: 6 },
|
|
58
|
-
meridiem: function(h) { if(h<4) return 'रात'; if(h<10) return 'सुबह'; if(h<17) return 'दोपहर'; if(h<20) return 'शाम'; return 'रात'; }
|
|
59
|
-
};
|
|
60
|
-
|
|
61
|
-
// ─── Spanish Locale ───────────────────────────────────────────────────────────
|
|
62
|
-
var es = {
|
|
63
|
-
name: 'es',
|
|
64
|
-
months: ['enero','febrero','marzo','abril','mayo','junio','julio','agosto','septiembre','octubre','noviembre','diciembre'],
|
|
65
|
-
monthsShort: ['ene.','feb.','mar.','abr.','may.','jun.','jul.','ago.','sep.','oct.','nov.','dic.'],
|
|
66
|
-
weekdays: ['domingo','lunes','martes','miércoles','jueves','viernes','sábado'],
|
|
67
|
-
weekdaysShort: ['dom.','lun.','mar.','mié.','jue.','vie.','sáb.'],
|
|
68
|
-
weekdaysMin: ['do','lu','ma','mi','ju','vi','sá'],
|
|
69
|
-
longDateFormat: { LT:'H:mm', LTS:'H:mm:ss', L:'DD/MM/YYYY', LL:'D [de] MMMM [de] YYYY', LLL:'D [de] MMMM [de] YYYY H:mm', LLLL:'dddd, D [de] MMMM [de] YYYY H:mm' },
|
|
70
|
-
calendar: { sameDay:'[hoy a la' , nextDay:'[mañana a la', nextWeek:'dddd [a la', lastDay:'[ayer a la', lastWeek:'[el] dddd [pasado a la', sameElse:'L' },
|
|
71
|
-
relativeTime: { future:'en %s', past:'hace %s', s:'unos segundos', ss:'%d segundos', m:'un minuto', mm:'%d minutos', h:'una hora', hh:'%d horas', d:'un día', dd:'%d días', w:'una semana', ww:'%d semanas', M:'un mes', MM:'%d meses', y:'un año', yy:'%d años' },
|
|
72
|
-
ordinal: function(n) { return n+'º'; },
|
|
73
|
-
week: { dow: 1, doy: 4 },
|
|
74
|
-
meridiem: function(h,m,isLower) { return h<12?(isLower?'am':'AM'):(isLower?'pm':'PM'); }
|
|
75
|
-
};
|
|
76
|
-
|
|
77
|
-
// ─── French ──────────────────────────────────────────────────────────────────
|
|
78
|
-
var fr = {
|
|
79
|
-
name: 'fr',
|
|
80
|
-
months: ['janvier','février','mars','avril','mai','juin','juillet','août','septembre','octobre','novembre','décembre'],
|
|
81
|
-
monthsShort: ['janv.','févr.','mars','avr.','mai','juin','juil.','août','sept.','oct.','nov.','déc.'],
|
|
82
|
-
weekdays: ['dimanche','lundi','mardi','mercredi','jeudi','vendredi','samedi'],
|
|
83
|
-
weekdaysShort: ['dim.','lun.','mar.','mer.','jeu.','ven.','sam.'],
|
|
84
|
-
weekdaysMin: ['di','lu','ma','me','je','ve','sa'],
|
|
85
|
-
longDateFormat: { LT:'HH:mm', LTS:'HH:mm:ss', L:'DD/MM/YYYY', LL:'D MMMM YYYY', LLL:'D MMMM YYYY HH:mm', LLLL:'dddd D MMMM YYYY HH:mm' },
|
|
86
|
-
calendar: { sameDay:"[Aujourd'hui à] LT", nextDay:'[Demain à] LT', nextWeek:'dddd [à] LT', lastDay:'[Hier à] LT', lastWeek:'dddd [dernier à] LT', sameElse:'L' },
|
|
87
|
-
relativeTime: { future:'dans %s', past:'il y a %s', s:'quelques secondes', ss:'%d secondes', m:'une minute', mm:'%d minutes', h:'une heure', hh:'%d heures', d:'un jour', dd:'%d jours', w:'une semaine', ww:'%d semaines', M:'un mois', MM:'%d mois', y:'un an', yy:'%d ans' },
|
|
88
|
-
ordinal: function(n) { return n+(n===1?'er':'e'); },
|
|
89
|
-
week: { dow: 1, doy: 4 },
|
|
90
|
-
meridiem: function(h,m,isLower) { return h<12?(isLower?'am':'AM'):(isLower?'pm':'PM'); }
|
|
91
|
-
};
|
|
92
|
-
|
|
93
|
-
// ─── German ───────────────────────────────────────────────────────────────────
|
|
94
|
-
var de = {
|
|
95
|
-
name: 'de',
|
|
96
|
-
months: ['Januar','Februar','März','April','Mai','Juni','Juli','August','September','Oktober','November','Dezember'],
|
|
97
|
-
monthsShort: ['Jan.','Feb.','März','Apr.','Mai','Juni','Juli','Aug.','Sep.','Okt.','Nov.','Dez.'],
|
|
98
|
-
weekdays: ['Sonntag','Montag','Dienstag','Mittwoch','Donnerstag','Freitag','Samstag'],
|
|
99
|
-
weekdaysShort: ['So.','Mo.','Di.','Mi.','Do.','Fr.','Sa.'],
|
|
100
|
-
weekdaysMin: ['So','Mo','Di','Mi','Do','Fr','Sa'],
|
|
101
|
-
longDateFormat: { LT:'HH:mm', LTS:'HH:mm:ss', L:'DD.MM.YYYY', LL:'D. MMMM YYYY', LLL:'D. MMMM YYYY HH:mm', LLLL:'dddd, D. MMMM YYYY HH:mm' },
|
|
102
|
-
calendar: { sameDay:'[heute um] LT [Uhr]', nextDay:'[morgen um] LT [Uhr]', nextWeek:'dddd [um] LT [Uhr]', lastDay:'[gestern um] LT [Uhr]', lastWeek:'[letzten] dddd [um] LT [Uhr]', sameElse:'L' },
|
|
103
|
-
relativeTime: { future:'in %s', past:'vor %s', s:'ein paar Sekunden', ss:'%d Sekunden', m:'einer Minute', mm:'%d Minuten', h:'einer Stunde', hh:'%d Stunden', d:'einem Tag', dd:'%d Tagen', w:'einer Woche', ww:'%d Wochen', M:'einem Monat', MM:'%d Monaten', y:'einem Jahr', yy:'%d Jahren' },
|
|
104
|
-
ordinal: function(n) { return n+'.'; },
|
|
105
|
-
week: { dow: 1, doy: 4 },
|
|
106
|
-
meridiem: function(h,m,isLower) { return h<12?(isLower?'am':'AM'):(isLower?'pm':'PM'); }
|
|
107
|
-
};
|
|
108
|
-
|
|
109
|
-
// ─── Arabic ───────────────────────────────────────────────────────────────────
|
|
110
|
-
var ar = {
|
|
111
|
-
name: 'ar',
|
|
112
|
-
months: ['يناير','فبراير','مارس','أبريل','مايو','يونيو','يوليو','أغسطس','سبتمبر','أكتوبر','نوفمبر','ديسمبر'],
|
|
113
|
-
monthsShort: ['يناير','فبراير','مارس','أبريل','مايو','يونيو','يوليو','أغسطس','سبتمبر','أكتوبر','نوفمبر','ديسمبر'],
|
|
114
|
-
weekdays: ['الأحد','الاثنين','الثلاثاء','الأربعاء','الخميس','الجمعة','السبت'],
|
|
115
|
-
weekdaysShort: ['أحد','اثنين','ثلاثاء','أربعاء','خميس','جمعة','سبت'],
|
|
116
|
-
weekdaysMin: ['ح','ن','ث','ر','خ','ج','س'],
|
|
117
|
-
longDateFormat: { LT:'HH:mm', LTS:'HH:mm:ss', L:'DD/MM/YYYY', LL:'D MMMM YYYY', LLL:'D MMMM YYYY HH:mm', LLLL:'dddd D MMMM YYYY HH:mm' },
|
|
118
|
-
calendar: { sameDay:'[اليوم عند الساعة] LT', nextDay:'[غدًا عند الساعة] LT', nextWeek:'dddd [عند الساعة] LT', lastDay:'[أمس عند الساعة] LT', lastWeek:'dddd [عند الساعة] LT', sameElse:'L' },
|
|
119
|
-
relativeTime: { future:'في %s', past:'منذ %s', s:'ثوان', ss:'%d ثانية', m:'دقيقة', mm:'%d دقائق', h:'ساعة', hh:'%d ساعات', d:'يوم', dd:'%d أيام', w:'أسبوع', ww:'%d أسابيع', M:'شهر', MM:'%d أشهر', y:'سنة', yy:'%d سنوات' },
|
|
120
|
-
ordinal: function(n) { return n; },
|
|
121
|
-
week: { dow: 6, doy: 12 },
|
|
122
|
-
meridiem: function(h,m,isLower) { return h<12?(isLower?'ص':'ص'):(isLower?'م':'م'); }
|
|
123
|
-
};
|
|
124
|
-
|
|
125
|
-
// ─── Japanese ─────────────────────────────────────────────────────────────────
|
|
126
|
-
var ja = {
|
|
127
|
-
name: 'ja',
|
|
128
|
-
months: ['1月','2月','3月','4月','5月','6月','7月','8月','9月','10月','11月','12月'],
|
|
129
|
-
monthsShort: ['1月','2月','3月','4月','5月','6月','7月','8月','9月','10月','11月','12月'],
|
|
130
|
-
weekdays: ['日曜日','月曜日','火曜日','水曜日','木曜日','金曜日','土曜日'],
|
|
131
|
-
weekdaysShort: ['日','月','火','水','木','金','土'],
|
|
132
|
-
weekdaysMin: ['日','月','火','水','木','金','土'],
|
|
133
|
-
longDateFormat: { LT:'HH:mm', LTS:'HH:mm:ss', L:'YYYY/MM/DD', LL:'YYYY年M月D日', LLL:'YYYY年M月D日 HH:mm', LLLL:'YYYY年M月D日 dddd HH:mm' },
|
|
134
|
-
calendar: { sameDay:'[今日] LT', nextDay:'[明日] LT', nextWeek:'[来週]dddd LT', lastDay:'[昨日] LT', lastWeek:'[前週]dddd LT', sameElse:'L' },
|
|
135
|
-
relativeTime: { future:'%s後', past:'%s前', s:'数秒', ss:'%d秒', m:'1分', mm:'%d分', h:'1時間', hh:'%d時間', d:'1日', dd:'%d日', w:'1週間', ww:'%d週間', M:'1ヶ月', MM:'%dヶ月', y:'1年', yy:'%d年' },
|
|
136
|
-
ordinal: function(n) { return n; },
|
|
137
|
-
week: { dow: 0, doy: 6 },
|
|
138
|
-
meridiem: function(h) { return h<12?'午前':'午後'; }
|
|
139
|
-
};
|
|
140
|
-
|
|
141
|
-
// ─── Locale Registry ─────────────────────────────────────────────────────────
|
|
142
|
-
var locales = { en: en, hi: hi, es: es, fr: fr, de: de, ar: ar, ja: ja };
|
|
143
|
-
var globalLocale = 'en';
|
|
144
|
-
|
|
145
|
-
function getLocale(name) {
|
|
146
|
-
return locales[name] || locales['en'];
|
|
147
|
-
}
|
|
148
|
-
|
|
149
|
-
function setGlobalLocale(name) {
|
|
150
|
-
if (locales[name]) globalLocale = name;
|
|
151
|
-
return globalLocale;
|
|
152
|
-
}
|
|
153
|
-
|
|
154
|
-
function getGlobalLocale() {
|
|
155
|
-
return globalLocale;
|
|
156
|
-
}
|
|
157
|
-
|
|
158
|
-
function getGlobalLocaleData() {
|
|
159
|
-
return locales[globalLocale];
|
|
160
|
-
}
|
|
161
|
-
|
|
162
|
-
function defineLocale(name, config) {
|
|
163
|
-
locales[name] = config;
|
|
164
|
-
config.name = name;
|
|
165
|
-
return config;
|
|
166
|
-
}
|
|
167
|
-
|
|
168
|
-
function listLocales() {
|
|
169
|
-
return Object.keys(locales);
|
|
170
|
-
}
|
|
171
|
-
|
|
172
|
-
module.exports = {
|
|
173
|
-
getLocale: getLocale,
|
|
174
|
-
setGlobalLocale: setGlobalLocale,
|
|
175
|
-
getGlobalLocale: getGlobalLocale,
|
|
176
|
-
getGlobalLocaleData: getGlobalLocaleData,
|
|
177
|
-
defineLocale: defineLocale,
|
|
178
|
-
listLocales: listLocales
|
|
179
|
-
};
|
package/src/parse.js
DELETED
|
@@ -1,335 +0,0 @@
|
|
|
1
|
-
'use strict';
|
|
2
|
-
|
|
3
|
-
// ─── Constants ───────────────────────────────────────────────────────────────
|
|
4
|
-
var ISO_REGEX = /^(\d{4})(-(\d{2})(-(\d{2})([T\s](\d{2}):(\d{2})(:(\d{2})(\.(\d+))?)?(Z|([+-])(\d{2}):?(\d{2}))?)?)?)?$/;
|
|
5
|
-
var ASP_DATE_REGEX = /\/Date\((-?\d+)(([+-])(\d{2})(\d{2}))?\)\//;
|
|
6
|
-
|
|
7
|
-
var TOKEN_REGEX = /(\[[^\[]*\])|(\\)?(Mo|MM?M?M?|Do|DDDo|DD?D?D?|ddd?d?|do?|w[o|w]?|W[o|W]?|Qo?|YYYY|YY|gg(ggg?)?|GG(GGG?)?|e|E|a|A|hh?|HH?|kk?|jj?|mm?|ss?|S{1,9}|x|X|zz?|ZZ?|.)/g;
|
|
8
|
-
|
|
9
|
-
var FORMAT_TOKENS = {
|
|
10
|
-
M: function(d){ return d.getMonth()+1; },
|
|
11
|
-
MM: function(d){ return pad(d.getMonth()+1); },
|
|
12
|
-
MMM: function(d,loc){ return loc.monthsShort[d.getMonth()]; },
|
|
13
|
-
MMMM: function(d,loc){ return loc.months[d.getMonth()]; },
|
|
14
|
-
D: function(d){ return d.getDate(); },
|
|
15
|
-
DD: function(d){ return pad(d.getDate()); },
|
|
16
|
-
Do: function(d,loc){ return loc.ordinal(d.getDate()); },
|
|
17
|
-
DDD: function(d){ return dayOfYear(d); },
|
|
18
|
-
DDDD: function(d){ return pad(dayOfYear(d),3); },
|
|
19
|
-
d: function(d){ return d.getDay(); },
|
|
20
|
-
dd: function(d,loc){ return loc.weekdaysMin[d.getDay()]; },
|
|
21
|
-
ddd: function(d,loc){ return loc.weekdaysShort[d.getDay()]; },
|
|
22
|
-
dddd: function(d,loc){ return loc.weekdays[d.getDay()]; },
|
|
23
|
-
e: function(d){ return d.getDay(); },
|
|
24
|
-
E: function(d){ return d.getDay()||7; },
|
|
25
|
-
w: function(d){ return weekOfYear(d); },
|
|
26
|
-
ww: function(d){ return pad(weekOfYear(d)); },
|
|
27
|
-
W: function(d){ return isoWeekOfYear(d); },
|
|
28
|
-
WW: function(d){ return pad(isoWeekOfYear(d)); },
|
|
29
|
-
YY: function(d){ return String(d.getFullYear()).slice(-2); },
|
|
30
|
-
YYYY: function(d){ return pad(d.getFullYear(),4); },
|
|
31
|
-
gg: function(d){ return String(weekYear(d)).slice(-2); },
|
|
32
|
-
gggg: function(d){ return weekYear(d); },
|
|
33
|
-
GG: function(d){ return String(isoWeekYear(d)).slice(-2); },
|
|
34
|
-
GGGG: function(d){ return isoWeekYear(d); },
|
|
35
|
-
Q: function(d){ return Math.ceil((d.getMonth()+1)/3); },
|
|
36
|
-
Qo: function(d,loc){ return loc.ordinal(Math.ceil((d.getMonth()+1)/3)); },
|
|
37
|
-
A: function(d){ return d.getHours()<12?'AM':'PM'; },
|
|
38
|
-
a: function(d){ return d.getHours()<12?'am':'pm'; },
|
|
39
|
-
H: function(d){ return d.getHours(); },
|
|
40
|
-
HH: function(d){ return pad(d.getHours()); },
|
|
41
|
-
h: function(d){ return d.getHours()%12||12; },
|
|
42
|
-
hh: function(d){ return pad(d.getHours()%12||12); },
|
|
43
|
-
k: function(d){ return d.getHours()||24; },
|
|
44
|
-
kk: function(d){ return pad(d.getHours()||24); },
|
|
45
|
-
m: function(d){ return d.getMinutes(); },
|
|
46
|
-
mm: function(d){ return pad(d.getMinutes()); },
|
|
47
|
-
s: function(d){ return d.getSeconds(); },
|
|
48
|
-
ss: function(d){ return pad(d.getSeconds()); },
|
|
49
|
-
S: function(d){ return Math.floor(d.getMilliseconds()/100); },
|
|
50
|
-
SS: function(d){ return pad(Math.floor(d.getMilliseconds()/10)); },
|
|
51
|
-
SSS: function(d){ return pad(d.getMilliseconds(),3); },
|
|
52
|
-
SSSS: function(d){ return pad(d.getMilliseconds(),3)+'0'; },
|
|
53
|
-
SSSSS:function(d){ return pad(d.getMilliseconds(),3)+'00'; },
|
|
54
|
-
Z: function(d){ return offsetStr(d,':'); },
|
|
55
|
-
ZZ: function(d){ return offsetStr(d,''); },
|
|
56
|
-
X: function(d){ return Math.floor(d.getTime()/1000); },
|
|
57
|
-
x: function(d){ return d.getTime(); },
|
|
58
|
-
z: function(d){ return 'UTC'+offsetStr(d,':'); },
|
|
59
|
-
zz: function(d){ return 'UTC'+offsetStr(d,':'); }
|
|
60
|
-
};
|
|
61
|
-
|
|
62
|
-
// ─── Helpers ─────────────────────────────────────────────────────────────────
|
|
63
|
-
function pad(n, len) {
|
|
64
|
-
var s = String(Math.abs(n));
|
|
65
|
-
while (s.length < (len||2)) s = '0'+s;
|
|
66
|
-
return (n<0?'-':'')+s;
|
|
67
|
-
}
|
|
68
|
-
|
|
69
|
-
function offsetStr(d, sep) {
|
|
70
|
-
var off = -d.getTimezoneOffset();
|
|
71
|
-
var sign = off>=0?'+':'-';
|
|
72
|
-
var abs = Math.abs(off);
|
|
73
|
-
return sign + pad(Math.floor(abs/60)) + sep + pad(abs%60);
|
|
74
|
-
}
|
|
75
|
-
|
|
76
|
-
function dayOfYear(d) {
|
|
77
|
-
var start = new Date(d.getFullYear(),0,0);
|
|
78
|
-
var diff = d - start;
|
|
79
|
-
return Math.floor(diff/86400000);
|
|
80
|
-
}
|
|
81
|
-
|
|
82
|
-
function weekOfYear(d) {
|
|
83
|
-
var jan1 = new Date(d.getFullYear(),0,1);
|
|
84
|
-
var dayNum = d.getDay()||7;
|
|
85
|
-
return Math.ceil((((d-jan1)/86400000)+jan1.getDay()+1)/7);
|
|
86
|
-
}
|
|
87
|
-
|
|
88
|
-
function isoWeekOfYear(d) {
|
|
89
|
-
var date = new Date(Date.UTC(d.getFullYear(), d.getMonth(), d.getDate()));
|
|
90
|
-
var dayNum = date.getUTCDay()||7;
|
|
91
|
-
date.setUTCDate(date.getUTCDate()+4-dayNum);
|
|
92
|
-
var yearStart = new Date(Date.UTC(date.getUTCFullYear(),0,1));
|
|
93
|
-
return Math.ceil((((date-yearStart)/86400000)+1)/7);
|
|
94
|
-
}
|
|
95
|
-
|
|
96
|
-
function weekYear(d) {
|
|
97
|
-
var w = weekOfYear(d);
|
|
98
|
-
var year = d.getFullYear();
|
|
99
|
-
if (w===52 && d.getMonth()===0) return year-1;
|
|
100
|
-
if (w===1 && d.getMonth()===11) return year+1;
|
|
101
|
-
return year;
|
|
102
|
-
}
|
|
103
|
-
|
|
104
|
-
function isoWeekYear(d) {
|
|
105
|
-
var w = isoWeekOfYear(d);
|
|
106
|
-
var year = d.getFullYear();
|
|
107
|
-
if (w===52 && d.getMonth()===0) return year-1;
|
|
108
|
-
if (w===1 && d.getMonth()===11) return year+1;
|
|
109
|
-
return year;
|
|
110
|
-
}
|
|
111
|
-
|
|
112
|
-
// ─── Parse Input ─────────────────────────────────────────────────────────────
|
|
113
|
-
function parseInput(input, fmt, strict, utcMode, tzOffset) {
|
|
114
|
-
if (input === undefined || input === null) return { d: new Date(), utc: !!utcMode, offset: tzOffset };
|
|
115
|
-
|
|
116
|
-
if (input && input.__isdatevolt) return { d: new Date(input._d.getTime()), utc: input._utc, offset: input._offset };
|
|
117
|
-
|
|
118
|
-
if (input instanceof Date) return { d: new Date(input.getTime()), utc: !!utcMode, offset: tzOffset };
|
|
119
|
-
|
|
120
|
-
if (typeof input === 'number') return { d: new Date(input), utc: !!utcMode, offset: tzOffset };
|
|
121
|
-
|
|
122
|
-
if (Array.isArray(input)) {
|
|
123
|
-
var a = input;
|
|
124
|
-
var d = utcMode
|
|
125
|
-
? new Date(Date.UTC(a[0]||0,a[1]||0,a[2]!==undefined?a[2]:1,a[3]||0,a[4]||0,a[5]||0,a[6]||0))
|
|
126
|
-
: new Date(a[0]||0,a[1]||0,a[2]!==undefined?a[2]:1,a[3]||0,a[4]||0,a[5]||0,a[6]||0);
|
|
127
|
-
return { d: d, utc: !!utcMode, offset: tzOffset };
|
|
128
|
-
}
|
|
129
|
-
|
|
130
|
-
if (typeof input === 'object' && !(input instanceof Date)) {
|
|
131
|
-
var obj = input;
|
|
132
|
-
var now = new Date();
|
|
133
|
-
var y = obj.year||obj.years||obj.y||now.getFullYear();
|
|
134
|
-
var mo = obj.month!==undefined?obj.month:(obj.months!==undefined?obj.months:(obj.M!==undefined?obj.M:now.getMonth()));
|
|
135
|
-
var day = obj.day||obj.days||obj.date||obj.d||now.getDate();
|
|
136
|
-
var h = obj.hour||obj.hours||obj.h||0;
|
|
137
|
-
var mi = obj.minute||obj.minutes||obj.m||0;
|
|
138
|
-
var s = obj.second||obj.seconds||obj.s||0;
|
|
139
|
-
var ms = obj.millisecond||obj.milliseconds||obj.ms||0;
|
|
140
|
-
return { d: new Date(y, mo, day, h, mi, s, ms), utc: false, offset: tzOffset };
|
|
141
|
-
}
|
|
142
|
-
|
|
143
|
-
if (typeof input === 'string') {
|
|
144
|
-
var aspMatch = input.match(ASP_DATE_REGEX);
|
|
145
|
-
if (aspMatch) return { d: new Date(parseInt(aspMatch[1],10)), utc: !!utcMode, offset: tzOffset };
|
|
146
|
-
|
|
147
|
-
if (fmt) {
|
|
148
|
-
var fmtResult = parseWithFormat(input, fmt, strict, utcMode);
|
|
149
|
-
return fmtResult;
|
|
150
|
-
}
|
|
151
|
-
|
|
152
|
-
return parseISO(input, utcMode);
|
|
153
|
-
}
|
|
154
|
-
|
|
155
|
-
return { d: new Date(NaN), utc: !!utcMode, offset: tzOffset };
|
|
156
|
-
}
|
|
157
|
-
|
|
158
|
-
function parseISO(str, utcMode) {
|
|
159
|
-
var m = str.trim().match(ISO_REGEX);
|
|
160
|
-
if (!m) return { d: new Date(str), utc: false, offset: undefined };
|
|
161
|
-
|
|
162
|
-
var year = parseInt(m[1],10);
|
|
163
|
-
var month = m[3] ? parseInt(m[3],10)-1 : 0;
|
|
164
|
-
var day = m[5] ? parseInt(m[5],10) : 1;
|
|
165
|
-
var hour = m[7] ? parseInt(m[7],10) : 0;
|
|
166
|
-
var min = m[8] ? parseInt(m[8],10) : 0;
|
|
167
|
-
var sec = m[10]? parseInt(m[10],10) : 0;
|
|
168
|
-
var msRaw = m[12]|| '0';
|
|
169
|
-
while (msRaw.length < 3) msRaw += '0';
|
|
170
|
-
var ms = parseInt(msRaw.slice(0,3),10);
|
|
171
|
-
|
|
172
|
-
var tzStr = m[13];
|
|
173
|
-
var hasTime = !!m[6];
|
|
174
|
-
|
|
175
|
-
if (tzStr) {
|
|
176
|
-
var utcMs = Date.UTC(year,month,day,hour,min,sec,ms);
|
|
177
|
-
if (tzStr !== 'Z') {
|
|
178
|
-
var tzSign = m[14]==='-' ? 1 : -1;
|
|
179
|
-
var tzH = parseInt(m[15],10);
|
|
180
|
-
var tzM = parseInt(m[16],10);
|
|
181
|
-
utcMs += tzSign*(tzH*60+tzM)*60000;
|
|
182
|
-
}
|
|
183
|
-
return { d: new Date(utcMs), utc: true, offset: undefined };
|
|
184
|
-
}
|
|
185
|
-
|
|
186
|
-
if (!hasTime && !utcMode) {
|
|
187
|
-
// date-only -> local midnight (moment behaviour)
|
|
188
|
-
return { d: new Date(year,month,day,0,0,0,0), utc: false, offset: undefined };
|
|
189
|
-
}
|
|
190
|
-
|
|
191
|
-
if (utcMode) {
|
|
192
|
-
return { d: new Date(Date.UTC(year,month,day,hour,min,sec,ms)), utc: true, offset: undefined };
|
|
193
|
-
}
|
|
194
|
-
|
|
195
|
-
return { d: new Date(year,month,day,hour,min,sec,ms), utc: false, offset: undefined };
|
|
196
|
-
}
|
|
197
|
-
|
|
198
|
-
// ─── Format-guided parsing ────────────────────────────────────────────────────
|
|
199
|
-
var PARSE_TOKENS = {
|
|
200
|
-
YYYY: { rx: '(\\d{4})', set: function(p,v){ p.year = parseInt(v,10); } },
|
|
201
|
-
YY: { rx: '(\\d{2})', set: function(p,v){ var n=parseInt(v,10); p.year = n<69?2000+n:1900+n; } },
|
|
202
|
-
M: { rx: '(\\d{1,2})', set: function(p,v){ p.month = parseInt(v,10)-1; } },
|
|
203
|
-
MM: { rx: '(\\d{2})', set: function(p,v){ p.month = parseInt(v,10)-1; } },
|
|
204
|
-
D: { rx: '(\\d{1,2})', set: function(p,v){ p.day = parseInt(v,10); } },
|
|
205
|
-
DD: { rx: '(\\d{2})', set: function(p,v){ p.day = parseInt(v,10); } },
|
|
206
|
-
H: { rx: '(\\d{1,2})', set: function(p,v){ p.hour = parseInt(v,10); } },
|
|
207
|
-
HH: { rx: '(\\d{2})', set: function(p,v){ p.hour = parseInt(v,10); } },
|
|
208
|
-
h: { rx: '(\\d{1,2})', set: function(p,v){ p.hour12 = parseInt(v,10); } },
|
|
209
|
-
hh: { rx: '(\\d{2})', set: function(p,v){ p.hour12 = parseInt(v,10); } },
|
|
210
|
-
m: { rx: '(\\d{1,2})', set: function(p,v){ p.minute = parseInt(v,10); } },
|
|
211
|
-
mm: { rx: '(\\d{2})', set: function(p,v){ p.minute = parseInt(v,10); } },
|
|
212
|
-
s: { rx: '(\\d{1,2})', set: function(p,v){ p.second = parseInt(v,10); } },
|
|
213
|
-
ss: { rx: '(\\d{2})', set: function(p,v){ p.second = parseInt(v,10); } },
|
|
214
|
-
S: { rx: '(\\d)', set: function(p,v){ p.ms = parseInt(v,10)*100; } },
|
|
215
|
-
SS: { rx: '(\\d{2})', set: function(p,v){ p.ms = parseInt(v,10)*10; } },
|
|
216
|
-
SSS: { rx: '(\\d{3})', set: function(p,v){ p.ms = parseInt(v,10); } },
|
|
217
|
-
A: { rx: '(AM|PM)', set: function(p,v){ p.ampm = v; } },
|
|
218
|
-
a: { rx: '(am|pm)', set: function(p,v){ p.ampm = v.toUpperCase(); } },
|
|
219
|
-
X: { rx: '(-?\\d+)', set: function(p,v){ p.unix = parseInt(v,10); } },
|
|
220
|
-
x: { rx: '(-?\\d+)', set: function(p,v){ p.unixMs = parseInt(v,10); } },
|
|
221
|
-
Z: { rx: '([+-]\\d{2}:\\d{2}|Z)', set: function(p,v){ p.tz = v; } },
|
|
222
|
-
ZZ: { rx: '([+-]\\d{4}|Z)', set: function(p,v){ p.tz = v; } }
|
|
223
|
-
};
|
|
224
|
-
|
|
225
|
-
function parseWithFormat(str, fmts, strict, utcMode) {
|
|
226
|
-
var formatList = Array.isArray(fmts) ? fmts : [fmts];
|
|
227
|
-
for (var fi = 0; fi < formatList.length; fi++) {
|
|
228
|
-
var result = tryParseFormat(str, formatList[fi], strict, utcMode);
|
|
229
|
-
if (result.valid) return { d: result.d, utc: !!utcMode, offset: undefined };
|
|
230
|
-
}
|
|
231
|
-
return { d: new Date(NaN), utc: !!utcMode, offset: undefined };
|
|
232
|
-
}
|
|
233
|
-
|
|
234
|
-
function tryParseFormat(str, fmt, strict, utcMode) {
|
|
235
|
-
var tokens = [];
|
|
236
|
-
var setters = [];
|
|
237
|
-
|
|
238
|
-
// Step 1: replace tokens with placeholders, escape literal separators
|
|
239
|
-
var parts = [];
|
|
240
|
-
var remaining = fmt;
|
|
241
|
-
var tokenRx = /YYYY|YY|MM|M|DD|D|HH|H|hh|h|mm|m|ss|s|SSS|SS|S|A|a|ZZ|Z|X|x|\[([^\]]*)\]/g;
|
|
242
|
-
var lastIdx = 0;
|
|
243
|
-
var m2;
|
|
244
|
-
var regexParts = [];
|
|
245
|
-
|
|
246
|
-
tokenRx.lastIndex = 0;
|
|
247
|
-
while ((m2 = tokenRx.exec(fmt)) !== null) {
|
|
248
|
-
// Escape literal text between tokens
|
|
249
|
-
var literal = fmt.slice(lastIdx, m2.index);
|
|
250
|
-
if (literal) regexParts.push(literal.replace(/[-\/\\^$*+?.()|[\]{}]/g, '\\$&'));
|
|
251
|
-
|
|
252
|
-
var tok = m2[0];
|
|
253
|
-
if (tok[0] === '[') {
|
|
254
|
-
// literal inside brackets
|
|
255
|
-
regexParts.push((m2[1]||'').replace(/[-\/\\^$*+?.()|[\]{}]/g, '\\$&'));
|
|
256
|
-
} else {
|
|
257
|
-
var def = PARSE_TOKENS[tok];
|
|
258
|
-
if (def) {
|
|
259
|
-
tokens.push(tok);
|
|
260
|
-
setters.push(def.set);
|
|
261
|
-
regexParts.push(def.rx);
|
|
262
|
-
} else {
|
|
263
|
-
regexParts.push(tok.replace(/[-\/\\^$*+?.()|[\]{}]/g, '\\$&'));
|
|
264
|
-
}
|
|
265
|
-
}
|
|
266
|
-
lastIdx = tokenRx.lastIndex;
|
|
267
|
-
}
|
|
268
|
-
// Remaining literal after last token
|
|
269
|
-
var tail = fmt.slice(lastIdx);
|
|
270
|
-
if (tail) regexParts.push(tail.replace(/[-\/\\^$*+?.()|[\]{}]/g, '\\$&'));
|
|
271
|
-
|
|
272
|
-
var pattern = regexParts.join('');
|
|
273
|
-
var regex = new RegExp(strict ? '^'+pattern+'$' : pattern, 'i');
|
|
274
|
-
var match = str.match(regex);
|
|
275
|
-
if (!match) return { valid: false };
|
|
276
|
-
|
|
277
|
-
var parts = { year: undefined, month: undefined, day: undefined, hour: undefined,
|
|
278
|
-
hour12: undefined, minute: undefined, second: undefined, ms: undefined,
|
|
279
|
-
ampm: undefined, tz: undefined, unix: undefined, unixMs: undefined };
|
|
280
|
-
|
|
281
|
-
for (var i = 0; i < setters.length; i++) {
|
|
282
|
-
setters[i](parts, match[i+1]);
|
|
283
|
-
}
|
|
284
|
-
|
|
285
|
-
if (parts.unixMs !== undefined) return { valid: true, d: new Date(parts.unixMs) };
|
|
286
|
-
if (parts.unix !== undefined) return { valid: true, d: new Date(parts.unix*1000) };
|
|
287
|
-
|
|
288
|
-
var now = new Date();
|
|
289
|
-
var year = parts.year !== undefined ? parts.year : now.getFullYear();
|
|
290
|
-
var month = parts.month !== undefined ? parts.month : 0;
|
|
291
|
-
var day = parts.day !== undefined ? parts.day : 1;
|
|
292
|
-
var hour = parts.hour !== undefined ? parts.hour : (parts.hour12 !== undefined ? parts.hour12 : 0);
|
|
293
|
-
var minute = parts.minute !== undefined ? parts.minute : 0;
|
|
294
|
-
var second = parts.second !== undefined ? parts.second : 0;
|
|
295
|
-
var ms = parts.ms !== undefined ? parts.ms : 0;
|
|
296
|
-
|
|
297
|
-
if (parts.ampm && parts.hour12 !== undefined) {
|
|
298
|
-
if (parts.ampm === 'PM' && hour < 12) hour += 12;
|
|
299
|
-
if (parts.ampm === 'AM' && hour === 12) hour = 0;
|
|
300
|
-
}
|
|
301
|
-
|
|
302
|
-
var d;
|
|
303
|
-
if (parts.tz) {
|
|
304
|
-
var tzStr = parts.tz;
|
|
305
|
-
if (tzStr === 'Z') {
|
|
306
|
-
d = new Date(Date.UTC(year,month,day,hour,minute,second,ms));
|
|
307
|
-
} else {
|
|
308
|
-
var clean = tzStr.replace(':','');
|
|
309
|
-
var sign = clean[0]==='-' ? 1 : -1;
|
|
310
|
-
var tzH = parseInt(clean.slice(1,3),10);
|
|
311
|
-
var tzM = parseInt(clean.slice(3,5),10);
|
|
312
|
-
var utcMs2 = Date.UTC(year,month,day,hour,minute,second,ms);
|
|
313
|
-
d = new Date(utcMs2 + sign*(tzH*60+tzM)*60000);
|
|
314
|
-
}
|
|
315
|
-
} else if (utcMode) {
|
|
316
|
-
d = new Date(Date.UTC(year,month,day,hour,minute,second,ms));
|
|
317
|
-
} else {
|
|
318
|
-
d = new Date(year,month,day,hour,minute,second,ms);
|
|
319
|
-
}
|
|
320
|
-
|
|
321
|
-
return { valid: !isNaN(d.getTime()), d: d };
|
|
322
|
-
}
|
|
323
|
-
|
|
324
|
-
module.exports = {
|
|
325
|
-
parseInput: parseInput,
|
|
326
|
-
FORMAT_TOKENS: FORMAT_TOKENS,
|
|
327
|
-
TOKEN_REGEX: TOKEN_REGEX,
|
|
328
|
-
pad: pad,
|
|
329
|
-
dayOfYear: dayOfYear,
|
|
330
|
-
weekOfYear: weekOfYear,
|
|
331
|
-
isoWeekOfYear: isoWeekOfYear,
|
|
332
|
-
weekYear: weekYear,
|
|
333
|
-
isoWeekYear: isoWeekYear,
|
|
334
|
-
offsetStr: offsetStr
|
|
335
|
-
};
|
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
|
-
// datevolt constructor
|
|
22
|
-
// ─────────────────────────────────────────────────────────────────────────────
|
|
23
|
-
function datevolt(input, fmt, strict, utcMode, tzOffset) {
|
|
24
|
-
if (!(this instanceof datevolt)) return new datevolt(input, fmt, strict, utcMode, tzOffset);
|
|
25
|
-
this.__isdatevolt = 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
|
-
datevolt.prototype._get = function(method) {
|
|
38
|
-
return this._utc ? this._d['getUTC'+method]() : this._d['get'+method]();
|
|
39
|
-
};
|
|
40
|
-
|
|
41
|
-
datevolt.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
|
-
datevolt.prototype.isValid = function() {
|
|
49
|
-
if (this._isValid === null) this._isValid = !isNaN(this._d.getTime());
|
|
50
|
-
return this._isValid;
|
|
51
|
-
};
|
|
52
|
-
|
|
53
|
-
datevolt.prototype.invalidAt = function() { return this._pf.overflow; };
|
|
54
|
-
datevolt.prototype.parsingFlags = function() { return this._pf; };
|
|
55
|
-
datevolt.prototype.creationData = function() {
|
|
56
|
-
return { input: undefined, format: undefined, locale: this._locale, isUTC: this._utc, strict: false };
|
|
57
|
-
};
|
|
58
|
-
|
|
59
|
-
// ─── Getters / Setters ───────────────────────────────────────────────────────
|
|
60
|
-
datevolt.prototype.millisecond = datevolt.prototype.milliseconds = function(v) {
|
|
61
|
-
if (v === undefined) return this._get('Milliseconds');
|
|
62
|
-
this._set('Milliseconds', v); return this;
|
|
63
|
-
};
|
|
64
|
-
datevolt.prototype.second = datevolt.prototype.seconds = function(v) {
|
|
65
|
-
if (v === undefined) return this._get('Seconds');
|
|
66
|
-
this._set('Seconds', v); return this;
|
|
67
|
-
};
|
|
68
|
-
datevolt.prototype.minute = datevolt.prototype.minutes = function(v) {
|
|
69
|
-
if (v === undefined) return this._get('Minutes');
|
|
70
|
-
this._set('Minutes', v); return this;
|
|
71
|
-
};
|
|
72
|
-
datevolt.prototype.hour = datevolt.prototype.hours = function(v) {
|
|
73
|
-
if (v === undefined) return this._get('Hours');
|
|
74
|
-
this._set('Hours', v); return this;
|
|
75
|
-
};
|
|
76
|
-
datevolt.prototype.date = function(v) {
|
|
77
|
-
if (v === undefined) return this._get('Date');
|
|
78
|
-
this._set('Date', v); return this;
|
|
79
|
-
};
|
|
80
|
-
datevolt.prototype.day = datevolt.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
|
-
datevolt.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
|
-
datevolt.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
|
-
datevolt.prototype.month = datevolt.prototype.months = function(v) {
|
|
100
|
-
if (v === undefined) return this._get('Month');
|
|
101
|
-
this._set('Month', v); return this;
|
|
102
|
-
};
|
|
103
|
-
datevolt.prototype.year = datevolt.prototype.years = function(v) {
|
|
104
|
-
if (v === undefined) return this._get('FullYear');
|
|
105
|
-
this._set('FullYear', v); return this;
|
|
106
|
-
};
|
|
107
|
-
datevolt.prototype.quarter = datevolt.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
|
-
datevolt.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
|
-
datevolt.prototype.week = datevolt.prototype.weeks = function() {
|
|
121
|
-
return parseModule.weekOfYear(this._d);
|
|
122
|
-
};
|
|
123
|
-
datevolt.prototype.isoWeek = datevolt.prototype.isoWeeks = function() {
|
|
124
|
-
return parseModule.isoWeekOfYear(this._d);
|
|
125
|
-
};
|
|
126
|
-
datevolt.prototype.weeksInYear = function() {
|
|
127
|
-
return parseModule.isoWeekOfYear(new Date(this._get('FullYear'), 11, 28));
|
|
128
|
-
};
|
|
129
|
-
datevolt.prototype.isoWeeksInYear = datevolt.prototype.weeksInYear;
|
|
130
|
-
|
|
131
|
-
// Generic get/set
|
|
132
|
-
datevolt.prototype.get = function(unit) {
|
|
133
|
-
var u = normalizeUnit(unit);
|
|
134
|
-
return this[u] ? this[u]() : undefined;
|
|
135
|
-
};
|
|
136
|
-
datevolt.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
|
-
datevolt.prototype.add = function(amount, unit) {
|
|
144
|
-
return _manipulate(this, amount, unit, 1);
|
|
145
|
-
};
|
|
146
|
-
datevolt.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
|
-
datevolt.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
|
-
datevolt.prototype.endOf = function(unit) {
|
|
210
|
-
return this.startOf(unit).add(1, unit).subtract(1, 'millisecond');
|
|
211
|
-
};
|
|
212
|
-
|
|
213
|
-
// ─── UTC / Local / Offset ─────────────────────────────────────────────────────
|
|
214
|
-
datevolt.prototype.utc = function() {
|
|
215
|
-
this._utc = true; return this;
|
|
216
|
-
};
|
|
217
|
-
datevolt.prototype.local = function() {
|
|
218
|
-
this._utc = false; return this;
|
|
219
|
-
};
|
|
220
|
-
datevolt.prototype.isUTC = function() { return this._utc; };
|
|
221
|
-
datevolt.prototype.isLocal = function() { return !this._utc; };
|
|
222
|
-
|
|
223
|
-
datevolt.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
|
-
datevolt.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
|
-
datevolt.prototype.parseZone = function() {
|
|
247
|
-
return this;
|
|
248
|
-
};
|
|
249
|
-
|
|
250
|
-
datevolt.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
|
-
datevolt.prototype.zoneAbbr = function() { return this._utc ? 'UTC' : ''; };
|
|
257
|
-
datevolt.prototype.zoneName = function() { return this._utc ? 'Coordinated Universal Time' : ''; };
|
|
258
|
-
|
|
259
|
-
// ─── Format ───────────────────────────────────────────────────────────────────
|
|
260
|
-
datevolt.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
|
-
datevolt.prototype.fromNow = function(withoutSuffix) { return this.from(new datevolt(), withoutSuffix); };
|
|
302
|
-
datevolt.prototype.toNow = function(withoutSuffix) { return this.to(new datevolt(), withoutSuffix); };
|
|
303
|
-
|
|
304
|
-
datevolt.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
|
-
datevolt.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
|
-
datevolt.prototype.calendar = function(refTime, formats) {
|
|
322
|
-
var loc = localeModule.getLocale(this._locale);
|
|
323
|
-
var ref = refTime ? (refTime instanceof datevolt ? refTime : new datevolt(refTime)) : new datevolt();
|
|
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
|
-
datevolt.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 datevolt(b).year()) +
|
|
351
|
-
(this.month() - new datevolt(b).month())/12 +
|
|
352
|
-
(this.date() - new datevolt(b).date())/(12*30.4375);
|
|
353
|
-
break;
|
|
354
|
-
case 'month':
|
|
355
|
-
result = (this.year()-new datevolt(b).year())*12 + (this.month()-new datevolt(b).month()) +
|
|
356
|
-
(this.date()-new datevolt(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 datevolt) return input._d;
|
|
371
|
-
if (input instanceof Date) return input;
|
|
372
|
-
return new datevolt(input)._d;
|
|
373
|
-
}
|
|
374
|
-
|
|
375
|
-
datevolt.prototype.isBefore = function(other, unit) {
|
|
376
|
-
if (!unit) return this._d.getTime() < _toDate(other).getTime();
|
|
377
|
-
return this.clone().endOf(unit)._d < new datevolt(other).startOf(unit)._d;
|
|
378
|
-
};
|
|
379
|
-
datevolt.prototype.isAfter = function(other, unit) {
|
|
380
|
-
if (!unit) return this._d.getTime() > _toDate(other).getTime();
|
|
381
|
-
return this.clone().startOf(unit)._d > new datevolt(other).endOf(unit)._d;
|
|
382
|
-
};
|
|
383
|
-
datevolt.prototype.isSame = function(other, unit) {
|
|
384
|
-
if (!unit) return this._d.getTime() === _toDate(other).getTime();
|
|
385
|
-
return this.clone().startOf(unit)._d.getTime() === new datevolt(other).startOf(unit)._d.getTime();
|
|
386
|
-
};
|
|
387
|
-
datevolt.prototype.isSameOrBefore = function(other, unit) {
|
|
388
|
-
return this.isSame(other,unit) || this.isBefore(other,unit);
|
|
389
|
-
};
|
|
390
|
-
datevolt.prototype.isSameOrAfter = function(other, unit) {
|
|
391
|
-
return this.isSame(other,unit) || this.isAfter(other,unit);
|
|
392
|
-
};
|
|
393
|
-
datevolt.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
|
-
datevolt.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
|
-
datevolt.prototype.isLeapYear = function() {
|
|
405
|
-
var y = this.year();
|
|
406
|
-
return (y%4===0 && y%100!==0) || y%400===0;
|
|
407
|
-
};
|
|
408
|
-
datevolt.prototype.isMoment = datevolt.prototype.isdatevolt = function() { return true; };
|
|
409
|
-
|
|
410
|
-
// ─── Output helpers ──────────────────────────────────────────────────────────
|
|
411
|
-
datevolt.prototype.valueOf = function() { return this._d.getTime(); };
|
|
412
|
-
datevolt.prototype.unix = function() { return Math.floor(this._d.getTime()/1000); };
|
|
413
|
-
datevolt.prototype.toDate = function() { return new Date(this._d.getTime()); };
|
|
414
|
-
datevolt.prototype.toArray = function() {
|
|
415
|
-
return [this.year(),this.month(),this.date(),this.hour(),this.minute(),this.second(),this.millisecond()];
|
|
416
|
-
};
|
|
417
|
-
datevolt.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
|
-
datevolt.prototype.toISOString = function() {
|
|
422
|
-
return this.isValid() ? this._d.toISOString() : null;
|
|
423
|
-
};
|
|
424
|
-
datevolt.prototype.toJSON = datevolt.prototype.toISOString;
|
|
425
|
-
datevolt.prototype.toString = function() {
|
|
426
|
-
return this.format('ddd MMM DD YYYY HH:mm:ss [GMT]ZZ');
|
|
427
|
-
};
|
|
428
|
-
datevolt.prototype.inspect = function() {
|
|
429
|
-
return 'datevolt("'+this.format()+'")';
|
|
430
|
-
};
|
|
431
|
-
datevolt.prototype.daysInMonth = function() {
|
|
432
|
-
return new Date(this.year(), this.month()+1, 0).getDate();
|
|
433
|
-
};
|
|
434
|
-
|
|
435
|
-
// ─── Locale ───────────────────────────────────────────────────────────────────
|
|
436
|
-
datevolt.prototype.locale = function(loc) {
|
|
437
|
-
if (loc === undefined) return this._locale;
|
|
438
|
-
this._locale = loc;
|
|
439
|
-
return this;
|
|
440
|
-
};
|
|
441
|
-
datevolt.prototype.lang = datevolt.prototype.locale;
|
|
442
|
-
|
|
443
|
-
datevolt.prototype.localeData = function() {
|
|
444
|
-
return localeModule.getLocale(this._locale);
|
|
445
|
-
};
|
|
446
|
-
|
|
447
|
-
// ─── Clone ────────────────────────────────────────────────────────────────────
|
|
448
|
-
datevolt.prototype.clone = function() {
|
|
449
|
-
var c = new datevolt(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 = datevolt;
|