mxDateTime format string

Alex Martelli aleaxit at
Mon Nov 13 11:50:27 CET 2000

"Martin Christensen" <knightsofspamalot-factotum at> wrote in
message news:87lmur4hy7.fsf at
> Howdy!
> I have about 35700 time strings that look not entirely unlike 'Sun Jul
> 16, 2000 8:23am', and I need to convert them to ISO standard time. The
> day of the week is just in the way, and can be easily removed, but
> what the rest is concerned, is there a time format string that matches
> this format for easy conversion? It can, of course, be done manually,
> but I'm sure that mxDateTime could do it faster for me. Apropos les

I think it could _on platforms where strptime is supported_, but not
portably, I think; I believe the Parser subpackage does not currently
support the 'am' notation (I'll be glad to be proved wrong, though!-).

One workable, portable approach might be...:

>>> # stand-in for 'for x in hugelistofdatestrings:'...:-)
>>> x='Sun Jul 16, 2000 8:23am'
>>> print

2000-07-16 08:23:00.00

If your starting datetimes are *not* GMT, you'll use different
'timezone' offset-numbers in lieu of +00 and +12, of course.

Or, you could tweak to recognize 'am' and 'pm'
directly as timezones, I guess.  That could be faster than
all those .replace calls.  I would NOT recommend modifying
the sources; however, for a given run...:

>>> def ampm(amorpm):
 if amorpm=='pm': return DateTime.TimeDelta(12)
 else: return DateTime.TimeDelta()

>>> DateTime.Timezone.utc_offset=ampm
>>> print DateTime.Parser.DateTimeFromString(x)
2000-07-16 08:23:00.00
>>> could just substitute on-the-fly the function
that utc_offset is bound to, knowing exactly HOW it
will be called those 37,500 times... (ain't Python
just wonderful?! you can get such "polymorphism"
effects, _without_ sourcechanges to distributed
packages, and even when classes and instances
thereof aren't in play...!-).

[Do recall to do this in a try/finally, re-binding
the original function to utc_offset, if you then
need to keep using mxDateTime "normally"!-)]


More information about the Python-list mailing list