[egenix-users] Unexpected Behaviour of DateTimeFromTicks
M.-A. Lemburg
mal at egenix.com
Fri May 25 16:33:08 CEST 2007
On 2007-05-25 15:12, Novin wrote:
> I'm trying to round a date to the nearest hour. To do so I converted it to
> a float using ticks(), rounded it then converted it back to a DateTime
> object
> using DateTimeFromTicks.
>
> Unfortunately this didn't work as I expected on clock change days - see
> below:
>
> Python 2.5 (r25:51908, Sep 19 2006, 09:52:17) [MSC v.1310 32 bit (Intel)]
> Type "copyright", "credits" or "license" for more information.
>
> IPython 0.8.1.rc3 -- An enhanced Interactive Python.
> ? -> Introduction to IPython's features.
> %magic -> Information about IPython's 'magic' % functions.
> help -> Python's own help system.
> object? -> Details about 'object'. ?object also works, ?? prints more.
>
> Welcome to pylab, a matplotlib-based Python environment.
> For more information, type 'help(pylab)'.
>
> In [1]: import mx.DateTime
>
> In [2]: mx.DateTime.__version__
> Out[2]: '3.0.0'
>
> In [3]: comdate = 35155.04167
>
> In [4]: mxdate1 = mx.DateTime.DateTimeFromCOMDate(comdate)
>
> In [5]: print mxdate1
> 1996-03-31 01:00:00.28
>
> In [6]: mxdate2 = mx.DateTime.DateTimeFromTicks(mxdate1.ticks())
>
> In [7]: mxdate1 == mxdate2
> Out[7]: False
>
> In [8]: print mxdate2
> 1996-03-31 00:00:00.28
>
>
> Is this the expected behaviour? It seems a little un-intuitive to me!
>
> NB: using .absvalues() and DateTimeFromAbsDateTime works as expected.
Hmm, on Linux I get:
>>> import mx.DateTime
>>> comdate = 35155.04167
>>> mxdate1 = mx.DateTime.DateTimeFromCOMDate(comdate)
>>> mxdate2 = mx.DateTime.DateTimeFromTicks(mxdate1.ticks())
>>> print mxdate1, mxdate2
1996-03-31 01:00:00.28 1996-03-31 01:00:00.28
They still don't compare equal due to float rounding issues:
>>> mxdate1 == mxdate2
False
Note that ticks always work with local time, so I suppose this is
due to problems with DST support in the MS C lib for that year.
Also tried a few other dates:
>>> comdate = 35255.04167
>>> mxdate1 = mx.DateTime.DateTimeFromCOMDate(comdate)
>>> mxdate2 = mx.DateTime.DateTimeFromTicks(mxdate1.ticks())
>>> print mxdate1, mxdate2
1996-07-09 01:00:00.28 1996-07-09 01:00:00.28
>>> comdate = 35105.04167
>>> mxdate1 = mx.DateTime.DateTimeFromCOMDate(comdate)
>>> mxdate2 = mx.DateTime.DateTimeFromTicks(mxdate1.ticks())
>>> print mxdate1, mxdate2
1996-02-10 01:00:00.28 1996-02-10 01:00:00.28
--
Marc-Andre Lemburg
eGenix.com
Professional Python Services directly from the Source (#1, May 25 2007)
>>> 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