# gnssWeekToUtc

Industry: GNSS

## Signature

```ts
gnssWeekToUtc(counter: GnssWeekCounter, week: number, timeOfWeekSeconds: number, rolloverEra: RolloverEra): string
```

Convert a week number and time of week, as a receiver reports them, to a UTC instant string.

- Name the counter the week came from: GPS and QZSS LNAV wrap every 1024 weeks (about 19.6 years), their CNAV every 8192, Galileo every 4096, BeiDou every 8192, NavIC LNAV every 1024 and L1NV every 8192.
- A week number alone cannot say which pass it is, so `rolloverEra` is required: an integer (era 0 starts at the system's epoch); `"rolling"` for the pass within half a counter period of today, or era 0, when the week would otherwise fall before the system's epoch; or `{ notBefore }` for the earliest pass at or after an instant you state, as a receiver that resolves weeks against its firmware build date does. An instant exactly at `notBefore` counts, and a `notBefore` before the counter's epoch gives era 0. Receivers that report a stale week need an integer or a pivot.
- `timeOfWeekSeconds` is seconds of week and may have a fraction; it is rounded to the nearest nanosecond, and a value that rounds to 604 800 s returns "". Convert a coarser broadcast count first (NavIC L5/S: TOWC × 12; NavIC L1: ITOW × 7200 + TOI × 18, per the NavIC ICDs). Both counts name the start of the next subframe (L5/S) or 18-second frame (L1), so the last one of a week (TOWC 50400; ITOW 83 with TOI 400) gives 604 800 s. That value is TOW 0 of the next week: pass `week + 1` and 0. When `week` is the counter's last (1023 or 8191), pass week 0, with `rolloverEra + 1` for an integer era or the same `"rolling"` or `{ notBefore }`.
- A week time inside an inserted leap second returns `…T23:59:60Z`. Only the `timescale/`, `gnss/` and `finance/` functions read that string back; other GMT functions reject a second of 60.
- GLONASS has no week counter, so it is not a counter here.
- A RINEX navigation-file week is a continuous count, not a broadcast week: read it with `rinexWeekToUtc`. A RINEX NavIC week given to `"IRN-L1NV"` would be 1024 weeks off.
- "" when `week`, `timeOfWeekSeconds` or `rolloverEra` is out of range (a `notBefore` that is not an instant, or is an unlisted second 60, included), for a result past year 9999, and on invalid input.

## Parameters

| Parameter | Type | Description |
| --- | --- | --- |
| `counter` | `GnssWeekCounter` | the week counter: "GPS-LNAV", "GPS-CNAV", "GAL", "BDT", "QZS-LNAV", "QZS-CNAV", "IRN-LNAV" or "IRN-L1NV" |
| `week` | `number` | week number as the counter shows it, 0 to the counter's modulus − 1 |
| `timeOfWeekSeconds` | `number` | seconds since the start of the week, 0 up to but not including 604 800, rounded to the nearest nanosecond; a value that rounds to 604 800 s returns "" |
| `rolloverEra` | `RolloverEra` | the counter's pass: an integer from 0, "rolling" (within half a counter period of today, or era 0, when the week would otherwise fall before the system's epoch), or `{ notBefore }` with an ISO 8601 UTC instant |

## Returns

ISO 8601 UTC instant string (`Z`), or "" on invalid input

## Types

### RolloverEra

Which pass of a wrapping week counter a week number belongs to, in one of three forms. An integer from 0 is the era itself (era 0 starts at the system's epoch). `"rolling"` picks the era that puts the week within half a counter period of today, or era 0, when the week would otherwise fall before the system's epoch; it is for live receiver output and reads the clock, so the same stored value can give a different answer in a later year. `{ notBefore }` picks the earliest occurrence at or after an instant you state (a receiver's firmware build date, or the start of a recorded log) and gives the same answer in any year.

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

```ts
type RolloverEra =
  | number
  | "rolling"
  | {
      /**
       * An ISO 8601 UTC instant; a listed leap second (`…T23:59:60Z`) is accepted. The week
       * resolves to the earliest instant at or after it; an instant exactly equal to it counts.
       * Before the counter's epoch it gives era 0. A value that is not an instant returns the
       * sentinel.
       */
      notBefore: string;
    };
```

## Related types

- [`GnssWeekCounter`](/gmt/reference/types/GnssWeekCounter/)

## Examples

```ts
import { gnssWeekToUtc } from "@northguild/gmt/gnss/convert";

gnssWeekToUtc("GPS-LNAV", 100, 0, 0) // "1981-12-05T23:59:59Z"
```

## Source

[gnss/convert/gnssWeekToUtc.ts](https://github.com/northguild/gmt/blob/main/packages/gmt/src/gnss/convert/gnssWeekToUtc.ts)
