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
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 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 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! :-)
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