[Tutor] python at midnight
Steven D'Aprano
steve at pearwood.info
Sun Nov 14 23:48:31 CET 2010
ingo wrote:
[...]
> Questions,
> Is there a date time library that accepts the 24:00? mxDateTime doesn't.
I don't know of any.
> Is there a way to set the limit 'from the outside' (subclassing???) or
> a simple way around?
Write a wrapper function:
#Untested
def datetime24(*args):
try:
return datetime.datetime(*args)
except ValueError:
args = list(args)
if len(args) >= 4 and args[3] == 24:
if ((not args[4:5] or args[4] == 0) and
(not args[5:6] or args[5] == 0)):
args[3] = 0
yesterday = datetime.datetime(*args)
return yesterday + datetime.timedelta(1, 0, 0)
raise
This catches the error raised by the datetime object. If the hour is 24
and the minute and second arguments either don't exist, or are 0, it
calculates the datetime from 0:00 instead of 24:00, then adds one day to
it. Otherwise it re-raises the error.
> How to get this functionality added to Python?
Make a feature request on the Python bug tracker:
http://bugs.python.org/
--
Steven
More information about the Tutor
mailing list