@northguild/gmt
Version:
Date and time library for JavaScript and TypeScript, built on TC39 Temporal: time zones, DST, business days, ISO 8601 strings in and out. Never throws.
198 lines (197 loc) • 4.2 kB
JavaScript
// src/internal/x12TimeCodes.ts
/**
* X12 data element 623 code → the offset its definition states, release 008010, read from
* Stedi's X12-licensed dictionary (https://www.stedi.com/edi/x12-008010/element/623); the X12
* text was not reached. Each comment quotes the definition. The offsets are the standard's data,
* written out rather than computed: `P` is plus and `M` is minus, and the `13`–`24` run counts
* down. `25`–`29` were added in release 006010. Typed by `X12OffsetTimeCode`, so a missing code
* fails typecheck.
*/
const X12_TIME_CODE_OFFSET = {
"01": "+01:00",
"02": "+02:00",
"03": "+03:00",
"04": "+04:00",
"05": "+05:00",
"06": "+06:00",
"07": "+07:00",
"08": "+08:00",
"09": "+09:00",
"10": "+10:00",
"11": "+11:00",
"12": "+12:00",
"13": "-12:00",
"14": "-11:00",
"15": "-10:00",
"16": "-09:00",
"17": "-08:00",
"18": "-07:00",
"19": "-06:00",
"20": "-05:00",
"21": "-04:00",
"22": "-03:00",
"23": "-02:00",
"24": "-01:00",
"25": "-02:30",
"26": "-03:30",
"27": "+05:30",
"28": "+09:30",
"29": "+10:30",
GM: "+00:00",
UT: "+00:00"
};
/**
* X12 data element 623 code → the zone its definition names, release 008010, read from Stedi's
* X12-licensed dictionary (https://www.stedi.com/edi/x12-008010/element/623); the X12 text was
* not reached. Each comment quotes the definition. Typed by `X12ZoneTimeCode`, so a missing code
* fails typecheck.
*/
const X12_TIME_CODE_ZONE = {
AD: {
zone: "Alaska",
daylight: true
},
AS: {
zone: "Alaska",
daylight: false
},
AT: {
zone: "Alaska",
daylight: null
},
CD: {
zone: "Central",
daylight: true
},
CS: {
zone: "Central",
daylight: false
},
CT: {
zone: "Central",
daylight: null
},
ED: {
zone: "Eastern",
daylight: true
},
ES: {
zone: "Eastern",
daylight: false
},
ET: {
zone: "Eastern",
daylight: null
},
HD: {
zone: "Hawaii-Aleutian",
daylight: true
},
HS: {
zone: "Hawaii-Aleutian",
daylight: false
},
HT: {
zone: "Hawaii-Aleutian",
daylight: null
},
LT: {
zone: "Local",
daylight: null
},
MD: {
zone: "Mountain",
daylight: true
},
MS: {
zone: "Mountain",
daylight: false
},
MT: {
zone: "Mountain",
daylight: null
},
ND: {
zone: "Newfoundland",
daylight: true
},
NS: {
zone: "Newfoundland",
daylight: false
},
NT: {
zone: "Newfoundland",
daylight: null
},
PD: {
zone: "Pacific",
daylight: true
},
PS: {
zone: "Pacific",
daylight: false
},
PT: {
zone: "Pacific",
daylight: null
},
TD: {
zone: "Atlantic",
daylight: true
},
TS: {
zone: "Atlantic",
daylight: false
},
TT: {
zone: "Atlantic",
daylight: null
}
};
/**
* The core of `x12TimeCodeOffset`: the `±HH:MM` offset an X12 time code names.
*
* @param timeCode an X12 data element 623 code
* @returns the offset, or `""` for any value that is not an offset code
*
* @example x12OffsetOf("27") // "+05:30"
* @example x12OffsetOf("ET") // ""
*/
function x12OffsetOf(timeCode) {
if (typeof timeCode !== "string" || !Object.hasOwn(X12_TIME_CODE_OFFSET, timeCode)) return "";
return X12_TIME_CODE_OFFSET[timeCode];
}
/**
* The core of `x12TimeCodeZone`: the named zone an X12 time code names.
*
* @param timeCode an X12 data element 623 code
* @returns a copy of the zone, or `null` for any value that is not a zone-name code
*
* @example x12ZoneOf("ED") // { zone: "Eastern", daylight: true }
* @example x12ZoneOf("27") // null
*/
function x12ZoneOf(timeCode) {
if (typeof timeCode !== "string" || !Object.hasOwn(X12_TIME_CODE_ZONE, timeCode)) return null;
return { ...X12_TIME_CODE_ZONE[timeCode] };
}
/**
* The core of `classifyX12TimeCode`: whether a code is an offset code or a zone-name code.
*
* @param timeCode an X12 data element 623 code
* @returns the class, or `null` for any other value
*
* @example classifyX12TimeCodeOf("27") // { kind: "offset", timeCode: "27" }
* @example classifyX12TimeCodeOf("XX") // null
*/
function classifyX12TimeCodeOf(timeCode) {
if (x12OffsetOf(timeCode) !== "") return {
kind: "offset",
timeCode
};
if (x12ZoneOf(timeCode) !== null) return {
kind: "zone",
timeCode
};
return null;
}
export { classifyX12TimeCodeOf, x12OffsetOf, x12ZoneOf };