Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
The table of contents is too big for display.
Diff view
Diff view
  •  
  •  
  •  
8 changes: 8 additions & 0 deletions .env
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
################################################################################
# As additional safeguards to reveal potential time zone related bugs,
# please comment out the below`TZ` environment (one at a time) to fake the
# local timezone in extreme edge cases.
# (Note: this is in POSIX format, so "+"/"-" means behind/ahead instead of another way around)
#
# TZ=GMT+23
# TZ=GMT-1
3 changes: 1 addition & 2 deletions .github/workflows/check-published-scheduled.yml
Original file line number Diff line number Diff line change
@@ -1,14 +1,13 @@
name: Check Published

on:
workflow_dispatch:
schedule:
- cron: '30 20 * * *'

jobs:
checkPublished:
runs-on: ubuntu-latest
name: Check Published
runs-on: ubuntu-latest
steps:
- name: Checkout Code
uses: actions/checkout@v4
Expand Down
4 changes: 2 additions & 2 deletions .github/workflows/ci.yml
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ jobs:
runs-on: ubuntu-latest
strategy:
matrix:
node-version: ['20.x', '21.x']
node-version: ['22.x', '24.x']
steps:
- name: Checkout Code
uses: actions/checkout@v4
Expand All @@ -39,7 +39,7 @@ jobs:
runs-on: ubuntu-latest
strategy:
matrix:
node-version: ['20.x', '21.x']
node-version: ['22.x', '24.x']
steps:
- name: Checkout Code
uses: actions/checkout@v4
Expand Down
4 changes: 2 additions & 2 deletions .github/workflows/codeql-analysis.yml
Original file line number Diff line number Diff line change
Expand Up @@ -24,8 +24,8 @@ jobs:
- name: Checkout repository
uses: actions/checkout@v4
- name: Initialize CodeQL
uses: github/codeql-action/init@v2
uses: github/codeql-action/init@v3
with:
languages: ${{ matrix.language }}
- name: Perform CodeQL Analysis
uses: github/codeql-action/analyze@v2
uses: github/codeql-action/analyze@v3
2 changes: 1 addition & 1 deletion .github/workflows/coverage.yml
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ jobs:
- name: Use Node.js
uses: actions/setup-node@v4
with:
node-version: '20.x'
node-version: '24.x'
registry-url: 'https://registry.npmjs.org'
- name: Install Dependencies
run: npm ci --ignore-scripts
Expand Down
4 changes: 1 addition & 3 deletions .github/workflows/publish-beta.yml
Original file line number Diff line number Diff line change
Expand Up @@ -17,16 +17,14 @@ jobs:
- name: Setup Node.js
uses: actions/setup-node@v4
with:
node-version: '20.x'
node-version: '24.x'
registry-url: 'https://registry.npmjs.org'
- name: Check Label
uses: checkdigit/github-actions/check-label@main
env:
GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }}
- name: Check Imports
uses: checkdigit/github-actions/check-imports@main
- name: Install latest npm
run: npm install -g npm@latest
- name: Install Dependencies
run: npm ci --ignore-scripts
- name: Audit Signatures
Expand Down
4 changes: 1 addition & 3 deletions .github/workflows/publish.yml
Original file line number Diff line number Diff line change
Expand Up @@ -14,10 +14,8 @@ jobs:
- name: Setup Node.js
uses: actions/setup-node@v4
with:
node-version: '20.x'
node-version: '24.x'
registry-url: 'https://registry.npmjs.org'
- name: Install latest npm
run: npm install -g npm@latest
- name: Install Dependencies
run: npm ci --ignore-scripts
- name: Audit Signatures
Expand Down
2 changes: 1 addition & 1 deletion LICENSE.txt
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
The MIT License

Copyright (c) 2022-2024 Check Digit, LLC.
Copyright (c) 2022-2025 Check Digit, LLC.

Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
Expand Down
74 changes: 26 additions & 48 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,87 +1,65 @@
# Check Digit Time Library

