the Gravity of Python 2

Ben Finney ben+python at benfinney.id.au
Thu Jan 9 08:35:00 EST 2014


Kushal Kumaran <kushal.kumaran at gmail.com> writes:

> Ben Finney <ben+python at benfinney.id.au> writes:
>
> > Kushal Kumaran <kushal.kumaran at gmail.com> writes:
> >
> >> Roy Smith <roy at panix.com> writes:
> >> > How, in Python, do you get an aware UTC datetime object?
> >>
> >> classmethod datetime.utcnow()
> >>
> >>     Return the current UTC date and time, with tzinfo None. […]
> >
> > No, that won't do what was asked. The ‘datetime.datetime.utcnow’
> > function explicitly returns a naive datetime object, not an aware
> > datetime object.
>
> Yes, but the documentation for utcnow explicitly tells you how to get
> an aware object.
>
>   "An aware current UTC datetime can be obtained by calling
>    datetime.now(timezone.utc)."

And we come full circle: This is exactly what Roy's original question
was (IIUC) trying to address. That process is not obvious, and it's not
simple: it's a series of difficult-to-discover function calls instead of
just one obvious one.

-- 
 \       “If you don't know what your program is supposed to do, you'd |
  `\                 better not start writing it.” —Edsger W. Dijkstra |
_o__)                                                                  |
Ben Finney




More information about the Python-list mailing list