Chris Douty | 4 Dec 2002 22:38

Why does TimestampFromMx chop off the milliseconds?

Why, oh why, do the various Time* functions in psycopg ignore 
fractional seconds?

I really need to preserve fractional seconds.  I just noticed that all 
of my timestamps were truncated by psycopg.  I can see two potential 
workarounds, use the str() function on my mx.DateTime object and insert 
the string.  The other option is to fix the source to psycopg so that 
these functions don't truncate my timestamps.  I guess that's not 
really a workaround, that open source. :-)

What really has me perplexed is that psycopg properly retains the 
fractional seconds when converting
a database timestamp field into a mx.DateTime object.  WTF?

Thanks a lot,
	Chris

--

-- 
Christopher Douty <Chris_Douty@...> +1-650-367-3129
Senior Engineer, Software & Systems  - AMPEX Data Systems Corp.
Chris Douty | 5 Dec 2002 02:21

Re: Why does TimestampFromMx chop off the milliseconds?

Hi all,

I know it's bad form to respond to yourself, but I figured out why 
psycopg.TimestampFromMX() chops off the fractional seconds.  Internally 
the strftime method of mx.DateTime objects is used, and the strftime 
function has no way to specify fractional seconds in the output.  
Additionally, the struct tm data type only uses an integer number of 
seconds.

I was able to successfully use str() on my DateTime objects to get a 
full timestamp like I wanted.

Now I am fighting a different battle.  Sometimes the original 
mx.DateTime object I inserted and the one retrieved from the database 
do not match.  I think that it probably has something to do with 
floating point rounding errors.

For example.

 >>> t1 = DateTime(2002,12,4,14,27,2.170)
 >>> str(t1)
'2002-12-02 16:27:02.16'
 >>> t1.absvalues()
(731186, 59222.169999999998)
 >>> 2+170/1000.0     # Which is how I calculate seconds for the 
DateTime constructor
2.1699999999999999

I think that the problem is that str() on a DateTime object truncates 
the seconds value at 2 digits, and there's no way I've fond to get it 
(Continue reading)

Federico Di Gregorio | 5 Dec 2002 14:47
Favicon
Gravatar

Re: Why does TimestampFromMx chop off the milliseconds?

Il gio, 2002-12-05 alle 02:21, Chris Douty ha scritto:
> Hi all,
> 
> I know it's bad form to respond to yourself, but I figured out why 
> psycopg.TimestampFromMX() chops off the fractional seconds.  Internally 
> the strftime method of mx.DateTime objects is used, and the strftime 
> function has no way to specify fractional seconds in the output.  
> Additionally, the struct tm data type only uses an integer number of 
> seconds.

you're right.

> I was able to successfully use str() on my DateTime objects to get a 
> full timestamp like I wanted.
> 
> Now I am fighting a different battle.  Sometimes the original 
> mx.DateTime object I inserted and the one retrieved from the database 
> do not match.  I think that it probably has something to do with 
> floating point rounding errors.
> 
> For example.
> 
>  >>> t1 = DateTime(2002,12,4,14,27,2.170)
>  >>> str(t1)
> '2002-12-02 16:27:02.16'
>  >>> t1.absvalues()
> (731186, 59222.169999999998)
>  >>> 2+170/1000.0     # Which is how I calculate seconds for the 
> DateTime constructor
> 2.1699999999999999
(Continue reading)

Chris Douty | 17 Dec 2002 22:43

Re: Why does TimestampFromMx chop off the milliseconds?

On Thursday, December 5, 2002, at 05:47 AM, Federico Di Gregorio wrote:
[snipped example of annoying problem]
> this is a generic problem of the floating point format. note that even 
> C
> or any other programming language using floating point values has this
> problem.
>
>> I think that the problem is that str() on a DateTime object truncates
>> the seconds value at 2 digits, and there's no way I've fond to get it
>> to give more precision or round correctly.  O suppose that I could 
>> make
>> my own version of strftime that does what I want in python.  Gah.
>
> if you do, please post it to the ML, to be included in future psycopg
> releases. a better way would be to patch mx.DateTime and/or strftime..
>

Yeah, I am well aware of most of the limitation of the floating point 
format.  (Inability to represent some values, inexact comparisons, 
etc.)  It was a combination of those that caused me problems.

I can't hack at strftime(), because it uses a struct tm as input and 
that structure has no fractional seconds.  I am also loathe to touch 
mx.DateTime because then I am making my own little branch.  I found 
this comment in mxDateTime.c in the mxDateTime_AsString function:

     /* When showing the seconds value, display the value truncated to 2
        decimal places -- not rounded, as this can cause the value of
        60.00 to show up even when the indictated time does not point
        to a leap second. */
(Continue reading)

Federico Di Gregorio | 18 Dec 2002 12:11
Favicon
Gravatar

Re: Why does TimestampFromMx chop off the milliseconds?

Il mar, 2002-12-17 alle 22:43, Chris Douty ha scritto:
[snip]
> Ultimately I wrote my own function in python, although now I think that 
> I need to investigate whether I should trucate at three digits too.  
> Hmmm.  There's always the math.modf() function.

thank you very much. i'll add your code as an example.

--

-- 
Federico Di Gregorio
Debian GNU/Linux Developer & Italian Press Contact        fog@...
INIT.D Developer                                           fog@...
  Se consideri l'uso del software libero una concessione tu stesso,
                           come potrai proporla agli altri? -- Nick Name

Gmane