- type leap-adjust
- fun leap-adjust/show
- type leaps-table
- fun leaps-table/expire
- fun leaps-table/show
- fun extend
- val leaps-table-pre1972
- val leaps-table-ti
- val leaps-table-y2017
- fun parse-leap-seconds
- fun parse-leap-seconds-dat

Leap second adjustments. For an instant `i`

after `start`

:

`TAI-offset = offset + (drift * days(i - drift-start))`

.

A leap second table describes when UTC leap seconds occur.

fun leaps-table/expire( ^leaps-tablestd/time/utc/leaps-table: V ) : instantstd/time/instant/instant: V

Automatically generated. Retrieves the `expire`

constructor field of the `leaps-tablestd/time/utc/leaps-table: V`

type.

Leap second table upto (but not including) 1972-01-01 UTC.

Default TI leaps table has leap second information up to the compiler release (currently `leaps-table-y2017std/time/utc/leaps-table-y2017: leaps-table`

).

Leap second table up to 2017-01-01Z.

fun parse-leap-seconds-dat( leaps : stringstd/core/types/string: V ) : leaps-tablestd/time/utc/leaps-table: V

Parse the standard UTC leap second adjustment file in the “old” .dat format as in https://maia.usno.navy.mil/ser7/tai-utc.dat, where entries have the shape

`1961 JAN 1 =JD 2437300.5 TAI-UTC= 1.4228180 S + (MJD - 37300.) X 0.001296 S`

which specifies the start time (`jdstd/time/instant/jd: (i : instant, ts : timescale) -> ddouble 2437300.5`

), new TAI-UTC offset
(`1.4228180`

s), and the drift, starting at `37300`

MJD of 0.001296s per day.
Lines that start with `#`

are comments. As an extension you can have an
expiration date on a line that starts with `#@`

followed by seconds since
the NTP epoch (1900-01-01). Just as in a standard IERS leap second file.

fun ts-ntp-create( leaps : leaps-tablestd/time/utc/leaps-table: V ) : timescalestd/time/instant/timescale: V

NTP time scale is equal
to the UTC time scale (`ts-utc`

).

fun ts-unix-create( leaps : leaps-tablestd/time/utc/leaps-table: V ) : timescalestd/time/instant/timescale: V

Unix time scale is equal
to the UTC time scale (`ts-utc`

).

fun ts-utc-create( leaps : leaps-tablestd/time/utc/leaps-table: V ) : timescalestd/time/instant/timescale: V

The UTC time scale.

fun ts-utc-sls-create( leaps : leaps-tablestd/time/utc/leaps-table: V ) : timescalestd/time/instant/timescale: V

Create a new UTC-SLS time scale from a provided leap second table `leaps`

.
Implements a UTC time scale except without ever showing leap seconds.
UTC-SLS is equivalent to UTC except for the last 1000 seconds of a day where
a leap second occurs. On such day, the leap second time step (positive or negative)
is distributed over the last 1000 seconds of the day. On the full hour, UTC
and UTC-SLS are equal again.

This is a recommended time scale to use for time stamps or communication with other services since it avoids any potential trouble with leap seconds while still being quite precise. See also: https://www.cl.cam.ac.uk/~mgk25/time/utc-sls.

You can create a UTC-SLS time scale based on the latest IERS leap second
data using `cal-utc-sls-load`

.

type utcstd/time/utc/utc: (E, V) -> V<e,a>

Return the UTC timescale with the latest leap second information.

Operations:

fun utc fun utcstd/time/utc/utc: () -> utc timescale.

Call the `fun utcstd/time/utc/utc: () -> utc timescale`

operation of the effect `utcstd/time/utc/utc: (E, V) -> V`

.

`utc-timestampstd/time/utc/utc-timestamp: V`

is UTC time since 2000-01-01.

fun float64/unix-instant( u : float64std/core/types/float64: V, frac : ? float64std/core/types/float64: V, ts : ? timescalestd/time/instant/timescale: V ) : instantstd/time/instant/instant: V

Given a Unix time stamp in (fractional) seconds (`secs`

) and an optional separate
fraction of seconds `frac`

(for increased precision for nanosecond timestamps), return an `instantstd/time/instant/instant: V`

.
that is `secs + frac`

seconds after the Unix epoch (`1970-01-01Z`

).

Unfortunately, Unix time stamps are ambigious.
The seconds `secs`

are interpreted as: `val days = secs / 86400`

