4

I have time in J2000 format, ie. seconds after noon on 1-1-2000, which I want to convert to UTC time in an array with the format [year-month-day hour:min:sec.millisec]. Is there some function in AstroPy or something similar to do the conversion?

Input: Time in J2000: 559889620.293 seconds

Desired Output: Time in UTC: 2017-09-28 16:53:40.293

Rose
  • 279
  • 1
  • 7
  • 20
  • 2
    If you really mean J2000, it is [defined](http://aa.usno.navy.mil/faq/docs/ICRS_doc.php) as 12:00 January 1, 2000, _terrestrial time_. This is equivalent to 11:58:56.171 UT1. If it matters, you could look up the difference between UT1 and UTC at that time, but it will be no more than ±0.9 s. You also need to consider whether, when the seconds were counted, leap seconds were included in the count or not. – Gerard Ashton Sep 30 '17 at 14:16

3 Answers3

7

If you want to use astropy you could use Time and TimeDelta from astropy.time:

>>> from astropy.time import TimeDelta, Time
>>> from astropy import units as u
>>> (Time(2000, format='jyear') + TimeDelta(559889620.293*u.s)).iso
'2017-09-28 16:53:35.293'

The Time(2000, format='jyear') is a good alternative if you don't want to remember what the baseline for julian dates (noon on 1.1.2000) is.

MSeifert
  • 145,886
  • 38
  • 333
  • 352
  • 2
    You don't actually need `TimeDelta`, and the following will work because you can add time Quantity objects to Time: `(Time('J2000.0') + 559889620.293*u.s).iso` – Tom Aldcroft Oct 03 '17 at 02:26
  • Why this one is 5 seconds before the expected output? – wim Aug 04 '20 at 23:21
  • @wim It looks like Astropy is ignoring the 5 leap-seconds between J2000 & 2017-09-28. Here's a machine-readable leap-seconds table, https://www.ietf.org/timezones/data/leap-seconds.list Also at https://raw.githubusercontent.com/eggert/tz/main/leap-seconds.list (Note that Paul Eggert is the [IANA TZ coordinator](https://www.iana.org/time-zones)). Here's some [Python code I wrote that parses that list](https://gist.github.com/PM2Ring/9dc0fa6a39dafb88b9e30f514b0dfead) – PM 2Ring Dec 15 '21 at 01:10
5

The offset is constant, so you can just add it:

>>> OFFSET = datetime(2000,1,1,12) - datetime(1970,1,1)
>>> datetime.utcfromtimestamp(559889620.293) + OFFSET
datetime.datetime(2017, 9, 28, 16, 53, 40, 293000)
wim
  • 338,267
  • 99
  • 616
  • 750
  • Note that the datetime module (and [Unix time](https://en.wikipedia.org/wiki/Unix_time)) ignores leap-seconds. Vast amounts of info on leap-seconds & time scales are available on the Lick Observatory site: https://www.ucolick.org/~sla/leapsecs/ – PM 2Ring Dec 15 '21 at 01:28
5

You can simply add a timedelta to a base datetime.

import datetime
datetime.datetime(2000, 1, 1, 12, 0) + datetime.timedelta(seconds=559889620.293)

returns:

datetime.datetime(2017, 9, 28, 16, 53, 40, 293000)
Mark Ransom
  • 299,747
  • 42
  • 398
  • 622