From 62be0b6507c46e4ee726ed327544ed78eeac4a4e Mon Sep 17 00:00:00 2001 From: Mitsunee <19474909+Mitsunee@users.noreply.github.com> Date: Sat, 23 Nov 2024 01:58:52 +0100 Subject: [PATCH] add: %ampm% variable --- src/formatTime.ts | 7 +++++++ tests/formatTime.test.ts | 18 +++++++++++++++++- the-plan.md | 36 +++++++++++++++++------------------- 3 files changed, 41 insertions(+), 20 deletions(-) diff --git a/src/formatTime.ts b/src/formatTime.ts index fb5b71f..28805f9 100644 --- a/src/formatTime.ts +++ b/src/formatTime.ts @@ -130,6 +130,13 @@ export function formatTime( case "%time%": return `${fmt("%#hour%")}:${fmt("%#min%")}:${fmt("%#sec%")}`; + case "%ampm%": { + const hour = time[utc ? "getUTCHours" : "getHours"](); + const out = hour >= 12 ? "pm" : "am"; + if (str == "%AMPM%") return out.toUpperCase(); + return out; + } + default: return str; } diff --git a/tests/formatTime.test.ts b/tests/formatTime.test.ts index b7d5708..adb1629 100644 --- a/tests/formatTime.test.ts +++ b/tests/formatTime.test.ts @@ -61,7 +61,7 @@ test("UTC vs local time", () => { }); test("other variables", () => { - const otherDate = new Date(`2024-12-25T00:17:32.123${localTz}`); + const otherDate = new Date(`2024-12-25T12:17:32.023${localTz}`); assert.is( formatTime("%month_short%", sampleDate, false, 24), "Nov", @@ -94,6 +94,22 @@ test("other variables", () => { "Wed", "test %day% on other time" ); + + assert.is( + formatTime("%ampm%", sampleDate, false, 12), + "am", + "test %ampm% with sample time (am)" + ); + assert.is( + formatTime("%ampm%", otherDate, false, 12), + "pm", + "test %ampm% with other time (pm)" + ); + assert.is( + formatTime("%AMPM%", otherDate, false, 12), + "PM", + "test %AMPM% with other time (PM, capitalized)" + ); }); test.run(); diff --git a/the-plan.md b/the-plan.md index 8984807..ef58618 100644 --- a/the-plan.md +++ b/the-plan.md @@ -52,25 +52,23 @@ Environment Variables can be used to override settings passed to `createLogger`: Replaces variables [word here] by being surrounded by `%` characters such as `%hours%`. Padding may be available for some variables using `#`. -| var | Description | Padding | Aliases | -| :---------------- | :--------------------------------------------------- | :------------------------------ | :-------------------------- | -| `"%year%"` | Current year | `%#year%` padded to 4 digits | -| `"%month%"` | Current month as number | `%#month%` padded to 2 digits | -| `"%date%"` | Current day of month as number | `"%#date%"` paddded to 2 digits | -| `"%hour%"` | Current hour | `"%#hour%"` padded to 2 digits | `hours` | -| `"%min%"` | Current minute | `"%#min%"` padded to 2 digits | `mins`, `minute`, `minutes` | -| `"%sec%"` | Current second | `"%#sec%"` padded to 2 digits | `second`, `seconds` | -| `"%day%"` | Current weekday as string such as `"Wed"` | -| `"%month_short%"` | Current month as string such as `"Dec"` | -| `"%month_long%"` | Current month as string such as `"December"` | | `month_full` | -| `"%date_ord%": ` | Current day of the month as string such as `"25th"` | -| `"%iso%"` | Current iso date as string such as `"2024-12-25"` | | `iso_short` | -| `"%iso_full%"` | Full ISO string such as `"2024-12-25T18:06:12.889Z"` | | `iso_long` | -| `"%time%"` | Alias of `"%#hour%:%#min%:%#sec"` \* | - -Ideas for new vars: milliseconds, AMPM - -\* this will need AMPM for 12hr time (not implemented yet) +| var | Description | Padding/Capitalization | Aliases | +| :---------------- | :-------------------------------------------------- | :------------------------------------- | :---------------------------- | +| `"%year%"` | Current year | `%#year%` padded to 4 digits | +| `"%month%"` | Current month as number | `%#month%` padded to 2 digits | +| `"%date%"` | Current day of month as number | `"%#date%"` paddded to 2 digits | +| `"%hour%"` | Current hour | `"%#hour%"` padded to 2 digits | `hours` | +| `"%min%"` | Current minute | `"%#min%"` padded to 2 digits | `mins`, `minute`, `minutes` | +| `"%sec%"` | Current second | `"%#sec%"` padded to 2 digits | `second`, `seconds` | +| `"%ms%"` | Current millisecond | `%#ms%` padded to 3 digits | `millisecond`, `milliseconds` | +| `"%day%"` | Current weekday as string such as`"Wed"` | +| `"%month_short%"` | Current month as string such as`"Dec"` | +| `"%month_long%"` | Current month as string such as`"December"` | | `month_full` | +| `"%date_ord%": ` | Current day of the month as string such as`"25th"` | +| `"%iso%"` | Current iso date as string such as`"2024-12-25"` | | `iso_short` | +| `"%iso_full%"` | Full ISO string such as`"2024-12-25T18:06:12.889Z"` | | `iso_long` | +| `"%time%"` | Alias of`"%#hour%:%#min%:%#sec"` | +| `"%ampm%"` | Current day period (i.e.`"am"` or `"pm"`). | Can be capitalized by using `"%AMPM%"` | Note: Some usecases may extend the formatter with additional variables such as `%name%` in Log Level Prefixes.