# [AstroPy] How to compute TAI-UTC using Astropy?

Russell Owen rowen at uw.edu
Sat Aug 24 19:24:26 EDT 2019

```Thank you for the suggestion, but I’m afraid I don’t see how it works.. The given date I have to work from is a date in UTC. By specifying “Time(date, scale=“tai”)” I would be lying to the system: saying the date was TAI when it is UTC. I would expect that to give the wrong answer very near a UTC leap second.

My code didn’t work at leap second either, but this code does:

def tai_from_utc(utc):
"""Return TAI in unix seconds, given UTC in unix seconds.
"""
astropy_utc = astropy.time.Time(utc, scale="utc", format="unix")
dt_utc = astropy_utc.utc.to_datetime()
dt_tai = astropy_utc.tai.to_datetime()
tai_minus_utc = (dt_tai - dt_utc).total_seconds()
return utc + tai_minus_utc

It seems quite clumsy to have to use datetime objects *and* astropy. I’d welcome a cleaner solution!

Regards,

Russell

> On Aug 24, 2019, at 2:12 PM, Aldcroft, Thomas <aldcroft at head.cfa.harvard.edu> wrote:
>
> Hi Russell,
>
> I think what you want is:
>
> In [1]: from astropy.time import Time
> In [2]: date = '2019-08-24 00:00:00'
> In [3]: dt = Time(date, scale='utc') - Time(date, scale='tai')
> In [4]: dt.sec
> Out[4]: 36.99999999999779
>
> - Tom
>
> On Fri, Aug 23, 2019 at 6:46 PM Russell Owen <rowen at uw.edu <mailto:rowen at uw.edu>> wrote:
> We do some work with TAI times as unix seconds. Possibly controversial, but it’s what we use.
>
> We sometimes need to convert normal unix times (UTC) to this standard.
>
> This naive solution does not work:
>
> def bogus_tai_from_utc(utc):
>     “””Failed attempt to return TAI in unix seconds given UTC in unix seconds.
>
>     This fails because given an astropy.time.Time astropy_time:
>     astropy_time.tai.unix == astropy_time.utc.unix
>     """
>     return astropy.time.Time(utc, format=“unix”, scale=“utc”).tai.unix
>
> Is there a nice way to ask AstroPy the value of TAI-UTC in seconds, given a UTC?
> If so, this would be perfect:
>
> tai_from_utc(utc):
>     tai_minus_utc = …? (presently 37 seconds)
>     return utc + tai_minus_utc
>
> The following ugly code works, but has error at the microsecond level and may not work at leap seconds:
>
> tai_minus_utc = (astropy_utc.tai.mjd - astropy_utc.utc.mjd)*24*60*60
>
> Regards,
>
> Russell
> _______________________________________________
> AstroPy mailing list
> AstroPy at python.org <mailto:AstroPy at python.org>
> https://mail.python.org/mailman/listinfo/astropy <https://mail.python.org/mailman/listinfo/astropy>
> _______________________________________________
> AstroPy mailing list
> AstroPy at python.org
> https://mail.python.org/mailman/listinfo/astropy

-------------- next part --------------
An HTML attachment was scrubbed...