A 203 Read as UTC
A 203 Read as UTC
A terminal sends a UN/EDIFACT DTM segment. Its value is 202406151430 and its format code is 203. The receiving system stores an instant.
The naive approach
// The naive approach: the digits of a 203 value, read as UTC
const value = "202406151430"; // DTM+137:202406151430:203'
const iso = value.slice(0, 4) + "-" + value.slice(4, 6) + "-" + value.slice(6, 8) + "T" + value.slice(8, 10) + ":" + value.slice(10, 12) + ":00Z";
new Date(iso).toISOString();
// "2024-06-15T14:30:00.000Z" — an instant the message never statedWatch it break
The naive approach silently produces wrong results — there is no GMT equivalent to demonstrate because the failure is not using GMT.
Why
Code 203 is CCYYMMDDHHMM with no offset: a local time at a place the value does not name. Appending Z invents the place. If the terminal is in Shanghai, the event happened at 06:30 UTC, eight hours earlier than the stored instant. parseEdifactDateTime returns a local date-time and no instant, so the zone has to come from you. A code that does carry its offset, such as 205, is read by parseEdifactOffsetDateTime, which returns the date-time with its offset.
The gmt approach
import { parseEdifactDateTime, parseEdifactOffsetDateTime } from "@northguild/gmt/intermodal/parse";
import { resolveLocal, toOffsetInstant } from "@northguild/gmt/instant/convert";
parseEdifactDateTime("202406151430", "203");
// "2024-06-15T14:30:00"
// The terminal is in Shanghai, so you say so.
resolveLocal("2024-06-15T14:30:00", "Asia/Shanghai");
// "2024-06-15T06:30:00Z"
// A 205 value carries its own offset, so it names an instant.
parseEdifactOffsetDateTime("202406151430+0200", "205");
// "2024-06-15T14:30:00+02:00"
toOffsetInstant("2024-06-15T14:30:00+02:00");
// { instant: "2024-06-15T12:30:00Z", offset: "+02:00" }Same widget, working
The widget above reads a 203 value and returns a local date-time with no offset. Switch to
parseEdifactOffsetDateTime with the
value 202406151430+0200 and code 205, and the result states its offset. Open the same value read in four zones
in the DTM Decoder: the four instants are 15 hours apart. The full rules are in Intermodal: EDI
Timestamps.