@krizaka/time
Date arithmetic on an instant, exact in any time zone: the facade the « Krizaka first » dates rule points to.
Install
npm install @krizaka/timeDate arithmetic for every Krizaka backend, in one place. The « Krizaka first » dates rule (@krizaka/config) refuses a date library inside a package; this is the facade it points to, so addSeconds, addHours, differenceInSeconds and isBefore are written once and the library behind them can change without touching a package.
import { addSeconds, differenceInSeconds, isBefore } from "@krizaka/time";
const resetAt = addSeconds(clock.now(), 900);
if (isBefore(resetAt, clock.now())) { /* ended */ }
const retryAfter = differenceInSeconds(resetAt, clock.now());- Exact on the instant. Adding two hours adds 7 200 000 ms whatever the server's time zone, so « a session of 14 days » is
addHours(now, 14 * 24)and lasts the same on every server, across a daylight-saving change. - Not the calendar. The day a law, a deadline or a receipt means, in a person's zone, is not arithmetic on an instant: use
localDayof@krizaka/intl. For that reason there is noaddDayshere. - Missing function? Add it here first (with its test), never import the library beside it.
- Pure: no clock of its own (take a
Clockfrom@krizaka/db), no environment, no I/O.
Validation shapes
isIsoDate (YYYY-MM-DD), isEmail, isHttpUrl, isSlug, isE164Phone, isCanadianPostalCode: type guards on unknown, pure, no I/O. The no-hand-written-validation-regex rule of @krizaka/config refuses a hand-written pattern for these shapes and points here. An email check says the shape is right, never that the mailbox exists. Missing a fact? Add it here first, with its test.
Exports
The 16 names the package exports, read from its types.
| Name | Kind | What it is |
|---|---|---|
addHours | type | |
addMinutes | type | |
addSeconds | type | |
differenceInMilliseconds | type | |
differenceInSeconds | type | |
isAfter | type | |
isBefore | type | |
isCanadianPostalCode | function | True for a Canadian postal code (H2T 1S6, with or without the space). |
isE164Phone | function | True for an international phone number in E.164 form (+14185551234). |
isEmail | function | True for [email protected] (no spaces, one @, a dotted domain). |
isHttpUrl | function | True for an absolute http: or https: URL that the platform's URL parser accepts. |
isIsoDate | function | True when value is a real calendar date written YYYY-MM-DD (2026-02-30 is not). |
isSlug | function | True for a lowercase URL slug: letters and digits separated by single hyphens. |
subHours | type | |
subMinutes | type | |
subSeconds | type |
Package
Changelog
0.2.0
Minor changes
isIsoDate,isEmail,isHttpUrl,isSlug,isE164Phone,isCanadianPostalCode: the helpers the new no-hand-written-validation-regex rule points to (krizaka/.github#422).
0.1.0
Minor changes
- New package: date arithmetic on an instant (
addSeconds,addMinutes,addHours,subSeconds,subMinutes,subHours,differenceInSeconds,differenceInMilliseconds,isBefore,isAfter), exact in any time zone: the facade the « Krizaka first » dates rule points to (krizaka-ts#76), so the new packages import no date library of their own.