and `val dsecs = secs % 86400`

,
where `days`

is the number of days since `1970-01-01Z`

and `dsecs`

is the SI seconds into
the day. This means that one cannot represent a possible extra leap second since it
will look as the first second of the next day. For example, here is how the time stamps look
around the leap second of `1973-01-01Z`

:

```
> instant(1972,12,31,23,59,59).unix-timestamp
94694399
> instant(1972,12,31,23,59,60).unix-timestamp
94694400
> instant(1973,1,1).unix-timestamp
94694400
```

Internally, this library uses proper `timestampstd/time/timestamp/timestamp: V`

s that *can* keep track of leap seconds.
To indicate a time in a leap second, you can use a fraction `frac`

that is larger than `1.0`

. For example:

```
> unix-instant(94694399.0).time
1972-12-31T23:59:59Z
> unix-instant(94694399.0,1.0).time
1972-12-31T23:59:60Z
> unix-instant(94694400.0).time
1973-01-01T00:00:00Z
```

This works well for systems that support `CLOCK_UTC`

.

fun int/unix-instant( u : intstd/core/types/int: V, frac : ? float64std/core/types/float64: V, ts : ? timescalestd/time/instant/timescale: V ) : instantstd/time/instant/instant: V

Create an instant from raw unix seconds since the unix epoch (1970-01-01T00:00:10 TAI)
Use a fraction `> 1`

to indicate a time inside a leap second.

fun timespan/unix-instant( t : timespanstd/time/timestamp/timespan: V, leap : ? intstd/core/types/int: V, ts : ? timescalestd/time/instant/timescale: V ) : instantstd/time/instant/instant: V

Create an instant from raw unix seconds since the unix epoch (1970-01-01T00:00:10 TAI) and optional leap seconds to designate instants inside a leap seconds.

fun get-leap-steps( table : ? leaps-tablestd/time/utc/leaps-table: V ) : liststd/core/types/list: V -> V<(utc-timestampstd/time/utc/utc-timestamp: V, timespanstd/time/timestamp/timespan: V, timespanstd/time/timestamp/timespan: V, (timespanstd/time/timestamp/timespan: V, utc-timestampstd/time/utc/utc-timestamp: V, ddoublestd/num/ddouble/ddouble: V))>

Get a list of leap second steps in a triple, NTP start time, offset just before, and the new offset at that time, the base offset, the drift start date and the drift rate.

fun ntp-instant( ntp : ddoublestd/num/ddouble/ddouble: V, leap : ? intstd/core/types/int: V ) : instantstd/time/instant/instant: V

Convert an NTP time in seconds since the NTP epoch (1900-01-01Z) to an instant.
Also takes an optional `leapstd/time/timestamp/leap: (t : timestamp) -> int`

argument if the NTP time is inside a leap second.

Return the NTP time of an instant since the NTP epoch (1900-01-01) Since NTP time stamps are ambigious, times inside a leap second show as occurring in the second after the leap second.

NTP time scale.
It equals the `ts-tistd/time/utc/ts-ti: timescale`

time scale.

The TI (*International Time*) time scale with a 2000-01-01Z UTC epoch.
This is the default time scale used in this library. It was a
proposed time scale at the 2004 ITU-R meeting as a replacement of UTC
without future leap seconds. In this library, we define TI to match
exactly UTC up to the compiler release date (currently 2017) but ignore any
possible future leap seconds after that date. This is the preferred time scale
in this library as it guarantees deterministic time calculations for any
future date, i.e. before 2017-01-01Z, TI == UTC, while after that, TI == TAI - 37s.

TI time scale with smoothed leap seconds.

Implements a TI time scale (`ts-tistd/time/utc/ts-ti: timescale`

) except without ever showing leap seconds.
TI-SLS is equivalent to TI except for the last 1000 seconds of a day where
a leap second occurs. On such day, the leap second time step (positive or negative)
is distributed over the last 1000 seconds of the day. On the full hour, TI
and TI-SLS are equal again.

Unix time scale based on Unix seconds.
It equals the `ts-tistd/time/utc/ts-ti: timescale`

time scale.

Get a standard Unix timestamp in fractional seconds since the Unix epoch (1970-01-01Z). Since Unix time stamps are ambigioous, instants inside a leap seconds show as occurring in the second after that.

