[Python-3000] __format__ and datetime

Paul Moore p.f.moore at gmail.com
Mon Sep 10 17:29:56 CEST 2007


On 10/09/2007, Eric Smith <eric+python-dev at trueblade.com> wrote:
> Martin v. Löwis wrote:
> >> I have a patch to add __format__ to datetime.time, .date, and .datetime.
> >>   For non-empty format_spec's, I just pass on to .strftime.  For empty
> >> format_spec's, it returns str(self).
> >>
> >> I think this is the only reasonable interpretation of format_spec's for
> >> datetime.  Does anyone think otherwise?
> >
> > Can you please show an example of how it would look like?
>
>  >>> import datetime
>  >>> format(datetime.datetime.now(), 'date: %Y-%m-%d time:%H:%M:%s')
> 'date: 2007-09-10 time:11:15:1189437339'
>  >>> format(datetime.datetime.now(), '')
> '2007-09-10T11:15:51.329639'

I'd like to see the default format specified (somewhere). I note that
the default format for datetime values seems to differ for me (on
3.0a1 on Windows)

Python 3.0a1 (py3k:57844, Aug 31 2007, 16:54:27) [MSC v.1310 32 bit
(Intel)] on win32
Type "help", "copyright", "credits" or "license" for more information.
>>> import datetime
>>> str(datetime.datetime.now())
'2007-09-10 16:26:25.218000'

(Note lack of 'T'). I'm not sure I like 6 decimal places of seconds to
be the default format, either, but consistency (with str()) and
accuracy (however extreme) may be more important here...

The date and time defaults (which appear to be %Y-%m-%d and %H:%M:%s)
seem perfectly acceptable, on the other hand.

Paul.


More information about the Python-3000 mailing list