utcnow

Ben Finney ben+python at benfinney.id.au
Mon Sep 17 00:48:09 EDT 2012


Nick the Gr33k <nikos.gr33k at gmail.com> writes:

> Hello is there a better way of writing this:
>
> date = ( datetime.datetime.utcnow() + datetime.timedelta(hours=3)
> ).strftime( '%y-%m-%d %H:%M:%S')
>
> something like:
>
> date = datetime.datetime.utcnow(hours=3).strftime( '%y-%m-%d %H:%M:%S')
>
> i prefer it if it could be written as this.

Break long complicated statements into simpler statements. You might
need to get used to naming your intermediate results.

    now = datetime.datetime.utcnow()
    later = now + datetime.timedelta(hours=3)
    timestamp_text = later.strftime("%Y-%m-%d %H:%M:%S")

> Also what about dayligh savings time?

What about it? What has your reading of the ‘datetime’ module
documentation taught you?

-- 
 \        “Don't worry about people stealing your ideas. If your ideas |
  `\     are any good, you'll have to ram them down people's throats.” |
_o__)                                                    —Howard Aiken |
Ben Finney



More information about the Python-list mailing list