[egenix-users] DateTimeFromString off by one sec?
M.-A. Lemburg
mal at egenix.com
Wed May 7 12:00:55 CEST 2008
On 2008-05-07 10:33, M.-A. Lemburg wrote:
> On 2008-05-06 23:27, Francesco Pierfederici wrote:
>> Hi,
>>
>> I am using mx.DateTime to parse dates and times in ISO format. I have
>> noticed that DateTimeFromString seems to be off by one second if the
>> time string being parsed ends with :59
>>
>>>>> mx.DateTime.Parser.DateTimeFromString('2008-05-06T19:30:09.59')
>> <mx.DateTime.DateTime object for '2008-05-06 19:30:09.58' at 651a8>
>>
>> but
>>
>>>>> mx.DateTime.Parser.DateTimeFromString('2008-05-06T19:30:09.58')
>> <mx.DateTime.DateTime object for '2008-05-06 19:30:09.58' at 8c250>
>>
>> Adding formats=['iso', ] does not change the results. I am using
>> mx.DateTime 3.0 by the way.
>>
>>
>> Am I doing something wrong?
>
> It is not not off by a second, it's off by 1/100 of a second.
>
> This looks a lot like a bug in the parser, since it starts to
> parse the fraction with a 1/100 second offset starting at
> x.59 onwards:
>
> >>> DateTimeFrom('2008-05-06 19:30:09.57')
> <mx.DateTime.DateTime object for '2008-05-06 19:30:09.57' at 2b1e3bdb08c8>
>
> >>> DateTimeFrom('2008-05-06 19:30:09.58')
> <mx.DateTime.DateTime object for '2008-05-06 19:30:09.58' at 2b1e3ba8bce8>
>
> >>> DateTimeFrom('2008-05-06 19:30:09.59')
> <mx.DateTime.DateTime object for '2008-05-06 19:30:09.58' at 2b1e3bdb08c8>
>
> >>> DateTimeFrom('2008-05-06 19:30:09.60')
> <mx.DateTime.DateTime object for '2008-05-06 19:30:09.59' at 2b1e3ba8bce8>
>
> >>> DateTimeFrom('2008-05-06 19:30:09.61')
> <mx.DateTime.DateTime object for '2008-05-06 19:30:09.60' at 2b1e3bdb08c8>
>
> >>> DateTimeFrom('2008-05-06 19:30:09.62')
> <mx.DateTime.DateTime object for '2008-05-06 19:30:09.61' at 2b1e3ba8bce8>
>
> Note that it works again, if you use three decimal places:
>
> >>> DateTimeFrom('2008-05-06 19:30:09.591')
> <mx.DateTime.DateTime object for '2008-05-06 19:30:09.59' at 2b1e3bdb08c8>
>
> >>> DateTimeFrom('2008-05-06 19:30:09.601')
> <mx.DateTime.DateTime object for '2008-05-06 19:30:09.60' at 2b1e3bdb08c8>
>
> However:
>
> >>> DateTimeFrom('2008-05-06 19:30:09.590')
> <mx.DateTime.DateTime object for '2008-05-06 19:30:09.58' at 2b1e3ba8bce8>
>
> >>> DateTimeFrom('2008-05-06 19:30:09.600')
> <mx.DateTime.DateTime object for '2008-05-06 19:30:09.59' at 2b1e3bdb08c8>
>
> Weird bug :-)
It appears to be a classical float rounding problem:
>>> DateTimeFrom('2008-05-06 19:30:09.58').second
9.5800000000000001
>>> DateTimeFrom('2008-05-06 19:30:09.59').second
9.5899999999999999
>>> DateTimeFrom('2008-05-06 19:30:09.60').second
9.5999999999999996
>>> DateTimeFrom('2008-05-06 19:30:09.61').second
9.6099999999999994
The value is parsed correctly using float():
>>> float('09.58')
9.5800000000000001
>>> float('09.59')
9.5899999999999999
>>> float('09.60')
9.5999999999999996
>>> float('09.61')
9.6099999999999994
It's just that the string formatting code truncates the
seconds value after two decimal places (to avoid
cases where it would print '19:30:60' using rounding).
I'm not sure how we could solve this. Perhaps we should
round for second values up to 59.99 (to two decimal places)
and truncate only for > 59.99 ?!
--
Marc-Andre Lemburg
eGenix.com
Professional Python Services directly from the Source (#1, May 07 2008)
>>> Python/Zope Consulting and Support ... http://www.egenix.com/
>>> mxODBC.Zope.Database.Adapter ... http://zope.egenix.com/
>>> mxODBC, mxDateTime, mxTextTools ... http://python.egenix.com/
________________________________________________________________________
:::: Try mxODBC.Zope.DA for Windows,Linux,Solaris,MacOSX for free ! ::::
eGenix.com Software, Skills and Services GmbH Pastor-Loeh-Str.48
D-40764 Langenfeld, Germany. CEO Dipl.-Math. Marc-Andre Lemburg
Registered at Amtsgericht Duesseldorf: HRB 46611
More information about the egenix-users
mailing list