mxDateTime format string

Alex Martelli aleaxit at yahoo.com
Mon Nov 13 05:50:27 EST 2000


"Martin Christensen" <knightsofspamalot-factotum at mail1.stofanet.dk> wrote in
message news:87lmur4hy7.fsf at fangorn.stofanet.dk...
> 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
DateTime.Parser.DateTimeFromString(x.replace('am','+00').replace('pm','+12')
)

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 Timezone.py 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 Timezone.py 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
>>>

...you 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"!-)]


Alex






More information about the Python-list mailing list