Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Why does datetime.utcnow() return a naive datetime? [duplicate]

Tags:

python

Concept question. Why does datetime.utcnow() return a naive date:

from datetime import datetime
datetime.utcnow()

instead of the time with UTC timezone specified like this:

from datetime import datetime, timezone
datetime.utcnow().replace(tzinfo=timezone.utc)

The datetime.py source indicates this is on purpose.

@classmethod
def utcnow(cls):
    "Construct a UTC datetime from time.time()."
    t = _time.time()
    return cls.utcfromtimestamp(t)

@classmethod
    def utcfromtimestamp(cls, t):
        """Construct a naive UTC datetime from a POSIX timestamp."""
        return cls._fromtimestamp(t, True, None)

Trying to learn the thinking behind it. Thank you.

Edit: From the linked question here (thank you) this appears to be the preferred approach in Python 3.2+:

from datetime import datetime, timezone
datetime.datetime.now(datetime.timezone.utc)
like image 866
Ender2050 Avatar asked Sep 28 '17 20:09

Ender2050


People also ask

What is datetime Utcnow ()?

The property UtcNow of the DateTime class returns the current date and time of the machine running the code, expressed in UTC format. UTC is a universal format to represent date and time as an alternative to local time. Also known as the GMT+00 timezone.

What is a naive datetime?

A naive datetime object contains no timezone information. The easiest way to tell if a datetime object is naive is by checking tzinfo. tzinfo will be set to None of the object is naive.

How do I use datetime Utcnow?

An alternative to using UtcNow is DateTimeOffset. UtcNow. While the former indicates that a date and time value is Coordinated Universal Time (UTC) by assigning DateTimeKind. Utc to its Kind property, the latter assigns the date and time value the UTC time's offset (equal to TimeSpan.

What does timezone naive mean?

Timezone-Aware datetime object: does have a timezone associated with it. Timezone-Naïve datetime object: does not have any timezone associated with it. Rules of thumb. Always work with "timezone-aware" datetime objects. Always store datetime in UTC and leave rendering of timezones to the front-end.

What does utcnow mean in datetime?

DateTime.UtcNow DateTime.UtcNow DateTime.UtcNow DateTime.UtcNow Property. Definition. Gets a DateTime object that is set to the current date and time on this computer, expressed as the Coordinated Universal Time (UTC).

Why is timestamp() not working on a naive DateTime object?

The Problem: if you call the timestamp () method on a naïve datetime object, Python will assume that the datetime object holds local time. Although the utcnow () in datetime.datetime.utcnow () might suggest otherwise, it gives you a naïve datetime object. That is, it does not "know" it's in UTC.

What is the return value of utcnow?

It tends to be between 0.5 and 15 milliseconds. Starting with the .NET Framework version 2.0, the return value is a DateTime whose Kind property returns DateTimeKind.Utc. An alternative to using UtcNow is DateTimeOffset.UtcNow.

What is a DateTime object?

Gets a DateTime object that is set to the current date and time on this computer, expressed as the Coordinated Universal Time (UTC). An object whose value is the current UTC date and time.


1 Answers

As far as I can tell this is so you can hurt yourself a lot, or at least I've found that particular decision has caused me no end of pain. I think the issue is that Python as shipped really only has support for UTC timezones and not for local timezones. The thinking seems to be that a lot of people will want to use naive dates for everything and keep track (possibly based on what program it is) whether it is UTC or local. so, involving timezones at all is an explicit decision that you as a programmer must take and unless you do that you'll never get a DateTime with a timezone.

Unfortunately, this decision combined with the idea that when converting (to posix times, naive datetimes are treated as local can lead to a lot of confusion. I've discussed some of the issues that came up for me here

like image 113
Sam Hartman Avatar answered Nov 09 '22 01:11

Sam Hartman