@uxf/localize 11.78.0 → 11.80.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/README.md +1 -0
- package/package.json +1 -1
- package/src/date/index.d.ts +1 -0
- package/src/date/index.js +16 -2
- package/src/format-datetime/format-datetime.js +2 -1
- package/src/index.test.js +30 -0
package/README.md
CHANGED
package/package.json
CHANGED
package/src/date/index.d.ts
CHANGED
|
@@ -1,5 +1,6 @@
|
|
|
1
1
|
import dayjs, { ConfigType, Dayjs, OptionType } from "dayjs";
|
|
2
2
|
import { TimeZone } from "../utils/time-zone";
|
|
3
|
+
export declare function resolveDateValue(date?: ConfigType, tz?: TimeZone): Date | dayjs.Dayjs;
|
|
3
4
|
export declare function dayjsTz(date?: ConfigType, format?: OptionType, strict?: boolean, tz?: TimeZone): Dayjs;
|
|
4
5
|
export declare const nowDayjs: (tz?: TimeZone) => dayjs.Dayjs;
|
|
5
6
|
export declare const now: (tz?: TimeZone) => Date;
|
package/src/date/index.js
CHANGED
|
@@ -27,13 +27,27 @@ var __importDefault = (this && this.__importDefault) || function (mod) {
|
|
|
27
27
|
};
|
|
28
28
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
29
29
|
exports.today = exports.todayDayjs = exports.now = exports.nowDayjs = void 0;
|
|
30
|
+
exports.resolveDateValue = resolveDateValue;
|
|
30
31
|
exports.dayjsTz = dayjsTz;
|
|
31
32
|
const dayjs_1 = __importStar(require("dayjs"));
|
|
32
33
|
const timezone_1 = __importDefault(require("dayjs/plugin/timezone"));
|
|
33
34
|
(0, dayjs_1.extend)(timezone_1.default);
|
|
35
|
+
/*
|
|
36
|
+
- If `value` is already a JS Date, keep it (it represents a concrete instant).
|
|
37
|
+
- Else if it's a date-only string (length 10, e.g. "YYYY-MM-DD"), parse it as midnight in actualTimezone `tz` (avoids unintended shifts).
|
|
38
|
+
- Otherwise (datetime strings, possibly with Z/offset), let Day.js parse the string as given; explicit offsets/Z will be honored.
|
|
39
|
+
*/
|
|
40
|
+
function resolveDateValue(date, tz = "Europe/Prague") {
|
|
41
|
+
return date instanceof Date
|
|
42
|
+
? date
|
|
43
|
+
: typeof date === "string" && date.length <= 10
|
|
44
|
+
? dayjs_1.default.tz(date, tz)
|
|
45
|
+
: (0, dayjs_1.default)(date);
|
|
46
|
+
}
|
|
34
47
|
function dayjsTz(date, format, strict, tz = "Europe/Prague") {
|
|
35
|
-
const
|
|
36
|
-
|
|
48
|
+
const dateValue = resolveDateValue(date, tz);
|
|
49
|
+
const convertedDate = dayjs_1.default.tz(dateValue, tz);
|
|
50
|
+
return (0, dayjs_1.default)(convertedDate, format, strict);
|
|
37
51
|
}
|
|
38
52
|
const nowDayjs = (tz) => dayjsTz(undefined, undefined, undefined, tz);
|
|
39
53
|
exports.nowDayjs = nowDayjs;
|
|
@@ -33,6 +33,7 @@ const timezone_1 = __importDefault(require("dayjs/plugin/timezone"));
|
|
|
33
33
|
const utc_1 = __importDefault(require("dayjs/plugin/utc"));
|
|
34
34
|
const react_1 = require("react");
|
|
35
35
|
const context_1 = require("../context/context");
|
|
36
|
+
const date_1 = require("../date");
|
|
36
37
|
const curry_1 = require("../utils/curry");
|
|
37
38
|
(0, dayjs_1.extend)(utc_1.default);
|
|
38
39
|
(0, dayjs_1.extend)(timezone_1.default);
|
|
@@ -40,7 +41,7 @@ function createFormatDatetime(localizeConfigs) {
|
|
|
40
41
|
return (locale, value, format, timeZone) => {
|
|
41
42
|
const localeDateTimes = localizeConfigs[locale].dateTime;
|
|
42
43
|
const actualTimezone = timeZone !== null && timeZone !== void 0 ? timeZone : "Europe/Prague";
|
|
43
|
-
const dateValue =
|
|
44
|
+
const dateValue = (0, date_1.resolveDateValue)(value, actualTimezone);
|
|
44
45
|
return dayjs_1.default.tz(dateValue, actualTimezone).format(localeDateTimes[format]);
|
|
45
46
|
};
|
|
46
47
|
}
|
package/src/index.test.js
CHANGED
|
@@ -5,6 +5,7 @@ var __importDefault = (this && this.__importDefault) || function (mod) {
|
|
|
5
5
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
6
6
|
const cs_1 = __importDefault(require("../locale/cs"));
|
|
7
7
|
const en_1 = __importDefault(require("../locale/en"));
|
|
8
|
+
const date_1 = require("./date");
|
|
8
9
|
const index_1 = require("./index");
|
|
9
10
|
const date = new Date("2023-07-21T07:58:35");
|
|
10
11
|
describe("basic createLocalize", function () {
|
|
@@ -19,3 +20,32 @@ describe("basic createLocalize", function () {
|
|
|
19
20
|
expect(formatDateTime("cs", date, "custom")).toBe("Fr 21. 07.");
|
|
20
21
|
});
|
|
21
22
|
});
|
|
23
|
+
describe("dayjsTz", function () {
|
|
24
|
+
test('parses "2025-08-20" (date-only) as 0:00 in Europe/Prague', () => {
|
|
25
|
+
expect((0, date_1.dayjsTz)("2025-08-20").format("H:mm")).toBe("0:00");
|
|
26
|
+
});
|
|
27
|
+
test('parses "2025/08/20" (slash date) as 00:00 in Europe/Prague', () => {
|
|
28
|
+
expect((0, date_1.dayjsTz)("2025/08/20").format("H:mm")).toBe("0:00");
|
|
29
|
+
});
|
|
30
|
+
test('parses "2025-08-20T11:30:00Z" (UTC) as 13:30', () => {
|
|
31
|
+
expect((0, date_1.dayjsTz)("2025-08-20T11:30:00Z").format("H:mm")).toBe("13:30");
|
|
32
|
+
});
|
|
33
|
+
test('parses "2025-08-20T11:30Z" (UTC short) as 13:30', () => {
|
|
34
|
+
expect((0, date_1.dayjsTz)("2025-08-20T11:30Z").format("H:mm")).toBe("13:30");
|
|
35
|
+
});
|
|
36
|
+
test('parses "2025-08-20T11:30:00+02:00" (offset with colon) as 11:30', () => {
|
|
37
|
+
expect((0, date_1.dayjsTz)("2025-08-20T11:30:00+02:00").format("H:mm")).toBe("11:30");
|
|
38
|
+
});
|
|
39
|
+
test('parses "2025-08-20T11:30:00+0200" (offset no colon) as 11:30', () => {
|
|
40
|
+
expect((0, date_1.dayjsTz)("2025-08-20T11:30:00+0200").format("H:mm")).toBe("11:30");
|
|
41
|
+
});
|
|
42
|
+
test('parses "2025-08-20T11:30:00-05:00" (negative offset) as 18:30', () => {
|
|
43
|
+
expect((0, date_1.dayjsTz)("2025-08-20T11:30:00-05:00").format("H:mm")).toBe("18:30");
|
|
44
|
+
});
|
|
45
|
+
test("parses new Date('2025-08-20T11:30:00+02:00') as 11:30", () => {
|
|
46
|
+
expect((0, date_1.dayjsTz)(new Date("2025-08-20T11:30:00+02:00")).format("H:mm")).toBe("11:30");
|
|
47
|
+
});
|
|
48
|
+
test("parses new Date('2025-08-20T11:30:00Z') as 13:30", () => {
|
|
49
|
+
expect((0, date_1.dayjsTz)(new Date("2025-08-20T11:30:00Z")).format("H:mm")).toBe("13:30");
|
|
50
|
+
});
|
|
51
|
+
});
|