Skip to content

Commit ec98320

Browse files
authored
Merge pull request #11143 from maxnoe/unix_time_ptp
Add TimeUnixPTP to time formats
2 parents 9603143 + 36c0f60 commit ec98320

4 files changed

Lines changed: 62 additions & 6 deletions

File tree

‎CHANGES.rst‎

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -68,6 +68,9 @@ astropy.tests
6868
astropy.time
6969
^^^^^^^^^^^^
7070

71+
- Add new time format ``unix_ptp`` (Precision Time Protocol) which provides
72+
the number of SI seconds since ``1970-01-01T00:00:00 TAI``. [#11143]
73+
7174
astropy.timeseries
7275
^^^^^^^^^^^^^^^^^^
7376

‎astropy/time/core.py‎

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -467,9 +467,9 @@ def format(self):
467467
that could be used for initialization. These can be listed with::
468468
469469
>>> list(Time.FORMATS)
470-
['jd', 'mjd', 'decimalyear', 'unix', 'unix_tai', 'cxcsec', 'gps', 'plot_date',
471-
'stardate', 'datetime', 'ymdhms', 'iso', 'isot', 'yday', 'datetime64',
472-
'fits', 'byear', 'jyear', 'byear_str', 'jyear_str']
470+
['jd', 'mjd', 'decimalyear', 'unix', 'unix_tai', 'unix_ptp', 'cxcsec',
471+
'gps', 'plot_date', 'stardate', 'datetime', 'ymdhms', 'iso', 'isot',
472+
'yday', 'datetime64', 'fits', 'byear', 'jyear', 'byear_str', 'jyear_str']
473473
"""
474474
return self._format
475475

@@ -1430,9 +1430,9 @@ class Time(TimeBase):
14301430
The allowed values for ``format`` can be listed with::
14311431
14321432
>>> list(Time.FORMATS)
1433-
['jd', 'mjd', 'decimalyear', 'unix', 'unix_tai', 'cxcsec', 'gps', 'plot_date',
1434-
'stardate', 'datetime', 'ymdhms', 'iso', 'isot', 'yday', 'datetime64',
1435-
'fits', 'byear', 'jyear', 'byear_str', 'jyear_str']
1433+
['jd', 'mjd', 'decimalyear', 'unix', 'unix_tai', 'unix_ptp', 'cxcsec',
1434+
'gps', 'plot_date', 'stardate', 'datetime', 'ymdhms', 'iso', 'isot',
1435+
'yday', 'datetime64', 'fits', 'byear', 'jyear', 'byear_str', 'jyear_str']
14361436
14371437
See also: http://docs.astropy.org/en/stable/time/
14381438

‎astropy/time/formats.py‎

Lines changed: 46 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -745,6 +745,52 @@ class TimeUnixTai(TimeUnix):
745745
epoch_scale = 'tai'
746746

747747

748+
class TimeUnixPTP(TimeUnix):
749+
"""
750+
Precision Time Protocol: SI seconds elapsed from 1970-01-01 00:00:00 TAI.
751+
752+
This convention matches the definition of the Precision Time Protocol,
753+
https://en.wikipedia.org/wiki/Precision_Time_Protocol and
754+
is also used by the White Rabbit protocol in High Energy Physics:
755+
https://white-rabbit.web.cern.ch/
756+
757+
For more information on this time format definition, see:
758+
https://www-acc.gsi.de/wiki/Timing/TimingSystemButisInterface#Time
759+
760+
This format is basically the same as the ``unix_tai`` with an offset of
761+
8 seconds due to the different epoch (1970-01-01 00:00:00 UTC vs. TAI).
762+
763+
This will generally differ from Unix time by the cumulative integral number
764+
of leap seconds since 1970-01-01 UTC plus the initial offset of 8 seconds.
765+
766+
Caveats:
767+
- Before 1972, fractional adjustments to UTC were made, so the difference
768+
between ``unix`` and ``unix_ptp`` time is no longer an integer.
769+
- Because of the fractional adjustments, to be very precise, ``unix_ptp``
770+
is the number of seconds since ``1970-01-01 00:00:00 TAI`` or equivalently
771+
``1969-12-31 23:59:51.999918 UTC``. The difference between TAI and UTC
772+
at that epoch was 8.000082 sec.
773+
- On the day of a leap second the difference between ``unix`` and ``unix_ptp``
774+
times increases linearly through the day by 1.0. See also the
775+
documentation for the `~astropy.time.TimeUnix` class.
776+
777+
Examples
778+
--------
779+
780+
>>> from astropy.time import Time
781+
>>> t = Time('2020-01-01', scale='utc')
782+
>>> t.unix_ptp - t.unix
783+
37.0
784+
785+
>>> t = Time('1970-01-01', scale='utc')
786+
>>> t.unix_ptp - t.unix # doctest: +FLOAT_CMP
787+
8.000082
788+
"""
789+
name = 'unix_ptp'
790+
epoch_val = '1970-01-01 00:00:00'
791+
epoch_scale = 'tai'
792+
793+
748794
class TimeCxcSec(TimeFromEpoch):
749795
"""
750796
Chandra X-ray Center seconds from 1998-01-01 00:00:00 TT.

‎astropy/time/tests/test_basic.py‎

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1435,6 +1435,13 @@ def test_unix_tai_format():
14351435
assert allclose_sec(t.unix_tai - t.unix, 8.2e-05)
14361436

14371437

1438+
def test_unix_ptp_format():
1439+
t = Time('2020-01-01', scale='utc')
1440+
assert allclose_sec(t.unix_ptp - t.unix_tai, 8)
1441+
t = Time('1970-01-01', scale='utc')
1442+
assert allclose_sec(t.unix_ptp - t.unix_tai, 8)
1443+
1444+
14381445
def test_set_format_shares_subfmt():
14391446
"""
14401447
Set format and round trip through a format that shares out_subfmt

0 commit comments

Comments
 (0)