@driveflux/time 6.0.3 → 6.0.4
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/dist/config.js +9 -8
- package/dist/index.js +25 -21
- package/dist/translations.js +8 -6
- package/package.json +6 -6
package/dist/config.js
CHANGED
|
@@ -1,14 +1,15 @@
|
|
|
1
1
|
import { initSingleton } from '@driveflux/singleton';
|
|
2
|
-
|
|
3
|
-
|
|
4
|
-
|
|
5
|
-
|
|
6
|
-
};
|
|
7
|
-
export
|
|
8
|
-
export
|
|
2
|
+
const getConfig = () => ({
|
|
3
|
+
timezone: typeof process !== 'undefined'
|
|
4
|
+
? process.env.TIME_ZONE || 'Asia/Kuala_Lumpur'
|
|
5
|
+
: 'Asia/Kuala_Lumpur',
|
|
6
|
+
});
|
|
7
|
+
export let config = initSingleton('timeConfig', getConfig);
|
|
8
|
+
export const resetConfig = () => {
|
|
9
9
|
config = initSingleton('timeConfig', getConfig, true);
|
|
10
10
|
return config;
|
|
11
11
|
};
|
|
12
|
-
export
|
|
12
|
+
export const setConfig = (key, value) => {
|
|
13
13
|
config[key] = value;
|
|
14
14
|
};
|
|
15
|
+
//# sourceMappingURL=config.js.map
|
package/dist/index.js
CHANGED
|
@@ -4,16 +4,17 @@ import { parse as fnsParse } from 'date-fns/parse';
|
|
|
4
4
|
import { startOfDay } from 'date-fns/startOfDay';
|
|
5
5
|
import * as dateFnsTz from 'date-fns-tz';
|
|
6
6
|
import { config } from './config.js';
|
|
7
|
-
|
|
7
|
+
const { fromZonedTime, toZonedTime } = dateFnsTz;
|
|
8
8
|
/**
|
|
9
9
|
* @see https://date-fns.org/docs/format
|
|
10
|
-
*/
|
|
11
|
-
export
|
|
12
|
-
export
|
|
13
|
-
export
|
|
10
|
+
*/
|
|
11
|
+
export const DEFAULT_DATE_FORMAT = 'dd-MM-yyyy';
|
|
12
|
+
export const DEFAULT_TIME_FORMAT = 'hh:mm a';
|
|
13
|
+
export const NUMBERED_TIME_FORMAT = 'hh:mm';
|
|
14
|
+
export const EASY_FORMATS = {
|
|
14
15
|
date: DEFAULT_DATE_FORMAT,
|
|
15
16
|
time: DEFAULT_TIME_FORMAT,
|
|
16
|
-
full:
|
|
17
|
+
full: `${DEFAULT_DATE_FORMAT} ${DEFAULT_TIME_FORMAT}`,
|
|
17
18
|
};
|
|
18
19
|
/**
|
|
19
20
|
* Formats a date according to date-fns format
|
|
@@ -23,47 +24,50 @@ export var EASY_FORMATS = {
|
|
|
23
24
|
* - full: dd-MM-yyyy hh:mm a
|
|
24
25
|
*
|
|
25
26
|
* @see https://date-fns.org/docs/format
|
|
26
|
-
*/
|
|
27
|
-
|
|
27
|
+
*/
|
|
28
|
+
export const format = (date, providedDateFormat, timezone) => {
|
|
29
|
+
let dateFormat = providedDateFormat || DEFAULT_DATE_FORMAT;
|
|
28
30
|
if (dateFormat in EASY_FORMATS) {
|
|
29
31
|
dateFormat = EASY_FORMATS[dateFormat];
|
|
30
32
|
}
|
|
31
|
-
|
|
33
|
+
const newDate = timezone
|
|
34
|
+
? toZonedTime(new Date(date), timezone)
|
|
35
|
+
: new Date(date);
|
|
32
36
|
return fnsFormat(newDate, dateFormat);
|
|
33
37
|
};
|
|
34
|
-
export
|
|
35
|
-
var date = arguments.length > 0 && arguments[0] !== void 0 ? arguments[0] : new Date(), providedDateFormat = arguments.length > 1 ? arguments[1] : void 0;
|
|
38
|
+
export const formatToLocal = (date = new Date(), providedDateFormat) => {
|
|
36
39
|
return format(new Date(date), providedDateFormat, config.timezone);
|
|
37
40
|
};
|
|
38
|
-
export
|
|
41
|
+
export const parse = (date) => {
|
|
39
42
|
return fnsParse(date, DEFAULT_DATE_FORMAT, new Date());
|
|
40
43
|
};
|
|
41
|
-
export
|
|
42
|
-
|
|
43
|
-
|
|
44
|
+
export const ensureExtremityOfDay = (date, startOrEnd, timezone) => {
|
|
45
|
+
const extremity = startOrEnd === 'end' ? endOfDay : startOfDay;
|
|
46
|
+
const tz = timezone || getTimeZone();
|
|
44
47
|
// First convert date to UTC, apply start of day, then convert back to timezone
|
|
45
48
|
return fromZonedTime(extremity(toZonedTime(date, tz)), tz);
|
|
46
49
|
};
|
|
47
|
-
export
|
|
50
|
+
export const getTimeZone = () => {
|
|
48
51
|
return config.timezone;
|
|
49
52
|
};
|
|
50
53
|
/**
|
|
51
54
|
*
|
|
52
55
|
* @param date Date is in UTC and this will convert to a zoned time (00:00:00 => 16:00:00)
|
|
53
56
|
* @returns
|
|
54
|
-
*/
|
|
55
|
-
|
|
57
|
+
*/
|
|
58
|
+
export const zonedDate = (date = new Date(), timezone) => {
|
|
56
59
|
return fromZonedTime(date, timezone || getTimeZone());
|
|
57
60
|
};
|
|
58
61
|
/**
|
|
59
62
|
*
|
|
60
63
|
* @param date Date is in UTC and this will convert to a zoned time (16:00:00 => 00:00:00)
|
|
61
64
|
* @returns
|
|
62
|
-
*/
|
|
63
|
-
|
|
65
|
+
*/
|
|
66
|
+
export const utcDate = (date = new Date(), timezone) => {
|
|
64
67
|
return toZonedTime(date, timezone || getTimeZone());
|
|
65
68
|
};
|
|
66
|
-
export
|
|
69
|
+
export const setTimeZone = (timezone) => {
|
|
67
70
|
config.timezone = timezone;
|
|
68
71
|
};
|
|
69
72
|
export * from './config.js';
|
|
73
|
+
//# sourceMappingURL=index.js.map
|
package/dist/translations.js
CHANGED
|
@@ -1,13 +1,15 @@
|
|
|
1
1
|
import { singleton } from '@driveflux/singleton';
|
|
2
|
-
export
|
|
2
|
+
export const translations = singleton('timeTranslations', {
|
|
3
3
|
am: 'am',
|
|
4
|
-
pm: 'pm'
|
|
4
|
+
pm: 'pm',
|
|
5
5
|
});
|
|
6
|
-
export
|
|
7
|
-
for(
|
|
8
|
-
translations[key] =
|
|
6
|
+
export const setTranslations = (ts) => {
|
|
7
|
+
for (const key in ts) {
|
|
8
|
+
translations[key] =
|
|
9
|
+
ts[key];
|
|
9
10
|
}
|
|
10
11
|
};
|
|
11
|
-
export
|
|
12
|
+
export const setTranslation = (key, value) => {
|
|
12
13
|
translations[key] = value;
|
|
13
14
|
};
|
|
15
|
+
//# sourceMappingURL=translations.js.map
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@driveflux/time",
|
|
3
|
-
"version": "6.0.
|
|
3
|
+
"version": "6.0.4",
|
|
4
4
|
"type": "module",
|
|
5
5
|
"exports": {
|
|
6
6
|
"./package.json": "./package.json",
|
|
@@ -17,14 +17,14 @@
|
|
|
17
17
|
"dist"
|
|
18
18
|
],
|
|
19
19
|
"dependencies": {
|
|
20
|
-
"@driveflux/singleton": "3.0.
|
|
21
|
-
"date-fns": "^4.1
|
|
20
|
+
"@driveflux/singleton": "3.0.2",
|
|
21
|
+
"date-fns": "^4.2.1",
|
|
22
22
|
"date-fns-tz": "^3.2.0"
|
|
23
23
|
},
|
|
24
24
|
"devDependencies": {
|
|
25
|
-
"@driveflux/fab": "4.0.
|
|
26
|
-
"@driveflux/tsconfig": "3.0.
|
|
27
|
-
"@types/node": "^25.
|
|
25
|
+
"@driveflux/fab": "4.0.2",
|
|
26
|
+
"@driveflux/tsconfig": "3.0.2",
|
|
27
|
+
"@types/node": "^25.9.1",
|
|
28
28
|
"del-cli": "^7.0.0",
|
|
29
29
|
"typescript": "^6.0.3"
|
|
30
30
|
},
|