[Tutor] datetime, time zones, and ISO time

David Perlman dperlman at wisc.edu
Wed Feb 17 19:44:02 CET 2010


I have been really scratching my head over this, it seems like there  
*should* be a nice easy way to do what I want but I can't find it for  
the life of me.

What I would like to do would be something like this:

 >>> datetime.datetime.now().isoformat()
'2010-02-17T12:13:17.913260-06:00'

But what actually happens is this:
'2010-02-17T12:13:17.913260'

I need to keep track of the time zone because I'm working with Google  
Calendar's query APIs, and it interprets all times as GMT unless you  
specify the time zone, which means my search results are wrong.   
Anyway, I was thinking I could get around it with something like:

now=datetime.datetime.now()
offset=datetime.datetime.utcoffset()
[somehow add the offset info into the "now" datetime object]
now.isoformat()

But a) I don't know how to stick the offset info into a datetime  
object, and the documentation doesn't seem to say anything about this;  
and b) the offset line doesn't work anyway:

 >>> datetime.datetime.utcoffset()
Traceback (most recent call last):
   File "<stdin>", line 1, in <module>
TypeError: descriptor 'utcoffset' of 'datetime.datetime' object needs  
an argument

I think there's a combination of problems here, involving me not  
understanding something fundamental about datetime objects, and also  
involving problems with the documentation (there are a whole lot of  
places where the optional arguments to the methods are left off of the  
syntax, for example.)

Can anyone help sort me out here?  In particular, is there a really  
straightforward way to do what I'm looking for?

One more general kind of confusion that this has raised for me is that  
it seems that in the datetime documentation (and possibly other places  
as well) there is an unexplained distinction between methods which can  
be used to give you new information, and methods which only can tell  
you something about the instance they are invoked on.  For example,  
this works the way I feel like it ought to:

 >>> datetime.datetime.now()
datetime.datetime(2010, 2, 17, 12, 42, 30, 520792)

But this doesn't work at all:
 >>> datetime.datetime.utcoffset()
Traceback (most recent call last):
   File "<stdin>", line 1, in <module>
TypeError: descriptor 'utcoffset' of 'datetime.datetime' object needs  
an argument

And I haven't been able to find another method instead of utcoffset()  
that will give me what I want.

Thanks very much to anyone who can illuminate my darkness on this  
matter.  :)

--
-dave----------------------------------------------------------------
"Pseudo-colored pictures of a person's brain lighting up are
undoubtedly more persuasive than a pattern of squiggles produced by a
polygraph.  That could be a big problem if the goal is to get to the
truth."  -Dr. Steven Hyman, Harvard





More information about the Tutor mailing list