views:

234

answers:

4

How does time.localtime() work exactly? I can call up the "array" (tupple, I think it is called - because it is immutable?) and reference/index components of it. For example:

>>> time.localtime()[0]
2010

But if I do:

print time.localtime()
time.struct_time(tm_year=2010, tm_mon=2, tm_mday=7, tm_hour=14, tm_min=46, tm_sec=58, tm_wday=6, tm_yday=38, tm_isdst=0)

First: How does time.localtime() know to return time.struct_time()?

Second: If I type print time.struct_time() it wants additional values to be passed to it (thus not giving me the same values it returned from time.localtime()) - how do I know what those values could possibly be? I checked the python documentation and I couldn't make 'heads nor tails' of it. Really just looking for an example here...

Third: When I index the tupple/array for time.localtime() it returns the proper associated value, "2010" for example, rather than "tm_year=2010" - how does it know to do this (generally speaking not asking for a lot here).

Forth: If I wanted to "call" tm_year from time.localtime() how can I do this? What I am doing (and don't 'feel' right about) is the following:

tm_year = str(time.localtime()[0])
tm_mon = str(time.localtime()[1])
tm_mday = str(time.localtime()[2])
tm_hour = str(time.localtime()[3])
tm_min = str(time.localtime()[4])

NOTE: I am saving them as strings for other reasons not explained in this question, just wanted to point out that I am creating my own variables (named exactly the same as they are in the tupple) and then just referencing the index value associated with the value I want

Is there a way to to just call time.localtime(tm_year) (I know that doesn't work as is, but just brainstorming...)

Thanks in advance... (and I have read "http://docs.python.org/library/time.html" but I am sure I missed some important information... any advice?)

-J

+1  A: 

time.localtime()[0] calls __gettiem__() on the time.struct_time instance. This is how it gets this list/tuple like behaviour from. Here is a really simple example

>>> class MyTime(object):
...     def __init__(self, year, month, mday, hour, minute):
...         self.data =  year, month, mday, hour, minute
...     def __getitem__(self, idx):
...         return self.data[idx]
...     def __str__(self):
...         return "MyTime(tm_year=%s, tm_mon=%s, tm_mday=%s, tm_hour=%s, tm_min=%s)"%self.data
... 
>>> x=MyTime(2010,2,7,14,46)
>>> x[0]
2010
>>> x[1]
2
>>> print x
MyTime(tm_year=2010, tm_mon=2, tm_mday=7, tm_hour=14, tm_min=46)

time.struct_time is a class. time.localtime() returns an instance of the class, which explains the difference when you try to print them

You can load up your 5 variables like this

>>> tm_year,tm_mon,tm_mday,tm_hour,tm_min=map(str,time.localtime()[:5])
gnibbler
Awesome explanation - thank you! That was exactly what I was looking for.
Nascent_Notes
A: 
  1. The print statement tries to convert all its arguments to a string representation. And the __repr__ or __str__ method of a struct_time object yields the result

    time.struct_time(tm_year=2010, tm_mon=2, tm_mday=7, tm_hour=14, tm_min=46, tm_sec=58, tm_wday=6, tm_yday=38, tm_isdst=0)

    See the Python docs

  2. Don't use it. It's an internal data structure

  3. When you use the index-operator [] it behaves like a tuple
tobidope
dont use repr or str because they are internal data structures? Or don't try to call time.struct_time? Is that because I can potentially alter some sort of sensitive system value? Or is it just 'bad form'/bad practice? (edited to fix wording)
Nascent_Notes
+1  A: 

You can make your own "named tuples", i.e., subclasses of tuple with items accessible by either indexing or as attributes, with collections.namedtuple (in Python 2.6 or better). There's nothing "magical" about named tuples in general, nor specifically about the one whose instances are returned by various functions in the time module (which is slightly different, because it predates collections.namedtuple).

So, for example, time.localtime().tm_year will give you the year, as you want (you can pass it to str and/or assign it to anything you want, of course).

But since you can also access the result as a tuple, you have alternatives such as slicing, tuple-unpacking, and the like.

When you call time.struct_time, you're building an arbitrary instance of it -- and you need to pass it one argument, a sequence with exactly nine items. (For a normal named tuple that's similar to it, you'd pass nine arguments instead -- of course it's easy to pass nine arguments if you have a nine-sequence foo, i.e., you call bar(*foo);-).

By the way, you focus on localtime, but that's not the only function returning a struct_time, of course -- gmtime and strptime return instances of the same type!-)

Alex Martelli
A: 

Nascent Notes,

The function time.localtime() accepts an optional argument of Epoch seconds, without the argument it takes the current time (probably from time.time()). The function then calculates what time this corresponds to in your timezone (i.e. your local time) and then returns the results in object of type time.struct_time.

I'm not sure why you want to create a struct_time object, as you're asking about the what the arguments should be, perhaps you're heading in the wrong direction.

It seems that you're interested in manipulating representations of time. I think that the datetime module may be more suitable for your purposes.

MattH