The structtime Tuple

It's possible to access a specific struct time tuple element by indexing it; for example, if t is a struct time tuple, t[o] is the year value. However, using indices is quite hard to follow, and you'll constantly have to look up the documentation to figure out which index is the correct index to the specific value you're looking for. Instead, you can use the member variables tm_year, tm_month, tm_day, tm_hour, tm_min, tm_sec, tm_wday, tm_yday, and tm_isdst to retrieve these values:

>>> from time import localtime >>> localtime()

(2008, 10, 26, 9, 37, 35, 6, 300, 0) >>> localtime().tm_year 2008

In this example, I've introduced the function localtime(), which returns the current time as a struct_time tuple.

Was this article helpful?

0 0

Post a comment