2047
Comment: new image
|
2684
ISO 8601 w/ xml.utils.iso8601
|
Deletions are marked like this. | Additions are marked like this. |
Line 36: | Line 36: |
== ISO 8601 with xml.utils.iso8601 == If you have {{{xml.utils.iso8601}}}, you can do the following: {{{ #!python import xml.utils.iso8601 import time # Present time, in ISO8601 print xml.utils.iso8601.tostring( time.time() ) # returns "2004-04-10T04:44:08.19Z" # From ISO8601 string, to seconds since epoch UTC print xml.utils.iso8601.parse( "2004-04-09T21:39:00-08:00" ) # -8:00 for Seattle, WA # The epoch, in ISO8601 print xml.utils.iso8601.ctime( 0 ) # returns "1969-12-31T16:00-08:00" }}} The module isn't perfect; If you omit the dashes, (perfectly valid ISO8601,) you'll get a {{{ValueError}}} exception. |
Working with Time
This page gives the basics of working with time in Python. More detail can be found in [http://www.python.org/doc/current/lib/module-time.html the time Module Documentation.]
Formats
There are several ways to work with time:
format |
Python |
seconds since the "Epoch" |
time.time() |
tuple |
time.gmtime() |
string |
time.ctime() |
The Epoch is January 1st, 1970, midnight, on UNIX systems. On other systems, look at the results of time.gmtime(0) to discover the date of the Epoch.
Measuring time in seconds since the Epoch is convenient for storage and comparing dates, because you only have a single number to consider.
The tuple contains several values, arranged the following way: year, month 1-12, day 1-31, hour 0-23, minutes 0-59, seconds 0-61, day 0-6 (Mon-Sun), day 1-366, and DST -1,0,+1. "DST" means "daylight savings time." For more information, see [http://www.python.org/doc/current/lib/module-time.html time Module Documentation.]
Using a tuple is convenient, because you can access particular numbers by index, and because you can easily compare dates. Remember that Python compares tuple data from front to back. The data is indexed so that comparing tuple times is intuitive.
The string format reads something like "Mon Feb 16 16:04:25 2004". You can't really compare these usefully, but they're what you need to display things to the user.
Converting Between Formats
attachment:TimeTransitionsImage/v1.png
(see TimeTransitionsImage for image details)
Time Zones
(nothing yet)
ISO 8601 with xml.utils.iso8601
If you have xml.utils.iso8601, you can do the following:
1 import xml.utils.iso8601
2 import time
3
4 # Present time, in ISO8601
5 print xml.utils.iso8601.tostring( time.time() ) # returns "2004-04-10T04:44:08.19Z"
6 # From ISO8601 string, to seconds since epoch UTC
7 print xml.utils.iso8601.parse( "2004-04-09T21:39:00-08:00" ) # -8:00 for Seattle, WA
8 # The epoch, in ISO8601
9 print xml.utils.iso8601.ctime( 0 ) # returns "1969-12-31T16:00-08:00"
The module isn't perfect; If you omit the dashes, (perfectly valid ISO8601,) you'll get a ValueError exception.
See Also
This page is meant just to give you the basics. You may also want to read:
[http://www.python.org/doc/current/lib/module-time.html the official Python time module documentation.]
Coordinated_Universal_Time ("UTC"), Greenwich_Mean_Time ("GMT")
Contributors
Discussion
(none)