@trackunit/date-and-time-utils 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/index.cjs.js +18 -20
- package/index.esm.js +18 -20
- package/package.json +1 -1
package/index.cjs.js
CHANGED
|
@@ -11,7 +11,7 @@ var polyfill = require('@js-temporal/polyfill');
|
|
|
11
11
|
* returns A time zone object. { id: string, offset: string, name: string }
|
|
12
12
|
*/
|
|
13
13
|
const getTimeZone = (timeZoneId) => {
|
|
14
|
-
const tzId = timeZoneId
|
|
14
|
+
const tzId = timeZoneId ?? Intl.DateTimeFormat().resolvedOptions().timeZone;
|
|
15
15
|
const offset = getTimeZoneOffset(tzId);
|
|
16
16
|
return {
|
|
17
17
|
id: tzId,
|
|
@@ -25,9 +25,8 @@ const getTimeZone = (timeZoneId) => {
|
|
|
25
25
|
* @param timeZone - The time zone to use for the conversion.
|
|
26
26
|
*/
|
|
27
27
|
const getTimeZoneOffset = (timeZone) => {
|
|
28
|
-
var _a, _b;
|
|
29
28
|
const timezone = polyfill.Temporal.TimeZone.from(timeZone);
|
|
30
|
-
return
|
|
29
|
+
return timezone.getOffsetStringFor?.(polyfill.Temporal.Now.instant()) ?? "";
|
|
31
30
|
};
|
|
32
31
|
/**
|
|
33
32
|
* Returns a list of all supported time zones.
|
|
@@ -104,35 +103,34 @@ const getHourCycle = (locale) => {
|
|
|
104
103
|
* Output: "5/10/23, 12:00:00 AM EDT"
|
|
105
104
|
*/
|
|
106
105
|
const formatDateUtil = (date, format, timeZone, locale) => {
|
|
107
|
-
var _a, _b, _c, _d, _e, _f;
|
|
108
106
|
const hourCycle = getHourCycle(locale);
|
|
109
107
|
const dateObj = timeZone ? toZonedDateTimeUtil(date, timeZone) : toTemporalUtil(toDateUtil(date));
|
|
110
|
-
if (
|
|
108
|
+
if (format?.selectFormat === "dateOnly") {
|
|
111
109
|
if (dateObj instanceof polyfill.Temporal.ZonedDateTime) {
|
|
112
110
|
return dateObj.toPlainDate().toLocaleString(locale, {
|
|
113
|
-
dateStyle:
|
|
111
|
+
dateStyle: format.dateFormat ?? "medium",
|
|
114
112
|
});
|
|
115
113
|
}
|
|
116
114
|
return dateObj.toLocaleString(locale, {
|
|
117
|
-
dateStyle:
|
|
115
|
+
dateStyle: format.dateFormat ?? "medium",
|
|
118
116
|
hourCycle,
|
|
119
117
|
});
|
|
120
118
|
}
|
|
121
|
-
if (
|
|
119
|
+
if (format?.selectFormat === "timeOnly") {
|
|
122
120
|
if (dateObj instanceof polyfill.Temporal.ZonedDateTime) {
|
|
123
121
|
return dateObj.toPlainTime().toLocaleString(locale, {
|
|
124
|
-
timeStyle:
|
|
122
|
+
timeStyle: format.timeFormat ?? "short",
|
|
125
123
|
hourCycle,
|
|
126
124
|
});
|
|
127
125
|
}
|
|
128
126
|
return dateObj.toLocaleString(locale, {
|
|
129
|
-
timeStyle:
|
|
127
|
+
timeStyle: format.timeFormat ?? "short",
|
|
130
128
|
hourCycle,
|
|
131
129
|
});
|
|
132
130
|
}
|
|
133
131
|
return dateObj.toLocaleString(locale, {
|
|
134
|
-
dateStyle:
|
|
135
|
-
timeStyle:
|
|
132
|
+
dateStyle: format?.dateFormat ?? "medium",
|
|
133
|
+
timeStyle: format?.timeFormat ?? "short",
|
|
136
134
|
hourCycle,
|
|
137
135
|
});
|
|
138
136
|
};
|
|
@@ -1156,7 +1154,7 @@ const getRelativeTimeFormat = (locale) => new Intl.RelativeTimeFormat(locale, {
|
|
|
1156
1154
|
* @returns {string} human-readable string
|
|
1157
1155
|
*/
|
|
1158
1156
|
const getDurationFormat = (duration, locale, format) => {
|
|
1159
|
-
return new Intl.DurationFormat(locale, { style: format
|
|
1157
|
+
return new Intl.DurationFormat(locale, { style: format ?? "long" }).format(duration);
|
|
1160
1158
|
};
|
|
1161
1159
|
/**
|
|
1162
1160
|
* convert a number to a type
|
|
@@ -1169,16 +1167,16 @@ const toDuration = (number, unit, roundAt) => {
|
|
|
1169
1167
|
switch (unit) {
|
|
1170
1168
|
case "days":
|
|
1171
1169
|
return polyfill.Temporal.Duration.from(durationBuilder(number, "days")).round({
|
|
1172
|
-
smallestUnit: roundAt
|
|
1170
|
+
smallestUnit: roundAt ?? "seconds",
|
|
1173
1171
|
});
|
|
1174
1172
|
case "hours":
|
|
1175
|
-
return polyfill.Temporal.Duration.from(durationBuilder(number, "hours")).round({ smallestUnit: roundAt
|
|
1173
|
+
return polyfill.Temporal.Duration.from(durationBuilder(number, "hours")).round({ smallestUnit: roundAt ?? "minutes" });
|
|
1176
1174
|
case "minutes":
|
|
1177
1175
|
return polyfill.Temporal.Duration.from(durationBuilder(number, "minutes")).round({
|
|
1178
|
-
smallestUnit: roundAt
|
|
1176
|
+
smallestUnit: roundAt ?? "seconds",
|
|
1179
1177
|
});
|
|
1180
1178
|
case "seconds":
|
|
1181
|
-
return polyfill.Temporal.Duration.from(durationBuilder(number, "seconds")).round({ smallestUnit: roundAt
|
|
1179
|
+
return polyfill.Temporal.Duration.from(durationBuilder(number, "seconds")).round({ smallestUnit: roundAt ?? "seconds" });
|
|
1182
1180
|
default:
|
|
1183
1181
|
throw new Error("Not a valid unit", { cause: unit });
|
|
1184
1182
|
}
|
|
@@ -1378,7 +1376,7 @@ const toZonedDateTimeUtil = (date, timeZoneId, calendar) => {
|
|
|
1378
1376
|
date = toTemporalUtil(date);
|
|
1379
1377
|
}
|
|
1380
1378
|
return date.toZonedDateTime({
|
|
1381
|
-
calendar: polyfill.Temporal.Calendar.from(calendar
|
|
1379
|
+
calendar: polyfill.Temporal.Calendar.from(calendar ?? "iso8601"),
|
|
1382
1380
|
timeZone: polyfill.Temporal.TimeZone.from(timeZoneId || date.toString() || polyfill.Temporal.Now.timeZoneId()),
|
|
1383
1381
|
});
|
|
1384
1382
|
};
|
|
@@ -1422,7 +1420,7 @@ const monthsUtil = (format, locale) => {
|
|
|
1422
1420
|
const monthsInYear = polyfill.Temporal.PlainDate.from(d.toISOString()).monthsInYear;
|
|
1423
1421
|
for (let month = 0; month < monthsInYear; ++month) {
|
|
1424
1422
|
d.setMonth(month);
|
|
1425
|
-
months.push(d.toLocaleString(locale, { month: format
|
|
1423
|
+
months.push(d.toLocaleString(locale, { month: format ?? "long" }));
|
|
1426
1424
|
}
|
|
1427
1425
|
return months;
|
|
1428
1426
|
};
|
|
@@ -1466,7 +1464,7 @@ const daysUtil = (format, locale) => {
|
|
|
1466
1464
|
const daysInWeek = polyfill.Temporal.PlainDate.from(d.toISOString()).daysInWeek;
|
|
1467
1465
|
for (let day = 0; day < daysInWeek; ++day) {
|
|
1468
1466
|
d.setDate(day);
|
|
1469
|
-
days.push(d.toLocaleString(locale, { weekday: format
|
|
1467
|
+
days.push(d.toLocaleString(locale, { weekday: format ?? "long" }));
|
|
1470
1468
|
}
|
|
1471
1469
|
return days;
|
|
1472
1470
|
};
|
package/index.esm.js
CHANGED
|
@@ -9,7 +9,7 @@ import { Temporal } from '@js-temporal/polyfill';
|
|
|
9
9
|
* returns A time zone object. { id: string, offset: string, name: string }
|
|
10
10
|
*/
|
|
11
11
|
const getTimeZone = (timeZoneId) => {
|
|
12
|
-
const tzId = timeZoneId
|
|
12
|
+
const tzId = timeZoneId ?? Intl.DateTimeFormat().resolvedOptions().timeZone;
|
|
13
13
|
const offset = getTimeZoneOffset(tzId);
|
|
14
14
|
return {
|
|
15
15
|
id: tzId,
|
|
@@ -23,9 +23,8 @@ const getTimeZone = (timeZoneId) => {
|
|
|
23
23
|
* @param timeZone - The time zone to use for the conversion.
|
|
24
24
|
*/
|
|
25
25
|
const getTimeZoneOffset = (timeZone) => {
|
|
26
|
-
var _a, _b;
|
|
27
26
|
const timezone = Temporal.TimeZone.from(timeZone);
|
|
28
|
-
return
|
|
27
|
+
return timezone.getOffsetStringFor?.(Temporal.Now.instant()) ?? "";
|
|
29
28
|
};
|
|
30
29
|
/**
|
|
31
30
|
* Returns a list of all supported time zones.
|
|
@@ -102,35 +101,34 @@ const getHourCycle = (locale) => {
|
|
|
102
101
|
* Output: "5/10/23, 12:00:00 AM EDT"
|
|
103
102
|
*/
|
|
104
103
|
const formatDateUtil = (date, format, timeZone, locale) => {
|
|
105
|
-
var _a, _b, _c, _d, _e, _f;
|
|
106
104
|
const hourCycle = getHourCycle(locale);
|
|
107
105
|
const dateObj = timeZone ? toZonedDateTimeUtil(date, timeZone) : toTemporalUtil(toDateUtil(date));
|
|
108
|
-
if (
|
|
106
|
+
if (format?.selectFormat === "dateOnly") {
|
|
109
107
|
if (dateObj instanceof Temporal.ZonedDateTime) {
|
|
110
108
|
return dateObj.toPlainDate().toLocaleString(locale, {
|
|
111
|
-
dateStyle:
|
|
109
|
+
dateStyle: format.dateFormat ?? "medium",
|
|
112
110
|
});
|
|
113
111
|
}
|
|
114
112
|
return dateObj.toLocaleString(locale, {
|
|
115
|
-
dateStyle:
|
|
113
|
+
dateStyle: format.dateFormat ?? "medium",
|
|
116
114
|
hourCycle,
|
|
117
115
|
});
|
|
118
116
|
}
|
|
119
|
-
if (
|
|
117
|
+
if (format?.selectFormat === "timeOnly") {
|
|
120
118
|
if (dateObj instanceof Temporal.ZonedDateTime) {
|
|
121
119
|
return dateObj.toPlainTime().toLocaleString(locale, {
|
|
122
|
-
timeStyle:
|
|
120
|
+
timeStyle: format.timeFormat ?? "short",
|
|
123
121
|
hourCycle,
|
|
124
122
|
});
|
|
125
123
|
}
|
|
126
124
|
return dateObj.toLocaleString(locale, {
|
|
127
|
-
timeStyle:
|
|
125
|
+
timeStyle: format.timeFormat ?? "short",
|
|
128
126
|
hourCycle,
|
|
129
127
|
});
|
|
130
128
|
}
|
|
131
129
|
return dateObj.toLocaleString(locale, {
|
|
132
|
-
dateStyle:
|
|
133
|
-
timeStyle:
|
|
130
|
+
dateStyle: format?.dateFormat ?? "medium",
|
|
131
|
+
timeStyle: format?.timeFormat ?? "short",
|
|
134
132
|
hourCycle,
|
|
135
133
|
});
|
|
136
134
|
};
|
|
@@ -1154,7 +1152,7 @@ const getRelativeTimeFormat = (locale) => new Intl.RelativeTimeFormat(locale, {
|
|
|
1154
1152
|
* @returns {string} human-readable string
|
|
1155
1153
|
*/
|
|
1156
1154
|
const getDurationFormat = (duration, locale, format) => {
|
|
1157
|
-
return new Intl.DurationFormat(locale, { style: format
|
|
1155
|
+
return new Intl.DurationFormat(locale, { style: format ?? "long" }).format(duration);
|
|
1158
1156
|
};
|
|
1159
1157
|
/**
|
|
1160
1158
|
* convert a number to a type
|
|
@@ -1167,16 +1165,16 @@ const toDuration = (number, unit, roundAt) => {
|
|
|
1167
1165
|
switch (unit) {
|
|
1168
1166
|
case "days":
|
|
1169
1167
|
return Temporal.Duration.from(durationBuilder(number, "days")).round({
|
|
1170
|
-
smallestUnit: roundAt
|
|
1168
|
+
smallestUnit: roundAt ?? "seconds",
|
|
1171
1169
|
});
|
|
1172
1170
|
case "hours":
|
|
1173
|
-
return Temporal.Duration.from(durationBuilder(number, "hours")).round({ smallestUnit: roundAt
|
|
1171
|
+
return Temporal.Duration.from(durationBuilder(number, "hours")).round({ smallestUnit: roundAt ?? "minutes" });
|
|
1174
1172
|
case "minutes":
|
|
1175
1173
|
return Temporal.Duration.from(durationBuilder(number, "minutes")).round({
|
|
1176
|
-
smallestUnit: roundAt
|
|
1174
|
+
smallestUnit: roundAt ?? "seconds",
|
|
1177
1175
|
});
|
|
1178
1176
|
case "seconds":
|
|
1179
|
-
return Temporal.Duration.from(durationBuilder(number, "seconds")).round({ smallestUnit: roundAt
|
|
1177
|
+
return Temporal.Duration.from(durationBuilder(number, "seconds")).round({ smallestUnit: roundAt ?? "seconds" });
|
|
1180
1178
|
default:
|
|
1181
1179
|
throw new Error("Not a valid unit", { cause: unit });
|
|
1182
1180
|
}
|
|
@@ -1376,7 +1374,7 @@ const toZonedDateTimeUtil = (date, timeZoneId, calendar) => {
|
|
|
1376
1374
|
date = toTemporalUtil(date);
|
|
1377
1375
|
}
|
|
1378
1376
|
return date.toZonedDateTime({
|
|
1379
|
-
calendar: Temporal.Calendar.from(calendar
|
|
1377
|
+
calendar: Temporal.Calendar.from(calendar ?? "iso8601"),
|
|
1380
1378
|
timeZone: Temporal.TimeZone.from(timeZoneId || date.toString() || Temporal.Now.timeZoneId()),
|
|
1381
1379
|
});
|
|
1382
1380
|
};
|
|
@@ -1420,7 +1418,7 @@ const monthsUtil = (format, locale) => {
|
|
|
1420
1418
|
const monthsInYear = Temporal.PlainDate.from(d.toISOString()).monthsInYear;
|
|
1421
1419
|
for (let month = 0; month < monthsInYear; ++month) {
|
|
1422
1420
|
d.setMonth(month);
|
|
1423
|
-
months.push(d.toLocaleString(locale, { month: format
|
|
1421
|
+
months.push(d.toLocaleString(locale, { month: format ?? "long" }));
|
|
1424
1422
|
}
|
|
1425
1423
|
return months;
|
|
1426
1424
|
};
|
|
@@ -1464,7 +1462,7 @@ const daysUtil = (format, locale) => {
|
|
|
1464
1462
|
const daysInWeek = Temporal.PlainDate.from(d.toISOString()).daysInWeek;
|
|
1465
1463
|
for (let day = 0; day < daysInWeek; ++day) {
|
|
1466
1464
|
d.setDate(day);
|
|
1467
|
-
days.push(d.toLocaleString(locale, { weekday: format
|
|
1465
|
+
days.push(d.toLocaleString(locale, { weekday: format ?? "long" }));
|
|
1468
1466
|
}
|
|
1469
1467
|
return days;
|
|
1470
1468
|
};
|