By Roger LeMesurier, Jul 2021

I find the time stamp, datetime, date, time, datetime strings, and other formats in python can get messy and unintuitive. Below are the most important ideas I've learned.

datetime

<aside> 💡 datetime is an official python module. It's the basis for almost all date and time handling in python.

</aside>

intro

https://docs.python.org/3/library/datetime.html

The datetime module supplies classes for manipulating dates and times. While date and time arithmetic is supported, the focus of the implementation is on efficient attribute extraction for output formatting and manipulation. - datetime documentation

<aside> 👯‍♂️ Datetime contains aware and naive objects.

</aside>

an aware object can locate itself relative to other aware objects. An aware object represents a specific moment in time that is not open to interpretation.

A naive object does not contain enough information to unambiguously locate itself relative to other date/time objects. Whether a naive object represents (UTC), local time, or time in some other timezone is purely up to the program, just like it is up to the program whether a particular number represents metres, miles, or mass.

tzinfo

datetime and time objects have an optional time zone information attribute, tzinfo, that can be set to an instance of a subclass of the abstract tzinfo class. These tzinfo objects capture information about the offset from UTC time, the time zone name, and whether daylight saving time is in effect.

timedelta objects

A timedelta object represents a duration, the difference between two dates or times.

datetime.timedelta(days=0, seconds=0, microseconds=0, milliseconds=0, minutes=0, hours=0, weeks=0)

Note: my observation is that the max increment/subunit for output is the day, and for input is the week. See examples here, computed in Jupyter notebook

>>>import datetime
>>>test = timedelta(weeks=(32*52))
>>>test
datetime.timedelta(days=11648)

>>>test = timedelta(seconds=(999999999))
>>>test
datetime.timedelta(days=11574, seconds=6399)

>>>test = timedelta(days=(999999999))
>>>test
datetime.timedelta(days=999999999)

>>>test = timedelta(weeks=(999999999))
>>>test
OverflowError: Python int too large to convert to C int

date objects

A date object represents a date (year, month and day) in an idealized calendar, the current Gregorian calendar indefinitely extended in both directions.

>>>import datetime
>>>datetime.date.min
datetime.date(1, 1, 1)
>>>datetime.date.max
datetime.date(9999, 12, 31)