@fr0st/datetime 6.0.1 → 7.0.0
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/Formats.md +475 -0
- package/LICENSE +0 -0
- package/README.md +407 -1868
- package/dist/frost-datetime.js +2526 -2376
- package/dist/frost-datetime.js.map +1 -1
- package/dist/frost-datetime.min.js +2 -1
- package/dist/frost-datetime.min.js.map +1 -1
- package/package.json +32 -19
- package/src/date-time.js +2094 -90
- package/src/factory.js +29 -19
- package/src/formatter/format.js +38 -29
- package/src/formatter/locale.js +80 -0
- package/src/formatter/locales.js +2 -2
- package/src/formatter/parse.js +26 -12
- package/src/formatter/tokens.js +113 -123
- package/src/formatter/utility.js +38 -64
- package/src/formatter/values.js +26 -22
- package/src/helpers.js +178 -52
- package/src/index.js +1 -186
- package/src/vars.js +4 -4
- package/src/prototype/attributes-get.js +0 -163
- package/src/prototype/attributes-set.js +0 -281
- package/src/prototype/comparisons.js +0 -605
- package/src/prototype/manipulate.js +0 -395
- package/src/prototype/output.js +0 -89
- package/src/prototype/utility.js +0 -127
- package/src/static/create.js +0 -222
- package/src/static/utility.js +0 -103
package/src/static/create.js
DELETED
|
@@ -1,222 +0,0 @@
|
|
|
1
|
-
import DateTime from './../date-time.js';
|
|
2
|
-
import { parseCompare, parseFactory } from './../helpers.js';
|
|
3
|
-
import { config, formats, formatTokenRegExp, parseOrderKeys } from './../vars.js';
|
|
4
|
-
import tokens from './../formatter/tokens.js';
|
|
5
|
-
|
|
6
|
-
/**
|
|
7
|
-
* DateTime (Static) Creation
|
|
8
|
-
*/
|
|
9
|
-
|
|
10
|
-
/**
|
|
11
|
-
* Create a new DateTime from an array.
|
|
12
|
-
* @param {number[]} dateArray The date to parse.
|
|
13
|
-
* @param {object} [options] Options for the new DateTime.
|
|
14
|
-
* @param {string} [options.timeZone] The timeZone to use.
|
|
15
|
-
* @param {string} [options.locale] The locale to use.
|
|
16
|
-
* @return {DateTime} A new DateTime object.
|
|
17
|
-
*/
|
|
18
|
-
export function fromArray(dateArray, options = {}) {
|
|
19
|
-
const dateValues = dateArray.slice(0, 3);
|
|
20
|
-
const timeValues = dateArray.slice(3);
|
|
21
|
-
|
|
22
|
-
if (dateValues.length < 3) {
|
|
23
|
-
dateValues.push(...new Array(3 - dateValues.length).fill(1));
|
|
24
|
-
}
|
|
25
|
-
|
|
26
|
-
if (timeValues.length < 4) {
|
|
27
|
-
timeValues.push(...new Array(4 - timeValues.length).fill(0));
|
|
28
|
-
}
|
|
29
|
-
|
|
30
|
-
return new DateTime(null, options)
|
|
31
|
-
.setTimestamp(0)
|
|
32
|
-
.setYear(...dateValues)
|
|
33
|
-
.setHours(...timeValues);
|
|
34
|
-
};
|
|
35
|
-
|
|
36
|
-
/**
|
|
37
|
-
* Create a new DateTime from a Date.
|
|
38
|
-
* @param {Date} date The date.
|
|
39
|
-
* @param {object} [options] Options for the new DateTime.
|
|
40
|
-
* @param {string} [options.timeZone] The timeZone to use.
|
|
41
|
-
* @param {string} [options.locale] The locale to use.
|
|
42
|
-
* @return {DateTime} A new DateTime object.
|
|
43
|
-
*/
|
|
44
|
-
export function fromDate(date, options = {}) {
|
|
45
|
-
return new DateTime(date.getTime(), options);
|
|
46
|
-
};
|
|
47
|
-
|
|
48
|
-
/**
|
|
49
|
-
* Create a new DateTime from a format string.
|
|
50
|
-
* @param {string} formatString The format string.
|
|
51
|
-
* @param {string} dateString The date string.
|
|
52
|
-
* @param {object} [options] Options for the new DateTime.
|
|
53
|
-
* @param {string} [options.timeZone] The timeZone to use.
|
|
54
|
-
* @param {string} [options.locale] The locale to use.
|
|
55
|
-
* @return {DateTime} A new DateTime object.
|
|
56
|
-
*/
|
|
57
|
-
export function fromFormat(formatString, dateString, options = {}) {
|
|
58
|
-
if (!('locale' in options)) {
|
|
59
|
-
options.locale = config.defaultLocale;
|
|
60
|
-
}
|
|
61
|
-
|
|
62
|
-
const values = [];
|
|
63
|
-
|
|
64
|
-
let match;
|
|
65
|
-
while (formatString && (match = formatString.match(formatTokenRegExp))) {
|
|
66
|
-
const token = match[1];
|
|
67
|
-
const position = match.index;
|
|
68
|
-
const length = match[0].length;
|
|
69
|
-
|
|
70
|
-
if (position) {
|
|
71
|
-
const formatTest = formatString.substring(0, position);
|
|
72
|
-
parseCompare(formatTest, dateString);
|
|
73
|
-
}
|
|
74
|
-
|
|
75
|
-
formatString = formatString.substring(position + length);
|
|
76
|
-
dateString = dateString.substring(position);
|
|
77
|
-
|
|
78
|
-
if (!token) {
|
|
79
|
-
const literal = match[0].slice(1, -1);
|
|
80
|
-
parseCompare(literal || `'`, dateString);
|
|
81
|
-
dateString = dateString.substring(literal.length);
|
|
82
|
-
continue;
|
|
83
|
-
}
|
|
84
|
-
|
|
85
|
-
if (!(token in tokens)) {
|
|
86
|
-
throw new Error(`Invalid token in DateTime format: ${token}`);
|
|
87
|
-
}
|
|
88
|
-
|
|
89
|
-
const regExp = tokens[token].regex(options.locale, length);
|
|
90
|
-
const matchedValue = dateString.match(new RegExp(`^${regExp}`));
|
|
91
|
-
|
|
92
|
-
if (!matchedValue) {
|
|
93
|
-
throw new Error(`Unmatched token in DateTime string: ${token}`);
|
|
94
|
-
}
|
|
95
|
-
|
|
96
|
-
const literal = matchedValue[0];
|
|
97
|
-
const value = tokens[token].input(options.locale, literal, length);
|
|
98
|
-
|
|
99
|
-
if (value !== null) {
|
|
100
|
-
const key = tokens[token].key;
|
|
101
|
-
values.push({ key, value, literal, token, length });
|
|
102
|
-
}
|
|
103
|
-
|
|
104
|
-
dateString = dateString.substring(literal.length);
|
|
105
|
-
}
|
|
106
|
-
|
|
107
|
-
if (formatString) {
|
|
108
|
-
parseCompare(formatString, dateString);
|
|
109
|
-
}
|
|
110
|
-
|
|
111
|
-
if (!('timeZone' in options)) {
|
|
112
|
-
options.timeZone = config.defaultTimeZone;
|
|
113
|
-
}
|
|
114
|
-
|
|
115
|
-
let timeZone = options.timeZone;
|
|
116
|
-
for (const { key, value } of values) {
|
|
117
|
-
if (key !== 'timeZone') {
|
|
118
|
-
continue;
|
|
119
|
-
}
|
|
120
|
-
|
|
121
|
-
timeZone = value;
|
|
122
|
-
}
|
|
123
|
-
|
|
124
|
-
let datetime = this.fromTimestamp(0, {
|
|
125
|
-
locale: options.locale,
|
|
126
|
-
}).setYear(1).setTimeZone(timeZone);
|
|
127
|
-
|
|
128
|
-
const methods = parseFactory();
|
|
129
|
-
|
|
130
|
-
const testValues = [];
|
|
131
|
-
|
|
132
|
-
for (const subKeys of parseOrderKeys) {
|
|
133
|
-
for (const subKey of subKeys) {
|
|
134
|
-
if (subKey === 'era' && !values.find((data) => data.key === 'year')) {
|
|
135
|
-
continue;
|
|
136
|
-
}
|
|
137
|
-
|
|
138
|
-
for (const data of values) {
|
|
139
|
-
const { key, value, literal, token, length } = data;
|
|
140
|
-
|
|
141
|
-
if (key !== subKey) {
|
|
142
|
-
continue;
|
|
143
|
-
}
|
|
144
|
-
|
|
145
|
-
// skip narrow month and day names if output already matches
|
|
146
|
-
if (length === 5 && ['M', 'L', 'E', 'e', 'c'].includes(token)) {
|
|
147
|
-
const fullToken = token.repeat(length);
|
|
148
|
-
if (datetime.format(fullToken) === literal) {
|
|
149
|
-
continue;
|
|
150
|
-
}
|
|
151
|
-
}
|
|
152
|
-
|
|
153
|
-
datetime = methods[key].set(datetime, value);
|
|
154
|
-
testValues.push(data);
|
|
155
|
-
}
|
|
156
|
-
}
|
|
157
|
-
}
|
|
158
|
-
|
|
159
|
-
let isValid = true;
|
|
160
|
-
for (const { key, value } of testValues) {
|
|
161
|
-
if (key in methods && methods[key].get(datetime) !== value) {
|
|
162
|
-
isValid = false;
|
|
163
|
-
break;
|
|
164
|
-
}
|
|
165
|
-
}
|
|
166
|
-
|
|
167
|
-
if (options.timeZone !== timeZone) {
|
|
168
|
-
datetime = datetime.setTimeZone(options.timeZone);
|
|
169
|
-
}
|
|
170
|
-
|
|
171
|
-
datetime.isValid = isValid;
|
|
172
|
-
|
|
173
|
-
return datetime;
|
|
174
|
-
};
|
|
175
|
-
|
|
176
|
-
/**
|
|
177
|
-
* Create a new DateTime from an ISO format string.
|
|
178
|
-
* @param {string} dateString The date string.
|
|
179
|
-
* @param {object} [options] Options for the new DateTime.
|
|
180
|
-
* @param {string} [options.timeZone] The timeZone to use.
|
|
181
|
-
* @param {string} [options.locale] The locale to use.
|
|
182
|
-
* @return {DateTime} A new DateTime object.
|
|
183
|
-
*/
|
|
184
|
-
export function fromISOString(dateString, options = {}) {
|
|
185
|
-
let date = this.fromFormat(formats.rfc3339_extended, dateString, {
|
|
186
|
-
locale: 'en',
|
|
187
|
-
});
|
|
188
|
-
|
|
189
|
-
if ('timeZone' in options) {
|
|
190
|
-
date = date.setTimeZone(options.timeZone);
|
|
191
|
-
}
|
|
192
|
-
|
|
193
|
-
if ('locale' in options) {
|
|
194
|
-
date = date.setLocale(options.locale);
|
|
195
|
-
}
|
|
196
|
-
|
|
197
|
-
return date;
|
|
198
|
-
};
|
|
199
|
-
|
|
200
|
-
/**
|
|
201
|
-
* Create a new DateTime from a timestamp.
|
|
202
|
-
* @param {number} timestamp The timestamp.
|
|
203
|
-
* @param {object} [options] Options for the new DateTime.
|
|
204
|
-
* @param {string} [options.timeZone] The timeZone to use.
|
|
205
|
-
* @param {string} [options.locale] The locale to use.
|
|
206
|
-
* @return {DateTime} A new DateTime object.
|
|
207
|
-
*/
|
|
208
|
-
export function fromTimestamp(timestamp, options = {}) {
|
|
209
|
-
return new DateTime(null, options)
|
|
210
|
-
.setTimestamp(timestamp);
|
|
211
|
-
};
|
|
212
|
-
|
|
213
|
-
/**
|
|
214
|
-
* Create a new DateTime for the current time.
|
|
215
|
-
* @param {object} [options] Options for the new DateTime.
|
|
216
|
-
* @param {string} [options.timeZone] The timeZone to use.
|
|
217
|
-
* @param {string} [options.locale] The locale to use.
|
|
218
|
-
* @return {DateTime} A new DateTime object.
|
|
219
|
-
*/
|
|
220
|
-
export function now(options = {}) {
|
|
221
|
-
return new DateTime(null, options);
|
|
222
|
-
};
|
package/src/static/utility.js
DELETED
|
@@ -1,103 +0,0 @@
|
|
|
1
|
-
import { config, monthDays } from './../vars.js';
|
|
2
|
-
|
|
3
|
-
/**
|
|
4
|
-
* DateTime (Static) Utility
|
|
5
|
-
*/
|
|
6
|
-
|
|
7
|
-
/**
|
|
8
|
-
* Get the day of the year for a year, month and date.
|
|
9
|
-
* @param {number} year The year.
|
|
10
|
-
* @param {number} month The month. (1, 12)
|
|
11
|
-
* @param {number} date The date.
|
|
12
|
-
* @return {number} The day of the year. (1, 366)
|
|
13
|
-
*/
|
|
14
|
-
export function dayOfYear(year, month, date) {
|
|
15
|
-
return new Array(month - 1)
|
|
16
|
-
.fill()
|
|
17
|
-
.reduce(
|
|
18
|
-
(d, _, i) =>
|
|
19
|
-
d + daysInMonth(year, i + 1),
|
|
20
|
-
date,
|
|
21
|
-
);
|
|
22
|
-
};
|
|
23
|
-
|
|
24
|
-
/**
|
|
25
|
-
* Get the number of days in a month, from a year and month.
|
|
26
|
-
* @param {number} year The year.
|
|
27
|
-
* @param {number} month The month. (1, 12)
|
|
28
|
-
* @return {number} The number of days in the month.
|
|
29
|
-
*/
|
|
30
|
-
export function daysInMonth(year, month) {
|
|
31
|
-
const date = new Date(Date.UTC(year, month - 1));
|
|
32
|
-
month = date.getUTCMonth();
|
|
33
|
-
|
|
34
|
-
return monthDays[month] +
|
|
35
|
-
(
|
|
36
|
-
month == 1 && isLeapYear(
|
|
37
|
-
date.getUTCFullYear(),
|
|
38
|
-
) ?
|
|
39
|
-
1 :
|
|
40
|
-
0
|
|
41
|
-
);
|
|
42
|
-
};
|
|
43
|
-
|
|
44
|
-
/**
|
|
45
|
-
* Get the number of days in a year.
|
|
46
|
-
* @param {number} year The year.
|
|
47
|
-
* @return {number} The number of days in the year.
|
|
48
|
-
*/
|
|
49
|
-
export function daysInYear(year) {
|
|
50
|
-
return !isLeapYear(year) ?
|
|
51
|
-
365 :
|
|
52
|
-
366;
|
|
53
|
-
};
|
|
54
|
-
|
|
55
|
-
/**
|
|
56
|
-
* Get the default locale.
|
|
57
|
-
* @return {string} The locale.
|
|
58
|
-
*/
|
|
59
|
-
export function getDefaultLocale() {
|
|
60
|
-
return config.defaultLocale;
|
|
61
|
-
};
|
|
62
|
-
|
|
63
|
-
/**
|
|
64
|
-
* Get the default timeZone.
|
|
65
|
-
* @return {string} The name of the timeZone.
|
|
66
|
-
*/
|
|
67
|
-
export function getDefaultTimeZone() {
|
|
68
|
-
return config.defaultTimeZone;
|
|
69
|
-
};
|
|
70
|
-
|
|
71
|
-
/**
|
|
72
|
-
* Return true if a year is a leap year.
|
|
73
|
-
* @param {number} year The year.
|
|
74
|
-
* @return {Boolean} TRUE if the year is a leap year, otherwise FALSE.
|
|
75
|
-
*/
|
|
76
|
-
export function isLeapYear(year) {
|
|
77
|
-
return new Date(year, 1, 29)
|
|
78
|
-
.getDate() === 29;
|
|
79
|
-
};
|
|
80
|
-
|
|
81
|
-
/**
|
|
82
|
-
* Set whether dates will be clamped when changing months.
|
|
83
|
-
* @param {Boolean} clampDates Whether to clamp dates.
|
|
84
|
-
*/
|
|
85
|
-
export function setDateClamping(clampDates) {
|
|
86
|
-
config.clampDates = clampDates;
|
|
87
|
-
};
|
|
88
|
-
|
|
89
|
-
/**
|
|
90
|
-
* Set the default locale.
|
|
91
|
-
* @param {string} locale The locale.
|
|
92
|
-
*/
|
|
93
|
-
export function setDefaultLocale(locale) {
|
|
94
|
-
config.defaultLocale = locale;
|
|
95
|
-
};
|
|
96
|
-
|
|
97
|
-
/**
|
|
98
|
-
* Set the default timeZone.
|
|
99
|
-
* @param {string} timeZone The name of the timeZone.
|
|
100
|
-
*/
|
|
101
|
-
export function setDefaultTimeZone(timeZone) {
|
|
102
|
-
config.defaultTimeZone = timeZone;
|
|
103
|
-
};
|