Is there a way to return a UTC datetime?

Using frappe.utils.data.nowtime() returns the local time, as it should. I’m looking for a clean way to return UTC without hard coding the timezone.

frappe.utils.data.get_time_zone() returns a string - America/ New_York in my case.

Suggestions welcome.

I think python datetime and timedelta objects have methods to do the conversion from local time to UTC.
https://docs.python.org/2/library/datetime.html

Hope it helps.

Try this

import datetime
import dateutil.tz

utcdate = datetime.datetime.now(dateutil.tz.tzutc())

Thanks @MCD-50 and @littlehera ! I got it working using datetime.datetime.utcnow(). Unfortunately another RTFM moment for me, I was handling the import wrong and thought there was a conflict with the frappe tools.

2 Likes

Well, we’ve all been there at least once in our lives. Congrats!
:grin: :wine_glass: