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
Jump to file
Failed to load files.
Loading
Diff view
Diff view
25 changes: 25 additions & 0 deletions .changeset/hl7-v2-timestamps.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
---
"@northguild/gmt": minor
---

A new `healthcare` namespace reads and writes the HL7 v2 date-time, the `DTM` and the older `TS` that most clinical feeds still send, such as `20240615103000-0500`.

- `parseHL7(value)` returns what the value states: an instant (it has an offset), a local time (it has none), a partial value (a year, month, day or hour), or the HL7 null value `""`.
- `formatHL7(isoString, { precision })` writes an ISO 8601 value as a `DTM`, cut to the precision and never rounded. It never writes a `TS`, which HL7 withdrew in v2.6.
- `isValidHL7(value)` is true exactly when `parseHL7` reads the value.

An HL7 time with no offset is the sender's local time, and HL7 says so. It is not UTC. `parseHL7` returns it as `kind: "local"` with no `instant`, so you must supply the sender's zone to `resolveLocal` to get one. A value coarser than a minute, such as a birth year, returns `kind: "partial"` and also has no instant. A `TS` degree of precision can only lower the precision, so `199904011200^L` is April 1999, not 12:00.

```typescript
import { formatHL7, parseHL7, resolveLocal } from "@northguild/gmt";

parseHL7("20240615103000-0500");
// { kind: "instant", instant: "2024-06-15T15:30:00Z", offset: "-05:00", local: "2024-06-15T10:30:00", precision: "second" }

parseHL7("20240615103000"); // { kind: "local", local: "2024-06-15T10:30:00", precision: "second" }
resolveLocal("2024-06-15T10:30:00", "Europe/Helsinki"); // "2024-06-15T07:30:00Z"

formatHL7("2024-06-15T15:30:00Z"); // "20240615153000+0000"
```

`parseHL7` returns `null`, `formatHL7` returns `""` and `isValidHL7` returns `false` for a value that does not fit, a day the month lacks, an hour of 24 or a second of 60. None of them throws. The functions are also at `@northguild/gmt/healthcare`, `@northguild/gmt/healthcare/parse`, `@northguild/gmt/healthcare/format` and `@northguild/gmt/healthcare/validate`, with the types `Hl7DateTime`, `Hl7Precision`, `Hl7PartialPrecision`, `Hl7MomentPrecision` and `Hl7FormatOptions`.
6 changes: 3 additions & 3 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -53,7 +53,7 @@ addZoned("2024-03-30T12:00:00+01:00[Europe/Berlin]", { hours: 24 }); // "2024-03
resolveLocal("2024-03-31T02:30:00", "Europe/Berlin", { disambiguation: "reject" }); // ""
```

- **745 functions, to the nanosecond**, tested in 17 locales.
- **748 functions, to the nanosecond**, tested in 17 locales.
- **Never throws.** ISO 8601 strings in. Invalid input returns `""`, `null`, `false` or `[]`.

The latest test counts, CI volume and the comparison with Luxon, date-fns, Moment.js, Day.js, Spacetime and `@internationalized/date` are on the [Why GMT](https://northguild.dev/gmt/why-gmt/) page. The full comparison and the method are on the [compare](https://northguild.dev/gmt/compare/) page.
Expand All @@ -79,9 +79,9 @@ convertUtcToZoned("2024-06-15T12:00:00Z", "Asia/Tokyo"); // "2024-06-15T21:00:00
```