fun utc-timescale( name : stringstd/core/types/string: V, leaps : leaps-tablestd/time/utc/leaps-table: V ) : timescalestd/time/instant/timescale: V

Create a new time scale based on UTC seconds with a given `name`

and a leap second table.

private import std/core/typesstd/core/types, std/core/hndstd/core/hnd, std/core/exnstd/core/exn, std/core/boolstd/core/bool, std/core/orderstd/core/order, std/core/charstd/core/char, std/core/intstd/core/int, std/core/vectorstd/core/vector, std/core/stringstd/core/string, std/core/sslicestd/core/sslice, std/core/liststd/core/list, std/core/maybestd/core/maybe, std/core/eitherstd/core/either, std/core/tuplestd/core/tuple, std/core/showstd/core/show, std/core/debugstd/core/debug, std/core/delayedstd/core/delayed, std/core/consolestd/core/console, std/corestd/core, std/num/float64std/num/float64, std/num/ddoublestd/num/ddouble, std/text/parsestd/text/parse, std/time/timestampstd/time/timestamp, std/time/durationstd/time/duration, std/time/instantstd/time/instant

^{1.}The difference TAI-UT2 was supposed to be zero on 1958-01-01Z TAI but different
observatories used their own versions of UT2 leading to longitude errors
of a “few 0.01s” [3]. At 1958-01-01 TAI the ΔT = TT - UT1 was 31.166s ± 0.003s
(see historical ΔT),
which equals 1958-01-01T00:00:00.018 UT1 time.
When we calculate UT2 using the current definition [2, page 85]:

UT2 = UT1 + 0.022·sin(2πτ) - 0.012·cos(2πτ) - 0.006·sin(4πτ) + 0.007·cos(4πτ)

where τ is the fraction of the Besselian year [4]:

τ = 1900.0 + (JD

_{TT}- 2415020.31352) / 365.242198781

we get 1958-01-01T00:00:00.013023922Z UT2. A difference of about 0.013s with TAI. ↩

## UTC time scales and leap second support.

## 1. UTC time calculation

The world's standard time scale is Universal Coordinated Time (UTC). UTC is directly based on International Atomic Time (TAI) (

`ts-taistd/time/instant/ts-tai: timescale`

) and uses standard SI seconds. The UTC time scale differs from TAI in that UTC stays within 1 second of UT1; the time scale based on the rotation of the Earth. To keep UTC close to UT1 about every 1.5 year aleap secondis added to the day (appearing as a 60th second in the last minute of the day, e.g. 23:59:60h).In order to calculate SI second durations between time instants, we need to know when a leap second is inserted. For example, there was a leap second inserted on 2017-01-01Z, so:

equals 3601 SI seconds to account for the extra leap second. We also need to know the inserted leap seconds to convert UTC time to TAI which forms the basis of many other time scales (see

`std/time/astro`

).The occurrence of leap seconds cannot be reliably predicted though and the IERS usually announces leap seconds about half a year in advance. The IERS leap second table contains a list of all currently announced leap seconds.

As such, any future duration calculation in UTC is essentially non-deterministic since it may be off by a number of (as yet unannounced) leap seconds. This means in practice:You cannot reliably tell how many SI seconds in the future a certain UTC time occurs (since there may be leap seconds inserted in the future). If you need to store a future date, then store it as an ISO calendar date, like

`2020-01-01T12:00:00Z`

, not as a time stamp.Do not use Unix time stamps as those may be ambiguous (see

`unix-instant`

); this library already defaults to time stamps as SI seconds since`epochstd/time/instant/epoch: instant`

which are monotonic and unambiguous. If no interaction with the user is needed, consider using TAI time and calendar instead.## 1.1. Effect types and future leap seconds

Since UTC time calculations depend on a leap second table (

`leaps-tablestd/time/utc/leaps-table: V`

), the UTC time scale can only be created from such table (`ts-utc-createstd/time/utc/ts-utc-create: (leaps : leaps-table) -> timescale`

). For most precise UTC time, you can use`ts-utc-load`

to automatically download and cache the latest IERS leap second information. This can be somewhat cumbersome though as this is a function with an`iostd/core/io: E`

effect.International Time (TI) (or

Temps International) is a proposed time scale that matches exactly UTC up to some date but with no further leap seconds added after that – which makes time calculations robust and deterministic with regard to future dates. For this reason, this library defaults to using TI instead of UTC. The TI time scale (`ts-tistd/time/utc/ts-ti: timescale`

) is defined to exactly match UTC before the compiler release date (currently 2019) but ignores any future leap seconds.## 1.2. UTC between 1961 and 1972

