Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Python datetime.utcnow() returning incorrect datetime

datetime.utcnow() 

This call is returning an incorrect datetime, delayed from UTC/GMT by 1 hour (check in: http://www.worldtimeserver.com/current_time_in_UTC.asp).

Is it working like it should be?

For example, it's returning, right now:

2015-02-17 23:58:44.761000. 

Current UTC time is: 00:58, not 23:58

like image 607
Johann Gomes Avatar asked Feb 18 '15 00:02

Johann Gomes


People also ask

What is Utcnow ()?

Returns the current UTC date and time.

Is Utcnow timezone aware?

datetime. utcnow() Returns the current utc time but is not timezone aware.

Should I use now or Utcnow?

You should always try to work with DateTime objects that have Kind=Utc , except during i/o (displaying and parsing). This means you should almost always be using DateTime. UtcNow , except for the cases where you're creating the object just to display it, and discard it right away.


1 Answers

I know I'm five years late, but I had the same problem tonight. In my experience, the solution to the problem was to use the aware UTC datetime:

utc_dt_aware = datetime.datetime.now(datetime.timezone.utc) 

If you google "utcnow() wrong" this is the first result you get, so I thought it would be good to answer anyway.

like image 100
matkes Avatar answered Oct 11 '22 06:10

matkes