Skip to content

Commit

Permalink
add: %ampm% variable
Browse files Browse the repository at this point in the history
  • Loading branch information
Mitsunee committed Nov 23, 2024
1 parent c5eb2f8 commit 62be0b6
Show file tree
Hide file tree
Showing 3 changed files with 41 additions and 20 deletions.
7 changes: 7 additions & 0 deletions src/formatTime.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}
Expand Down
18 changes: 17 additions & 1 deletion tests/formatTime.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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",
Expand Down Expand Up @@ -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();
36 changes: 17 additions & 19 deletions the-plan.md
Original file line number Diff line number Diff line change
Expand Up @@ -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.

Expand Down

0 comments on commit 62be0b6

Please sign in to comment.