Question

I am trying to parse a binary file for which I have the file format for. I have 3-4byte variables that are list as "CTime" I read them in using struct.unpack. I want to convert this number to an actual day time year value. The value that I have seems to be the number of seconds passed since an absolute beginning. I remember in R there is some way that you can get the date back from the seconds elapsed. Is there a way of doing this in python? or even if I should be reading in the CTime as an integer.

I read: Convert ctime to unicode and unicode to ctime python but its not quite what I'm trying to do.

Thanks in advance

Was it helpful?

Solution

You can use time module

>>> import time
>>> t1=time.gmtime(1284286794)
>>> t1
time.struct_time(tm_year=2010, tm_mon=9, tm_mday=12, tm_hour=10, tm_min=19, 
             tm_sec=54, tm_wday=6, tm_yday=255, tm_isdst=0)

OTHER TIPS

import datetime
import time
seconds_since_epoch = time.time()
print datetime.datetime.fromtimestamp(seconds_since_epoch)

 #prints "2012-09-06 16:29:48.709000"
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top