UTC only got integral leap seconds after 1972-01-01Z. Before 1972, the UTC time was broadcast using a combination of fixed time steps (i.e.

mini leap seconds) and an offset from the atomic clock frequency. By adjusting the frequency, UTC time was effectively running at a linearly adjusted rate relative to TAI. We call this adjustmentdrift. For example, between 1963-11-01Z and 1964-01-01Z the frequency offset was -130×10^{-10}which means UTC was running ahead of TAI by 0.0011232 seconds per day. The drift was then defined as:(TAI-UTC) = 1.9458580 + 0.0011232×Δ(1962-01-01)

where the function Δ(

date) returns the number of days sincedate. Therefore, the difference started as 2.6972788s (as there were 669 days between 1962-01-01 and 1963-11-01) and increased linearly up to 2.7657940s on 1964-01-01Z. For dates between 1961-01-01Z and 1972-01-01Z the library calculates the UTC time based on the historical USNO drift data (see Table 1).Here are some interesting time steps that occurred in this time frame:

There was a negative time step of -0.1s inserted on 1968-02-01Z:

The only other negative time step was inserted on 1961-08-01Z with a duration of -0.05s.

On the transition to modern UTC at 1972-01-01Z there is a mini leap second of 0.107758s. (because the final drift just before 1971-01-01 is 9.892242s)

UTC was only established in 1960 (Arias and Guinot [1]), and the TAI instant 1961-01-01 00:00:01.422818Z TAI was set to be UTC instant 1961-01-01Z exactly.

## 1.3. UTC before 1961

The TAI timescale was established with a 1958-01-01Z epoch, where the difference (UT2 - TAI) was set to be approximately zero

^{1}. In the time frame 1958 up to 1961 the relation between TAI and UTC is not formally defined.We can however use the time steps and frequency adjustments as broadcast by the NIST WWV radio station (see Explanatory Supplement to the Astronomical Almanac [2, pages 86–87]). Historically there were 18 20ms time steps from 1958. The frequency offset in the year 1958 is not clear, and is described in the Supplement as “.. an offset of

about-100×10^{-10}during 1958”. We fixed the offset at -85×10^{-10}in order to have a zero difference from TAI at exactly 1958-01-01Z.In the library, this makes UTC coincide with TAI up to 1958-01-01, interpolated with ‘drift’ up to 1972-01-01, and using integral leap-seconds after that.

Interestingly, in the time before 1961, most leap steps occurred at 19:00h instead of at the last second of the day, for example, on 1959-01-28 there was a 0.02s leap step:

showing the seconds at 60 just before 19:00h.

## 1.4. All historical leap second adjustments

Table 1 shows all leap second time steps from 1958 up to 2017. This table is calculated and uses historical data for time steps before 1972.

^{-10}^{-10}^{-10}^{-10}^{-10}^{-10}^{-10}^{-10}^{-10}^{-10}^{-10}^{-10}^{-10}^{-10}^{-10}^{-10}^{-10}^{-10}^{-10}^{-10}^{-10}^{-10}^{-10}^{-10}^{-10}^{-10}^{-10}^{-10}^{-10}^{-10}^{-10}^{-10}^{-10}^{-10}Table 1.Leap second time steps since 1958. The function Δ(date) returns the number of days sincedate. Entries after 1972 are based on IERS data. The entries before 1972 are derived from theExplanatory Supplement to the Astronomical Almanac, by P. Kenneth Seidelmann [2, pages 86–87]. Entries before 1961 are based on the time steps broadcast by the NIST WWV radio station.– Daan Leijen, 2016.

## References

Coordinated universal time UTC: historical background and perspectives. Journées Systèmes de Référence Spatio-Temporels. 2004. pdf. 🔎Explanatory Supplement to the Astronomical Almanac. University Science Books, 1992. book 🔎History of the Bureau International de l'Heure. In “Polar Motion: Historical and Scientific Problems”, ASP Conference Series, Vol. 208, Edited by Steven Dick, Dennis McCarthy, and Brian Luzum. ISBN: 1-58381-039-0, 2000., p.175. pdf 🔎Precession Matrix Based on IAU(1976). System of Astronomical Constants, Astronomy & Astrophysics, Vol. 73, pages 282–284. 1979. wikipedia 🔎.