# classifyX12TimeCode

Industry: Intermodal

## Signature

```ts
classifyX12TimeCode(timeCode: string): X12TimeCodeClass | null
```

Say whether an X12 time code (data element 623) states an offset or names a zone, and return the code narrowed to the function that reads it.

- Data element 623 is the code beside the date and time of an X12 `AT7`, `G62` or `DTM` segment. 31 of its 56 codes state an offset from UTC and 25 name a zone, and each group has its own reader. A segment reader holds the code as a plain string: it calls this function, tests `kind`, and passes `timeCode` on. Testing `kind` narrows `timeCode` to that reader's union, so the call needs no cast. Codes are release 008010, read from [Stedi's X12-licensed dictionary](https://www.stedi.com/edi/x12-008010/element/623); the X12 text itself was not reached. ### Kinds

  | `kind` | Codes | Read by |
  |---|---|---|
  | `"offset"` | `01`–`29`, `UT`, `GM` | `x12TimeCodeOffset` |
  | `"zone"` | `AD`, `AS`, `AT`, `CD`, `CS`, `CT`, `ED`, `ES`, `ET`, `HD`, `HS`, `HT`, `MD`, `MS`, `MT`, `ND`, `NS`, `NT`, `PD`, `PS`, `PT`, `TD`, `TS`, `TT`, `LT` | `x12TimeCodeZone` |

- **The narrowing pattern.** With `const classified = classifyX12TimeCode(code)`, the line `if (classified?.kind === "offset") x12TimeCodeOffset(classified.timeCode)` compiles with no cast: inside the test, `classified.timeCode` is `X12OffsetTimeCode`. The `"zone"` branch passes `classified.timeCode` to `x12TimeCodeZone` the same way.
- Both members are always present, and each call returns a new object.
- `GM` ("Greenwich Mean Time") is an offset code: GMT reads it as `+00:00`, as `x12TimeCodeOffset` explains.
- `25`–`29` were added in release 006010. The function takes no release, so it reads them.
- This is a time code, not a format qualifier: `TS` here is Atlantic Standard Time. Classify a data element 1250 code with `classifyX12DateTimePeriodFormat`.
- Matching is exact: two characters, upper case, no padding.
- Returns `null` for a code that is not one of the 56 and for a non-string. It returns a result exactly when `isValidX12TimeCode` is true.

## Parameters

| Parameter | Type | Description |
| --- | --- | --- |
| `timeCode` | `string` | X12 data element 623 time code, as it arrives (e.g. "20") |

## Returns

`{ kind, timeCode }`, or null on invalid input

## Types

### X12TimeCodeClass

An X12 data element 623 time code with what it states: the result of `classifyX12TimeCode`.

```ts
import type { X12TimeCodeClass } from "@northguild/gmt";
```

```ts
type X12TimeCodeClass =
  | {
      /** The code states an offset from UTC: read it with `x12TimeCodeOffset`. */
      kind: "offset";
      /** The code itself, narrowed to the 31 codes that state an offset. */
      timeCode: X12OffsetTimeCode;
    }
  | {
      /** The code names a zone and states no offset: read it with `x12TimeCodeZone`. */
      kind: "zone";
      /** The code itself, narrowed to the 25 codes that name a zone. */
      timeCode: X12ZoneTimeCode;
    };
```

## Related types

- [`X12OffsetTimeCode`](/gmt/reference/types/X12OffsetTimeCode/)
- [`X12ZoneTimeCode`](/gmt/reference/types/X12ZoneTimeCode/)

## Examples

```ts
import { classifyX12TimeCode } from "@northguild/gmt/intermodal/parse";

classifyX12TimeCode("20") // { kind: "offset", timeCode: "20" }
```

## Source

[intermodal/parse/classifyX12TimeCode.ts](https://github.com/northguild/gmt/blob/main/packages/gmt/src/intermodal/parse/classifyX12TimeCode.ts)
