NameError: name 'UTC' is not defined

You'll need to use an additional library such as pytz. Python's datetime module doesn't include any tzinfo classes, including UTC, and certainly not your local timezone.

Edit: as of Python 3.2 the datetime module includes a timezone object with a utc member. The canonical way of getting the current UTC time is now:

from datetime import datetime, timezone
x = datetime.now(timezone.utc)

You'll still need another library such as pytz for other timezones.


If all you're looking for is the time now in UTC, datetime has a builtin for that:

x = datetime.utcnow()

Unfortunately it doesn't include any tzinfo, but it does give you the UTC time.

Alternatively if you do need the tzinfo you can do this:

from datetime import datetime
import pytz
x = datetime.now(tz=pytz.timezone('UTC'))

You may also be interested in a list of the timezones: Python - Pytz - List of Timezones?