views:

285

answers:

3
datetime.datetime.utcnow()
datetime.datetime(2010, 2, 25, 4, 14, 37, 366086)

Why does this datetime not have any tz info say its a utc date.

A: 

UTC dates don't need any timezone info since they're UTC, which by definition means that they have no offset.

Ignacio Vazquez-Abrams
As far as I can tell from http://docs.python.org/library/datetime.html, a datetime without a tzinfo is one where the time zone is unspecified. Here the time zone *has* been specified, so logically it should be present. There's a big difference between a date/time without an associated time zone and one which is definitely in UTC. (Ideally they should be different types IMO, but that's another matter...)
Jon Skeet
A: 

The standard Python libraries don't include any tzinfo classes. I can only guess at the reasons. Personally I think it was a mistake not to include a tzinfo class for UTC, because that one is uncontroversial enough to have a standard implementation.

Mark Ransom
+1  A: 

That means it is timezone naive, so you can't use it with datetime.astimezone

you can give it a timezone like this

import pytz  # 3rd party
u=datetime.utcnow()
u=u.replace(tzinfo=pytz.utc)

now you can change timezones

print datetime.astimezone(u, pytz.timezone("EST"))
gnibbler
But there's no good reason for it to be timezone naive - it's specified to be UTC. Why do you need to search out a third party library to make it work properly?
Mark Ransom
I think it may have been a licensing thing, so pytz can't be included in the stdlib
gnibbler
Doesn't matter if all of pytz couldn't be included, I still think the standard library should have implemented a tzinfo for UTC.
Mark Ransom
I agree; for me ‘naïve’ times are completely useless. There is discussion on the python list at the moment about adding pytz to the stdlib; the issue is not licensing but the fact that the timezone data is updated so often (which Python itself can't be). Also pytz doesn't implement the tzinfo interface in the expected way so you can get errors if you try to use some of the city timezones in `astimezone`. So datetime not only has no native timezones, but the only widely-available implementation of tzinfo is non-compliant to the supposed standard.
bobince
So in all I consider the date/time libraries in Python a failure. They don't fit my head at all. All I want is a UTC timestamp for all storage purposes, coupled with timezone-aware parsing and formatting. In the end I gave up and wrote my own code for the most popular timezones.
bobince