Transition from datetime to astropy.time

The SunPy project is very happy to announce that the time handling in SunPy will be transitioned from python datetime module to astropy.time module. The changes for this transition are under review in the pull request #2691. These changes are scheduled for SunPy 1.0.

This comes with numerous benefits:

  • Support for non-UTC time scales.

    UTC as well as non-UTC time scales like TAI, TT, UT1 etc. can be used with astropy.time.Time.

    >>> t = Time('2012-06-18T02:00:05.453', scale='tai')
    >>> t
    <Time object: scale='tai' format='isot' value=2012-06-18T02:00:05.453>

    Time also provides easy conversion between different scales.

    >>> t.utc
    <Time object: scale='utc' format='isot' value=2012-06-18T01:59:31.453>
  • Support for high precision times.

    Time can provide sub-nanosecond precision for time objects while python datetime was restricted to microseconds.

    >>> t = Time('2012-06-18T02:00:05.453123123')
    >>> t
    <Time object: scale='utc' format='isot' value=2012-06-18T02:00:05.453>
    >>> t.precision = 9
    >>> t
    <Time object: scale='utc' format='isot' value=2012-06-18T02:00:05.453123123>
  • Support for leap seconds

    This was one of the bigger motivation for the transition to astropy.time.Time. datetime has no support for leap second while Time supports leap seconds. A leap second is a one-second adjustment applied to UTC to keep it close to the mean solar time.

    >>> Time('2016-12-31T23:59:60')
    <Time object: scale='utc' format='isot' value=2016-12-31T23:59:60.000>
    >>> Time('2016-12-31T23:59:59') + 1 * u.s
    <Time object: scale='utc' format='isot' value=2016-12-31T23:59:60.000>
  • Support for numerous formats

    Time can parse numerous formats including python datetime.

    >>> list(Time.FORMATS)
    ['jd', 'mjd', 'decimalyear', 'unix', 'cxcsec', 'gps', 'plot_date', 'datetime', 'iso', 'isot', 'yday', 'fits', 'byear', 'jyear', 'byear_str', 'jyear_str']
    >>> import datetime
    >>> Time(
    <Time object: scale='utc' format='datetime' value=2018-10-20 15:36:16.364089>

Changes to SunPy

The work on the transtion from datetime to astropy.time.Time was done as a part of Vishnunarayan’s Google Summer of Code 2018 project. To see more about the project:


comments powered by Disqus