@gobrand/tiempo 2.6.3 → 2.7.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/dist/chunk-YQBM63DC.js +57 -0
- package/dist/chunk-YQBM63DC.js.map +1 -0
- package/dist/index.d.ts +1 -1
- package/dist/index.d.ts.map +1 -1
- package/dist/index.js +1 -1
- package/dist/simpleFormat.d.ts +48 -37
- package/dist/simpleFormat.d.ts.map +1 -1
- package/dist/simpleFormat.js +1 -1
- package/package.json +1 -1
- package/dist/chunk-TFSZ55L7.js +0 -46
- package/dist/chunk-TFSZ55L7.js.map +0 -1
|
@@ -0,0 +1,57 @@
|
|
|
1
|
+
// src/simpleFormat.ts
|
|
2
|
+
import { Temporal } from "@js-temporal/polyfill";
|
|
3
|
+
function simpleFormat(input, options) {
|
|
4
|
+
const { locale = "en-US", timeZone } = options;
|
|
5
|
+
const dateFormat = "date" in options ? options.date : void 0;
|
|
6
|
+
const timeFormat = "time" in options ? options.time : void 0;
|
|
7
|
+
let zdt;
|
|
8
|
+
if (input instanceof Temporal.Instant) {
|
|
9
|
+
const tz = timeZone ?? "UTC";
|
|
10
|
+
zdt = input.toZonedDateTimeISO(tz);
|
|
11
|
+
} else if (input instanceof Temporal.ZonedDateTime) {
|
|
12
|
+
if (timeZone) {
|
|
13
|
+
zdt = input.toInstant().toZonedDateTimeISO(timeZone);
|
|
14
|
+
} else {
|
|
15
|
+
zdt = input;
|
|
16
|
+
}
|
|
17
|
+
} else {
|
|
18
|
+
zdt = input.toZonedDateTime({ timeZone: timeZone ?? "UTC", plainTime: new Temporal.PlainTime() });
|
|
19
|
+
}
|
|
20
|
+
const parts = [];
|
|
21
|
+
if (dateFormat) {
|
|
22
|
+
const currentYear = Temporal.Now.plainDateISO().year;
|
|
23
|
+
const showYear = dateFormat === "full" ? true : dateFormat === "compact" ? false : zdt.year !== currentYear;
|
|
24
|
+
const dateOptions = {
|
|
25
|
+
day: "numeric",
|
|
26
|
+
month: "short",
|
|
27
|
+
year: showYear ? "numeric" : void 0
|
|
28
|
+
};
|
|
29
|
+
parts.push(zdt.toLocaleString(locale, dateOptions));
|
|
30
|
+
}
|
|
31
|
+
if (timeFormat) {
|
|
32
|
+
if (timeFormat === "compact") {
|
|
33
|
+
parts.push(formatCompactTime(zdt));
|
|
34
|
+
} else {
|
|
35
|
+
const timeOptions = {
|
|
36
|
+
hour: "numeric",
|
|
37
|
+
minute: "2-digit",
|
|
38
|
+
hour12: timeFormat === "12h"
|
|
39
|
+
};
|
|
40
|
+
parts.push(zdt.toLocaleString(locale, timeOptions));
|
|
41
|
+
}
|
|
42
|
+
}
|
|
43
|
+
return parts.join(", ");
|
|
44
|
+
}
|
|
45
|
+
function formatCompactTime(zdt) {
|
|
46
|
+
const hour12 = zdt.hour % 12 || 12;
|
|
47
|
+
const ampm = zdt.hour < 12 ? "am" : "pm";
|
|
48
|
+
if (zdt.minute === 0) {
|
|
49
|
+
return `${hour12}${ampm}`;
|
|
50
|
+
}
|
|
51
|
+
return `${hour12}:${zdt.minute.toString().padStart(2, "0")}${ampm}`;
|
|
52
|
+
}
|
|
53
|
+
|
|
54
|
+
export {
|
|
55
|
+
simpleFormat
|
|
56
|
+
};
|
|
57
|
+
//# sourceMappingURL=chunk-YQBM63DC.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"sources":["../src/simpleFormat.ts"],"sourcesContent":["import { Temporal } from '@js-temporal/polyfill';\nimport type { Timezone } from './types';\n\ninterface BaseOptions {\n locale?: string;\n timeZone?: Timezone;\n}\n\ninterface DateOnlyOptions extends BaseOptions {\n date: 'compact' | 'auto' | 'full';\n time?: never;\n}\n\ninterface TimeOnlyOptions extends BaseOptions {\n date?: never;\n time: '12h' | '24h' | 'compact';\n}\n\ninterface DateAndTimeOptions extends BaseOptions {\n date: 'compact' | 'auto' | 'full';\n time: '12h' | '24h' | 'compact';\n}\n\nexport type SimpleFormatOptions = DateOnlyOptions | TimeOnlyOptions | DateAndTimeOptions;\n\n/**\n * Format a Temporal date/time in a human-friendly way.\n *\n * Pass `date`, `time`, or both to control what's displayed.\n * At least one of `date` or `time` is required.\n *\n * @param input - A Temporal.Instant, ZonedDateTime, or PlainDate\n * @param options - Format options (at least `date` or `time` required)\n *\n * Date formats:\n * - `'compact'` - \"Dec 23\" (never shows year)\n * - `'auto'` - \"Dec 23\" or \"Dec 23, 2020\" (shows year only if not current year)\n * - `'full'` - \"Dec 23, 2026\" (always shows year)\n *\n * Time formats:\n * - `'12h'` - \"3:30 PM\"\n * - `'24h'` - \"15:30\"\n * - `'compact'` - \"9am\" or \"2:30pm\" (omits minutes when zero, lowercase am/pm)\n *\n * @example\n * ```typescript\n * const zdt = Temporal.ZonedDateTime.from(\"2026-12-23T09:00:00[America/New_York]\");\n * const pastZdt = Temporal.ZonedDateTime.from(\"2020-12-23T14:30:00[America/New_York]\");\n *\n * // Date only\n * simpleFormat(zdt, { date: 'compact' }); // \"Dec 23\"\n * simpleFormat(zdt, { date: 'auto' }); // \"Dec 23\"\n * simpleFormat(pastZdt, { date: 'auto' }); // \"Dec 23, 2020\"\n * simpleFormat(zdt, { date: 'full' }); // \"Dec 23, 2026\"\n *\n * // Time only\n * simpleFormat(zdt, { time: 'compact' }); // \"9am\"\n * simpleFormat(pastZdt, { time: 'compact' }); // \"2:30pm\"\n * simpleFormat(zdt, { time: '12h' }); // \"9:00 AM\"\n * simpleFormat(zdt, { time: '24h' }); // \"09:00\"\n *\n * // Date and time\n * simpleFormat(zdt, { date: 'auto', time: 'compact' }); // \"Dec 23, 9am\"\n * simpleFormat(zdt, { date: 'full', time: '12h' }); // \"Dec 23, 2026, 9:00 AM\"\n *\n * // With Instant (timeZone required)\n * const instant = Temporal.Instant.from(\"2026-12-23T14:00:00Z\");\n * simpleFormat(instant, { date: 'auto', timeZone: 'America/New_York' }); // \"Dec 23\"\n * ```\n */\nexport function simpleFormat(\n input: Temporal.PlainDate | Temporal.ZonedDateTime | Temporal.Instant,\n options: SimpleFormatOptions\n): string {\n const { locale = 'en-US', timeZone } = options;\n const dateFormat = 'date' in options ? options.date : undefined;\n const timeFormat = 'time' in options ? options.time : undefined;\n\n // Convert input to ZonedDateTime for consistent handling\n let zdt: Temporal.ZonedDateTime;\n\n if (input instanceof Temporal.Instant) {\n const tz = timeZone ?? 'UTC';\n zdt = input.toZonedDateTimeISO(tz);\n } else if (input instanceof Temporal.ZonedDateTime) {\n if (timeZone) {\n zdt = input.toInstant().toZonedDateTimeISO(timeZone);\n } else {\n zdt = input;\n }\n } else {\n // PlainDate - convert to ZonedDateTime at midnight UTC for formatting\n zdt = input.toZonedDateTime({ timeZone: timeZone ?? 'UTC', plainTime: new Temporal.PlainTime() });\n }\n\n const parts: string[] = [];\n\n // Format date part\n if (dateFormat) {\n const currentYear = Temporal.Now.plainDateISO().year;\n const showYear =\n dateFormat === 'full' ? true : dateFormat === 'compact' ? false : zdt.year !== currentYear;\n\n const dateOptions: Intl.DateTimeFormatOptions = {\n day: 'numeric',\n month: 'short',\n year: showYear ? 'numeric' : undefined,\n };\n\n parts.push(zdt.toLocaleString(locale, dateOptions));\n }\n\n // Format time part\n if (timeFormat) {\n if (timeFormat === 'compact') {\n parts.push(formatCompactTime(zdt));\n } else {\n const timeOptions: Intl.DateTimeFormatOptions = {\n hour: 'numeric',\n minute: '2-digit',\n hour12: timeFormat === '12h',\n };\n parts.push(zdt.toLocaleString(locale, timeOptions));\n }\n }\n\n return parts.join(', ');\n}\n\n/**\n * Format time in compact style: \"9am\" or \"2:30pm\"\n * Omits minutes when they're zero for cleaner display.\n */\nfunction formatCompactTime(zdt: Temporal.ZonedDateTime): string {\n const hour12 = zdt.hour % 12 || 12;\n const ampm = zdt.hour < 12 ? 'am' : 'pm';\n\n if (zdt.minute === 0) {\n return `${hour12}${ampm}`;\n }\n return `${hour12}:${zdt.minute.toString().padStart(2, '0')}${ampm}`;\n}\n"],"mappings":";AAAA,SAAS,gBAAgB;AAsElB,SAAS,aACd,OACA,SACQ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GAAG;AACpB,WAAO,GAAG,MAAM,GAAG,IAAI;AAAA,EACzB;AACA,SAAO,GAAG,MAAM,IAAI,IAAI,OAAO,SAAS,EAAE,SAAS,GAAG,GAAG,CAAC,GAAG,IAAI;AACnE;","names":[]}
|
package/dist/index.d.ts
CHANGED
|
@@ -9,7 +9,7 @@ export { toIso9075, type ToIso9075Options, type Iso9075Representation, type Iso9
|
|
|
9
9
|
export { toDate } from './toDate';
|
|
10
10
|
export { format, type FormatOptions } from './format';
|
|
11
11
|
export { formatPlainDate, type FormatPlainDateOptions } from './formatPlainDate';
|
|
12
|
-
export { simpleFormat } from './simpleFormat';
|
|
12
|
+
export { simpleFormat, type SimpleFormatOptions } from './simpleFormat';
|
|
13
13
|
export { today } from './today';
|
|
14
14
|
export { now } from './now';
|
|
15
15
|
export { startOfDay } from './startOfDay';
|
package/dist/index.d.ts.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../src/index.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,WAAW,EAAE,MAAM,eAAe,CAAC;AAC5C,OAAO,EAAE,WAAW,EAAE,MAAM,eAAe,CAAC;AAC5C,OAAO,EAAE,WAAW,EAAE,MAAM,eAAe,CAAC;AAC5C,YAAY,EAAE,QAAQ,EAAE,YAAY,EAAE,MAAM,SAAS,CAAC;AACtD,OAAO,EAAE,eAAe,EAAE,MAAM,mBAAmB,CAAC;AACpD,OAAO,EAAE,KAAK,EAAE,MAAM,SAAS,CAAC;AAChC,OAAO,EAAE,KAAK,EAAE,KAAK,OAAO,EAAE,KAAK,YAAY,EAAE,MAAM,SAAS,CAAC;AACjE,OAAO,EACL,SAAS,EACT,KAAK,gBAAgB,EACrB,KAAK,qBAAqB,EAC1B,KAAK,WAAW,GACjB,MAAM,aAAa,CAAC;AACrB,OAAO,EAAE,MAAM,EAAE,MAAM,UAAU,CAAC;AAClC,OAAO,EAAE,MAAM,EAAE,KAAK,aAAa,EAA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AAO,EAAE,mBAAmB,EAAE,MAAM,uBAAuB,CAAC;AAC5D,OAAO,EAAE,mBAAmB,EAAE,MAAM,uBAAuB,CAAC;AAC5D,OAAO,EAAE,iBAAiB,EAAE,MAAM,qBAAqB,CAAC;AACxD,OAAO,EAAE,gBAAgB,EAAE,MAAM,oBAAoB,CAAC;AACtD,OAAO,EAAE,iBAAiB,EAAE,MAAM,qBAAqB,CAAC;AACxD,OAAO,EAAE,kBAAkB,EAAE,MAAM,sBAAsB,CAAC;AAC1D,OAAO,EAAE,iBAAiB,EAAE,MAAM,qBAAqB,CAAC;AACxD,OAAO,EACL,kBAAkB,EAClB,KAAK,yBAAyB,GAC/B,MAAM,sBAAsB,CAAC;AAC9B,OAAO,EACL,oBAAoB,EACpB,KAAK,2BAA2B,GACjC,MAAM,wBAAwB,CAAC;AAChC,OAAO,EACL,oBAAoB,EACpB,KAAK,2BAA2B,GACjC,MAAM,wBAAwB,CAAC;AAChC,OAAO,EACL,kBAAkB,EAClB,KAAK,yBAAyB,GAC/B,MAAM,sBAAsB,CAAC;AAC9B,OAAO,EAAE,iBAAiB,EAAE,MAAM,qBAAqB,CAAC;AACxD,OAAO,EAAE,gBAAgB,EAAE,MAAM,oBAAoB,CAAC;AACtD,OAAO,EAAE,gBAAgB,EAAE,MAAM,oBAAoB,CAAC;AACtD,OAAO,EAAE,iBAAiB,EAAE,MAAM,qBAAqB,CAAC;AACxD,OAAO,EAAE,gBAAgB,EAAE,MAAM,oBAAoB,CAAC;AACtD,OAAO,EAAE,gBAAgB,EAAE,MAAM,oBAAoB,CAAC"}
|
package/dist/index.js
CHANGED
package/dist/simpleFormat.d.ts
CHANGED
|
@@ -1,56 +1,67 @@
|
|
|
1
1
|
import { Temporal } from '@js-temporal/polyfill';
|
|
2
2
|
import type { Timezone } from './types';
|
|
3
|
-
interface
|
|
3
|
+
interface BaseOptions {
|
|
4
4
|
locale?: string;
|
|
5
|
-
year?: 'auto' | 'always' | 'never';
|
|
6
|
-
}
|
|
7
|
-
interface ZonedDateTimeOptions {
|
|
8
|
-
locale?: string;
|
|
9
|
-
time?: '12h' | '24h';
|
|
10
5
|
timeZone?: Timezone;
|
|
11
|
-
year?: 'auto' | 'always' | 'never';
|
|
12
6
|
}
|
|
13
|
-
interface
|
|
14
|
-
|
|
15
|
-
time?:
|
|
16
|
-
|
|
17
|
-
|
|
7
|
+
interface DateOnlyOptions extends BaseOptions {
|
|
8
|
+
date: 'compact' | 'auto' | 'full';
|
|
9
|
+
time?: never;
|
|
10
|
+
}
|
|
11
|
+
interface TimeOnlyOptions extends BaseOptions {
|
|
12
|
+
date?: never;
|
|
13
|
+
time: '12h' | '24h' | 'compact';
|
|
14
|
+
}
|
|
15
|
+
interface DateAndTimeOptions extends BaseOptions {
|
|
16
|
+
date: 'compact' | 'auto' | 'full';
|
|
17
|
+
time: '12h' | '24h' | 'compact';
|
|
18
18
|
}
|
|
19
|
+
export type SimpleFormatOptions = DateOnlyOptions | TimeOnlyOptions | DateAndTimeOptions;
|
|
19
20
|
/**
|
|
20
|
-
* Format a Temporal date in a human-friendly way
|
|
21
|
+
* Format a Temporal date/time in a human-friendly way.
|
|
21
22
|
*
|
|
22
|
-
*
|
|
23
|
-
*
|
|
24
|
-
*
|
|
23
|
+
* Pass `date`, `time`, or both to control what's displayed.
|
|
24
|
+
* At least one of `date` or `time` is required.
|
|
25
|
+
*
|
|
26
|
+
* @param input - A Temporal.Instant, ZonedDateTime, or PlainDate
|
|
27
|
+
* @param options - Format options (at least `date` or `time` required)
|
|
28
|
+
*
|
|
29
|
+
* Date formats:
|
|
30
|
+
* - `'compact'` - "Dec 23" (never shows year)
|
|
31
|
+
* - `'auto'` - "Dec 23" or "Dec 23, 2020" (shows year only if not current year)
|
|
32
|
+
* - `'full'` - "Dec 23, 2026" (always shows year)
|
|
33
|
+
*
|
|
34
|
+
* Time formats:
|
|
35
|
+
* - `'12h'` - "3:30 PM"
|
|
36
|
+
* - `'24h'` - "15:30"
|
|
37
|
+
* - `'compact'` - "9am" or "2:30pm" (omits minutes when zero, lowercase am/pm)
|
|
25
38
|
*
|
|
26
39
|
* @example
|
|
27
40
|
* ```typescript
|
|
28
|
-
*
|
|
29
|
-
* const
|
|
30
|
-
* const date2020 = Temporal.ZonedDateTime.from("2020-12-23T15:30:00[America/New_York]");
|
|
41
|
+
* const zdt = Temporal.ZonedDateTime.from("2026-12-23T09:00:00[America/New_York]");
|
|
42
|
+
* const pastZdt = Temporal.ZonedDateTime.from("2020-12-23T14:30:00[America/New_York]");
|
|
31
43
|
*
|
|
32
|
-
*
|
|
33
|
-
* simpleFormat(
|
|
34
|
-
* simpleFormat(
|
|
35
|
-
* simpleFormat(
|
|
44
|
+
* // Date only
|
|
45
|
+
* simpleFormat(zdt, { date: 'compact' }); // "Dec 23"
|
|
46
|
+
* simpleFormat(zdt, { date: 'auto' }); // "Dec 23"
|
|
47
|
+
* simpleFormat(pastZdt, { date: 'auto' }); // "Dec 23, 2020"
|
|
48
|
+
* simpleFormat(zdt, { date: 'full' }); // "Dec 23, 2026"
|
|
36
49
|
*
|
|
37
|
-
* //
|
|
38
|
-
* simpleFormat(
|
|
39
|
-
* simpleFormat(
|
|
40
|
-
* simpleFormat(
|
|
50
|
+
* // Time only
|
|
51
|
+
* simpleFormat(zdt, { time: 'compact' }); // "9am"
|
|
52
|
+
* simpleFormat(pastZdt, { time: 'compact' }); // "2:30pm"
|
|
53
|
+
* simpleFormat(zdt, { time: '12h' }); // "9:00 AM"
|
|
54
|
+
* simpleFormat(zdt, { time: '24h' }); // "09:00"
|
|
41
55
|
*
|
|
42
|
-
* //
|
|
43
|
-
*
|
|
44
|
-
* simpleFormat(
|
|
45
|
-
* simpleFormat(instant, { timeZone: 'America/New_York', time: '12h' }); // "Dec 23, 3:30 PM"
|
|
56
|
+
* // Date and time
|
|
57
|
+
* simpleFormat(zdt, { date: 'auto', time: 'compact' }); // "Dec 23, 9am"
|
|
58
|
+
* simpleFormat(zdt, { date: 'full', time: '12h' }); // "Dec 23, 2026, 9:00 AM"
|
|
46
59
|
*
|
|
47
|
-
* // With
|
|
48
|
-
* const
|
|
49
|
-
* simpleFormat(
|
|
60
|
+
* // With Instant (timeZone required)
|
|
61
|
+
* const instant = Temporal.Instant.from("2026-12-23T14:00:00Z");
|
|
62
|
+
* simpleFormat(instant, { date: 'auto', timeZone: 'America/New_York' }); // "Dec 23"
|
|
50
63
|
* ```
|
|
51
64
|
*/
|
|
52
|
-
export declare function simpleFormat(input: Temporal.PlainDate, options
|
|
53
|
-
export declare function simpleFormat(input: Temporal.ZonedDateTime, options?: ZonedDateTimeOptions): string;
|
|
54
|
-
export declare function simpleFormat(input: Temporal.Instant, options: InstantOptions): string;
|
|
65
|
+
export declare function simpleFormat(input: Temporal.PlainDate | Temporal.ZonedDateTime | Temporal.Instant, options: SimpleFormatOptions): string;
|
|
55
66
|
export {};
|
|
56
67
|
//# sourceMappingURL=simpleFormat.d.ts.map
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"simpleFormat.d.ts","sourceRoot":"","sources":["../src/simpleFormat.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,QAAQ,EAAE,MAAM,uBAAuB,CAAC;AACjD,OAAO,KAAK,EAAE,QAAQ,EAAE,MAAM,SAAS,CAAC;
|
|
1
|
+
{"version":3,"file":"simpleFormat.d.ts","sourceRoot":"","sources":["../src/simpleFormat.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,QAAQ,EAAE,MAAM,uBAAuB,CAAC;AACjD,OAAO,KAAK,EAAE,QAAQ,EAAE,MAAM,SAAS,CAAC;AAExC,UAAU,WAAW;IACnB,MAAM,CAAC,EAAE,MAAM,CAAC;IAChB,QAAQ,CAAC,EAAE,QAAQ,CAAC;CACrB;AAED,UAAU,eAAgB,SAAQ,WAAW;IAC3C,IAAI,EAAE,SAAS,GAAG,MAAM,GAAG,MAAM,CAAC;IAClC,IAAI,CAAC,EAAE,KAAK,CAAC;CACd;AAED,UAAU,eAAgB,SAAQ,WAAW;IAC3C,IAAI,CAAC,EAAE,KAAK,CAAC;IACb,IAAI,EAAE,KAAK,GAAG,KAAK,GAAG,SAAS,CAAC;CACjC;AAED,UAAU,kBAAmB,SAAQ,WAAW;IAC9C,IAAI,EAAE,SAAS,GAAG,MAAM,GAAG,MAAM,CAAC;IAClC,IAAI,EAAE,KAAK,GAAG,KAAK,GAAG,SAAS,CAAC;CACjC;AAED,MAAM,MAAM,mBAAmB,GAAG,eAAe,GAAG,eAAe,GAAG,kBAAkB,CAAC;AAEzF;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;GA4CG;AACH,wBAAgB,YAAY,CAC1B,KAAK,EAAE,QAAQ,CAAC,SAAS,GAAG,QAAQ,CAAC,aAAa,GAAG,QAAQ,CAAC,OAAO,EACrE,OAAO,EAAE,mBAAmB,GAC3B,MAAM,CAsDR"}
|
package/dist/simpleFormat.js
CHANGED
package/package.json
CHANGED
package/dist/chunk-TFSZ55L7.js
DELETED
|
@@ -1,46 +0,0 @@
|
|
|
1
|
-
// src/simpleFormat.ts
|
|
2
|
-
import { Temporal } from "@js-temporal/polyfill";
|
|
3
|
-
function simpleFormat(input, options = {}) {
|
|
4
|
-
const locale = options.locale ?? "en-US";
|
|
5
|
-
const time = "time" in options ? options.time : void 0;
|
|
6
|
-
const timeZone = "timeZone" in options ? options.timeZone : void 0;
|
|
7
|
-
const yearOption = "year" in options ? options.year : void 0;
|
|
8
|
-
let year;
|
|
9
|
-
let dateTimeForFormat;
|
|
10
|
-
if (input instanceof Temporal.Instant) {
|
|
11
|
-
const tz = timeZone ?? "UTC";
|
|
12
|
-
const zoned = input.toZonedDateTimeISO(tz);
|
|
13
|
-
year = zoned.year;
|
|
14
|
-
dateTimeForFormat = zoned;
|
|
15
|
-
} else if (input instanceof Temporal.ZonedDateTime) {
|
|
16
|
-
if (timeZone) {
|
|
17
|
-
const zoned = input.toInstant().toZonedDateTimeISO(timeZone);
|
|
18
|
-
year = zoned.year;
|
|
19
|
-
dateTimeForFormat = zoned;
|
|
20
|
-
} else {
|
|
21
|
-
year = input.year;
|
|
22
|
-
dateTimeForFormat = input;
|
|
23
|
-
}
|
|
24
|
-
} else {
|
|
25
|
-
year = input.year;
|
|
26
|
-
dateTimeForFormat = input;
|
|
27
|
-
}
|
|
28
|
-
const currentYear = Temporal.Now.plainDateISO().year;
|
|
29
|
-
const showYear = yearOption === "always" ? true : yearOption === "never" ? false : year !== currentYear;
|
|
30
|
-
const dateOptions = {
|
|
31
|
-
day: "numeric",
|
|
32
|
-
month: "short",
|
|
33
|
-
year: showYear ? "numeric" : void 0
|
|
34
|
-
};
|
|
35
|
-
if (time && !(input instanceof Temporal.PlainDate)) {
|
|
36
|
-
dateOptions.hour = "numeric";
|
|
37
|
-
dateOptions.minute = "2-digit";
|
|
38
|
-
dateOptions.hour12 = time === "12h";
|
|
39
|
-
}
|
|
40
|
-
return dateTimeForFormat.toLocaleString(locale, dateOptions);
|
|
41
|
-
}
|
|
42
|
-
|
|
43
|
-
export {
|
|
44
|
-
simpleFormat
|
|
45
|
-
};
|
|
46
|
-
//# sourceMappingURL=chunk-TFSZ55L7.js.map
|
|
@@ -1 +0,0 @@
|
|
|
1
|
-
{"version":3,"sources":["../src/simpleFormat.ts"],"sourcesContent":["import { Temporal } from '@js-temporal/polyfill';\nimport type { Timezone } from './types';\n\n// simpleFormat options - discriminated by input type\ninterface PlainDateOptions {\n locale?: string;\n year?: 'auto' | 'always' | 'never';\n}\n\ninterface ZonedDateTimeOptions {\n locale?: string;\n time?: '12h' | '24h';\n timeZone?: Timezone;\n year?: 'auto' | 'always' | 'never';\n}\n\ninterface InstantOptions {\n locale?: string;\n time?: '12h' | '24h';\n timeZone: Timezone; // required for Instant\n year?: 'auto' | 'always' | 'never';\n}\n\n/**\n * Format a Temporal date in a human-friendly way: \"Dec 23\" or \"Dec 23, 2020\"\n *\n * By default (year: 'auto'), shows the year only if the date is not in the current year.\n * Use year: 'always' to always show the year, or year: 'never' to always hide it.\n * Optionally includes time in 12-hour or 24-hour format.\n *\n * @example\n * ```typescript\n * // Assuming current year is 2026\n * const date2026 = Temporal.ZonedDateTime.from(\"2026-12-23T15:30:00[America/New_York]\");\n * const date2020 = Temporal.ZonedDateTime.from(\"2020-12-23T15:30:00[America/New_York]\");\n *\n * simpleFormat(date2026); // \"Dec 23\"\n * simpleFormat(date2020); // \"Dec 23, 2020\"\n * simpleFormat(date2026, { time: '12h' }); // \"Dec 23, 3:30 PM\"\n * simpleFormat(date2026, { time: '24h' }); // \"Dec 23, 15:30\"\n *\n * // Control year display\n * simpleFormat(date2026, { year: 'always' }); // \"Dec 23, 2026\"\n * simpleFormat(date2020, { year: 'never' }); // \"Dec 23\"\n * simpleFormat(date2020, { year: 'auto' }); // \"Dec 23, 2020\" (default behavior)\n *\n * // With Instant (timeZone required)\n * const instant = Temporal.Instant.from(\"2026-12-23T20:30:00Z\");\n * simpleFormat(instant, { timeZone: 'America/New_York' }); // \"Dec 23\"\n * simpleFormat(instant, { timeZone: 'America/New_York', time: '12h' }); // \"Dec 23, 3:30 PM\"\n *\n * // With PlainDate (no time option)\n * const plain = Temporal.PlainDate.from(\"2020-12-23\");\n * simpleFormat(plain); // \"Dec 23, 2020\"\n * ```\n */\nexport function simpleFormat(input: Temporal.PlainDate, options?: PlainDateOptions): string;\nexport function simpleFormat(input: Temporal.ZonedDateTime, options?: ZonedDateTimeOptions): string;\nexport function simpleFormat(input: Temporal.Instant, options: InstantOptions): string;\nexport function simpleFormat(\n input: Temporal.PlainDate | Temporal.ZonedDateTime | Temporal.Instant,\n options: PlainDateOptions | ZonedDateTimeOptions | InstantOptions = {}\n): string {\n const locale = options.locale ?? 'en-US';\n const time = 'time' in options ? options.time : undefined;\n const timeZone = 'timeZone' in options ? options.timeZone : undefined;\n const yearOption = 'year' in options ? options.year : undefined;\n\n // Get year from the input (converting Instant to ZonedDateTime if needed)\n let year: number;\n let dateTimeForFormat: Temporal.PlainDate | Temporal.ZonedDateTime;\n\n if (input instanceof Temporal.Instant) {\n const tz = timeZone ?? 'UTC';\n const zoned = input.toZonedDateTimeISO(tz);\n year = zoned.year;\n dateTimeForFormat = zoned;\n } else if (input instanceof Temporal.ZonedDateTime) {\n if (timeZone) {\n const zoned = input.toInstant().toZonedDateTimeISO(timeZone);\n year = zoned.year;\n dateTimeForFormat = zoned;\n } else {\n year = input.year;\n dateTimeForFormat = input;\n }\n } else {\n year = input.year;\n dateTimeForFormat = input;\n }\n\n // Determine if year should be shown\n const currentYear = Temporal.Now.plainDateISO().year;\n const showYear =\n yearOption === 'always' ? true : yearOption === 'never' ? false : year !== currentYear;\n\n const dateOptions: Intl.DateTimeFormatOptions = {\n day: 'numeric',\n month: 'short',\n year: showYear ? 'numeric' : undefined,\n };\n\n // Add time options if requested and input supports it\n if (time && !(input instanceof Temporal.PlainDate)) {\n dateOptions.hour = 'numeric';\n dateOptions.minute = '2-digit';\n dateOptions.hour12 = time === '12h';\n }\n\n return dateTimeForFormat.toLocaleString(locale, dateOptions);\n}\n"],"mappings":";AAAA,SAAS,gBAAgB;AA2DlB,SAAS,aACd,OACA,UAAoE,CAAC,GAC7D;AACR,QAAM,SAAS,QAAQ,UAAU;AACjC,QAAM,OAAO,UAAU,UAAU,QAAQ,OAAO;AAChD,QAAM,WAAW,cAAc,UAAU,QAAQ,WAAW;AAC5D,QAAM,aAAa,UAAU,UAAU,QAAQ,OAAO;AAGtD,MAAI;AACJ,MAAI;AAEJ,MAAI,iBAAiB,SAAS,SAAS;AACrC,UAAM,KAAK,YAAY;AACvB,UAAM,QAAQ,MAAM,mBAAmB,EAAE;AACzC,WAAO,MAAM;AACb,wBAAoB;AAAA,EACtB,WAAW,iBAAiB,SAAS,eAAe;AAClD,QAAI,UAAU;AACZ,YAAM,QAAQ,MAAM,UAAU,EAAE,mBAAmB,QAAQ;AAC3D,aAAO,MAAM;AACb,0BAAoB;AAAA,IACtB,OAAO;AACL,aAAO,MAAM;AACb,0BAAoB;AAAA,IACtB;AAAA,EACF,OAAO;AACL,WAAO,MAAM;AACb,wBAAoB;AAAA,EACtB;AAGA,QAAM,cAAc,SAAS,IAAI,aAAa,EAAE;AAChD,QAAM,WACJ,eAAe,WAAW,OAAO,eAAe,UAAU,QAAQ,SAAS;AAE7E,QAAM,cAA0C;AAAA,IAC9C,KAAK;AAAA,IACL,OAAO;AAAA,IACP,MAAM,WAAW,YAAY;AAAA,EAC/B;AAGA,MAAI,QAAQ,EAAE,iBAAiB,SAAS,YAAY;AAClD,gBAAY,OAAO;AACnB,gBAAY,SAAS;AACrB,gBAAY,SAAS,SAAS;AAAA,EAChC;AAEA,SAAO,kBAAkB,eAAe,QAAQ,WAAW;AAC7D;","names":[]}
|