Extended date and time

Colin J. Williams cjw at sympatico.ca
Sun Nov 11 19:29:40 CET 2007


Adam Pletcher wrote:
> The "time" module in the standard library does epoch, and conversions.
>  
> Get current local time in seconds since epoch (1970):
>  
>> >> import time
>> >> now_secs = time.time()
>> >> print now_secs
> 1194790069.33
>  
> Convert to a struct_time object for conversions:
>  
>> >> now_struct = time.localtime(now_secs)
>> >> print now_struct
> (2007, 11, 11, 8, 7, 49, 6, 315, 0)
>  
> Make it a readable string:
>  
>> >> now_string = time.strftime('%a %m/%d/%Y, %I:%M:%S %p', now_struct)
>> >> print now_string
> 'Sun 11/11/2007, 08:07:49 AM'
>  
> Convert string back into a struct_time object, then seconds again:
>  
>> >> now_struct2 = time.strptime(now_string, '%a %m/%d/%Y, %I:%M:%S %p')
>> >> print now_struct2
> (2007, 11, 11, 8, 7, 49, 6, 315, -1)
>> >> now2 = time.mktime(now_struct2)
>> >> print now2
> 1194790069.0
>  
> ... 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.
>  
> - Adam
What about November 5, 1605 ?

Colin W.
> 
>  
> ------------------------------------------------------------------------
> *From:* python-list-bounces+adam=volition-inc.com at python.org on behalf 
> of Jeremy Sanders
> *Sent:* Sat 11/10/2007 9:37 AM
> *To:* python-list at python.org
> *Subject:* Extended date and time
> 
> Hi - I need to add support to a program for dates and times. The built-in
> Python library seems to be okay for many purposes, but what I would like
> would be Unix epoch style times (seconds relative to some date), covering a
> large period from the past to the future. What would be nice would be a
> library which can take floating point seconds from an epoch.
> 
> Does anyone know of a library which can convert from human style dates and
> times to a floating point epoch and back again? I expect I could fudge the
> fractional seconds with the built-in library, but I can't see how to get
> dates in the past.
> 
> Thanks, Jeremy.
> 
> --
> Jeremy Sanders
> http://www.jeremysanders.net/
> --
> http://mail.python.org/mailman/listinfo/python-list
> 




More information about the Python-list mailing list