Django / python - resolving confusion regarding dates and timezone awareness

I am actively working with dates in python / django. To solve the various use cases, I blindly tried different approaches until one of them worked without learning the logic of how the various functions work.

Now it's a crunch. I would like to ask a couple of questions regarding the intricacies of dates and time zones in django / python.

How to interpret a datetime object that already has a timezone?

To clarify, let's say I do the following:

 >>> generate_a_datetime() datetime.datetime(2015, 12, 2, 0, 0, tzinfo=<DstTzInfo 'Canada/Eastern' LMT-1 day, 18:42:00 STD>) >>> 

The console output seems to me ambiguous:

Q1) This datetime object says it is 2015-12-02 - What does the generate_a_datetime function tell me? This suggests that "a person standing in eastern Canada, looking at his calendar, sees" 2015-12-02 "? Or does it mean" This is "2015-12-02 UTC" ... but do not forget to set it to East Canadian Time Zone! "

django.utils.timezone.make_aware confuses me.

For instance:

 >>> from django.utils import timezone >>> import pytz >>> tz = pytz.timezone('Canada/Eastern') >>> now_unaware = datetime.datetime.now() >>> now_aware_with_django = timezone.make_aware(now_unaware, tz) >>> now_aware_with_datetime = now_unaware.replace(tzinfo=tz) >>> now_unaware datetime.datetime(2015, 12, 2, 22, 1, 19, 564003) >>> now_aware_with_django datetime.datetime(2015, 12, 2, 22, 1, 19, 564003, tzinfo=<DstTzInfo 'Canada/Eastern' EST-1 day, 19:00:00 STD>) >>> now_aware_with_datetime datetime.datetime(2015, 12, 2, 22, 1, 19, 564003, tzinfo=<DstTzInfo 'Canada/Eastern' LMT-1 day, 18:42:00 STD>) >>> 

The now_aware_with_django and now_aware_with_datetime objects seem to behave similarly, but their output to the console suggests that they are different.

Q2) What is the difference between now_aware_with_django and now_aware_with_datetime ?

Q3) How do I know if I need to use timezone.make_aware or datetime.replace ?

Naive datetimes versus UTC time

UTC means that the time does not change. โ€œNaive,โ€ apparently means that time is not connected with it.

Q4) What is the difference between naive and temporary UTC data? It seems that they are exactly the same - not imposing any kind of conversion on the actual value of time.

Q5) How do I know when I want to use naive times and when I want to use UTC time?

If I could get an answer to all 5 questions that would be superbly great. Thank you very much!

+5
source share
1 answer

Q1) This datetime object says it is 2015-12-02. What does the generate_a_datetime function say? This suggests that "a person standing in eastern Canada, looking at his calendar, sees" 2015-12-02 "? Or does it mean" This is "2015-12-02 UTC" ... but do not forget to set it to East Canadian Time Zone! "

The first interpretation was correct. The time zone-oriented dat-time is already โ€œsetโ€ for you, and tzinfo just tells you which time zone it is.

Q2) What is the difference between now_aware_with_django and now_aware_with_datetime ?

In the first case, you create a date and time that represents the same point in time as โ€œnaive,โ€ and assuming that the naive was in your local time zone.

In the second case, you say that the naive was already in the time zone that you provide, and then you just click on tzinfo.

Q3) How do I know if I need to use timezone.make_aware or datetime.replace?

Well, since they do different things, you need to know what you are trying to do in order to know what to use. If you want to convert from a naive time zone (in your local time) to another time zone, you can use make_aware for this. If you already know the time zone of your naive datetime, you simply use the replacement (or look at localize in pytz , which is a little more cautious about this task).

Note: usually, if you have some kind of naive datetime that hangs in the first place, you are doing something wrong earlier, and you should catch it earlier. Try to find them on the border of your application - I will talk about this in Q5.

Q4) What is the difference between naive and temporary UTC data? They seem to be exactly the same - not imposing any kind of conversion on the actual value of time.

A naive datetime is simply a datetime that does not tell you what time zone it is in. This is not necessarily UTC, it can be anything. It is similar to bytestrings and unicode - you should know what encoding should say what decoded bytes say. For a naive datetime, you need to know what time zone it is in before you can tell what time it really represents. Therefore, in this sense, UTC date and time provide more information than a naive datetime.

UTC is coordinating universal time, blaming the French for a strange acronym. Time zones are usually defined as being a whole number of hours different from UTC, and for all practical purposes, you can think of UTC as a time zone that is 0 hours different from UTC. And it is like GMT without summer savings.

Q5) How do I know when I want to use naive times and when I want to use UTC time?

There are disagreements about this. My recommendation is to always work with everyone in UTC inside your application (and store in UTC only in UTC)! When any datetime data enters your application, however it enters your application, make sure that it is correctly converted to UTC. It also means that anywhere in your application that uses datetime.now() (which is a naive datetime with a โ€œmissingโ€ tzinfo, which should be the machineโ€™s local time zone) uses datetime.utcnow() (which is a naive datetime in UTC) or even better datetime.now(tz=pytz.utc) (which is known in the time zone).

Go to the local time zone at the end of the display of your application. You can usually do this using template tags or even using javascript clients.

+1
source

Source: https://habr.com/ru/post/1237290/


All Articles