-
Notifications
You must be signed in to change notification settings - Fork 0
standalone format #19
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from all commits
Commits
Show all changes
7 commits
Select commit
Hold shift + click to select a range
48ab812
standalone format
magmacomputing b385705
1st review
magmacomputing 51f60ee
general review
magmacomputing ce5fe07
wrap up 1st review
magmacomputing 46783c1
regression
magmacomputing 681b988
stderr from pledge.class.test
magmacomputing 604cd2c
2nd review
magmacomputing File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
Large diffs are not rendered by default.
Oops, something went wrong.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,123 @@ | ||
| # Smart Formatting Guide | ||
|
|
||
| Tempo provides a powerful token-based formatting engine that goes beyond the standard ISO strings of native `Temporal`. | ||
|
|
||
| ## 🚀 Standalone Formatting (Zero-Overhead) | ||
|
|
||
| If you have a native `Temporal.ZonedDateTime` and want to format it using Tempo's readable tokens, you can use the standalone `format` function. This allows you to use Tempo's formatting logic without importing the full `Tempo` class. | ||
|
|
||
| ```typescript | ||
| import { format } from '@magmacomputing/tempo/format'; | ||
|
|
||
| const zdt = Temporal.Now.zonedDateTimeISO(); | ||
| const str = format(zdt, '{mon} {day}, {yyyy}'); | ||
|
|
||
| console.log(str); // e.g., "October 24, 2026" | ||
| ``` | ||
|
|
||
| > [!IMPORTANT] | ||
| > **Terms and Standalone Formatting**: When using `format()` with native `Temporal` objects, **Terms** (tokens starting with `#`) are not resolved. To use Terms resolution in your format strings, you must either pass a `Tempo` instance to the `format()` utility or use the class-based `.format()` method. | ||
|
|
||
| ### Supported Input Types | ||
| The engine can interpret: | ||
| * **Temporal Objects**: `ZonedDateTime`, `Instant` (auto-projected to ZDT), `PlainDate`, `PlainDateTime`. | ||
| * **Tempo Instances**: Any instance of the `Tempo` class. | ||
| * **ISO Strings**: Valid Temporal ISO-8601 strings. | ||
| * **Defaults**: If no object is provided, it defaults to **Now** in the configured timezone. | ||
|
|
||
| --- | ||
|
|
||
| ## 🏗️ Class-Based Formatting | ||
|
|
||
| When using the `Tempo` class, the `.format()` method is available on every instance. | ||
|
|
||
| ```typescript | ||
| import { Tempo } from '@magmacomputing/tempo'; | ||
|
|
||
| const t = new Tempo('2026-10-24T15:30:00'); | ||
| console.log(t.format('display')); // Sat, 24 Oct 2026 (using a named format alias) | ||
| ``` | ||
|
|
||
| ### Named Formats | ||
| Tempo comes with several pre-configured format aliases. You can also define your own globally during initialization. | ||
|
|
||
| ```typescript | ||
| Tempo.init({ | ||
| formats: { | ||
| 'fancy': '{mon} the {dd}th day of {yyyy}' | ||
| } | ||
| }); | ||
|
|
||
| const t = new Tempo('2026-10-24'); | ||
| console.log(t.format('fancy')); // October the 24th day of 2026 | ||
| ``` | ||
|
|
||
| --- | ||
|
|
||
| ## 🧩 Modularity: Core vs. Full | ||
|
|
||
| Like the parsing engine, the formatting engine is modular: | ||
|
|
||
| | Version | Formatting Status | | ||
| | :--- | :--- | | ||
| | **Tempo Full** | **Built-in**. Works out of the box. | | ||
| | **Tempo Core** | **Opt-in**. You must call `Tempo.extend(FormatModule)` to enable `.format()`. | | ||
|
|
||
| ### Enabling Formatting in Core | ||
| If you are using `@magmacomputing/tempo/core`, you must explicitly register the formatting engine: | ||
|
|
||
| ```typescript | ||
| import { Tempo } from '@magmacomputing/tempo/core'; | ||
| import { FormatModule } from '@magmacomputing/tempo/format'; | ||
|
|
||
| Tempo.extend(FormatModule); | ||
| ``` | ||
|
|
||
| --- | ||
|
|
||
| ## 🔠 Supported Tokens | ||
|
|
||
| | Token | Description | Example | | ||
| | :--- | :--- | :--- | | ||
| | `{yyyy}` | 4-digit Year | `2026` | | ||
| | `{yy}` | 2-digit Year | `26` | | ||
| | `{yw}` | Year of Week (ISO) | `2026` | | ||
| | `{yyww}` | Year & Week (ISO) | `202617` | | ||
| | `{mon}` | Full Month Name | `October` | | ||
| | `{mmm}` | Short Month Name | `Oct` | | ||
| | `{mm}` | 2-digit Month | `10` | | ||
| | `{dd}` | 2-digit Day | `24` | | ||
| | `{day}` | Unpadded Day | `24` (or `9`) | | ||
| | `{wkd}` | Full Weekday Name | `Saturday` | | ||
| | `{www}` | Short Weekday Name | `Sat` | | ||
| | `{dow}` | Day of Week (1-7) | `6` | | ||
| | `{hh}` | 2-digit Hour (24h) | `15` | | ||
| | `{HH}` | 2-digit Hour (12h) | `03` | | ||
| | `{mer}` | am/pm marker | `pm` | | ||
| | `{MER}` | AM/PM marker | `PM` | | ||
| | `{mi}` | Minutes | `30` | | ||
| | `{ss}` | Seconds | `45` | | ||
| | `{hhmiss}` | Compact Time (24h) | `153045` | | ||
| | `{ms}` | 3-digit Milliseconds | `123` | | ||
| | `{us}` | 3-digit Microseconds | `456` | | ||
| | `{ns}` | 3-digit Nanoseconds | `789` | | ||
| | `{ff}` | Fractional Seconds | `123456789` | | ||
| | `{ts}` | Unix Timestamp | `1792843200000` | | ||
| | `{nano}` | Nanosecond Timestamp | `1792843200000000000` | | ||
| | `{tz}` | Time Zone ID | `Australia/Sydney` | | ||
|
|
||
| ### 🔄 Automatic Meridiem | ||
| If your format string contains `{HH}` (12-hour clock) but lacks a `{mer}` or `{MER}` token, Tempo will automatically append `{mer}` to the end of the last time component to ensure the time remains unambiguous. | ||
|
|
||
| ```typescript | ||
| t.format('{HH}:{mi}'); // "03:30pm" (auto-appended pm) | ||
| ``` | ||
|
|
||
| ### 🔢 Numeric Resolution | ||
| If your format string consists *only* of numeric tokens (e.g., `{yyyy}{mm}{dd}`), the `format()` function will return a **Number** instead of a string. This is useful for generating sortable keys or IDs. | ||
|
|
||
| ```typescript | ||
| const key = t.format('{yyyy}{mm}{dd}'); | ||
| console.log(typeof key); // "number" | ||
| console.log(key); // 20261024 | ||
|
coderabbitai[bot] marked this conversation as resolved.
|
||
| ``` | ||
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.