# classifyX12DateTimePeriodFormat

Industry: Intermodal

## Signature

```ts
classifyX12DateTimePeriodFormat(format: string): X12DateTimePeriodFormatClass | null
```

Name the kind of value an X12 data element 1250 format qualifier states, and return the code narrowed to that kind, so a code that arrives as data reaches the function that reads it.

- Each kind has its own parser, formatter and validator, and each takes only its own codes. A generic `DTP` reader holds `DTP-02` as a plain string: it calls this function, tests `kind`, and passes `format` on. Testing `kind` narrows `format` to that kind's union, so the call needs no cast. Codes are release 005010, read from [Stedi's X12-licensed dictionary](https://www.stedi.com/edi/x12-005010/element/1250); the X12 text itself was not reached. ### Kinds

  | `kind` | Codes | Read by |
  |---|---|---|
  | `"date"` | `D8`, `DB` | `parseX12Date` |
  | `"time"` | `TM`, `TS` | `parseX12Time(value, format)` |
  | `"dateTime"` | `DT`, `RTS` | `parseX12DateTime` |
  | `"dateRange"` | `RD8`, `RD` | `parseX12DateRange` |
  | `"dateTimeRange"` | `RDT`, `DTS` | `parseX12DateTimeRange` |

- **The narrowing pattern.** With `const classified = classifyX12DateTimePeriodFormat(code)`, the line `if (classified?.kind === "date") parseX12Date(value, classified.format)` compiles with no cast: inside the test, `classified.format` is `X12DateFormat`.
- Both members are always present, and each call returns a new object.
- `RTS` is one date-time despite its `R`, and `DTS` is a range despite having none.
- Returns `null` for every code no function reads: the two-digit-year codes (`D6`, `TT`, `TR`, `RD6`, `TU`), `TC`, `EH`, `DDT`, `DTD`, `RTM`, `UN` and every other 1250 code.
- This is a format qualifier, not a time code: `TS` here is the format `HHMMSS`. Classify a data element 623 time code with `classifyX12TimeCode`.
- Matching is exact: `"d8"` and `" D8"` are not codes.
- Returns a result exactly when `isValidX12DateTimePeriodFormat` is true.

## Parameters

| Parameter | Type | Description |
| --- | --- | --- |
| `format` | `string` | X12 data element 1250 format qualifier, as it arrives (e.g. "D8") |

## Returns

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

## Types

### X12DateTimePeriodFormatClass

An X12 data element 1250 format qualifier with the kind of value it states: the result of `classifyX12DateTimePeriodFormat`.

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

```ts
type X12DateTimePeriodFormatClass =
  | {
      /** The kind of value the code states: a calendar date, read by `parseX12Date`. */
      kind: "date";
      /** The code itself, narrowed to the codes of that kind, so it fits the kind's functions. */
      format: X12DateFormat;
    }
  | {
      /** The kind of value the code states: a time of day, read by `parseX12Time(value, format)`. */
      kind: "time";
      /** The code itself, narrowed to the codes of that kind, so it fits the kind's functions. */
      format: X12TimeFormat;
    }
  | {
      /** The kind of value the code states: a local date and time, read by `parseX12DateTime`. */
      kind: "dateTime";
      /** The code itself, narrowed to the codes of that kind, so it fits the kind's functions. */
      format: X12DateTimeFormat;
    }
  | {
      /** The kind of value the code states: a range between two dates, read by `parseX12DateRange`. */
      kind: "dateRange";
      /** The code itself, narrowed to the codes of that kind, so it fits the kind's functions. */
      format: X12DateRangeFormat;
    }
  | {
      /** The kind of value the code states: a range between two local date-times, read by `parseX12DateTimeRange`. */
      kind: "dateTimeRange";
      /** The code itself, narrowed to the codes of that kind, so it fits the kind's functions. */
      format: X12DateTimeRangeFormat;
    };
```

## Related types

- [`X12DateFormat`](/gmt/reference/types/X12DateFormat/)
- [`X12TimeFormat`](/gmt/reference/types/X12TimeFormat/)
- [`X12DateTimeFormat`](/gmt/reference/types/X12DateTimeFormat/)
- [`X12DateRangeFormat`](/gmt/reference/types/X12DateRangeFormat/)
- [`X12DateTimeRangeFormat`](/gmt/reference/types/X12DateTimeRangeFormat/)

## Examples

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

classifyX12DateTimePeriodFormat("D8") // { kind: "date", format: "D8" }
```

## Source

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