Google Groups no longer supports new Usenet posts or subscriptions. Historical content remains viewable.
Dismiss

converting a timezone-less datetime to seconds since the epoch

4 views
Skip to first unread message

Chris Withers

unread,
Mar 16, 2010, 9:47:12 AM3/16/10
to pytho...@python.org, petra...@glcuk.com
Hi All,

We have a bunch of datetime objects that have tzinfo=None.
We want to turn them into float timestamps in seconds since the epoch.

Here's the first attempt:

import time
from datetime import datetime
from unittest import TestCase

def timestamp(dttm):
return time.mktime(dttm.timetuple())

class Test(TestCase):

def check(self,*args):
epoch = datetime.utcfromtimestamp(0)
dt = datetime(*args)
actual = timestamp(dt)
d = dt - epoch
expected = d.seconds + 60*60*24*d.days
self.assertEquals(expected,actual,
'%s != %s (diff %s)'%(expected,actual,expected-actual))

def test_xmas(self):
self.check(2009, 12, 25, 1, 2, 3, 456789)

def test_midsummer(self):
self.check(2009, 6, 21, 2, 3, 4, 5678)

For me, test_midsummer fails. I'd be interested in knowing wheher both
tests pass for other people.

I'd be *more* interested in knowing either why the timestamp function or
the tests are wrong and how to correct them...

cheers,

Chris

Chris Rebert

unread,
Mar 16, 2010, 10:13:04 AM3/16/10
to Chris Withers, pytho...@python.org, petra...@glcuk.com
On Tue, Mar 16, 2010 at 6:47 AM, Chris Withers <ch...@simplistix.co.uk> wrote:
> Hi All,
>
> We have a bunch of datetime objects that have tzinfo=None.
> We want to turn them into float timestamps in seconds since the epoch.
>
> Here's the first attempt:
>
> import time
> from datetime import datetime
> from unittest import TestCase
>
> def timestamp(dttm):
>    return time.mktime(dttm.timetuple())

from calendar import timegm

def timestamp(dttm):
return timegm(dttm.utctimetuple())
#the *utc*timetuple change is just for extra consistency
#it shouldn't actually make a difference here

And problem solved. As for what the problem was:

Paraphrasing the table I got added to the time module docs:
(http://docs.python.org/library/time.html)

To convert from struct_time in ***UTC***
to seconds since the epoch
use calendar.timegm()

To convert struct_time in ***local*** time
to seconds since the epoch
use time.mktime()

> I'd be *more* interested in knowing either why the timestamp function or the
> tests are wrong and how to correct them...

You used a function intended for local times on UTC time data, and
therefore got incorrect results.

Cheers,
Chris
--
Entering the workforce in 2012
http://blog.rebertia.com

Chris Withers

unread,
Mar 18, 2010, 9:44:53 AM3/18/10
to Chris Rebert, pytho...@python.org, petra...@glcuk.com
Hey Chris,

Chris Rebert wrote:
>> def timestamp(dttm):
>> return time.mktime(dttm.timetuple())
>
> from calendar import timegm
>
> def timestamp(dttm):
> return timegm(dttm.utctimetuple())
> #the *utc*timetuple change is just for extra consistency
> #it shouldn't actually make a difference here

Ah, right. What on earth is timegm doing in calendar?
No way I ever would have thought to look there...

I wonder what the best way to get this stuff documented in the datetime
modules docs is?

Chris

--
Simplistix - Content Management, Batch Processing & Python Consulting
- http://www.simplistix.co.uk

Chris Withers

unread,
Apr 7, 2010, 4:57:10 AM4/7/10
to Chris Rebert, pytho...@python.org, petra...@glcuk.com
Hi Chris,

Chris Rebert wrote:
> from calendar import timegm
>
> def timestamp(dttm):
> return timegm(dttm.utctimetuple())
> #the *utc*timetuple change is just for extra consistency
> #it shouldn't actually make a difference here
>

> And problem solved. As for what the problem was:
>
> Paraphrasing the table I got added to the time module docs:
> (http://docs.python.org/library/time.html)

That table is not obvious :-/
Could likely do with its own section...

> To convert from struct_time in ***UTC***
> to seconds since the epoch
> use calendar.timegm()

...and really, wtf is timegm doing in calendar rather than in time? ;-)

>> I'd be *more* interested in knowing either why the timestamp function or the
>> tests are wrong and how to correct them...
>
> You used a function intended for local times on UTC time data, and
> therefore got incorrect results.

Thanks for the info, I don't think I'd ever have gotten to the bottom of
this on my own! :-)

Floris Bruynooghe

unread,
Apr 7, 2010, 8:38:14 AM4/7/10
to
On Apr 7, 9:57 am, Chris Withers <ch...@simplistix.co.uk> wrote:

> Chris Rebert wrote:
> > To convert from struct_time in ***UTC***
> > to seconds since the epoch
> > use calendar.timegm()
>
> ...and really, wtf is timegm doing in calendar rather than in time? ;-)

You're not alone in finding this strange: http://bugs.python.org/issue6280

(the short apologetic reason is that timegm is written in python
rather the C)

Regards
Floris

0 new messages