[AstroPy] Time custom formats
aldcroft at head.cfa.harvard.edu
Mon Feb 22 07:07:48 EST 2016
What you want is exactly a custom format, and in this case it's quite easy
to get both input and output representation in the format you want. This
is just copied from the existing TimeYearDayTime class and modified
slightly. When you define the class like this it is automatically
registered as an available format like the rest, including automatic input
parsing when possible.
from astropy.time import TimeISO
Year, day-of-year and time as "<YYYY>-<DOY>T<HH>:<MM>:<SS.sss...>".
The day-of-year (DOY) goes from 001 to 365 (366 in leap years).
For example, 2000-001T00:00:00.000 is midnight on January 1, 2000.
The allowed subformats are:
- 'date_hms': date + hours, mins, secs (and optional fractional secs)
- 'date_hm': date + hours, mins
- 'date': date
name = 'yday_custom'
subfmts = (('date_hms',
In : t = Time.now()
In : t.yday_custom
In : t2 = Time('2016-001T00:00:00')
In : t2.iso
Out: '2016-01-01 00:00:00.000'
On Fri, Feb 19, 2016 at 8:51 AM, Paul Ray <paul.ray at nrl.navy.mil> wrote:
> I've been trying to figure out how to print an astropy.Time with a custom
> string format and have not been able to.
> In : t = astropy.time.Time.now()
> In : t.yday
> Out: '2016:050:13:45:21.260'
> Now, what if I want the string '2016-050T13:45:21' instead?
> What I'd like is basically the astropy equivalent of the C library
> I see there is a way to make a new subclass of TimeFormat, but I don't
> really want a whole new format, just a way to customize the string
> representation (possibly for input as well as output). Maybe all I need is
> an example of how to subclass TimeFormat to accomplish that. I had some
> trouble getting it to work in the little experimenting I did.
> Thanks for any tips,
> -- Paul
> AstroPy mailing list
> AstroPy at scipy.org
-------------- next part --------------
An HTML attachment was scrubbed...
More information about the AstroPy