Question

I have converted a ctime value to unicode

1295478503.6789999 to 'Wed Jan 19 18:08:23 2011'

Can I go backwards? From the second line to the first?

Was it helpful?

Solution

You want the Python time module, specifically the strptime() function. (How'd you convert from ctime to unicode/string? Probably with time.strftime(). So it makes sense to look for the inverse direction of that, which is time.strptime().)

Here's a good Google query for this, too: http://www.google.com/search?q=python+parse+time+string

Sample:

entropy:~>python
Python 2.6.1 (r261:67515, Jun 24 2010, 21:47:49) 
[GCC 4.2.1 (Apple Inc. build 5646)] on darwin
Type "help", "copyright", "credits" or "license" for more information.
>>> import time
>>> u = u'Wed Jan 19 18:08:23 2011'
>>> t = time.strptime(u)
>>> t
time.struct_time(tm_year=2011, tm_mon=1, tm_mday=19, tm_hour=18, tm_min=8, tm_sec=23, tm_wday=2, tm_yday=19, tm_isdst=-1)
>>> time.mktime(t)
1295489303.0

OTHER TIPS

import time
time.mktime(time.strptime('Wed Jan 19 18:08:23 2011'))
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top