0

How can I specify a timezone like "Amazon Time" (UTC -4) in a datetime function?

Every reference that I search mentions the creation of a tzinfo class, but that seems too much hurdle for a simple problem. Is there a simpler way to do it?

from datetime import datetime

# Why can't I do something like this?
datetime.utcnow(offset=-4)
Victor Valente
  • 761
  • 9
  • 24
  • @deceze: You redirected this duplicate to another duplicate.. [See: here](https://stackoverflow.com/questions/1398674/display-the-time-in-a-different-time-zone) – Maximilian Burszley Mar 04 '19 at 20:42

1 Answers1

3

You can pass a timezone to the now function:

import datetime
import pytz

datetime.datetime.now(pytz.timezone('America/Manaus'))

# => datetime.datetime(2019, 3, 4, 16, 38, 41, 584695, tzinfo=<DstTzInfo 'America/Manaus' AMT-1 day, 20:00:00 STD>)

I got the timezone name from this wiki page.

Maximilian Burszley
  • 18,243
  • 4
  • 34
  • 63