<HTML dir=ltr><HEAD><TITLE>Extended date and time</TITLE>
<META http-equiv=Content-Type content="text/html; charset=unicode">
<META content="MSHTML 6.00.6000.16544" name=GENERATOR></HEAD>
<BODY>
<DIV dir=ltr><FONT face=Arial color=#000000 size=2>The "time" module in the standard library does epoch, and conversions.</FONT></DIV>
<DIV dir=ltr><FONT face=Arial size=2></FONT> </DIV>
<DIV dir=ltr><FONT face=Arial size=2>Get current local time in seconds since epoch (1970):</FONT></DIV>
<DIV dir=ltr><FONT face="Courier New" size=2></FONT> </DIV>
<DIV dir=ltr><FONT face="Courier New" size=2>>>> import time</FONT></DIV>
<DIV dir=ltr><FONT face="Courier New" size=2>>>> now_secs = time.time()</FONT></DIV>
<DIV dir=ltr><FONT face="Courier New" size=2>>>> print now_secs</FONT></DIV>
<DIV dir=ltr><FONT face="Courier New" size=2>1194790069.33</FONT></DIV>
<DIV dir=ltr><FONT size=2></FONT> </DIV>
<DIV dir=ltr><FONT face=Arial size=2>Convert to a struct_time object for conversions:</FONT></DIV>
<DIV dir=ltr><FONT face="Courier New" size=2></FONT> </DIV>
<DIV dir=ltr><FONT face="Courier New" size=2>>>> now_struct = time.localtime(now_secs)</FONT></DIV>
<DIV dir=ltr><FONT face="Courier New" size=2>>>> print now_struct</FONT></DIV>
<DIV dir=ltr><FONT face="Courier New" size=2>(2007, 11, 11, 8, 7, 49, 6, 315, 0)</FONT></DIV>
<DIV dir=ltr><FONT size=2></FONT> </DIV>
<DIV dir=ltr><FONT face=Arial size=2>Make it a readable string:</FONT></DIV>
<DIV dir=ltr><FONT face="Courier New" size=2></FONT> </DIV>
<DIV dir=ltr><FONT face="Courier New" size=2>>>> now_string = time.strftime('%a %m/%d/%Y, %I:%M:%S %p', now_struct)</FONT></DIV>
<DIV dir=ltr><FONT face="Courier New" size=2>>>> print now_string</FONT></DIV>
<DIV dir=ltr><FONT face="Courier New" size=2>'Sun 11/11/2007, 08:07:49 AM'</FONT></DIV>
<DIV dir=ltr><FONT face="Courier New" size=2></FONT> </DIV>
<DIV dir=ltr><FONT face=Arial size=2>Convert string back into a struct_time object, then seconds again:</FONT></DIV>
<DIV dir=ltr> </DIV>
<DIV dir=ltr><FONT face="Courier New" size=2>>>> now_struct2 = time.strptime(now_string, '%a %m/%d/%Y, %I:%M:%S %p')</FONT></DIV>
<DIV dir=ltr><FONT face="Courier New" size=2>>>> print now_struct2</FONT></DIV>
<DIV dir=ltr><FONT face="Courier New" size=2>(2007, 11, 11, 8, 7, 49, 6, 315, -1)</FONT></DIV>
<DIV dir=ltr><FONT face="Courier New" size=2>>>> now2 = time.mktime(now_struct2)</FONT></DIV>
<DIV dir=ltr><FONT face="Courier New" size=2>>>> print now2</FONT></DIV>
<DIV dir=ltr><FONT face="Courier New" size=2>1194790069.0</FONT></DIV>
<DIV dir=ltr><FONT face=Arial size=2></FONT> </DIV>
<DIV dir=ltr><FONT face=Arial size=2>... etc.  If you're starting the other direction, change the format string passed to strptime to match the pattern of your existing strings.  The standard docs for the time module has all the details.</FONT></DIV>
<DIV dir=ltr><FONT face=Arial size=2></FONT> </DIV>
<DIV dir=ltr><FONT face=Arial size=2>- Adam</FONT></DIV>
<DIV dir=ltr><BR> </DIV>
<DIV dir=ltr>
<HR tabIndex=-1>
</DIV>
<DIV dir=ltr><FONT face=Tahoma size=2><B>From:</B> python-list-bounces+adam=volition-inc.com@python.org on behalf of Jeremy Sanders<BR><B>Sent:</B> Sat 11/10/2007 9:37 AM<BR><B>To:</B> python-list@python.org<BR><B>Subject:</B> Extended date and time<BR></FONT><BR></DIV>
<DIV>
<P><FONT size=2>Hi - I need to add support to a program for dates and times. The built-in<BR>Python library seems to be okay for many purposes, but what I would like<BR>would be Unix epoch style times (seconds relative to some date), covering a<BR>large period from the past to the future. What would be nice would be a<BR>library which can take floating point seconds from an epoch.<BR><BR>Does anyone know of a library which can convert from human style dates and<BR>times to a floating point epoch and back again? I expect I could fudge the<BR>fractional seconds with the built-in library, but I can't see how to get<BR>dates in the past.<BR><BR>Thanks, Jeremy.<BR><BR>--<BR>Jeremy Sanders<BR><A href="http://www.jeremysanders.net/">http://www.jeremysanders.net/</A><BR>--<BR><A href="http://mail.python.org/mailman/listinfo/python-list">http://mail.python.org/mailman/listinfo/python-list</A><BR></FONT></P></DIV></BODY></HTML>