Copyright (c) 2022-2024 [Check Digit, LLC](https://checkdigit.com)
Copyright (c) 2022-2025 [Check Digit, LLC](https://checkdigit.com)

The Check Digit time library is the officially sanctioned method for Check Digit services to deal with time. It is a partial implementation of the new TC39 Temporal proposal
and date manipulation functions provided by date-fns. Features:
The Check Digit time library is the officially sanctioned method for Check Digit services to deal with time. It has date manipulation functions provided by date-fns. Features:

- Nanosecond precision date times, simulated with Node's built-in high-resolution timer
- Tests that verify compliance with latest TC39 polyfill
- Designed to be removed once Temporal is included, by default, within V8.
- Implements most of Temporal.Instant, except timezone/calendar functionality
- Implements Temporal.Now.instant()
- Stripped down `date-fns` functions
- `date-fns-tz` for timezone support
- `formatUtc` for formatting UTC dates

Temporal (vs the millisecond precision of standard Date built-in) is useful for webservices recording the time of events, where concurrent activity may occur and millisecond precision does not provide adequate uniqueness and ordering.

The [`date-fns`](https://date-fns.org) library is included. The only locale currently supported is `en-US`, but otherwise contains all
available functionality as of v3.6.0.
available functionality as of v4.1.0 except that the code in `fp` folder are excluded, which contain functional programming related stuff that is not used at the moment.

Additionally, the [`date-fns-tz`](https://github.com/marnusw/date-fns-tz) library is included. Contains all
available functionality as of v1.3.7 except that the code in `fp` folder is excluded, which contains functional programming related stuff that is not used at the moment.
available functionality as of v3.2.0 except that the code in `fp` folder are excluded, which contain functional programming related stuff that is not used at the moment.

### `formatUtc`
## `formatUtc`

The `formatUtc` function is a wrapper around `date-fns-tz`'s `format` function, but with the following differences:

- There is no `options` argument, `timeZone` is always UTC.

Generally speaking, `formatUtc` should be used in place of `format` or `tzFormat`, unless non-UTC time zones are required.

### Important note about `Instant.toString()`

Unlike built-in `Date.toISOString()`, `Instant.toString()` will not add fractional second digits if those values are zero.

E.g.

```
new Date(0).toISOString() -> 1970-01-01T00:00:00.100Z

vs

Temporal.Instant(0n).toString() -> 1970-01-01T00:00:00.1Z
```

### Why not just use the polyfill?

The polyfill is not production ready. The implementation is designed to be used in the browser so cannot make use of Node's high-resolution timer to more accurately simulate nanosecond precision time. Crucially, the polyfill does not guarantee always increasing nanosecond precision times on subsequent calls, which makes it a non-starter for recording the time of events in production webservices.

### Installing and usage
## Installing and usage

`npm install @checkdigit/time` then:

```
import { Temporal } from '@checkdigit/time'; // delete this once Temporal becomes a built-in

// print out nanosecond-precision ISO8601 datetime
console.log('Current time', Temporal.Now.instant().toString());
```

For `date-fns` functionality:

```
```typescript
import { formatUtc } from '@checkdigit/time';

console.log(formatUtc(new Date(), 'yyyy-MM-dd'));
```

### Documentation

The stage 3 proposal can be found here: https://github.com/tc39/proposal-temporal

The reference documentation is here: https://tc39.es/proposal-temporal/docs/
## Documentation

The documentation for `date-fns` is here: https://date-fns.org/

The documentation for `date-fns-tz` is here: https://github.com/marnusw/date-fns-tz

### Maintenance notes:
## Migration guide

following are the main steps to sync up with the latest date-fns/date-fns-tz

- clone 3rd party repo
- remove test/fp stuff
- copy/override into @checkdigit/time repo's corresponding folder
- update CHANGELOG.md (it helps to understand the changes since the last update and to prepare accordingly)
- replace imports from '.js' to '.ts'
- Add comments in the beginning of the files to silence ts/lint errors if it forces overwhelming changes otherwise
- check things which compiles but may have other issues, e.g. import { type ZZZ } => import type { ZZZ }
- adjust tsconfig.json compilerOptions if necessary
- apply previous patch (e.g. daylight saving timezone, etc.)
- pass existing local tests
- beta-test across dependent repos to make sure it still works

## Critical maintenance notes (VERY IMPORTANT!!!):

When updating the latest code from the original repositories, except making all the necessary changes to make them fully typescript compatible, please remember to carry over the patches to overcome the following issues:

- spring-forward support
- nanosecond support
- hours is incorrect when it's close to daylight saving timezone threshold

Please search for `[PATCH:]` in the existing codebase.
14 changes: 14 additions & 0 deletions eslint.config.mjs
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
import checkdigitConfig from '@checkdigit/eslint-config';

export default [
...checkdigitConfig,
{
files: ['src/date-fns/**', 'src/date-fns-tz/**'],
rules: {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Is there a way to turn these rules off only for the stuff inside date-fns and date-fns-tz directories

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

good point, will try

'@checkdigit/file-path-comment': 'off',
'markdown/no-missing-label-refs': 'off',
'markdown/fenced-code-language': 'off',
'unicorn/filename-case': 'off',
},
},
];
Loading
Loading