PEP 321: Date/Time Parsing and Formatting

Bengt Richter bokr at oz.net
Mon Nov 17 21:29:16 EST 2003


On Mon, 17 Nov 2003 20:19:12 -0500, "John Roth" <newsgroups at jhrothjr.com> wrote:

>
>"Bengt Richter" <bokr at oz.net> wrote in message
>news:bpbq7h$nf2$0 at 216.39.172.122...
>> On Mon, 17 Nov 2003 17:59:24 -0500, "John Roth" <newsgroups at jhrothjr.com>
>wrote:
>>
>> >
>> >"Gerrit Holl" <gerrit at nl.linux.org> wrote in message
>> >news:mailman.803.1069091744.702.python-list at python.org...
>> >> Hi,
>> >>
>> >> PEP 321 reads:
>> >> > Python 2.3 added a number of simple date and time types in the
>> >> > ``datetime`` module.  There's no support for parsing strings in
>various
>> >> > formats and returning a corresponding instance of one of the types.
>> >> > This PEP proposes adding a family of predefined parsing function for
>> >> > several commonly used date and time formats, and a facility for
>generic
>> >> > parsing.
>> >>
>> >> I was recently surprised by this fact. I don't know why there isn't
>> >> such a function/method. In my opinion, it isn't a question of whether
>> >> to add them or not, but how.
>> >>
>> >> > Input Formats
>> >> > =======================
>> >> >
>> >> > Useful formats to support include:
>> >> >
>> >> > * `ISO8601`_
>> >> > * ARPA/`RFC2822`_
>> >> > * `ctime`_
>> >> > * Formats commonly written by humans such as the American
>> >> >   "MM/DD/YYYY", the European "YYYY/MM/DD", and variants such as
>> >> >   "DD-Month-YYYY".
>> >
>> >I didn't notice this going past the first time: YYYY/MM/DD is the
>>                                                               ^--UIAM,
>NOT!
>> >ISO standard format, DD/MM/YYYY is the European variant to
>> >the American MM/DD/YYYY.
>>
>> I thought '-' delimiters were standard, and the yyyy-mm-dd ordering
>standard,
>> and anything else a variant.
>
>I usually don't worry about the delimiters, but you're probably right. The
>ISO
>standard is yyyy-mm-dd, however, the *customary* usage in most of the
>world is dd-mm-yyyy, and in the US it's mm-dd-yyyy. In fact, you'd have
>difficulty finding anyone around these parts who knows that year first
>is a standard, let alone *the* international standard. And I very seldom
>see it on the web sites I visit, regardless of country of origin.
>
Well, I have long had my NT4 set up to do thus:

[18:15] C:\pywk\clp>dir d:\python23\*exe
 Volume in drive D is MS Tools
 Volume Serial Number is 14B9-2636

 Directory of d:\python23

03-10-02  20:03                 20,538 python.exe
03-10-02  20:04                 20,539 pythonw.exe
01-09-28  18:00                164,864 UNWISE.EXE
03-10-02  20:03                 16,384 w9xpopen.exe
               4 File(s)        222,325 bytes
                            170,338,304 bytes free

And a utility takes that as a stream and uses directories to make full paths, and
filters it with a regex ('.' here):

[18:17] C:\pywk\clp>dirf . d:\python23\*exe
03-10-02  20:03     20,538 d:\python23\python.exe
03-10-02  20:04     20,539 d:\python23\pythonw.exe
01-09-28  18:00    164,864 d:\python23\UNWISE.EXE
03-10-02  20:03     16,384 d:\python23\w9xpopen.exe

But the real reason is

[18:17] C:\pywk\clp>dirf . d:\python23\*exe |sort
01-09-28  18:00    164,864 d:\python23\UNWISE.EXE
03-10-02  20:03     16,384 d:\python23\w9xpopen.exe
03-10-02  20:03     20,538 d:\python23\python.exe
03-10-02  20:04     20,539 d:\python23\pythonw.exe

Any order other than y-m-d is brain dead if you are going to sort on the text.
My dirf thing can go into nested dirs, so dirf . what*ever/s/od doesn't make an overall sort,
but piping it to sort does. Of course, pre-2000 comes out at the wrong end of the sort,
but I couldn't convince windows to give me yyyy-mm-dd at the time ;-/

It is often very handy to be able to sort a full directory with full paths, to see things
that happened in time sequence around some event time.

BTW, maybe we could make an msys-style version of the above a platform-independent
os.listdir format? (I.e., d:\python23\python.exe => /d/python23/python.exe on windows).

>John Roth
>>
>> See
>>     http://www.w3.org/TR/NOTE-datetime
>> or
>>     http://www.iso.ch/iso/en/prods-services/popstds/datesandtime.html
>>
>> [...]
>>
>> Regards,
>> Bengt Richter
>
>

Regards,
Bengt Richter




More information about the Python-list mailing list