Every function has a reference page with a live playground:
[plain](https://northguild.dev/gmt/reference/plain/) · [zoned](https://northguild.dev/gmt/reference/zoned/) · [utc](https://northguild.dev/gmt/reference/utc/) · [unix](https://northguild.dev/gmt/reference/unix/) · [instant](https://northguild.dev/gmt/reference/instant/) · [calendar](https://northguild.dev/gmt/reference/calendar/) · [duration](https://northguild.dev/gmt/reference/duration/) · [interval](https://northguild.dev/gmt/reference/interval/) · [workforce](https://northguild.dev/gmt/reference/workforce/) · [span](https://northguild.dev/gmt/reference/span/) · [precision](https://northguild.dev/gmt/reference/precision/) · [recurrence](https://northguild.dev/gmt/reference/recurrence/) · [timescale](https://northguild.dev/gmt/reference/timescale/) · [gnss](https://northguild.dev/gmt/reference/gnss/) · [transport](https://northguild.dev/gmt/reference/transport/) · [intermodal](https://northguild.dev/gmt/reference/intermodal/) · [finance](https://northguild.dev/gmt/reference/finance/)
[plain](https://northguild.dev/gmt/reference/plain/) · [zoned](https://northguild.dev/gmt/reference/zoned/) · [utc](https://northguild.dev/gmt/reference/utc/) · [unix](https://northguild.dev/gmt/reference/unix/) · [instant](https://northguild.dev/gmt/reference/instant/) · [calendar](https://northguild.dev/gmt/reference/calendar/) · [duration](https://northguild.dev/gmt/reference/duration/) · [interval](https://northguild.dev/gmt/reference/interval/) · [workforce](https://northguild.dev/gmt/reference/workforce/) · [span](https://northguild.dev/gmt/reference/span/) · [precision](https://northguild.dev/gmt/reference/precision/) · [recurrence](https://northguild.dev/gmt/reference/recurrence/) · [timescale](https://northguild.dev/gmt/reference/timescale/) · [gnss](https://northguild.dev/gmt/reference/gnss/) · [transport](https://northguild.dev/gmt/reference/transport/) · [intermodal](https://northguild.dev/gmt/reference/intermodal/) · [finance](https://northguild.dev/gmt/reference/finance/) · [healthcare](https://northguild.dev/gmt/reference/healthcare/)

By industry: [Workforce](https://northguild.dev/gmt/industries/workforce/) · [Transport](https://northguild.dev/gmt/industries/transport/) · [Intermodal](https://northguild.dev/gmt/industries/intermodal/) · [GNSS](https://northguild.dev/gmt/industries/gnss/) · [Maritime](https://northguild.dev/gmt/industries/maritime/) · [Finance](https://northguild.dev/gmt/industries/finance/)
By industry: [Workforce](https://northguild.dev/gmt/industries/workforce/) · [Transport](https://northguild.dev/gmt/industries/transport/) · [Intermodal](https://northguild.dev/gmt/industries/intermodal/) · [GNSS](https://northguild.dev/gmt/industries/gnss/) · [Maritime](https://northguild.dev/gmt/industries/maritime/) · [Finance](https://northguild.dev/gmt/industries/finance/) · [Healthcare](https://northguild.dev/gmt/industries/healthcare/)

Then the [guides](https://northguild.dev/gmt/guides/) show how to do each job, and the [tools](https://northguild.dev/gmt/tools/) let you try it on your own values.

Expand Down
5 changes: 5 additions & 0 deletions apps/dox/scripts/grow-measure.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -200,6 +200,11 @@ const PAGES = [
preset: PRESET,
drag: null,
},
{
slug: "hl7-timestamp-reader",
preset: PRESET,
drag: null,
},
{
slug: "x12-time-reader",
preset: { ...PRESET, nth: 0 },
Expand Down
4 changes: 4 additions & 0 deletions apps/dox/scripts/html-diff.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -122,6 +122,10 @@ const PAGES = [
path: "tools/dtm-decoder",
widget: "dtm-decoder",
},
{
path: "tools/hl7-timestamp-reader",
widget: "hl7-timestamp-reader",
},
{
path: "tools/x12-time-reader",
widget: "x12-time-reader",
Expand Down
1 change: 1 addition & 0 deletions apps/dox/scripts/visual-snapshot.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -133,6 +133,7 @@ const PAGES = [
{ slug: "tool-eta-drift", path: "/tools/eta-drift/" },
{ slug: "tool-departure-board", path: "/tools/departure-board/" },
{ slug: "tool-dtm-decoder", path: "/tools/dtm-decoder/" },
{ slug: "tool-hl7-timestamp-reader", path: "/tools/hl7-timestamp-reader/" },
{ slug: "tool-x12-time-reader", path: "/tools/x12-time-reader/" },
{ slug: "tool-demurrage-clock", path: "/tools/demurrage-clock/" },
{ slug: "tool-bl-date-stamp", path: "/tools/bl-date-stamp/" },
Expand Down
54 changes: 54 additions & 0 deletions apps/dox/src/components/Hl7TimestampReader.astro
Original file line number Diff line number Diff line change
@@ -0,0 +1,54 @@
---
/**
* HL7 Timestamp Reader widget (HLTH-93).
*
* A typed HL7 v2 date-time (a DTM or the older TS): `parseHL7` says whether it
* states an instant, an offset, a local time or only a partial date, the widget
* reads a value with no offset in the zones you choose, and it shows what
* `formatHL7` writes back at each precision.
*
* **The markup and the wiring both live in `lib/hl7-timestamp-reader-mount.ts`**,
* so the chat rail mounts the same widget this page server-renders.
*/

import { renderHl7TimestampReaderTemplate } from "../lib/hl7-timestamp-reader-mount";

/**
* `fullbleed` lets the page say the widget may use the whole main pane; see
* DtmDecoder.astro for the marker it adds and why it sits on a wrapper.
*/
interface Props {
fullbleed?: boolean;
}

const { fullbleed = false } = Astro.props;
const html = renderHl7TimestampReaderTemplate("hl7-timestamp-reader", {}).value;
---

{
fullbleed ? (
<div data-tools-fullbleed set:html={html} />
) : (
<Fragment set:html={html} />
)
}

<script>
import { mountHl7TimestampReader } from "../lib/hl7-timestamp-reader-mount";
import { showUnavailable } from "../lib/widget-mount";
import { seedFromLocation } from "../lib/widget-permalink";

if (typeof window !== "undefined") {
/* A `?w=hl7&wa=` permalink seeds the widget with the values it names; see
lib/widget-permalink.ts. Absent or malformed, the widget opens on its
first preset. */
const seed = seedFromLocation("hl7");
document
.querySelectorAll<HTMLElement>('[data-widget="hl7-timestamp-reader"]')
.forEach((el) => {
mountHl7TimestampReader(el, seed, new AbortController().signal).catch(
(error) => showUnavailable(el, error),
);
});
}
</script>
2 changes: 2 additions & 0 deletions apps/dox/src/components/ask/widget-graph.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -159,6 +159,8 @@ describe("the chat island's static import graph", () => {
"lib/edi-widgets.ts",
"lib/edi-lib.ts",
"lib/dtm-decoder.ts",
"lib/hl7-lib.ts",
"lib/hl7-timestamp-reader.ts",
"lib/x12-time-reader.ts",
"lib/gnss-widgets.ts",
"lib/gnss-lib.ts",
Expand Down
28 changes: 28 additions & 0 deletions apps/dox/src/components/ask/widget-registry.ts
Original file line number Diff line number Diff line change
Expand Up @@ -68,6 +68,7 @@ import type { PunctualityBoardArgs } from "~/lib/punctuality-board-mount";
import type { EtaDriftArgs } from "~/lib/eta-drift-mount";
import type { DepartureBoardArgs } from "~/lib/departure-board-mount";
import type { DtmDecoderArgs } from "~/lib/dtm-decoder-mount";
import type { Hl7Args } from "~/lib/hl7-timestamp-reader-mount";
import type { X12TimeReaderArgs } from "~/lib/x12-time-reader-mount";
import type { ZonePlannerArgs } from "~/lib/zone-planner-mount";
import type { GnssWeekDecoderArgs } from "~/lib/gnss-week-decoder-mount";
Expand Down Expand Up @@ -107,6 +108,7 @@ import {
showDepartureBoardInput,
showDstInspectorInput,
showDtmDecoderInput,
showHl7TimestampReaderInput,
showEpcisEventReaderInput,
showElapsedVsClockInput,
showGnssRolloverCheckerInput,
Expand Down Expand Up @@ -640,6 +642,31 @@ const dtmEntry = defineWidget<DtmDecoderArgs>({
checkZones([zone1, zone2, zone3, zone4].filter((z): z is string => !!z)),
});

const hl7Entry = defineWidget<Hl7Args>({
title: "HL7 timestamp reader",
kind: "hl7",
parse: (input) => {
const result = showHl7TimestampReaderInput.safeParse(input);
return result.success
? { ok: true, args: result.data }
: {
ok: false,
reason: "The widget was asked for with arguments that don't fit.",
};
},
/* Seeded in the template and again by the mount: every argument is a control
value. Only the four zones are checked here; a value the library does not
read is the widget's own NO SIGNAL, with its reason. */
load: () =>
import("~/lib/hl7-timestamp-reader-mount").then((m) => ({
renderTemplate: (idPrefix, args) =>
m.renderHl7TimestampReaderTemplate(idPrefix, args),
mount: m.mountHl7TimestampReader,
})),
validate: ({ zone1, zone2, zone3, zone4 }) =>
checkZones([zone1, zone2, zone3, zone4].filter((z): z is string => !!z)),
});

const x12TimeEntry = defineWidget<X12TimeReaderArgs>({
title: "X12 time reader",
kind: "xtime",
Expand Down Expand Up @@ -1332,6 +1359,7 @@ export const WIDGET_REGISTRY: Record<string, AnyWidgetEntry | undefined> = {
showDepartureBoard: departureEntry,
showZonePlanner: plannerEntry,
showDtmDecoder: dtmEntry,
showHl7TimestampReader: hl7Entry,
showX12TimeReader: x12TimeEntry,
showDemurrageClock: demurrageEntry,
showBlDateStamp: blDateEntry,
Expand Down
2 changes: 1 addition & 1 deletion apps/dox/src/content/docs/guides/index.mdx
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ same patterns to coding agents.
| [Conversion](./conversion/) | Between plain/zoned/unix/UTC and calendar systems, foreign epochs (NTP, FILETIME, Excel, PostgreSQL), time-ordered IDs, [database timestamps](./conversion/databases/) (PostgreSQL, MySQL, SQL Server, Oracle, SQLite, MongoDB, DynamoDB, Redis, Couchbase, Cassandra, ScyllaDB, Neo4j), Parquet, Arrow, protobuf |
| [Intervals](./intervals/interval-basics/) | Range math — containment, overlap, set operations, splitting, [hours of work](./industries/workforce-hours-of-work/) |
| [Integration](./integration/) | Cache keys, routers, tables, lint config |
| [Industries](./industries/) | The industry layers on top of the core — transport legs and dwell, cut-offs, multi-leg scheduling, punctuality and timestamp classes, free time and demurrage, billing deadlines, bill of lading dates and multimodal ETAs, EDI timestamps — with real-world scenarios |
| [Industries](./industries/) | The industry layers on top of the core — transport legs and dwell, cut-offs, multi-leg scheduling, punctuality and timestamp classes, free time and demurrage, billing deadlines, bill of lading dates and multimodal ETAs, EDI timestamps, HL7 clinical timestamps — with real-world scenarios |
| [How-to](./how-to/) | Short answers to common tasks — add business days, convert between time zones, check whether a local time exists on a DST day |

## Reading order
Expand Down
Loading
Loading