Perform the following steps for this recipe:
- The only reliable way to retrieve the current datetime is by using datetime.datetime.utcnow(). Independently of where the user is and how the system is configured, it will always return the UTC time. So we need to make it time-zone-aware to be able to decline it to any time zone in the world:
import datetime
def now():
return datetime.datetime.utcnow().replace(tzinfo=datetime.timezone.utc)
- Once we have a time-zone-aware current time, it is possible to convert it to any other time zone, so that we can display to our users the value in their own time zone:
def astimezone(d, offset):
return d.astimezone(datetime.timezone(datetime.timedelta(hours=offset)))
- Now, given I'm currently ...