<div dir="ltr">Hi Paul,<div><br></div><div>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.</div><div><br></div><div><div>f<font face="monospace, monospace">rom astropy.time import TimeISO</font></div><div><font face="monospace, monospace"><br></font></div><div><font face="monospace, monospace">class TimeYearDayTimeCustom(TimeISO):</font></div><div><font face="monospace, monospace">    """</font></div><div><font face="monospace, monospace">    Year, day-of-year and time as "<YYYY>-<DOY>T<HH>:<MM>:<SS.sss...>".</font></div><div><font face="monospace, monospace">    The day-of-year (DOY) goes from 001 to 365 (366 in leap years).</font></div><div><font face="monospace, monospace">    For example, 2000-001T00:00:00.000 is midnight on January 1, 2000.</font></div><div><font face="monospace, monospace">    The allowed subformats are:</font></div><div><font face="monospace, monospace">    - 'date_hms': date + hours, mins, secs (and optional fractional secs)</font></div><div><font face="monospace, monospace">    - 'date_hm': date + hours, mins</font></div><div><font face="monospace, monospace">    - 'date': date</font></div><div><font face="monospace, monospace">    """</font></div><div><font face="monospace, monospace"><br></font></div><div><font face="monospace, monospace">    name = 'yday_custom'</font></div><div><font face="monospace, monospace">    subfmts = (('date_hms',</font></div><div><font face="monospace, monospace">                '%Y-%jT%H:%M:%S',</font></div><div><font face="monospace, monospace">                '{year:d}-{yday:03d}T{hour:02d}:{min:02d}:{sec:02d}'),</font></div><div><font face="monospace, monospace">               ('date_hm',</font></div><div><font face="monospace, monospace">                '%Y-%jT%H:%M',</font></div><div><font face="monospace, monospace">                '{year:d}-{yday:03d}T{hour:02d}:{min:02d}'),</font></div><div><font face="monospace, monospace">               ('date',</font></div><div><font face="monospace, monospace">                '%Y-%j',</font></div><div><font face="monospace, monospace">                '{year:d}-{yday:03d}'))</font></div></div><div><font face="monospace, monospace"><br></font></div><div><div><font face="monospace, monospace">In [4]: t = Time.now()</font></div><div><font face="monospace, monospace"><br></font></div><div><font face="monospace, monospace">In [5]: t.yday_custom</font></div><div><font face="monospace, monospace">Out[5]: '2016-053T12:01:41.663'</font></div><div><font face="monospace, monospace"><br></font></div><div><font face="monospace, monospace">In [6]: t2 = Time('2016-001T00:00:00')</font></div><div><font face="monospace, monospace"><br></font></div><div><font face="monospace, monospace">In [7]: t2.iso</font></div><div><font face="monospace, monospace">Out[7]: '2016-01-01 00:00:00.000'</font></div></div><div><br></div><div>- Tom</div><div><br></div><div><br></div><div><br></div><div><br></div></div><div class="gmail_extra"><br><div class="gmail_quote">On Fri, Feb 19, 2016 at 8:51 AM, Paul Ray <span dir="ltr"><<a href="mailto:paul.ray@nrl.navy.mil" target="_blank">paul.ray@nrl.navy.mil</a>></span> wrote:<br><blockquote class="gmail_quote" style="margin:0 0 0 .8ex;border-left:1px #ccc solid;padding-left:1ex">Hi,<br>
<br>
I've been trying to figure out how to print an astropy.Time with a custom string format and have not been able to.<br>
<br>
In [2]: t = astropy.time.Time.now()<br>
<br>
In [3]: t.yday<br>
Out[3]: '2016:050:13:45:21.260'<br>
<br>
Now, what if I want the string '2016-050T13:45:21' instead?<br>
What I'd like is basically the astropy equivalent of the C library strftime().<br>
<br>
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.<br>
<br>
Thanks for any tips,<br>
<br>
-- Paul<br>
<br>
<br>
_______________________________________________<br>
AstroPy mailing list<br>
<a href="mailto:AstroPy@scipy.org">AstroPy@scipy.org</a><br>
<a href="https://mail.scipy.org/mailman/listinfo/astropy" rel="noreferrer" target="_blank">https://mail.scipy.org/mailman/listinfo/astropy</a><br>
</blockquote></div><br></div>