In Python, dates and times may involve several different data types and functions. The following is a review of the 3 different types of values that represent time:
The Unix epoch timestamp (used in the time module) is a floating-point or integer value that represents the number of seconds since midnight, January 1, 1970, 0 O'Clock (UTC).
A DateTime object (which belongs to a datetime module) contains some integer values that are stored in attributes such as year, month, day, hour, minute, and second.
The Timedelta object (which belongs to the DateTime module) represents a period of time, not a specific moment.
The time function and its parameters and return values are reviewed below:
The Time.time () function returns a floating-point value that represents the Unix epoch timestamp for the current moment.
The Time.sleep (seconds) function lets the program pause the number of seconds specified by the seconds parameter.
The Datetime.datetime (year, month, day, hour, minute, second) function returns a DateTime object at the moment specified by the parameter. If you do not provide hour, minute, or second parameters, they default to 0.
The Datetime.datetime.now () function returns the DateTime object at the current moment.
The Datetime.datetime.fromtimestamp (Epoch) function returns a DateTime object that represents the moment of the epoch timestamp parameter.
Datetime.timedelta (weeks, days, hours, minutes, seconds, milliseconds, microseconds) function returns a Timedelta object that represents a period of time. The function's keyword arguments are optional and do not include month or year.
The Total_seconds () method is used for the Timedelta object, which returns the number of seconds represented by the Timedelta object.
The strftime (format) method returns a String that represents the time represented by a DateTime object in a custom format in the format string. See table 15-1 for a detailed format.
The Datetime.datetime.strptime (time_string, format) function returns a DateTime object whose moment is specified by time_string and is parsed using the format string parameter. See table 15-1 for a detailed format.
Table 15-1 strftime () directive
strftime directive meaning
%Y with a century, for example ' 2014 '
%y years, ' 00 ' to ' 99 ' (1970 to 2069)
%m numbers for the month, ' 01 ' to ' 12 '
%B full month, e.g. ' November '
%b abbreviated month, e.g. ' Nov '
%d January of the day, ' 01 ' to ' 31 '
%j the first day of the year, ' 001 ' to ' 366 '
%w Day of the week, ' 0 ' (Sunday) to ' 6 ' (Saturday)
%A full weeks, e.g. ' Monday '
%a abbreviated weeks, e.g. ' Mon '
%H hours (24 hour clock), ' 00 ' to ' 23 '
%I Hours (12 hour clock), ' 01 ' to ' 12 '
%M points, ' 00 ' to ' 59 '
%s seconds, ' 00 ' to ' 59 '
%p ' AM ' or ' PM '
Percent is the '% ' character
python3-Notes-e-013-Library-python Summary of time functions