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!