Time formats#
Man’s inventivness regarding ways of representing dates and time points is truly inspiring … and a source of headache.
Oracle suggests:
time formats as (possible subsets of) HH:MM:SS XM (XM = AM/PM), with possible separators: “.”, “,”, “-”, “ “, “:”
18 different date formats.
Unix timestamp
The number of seconds (and milli-, micro- and nanoseconds) since 1 January 1970 at the UTC timezone.
Examples at https://www.unixtimestamp.com/
from datetime import datetime
# Getting the current date and time
dt = datetime.now()
# getting the timestamp
ts = datetime.timestamp(dt)
print("Date and time is:", dt)
print("Timestamp is:", ts)
Date and time is: 2024-12-04 21:23:08.050398
Timestamp is: 1733343788.050398
ISO standard#
W3.org: ISO 8601 and Wikipedia: ISO 8601
Year:
YYYY (e.g., 1997)
Year and month:
YYYY-MM (e.g., 1997-07)
Complete date:
YYYY-MM-DD (e.g., 1997-07-16)
Complete date plus hours and minutes:
YYYY-MM-DDThh:mmTZD (e.g., 1997-07-16T19:20+01:00)
Complete date plus hours, minutes and seconds:
YYYY-MM-DDThh:mm:ssTZD (e.g., 1997-07-16T19:20:30+01:00)
Complete date plus hours, minutes, seconds and a decimal fraction of a second
YYYY-MM-DDThh:mm:ss.sTZD (e.g., 1997-07-16T19:20:30.45+01:00)
where:
YYYY = four-digit year
MM = two-digit month (01=January, etc.)
DD = two-digit day of month (01 through 31)
hh = two digits of hour (00 through 23) (am/pm NOT allowed)
mm = two digits of minute (00 through 59)
ss = two digits of second (00 through 59)
s = one or more digits representing a decimal fraction of a second
TZD = time zone designator (Z or +hh:mm or -hh:mm)
Time conversion#
Basic time formats can be converted using Python’s datetime library.
Pandas has its own datetime converter.
now = datetime.now()
formatted = now.strftime("%d/%m-%Y %H:%M:%S")
print("An arbitrary format:", formatted)
reformatted = datetime.strptime(formatted, "%d/%m-%Y %H:%M:%S")
print("Reformatted to the standard:", reformatted)
An arbitrary format: 04/12-2024 21:23:08
Reformatted to the standard: 2024-12-04 21:23:08
Exercise#
Read the white_rabbit.dat data using Pandas.
Convert the columns into standard Python format datetimes in the same timezone.
Concatenate the three columns into a single time series.
Convert it to your local timezone.
Confirm vilsually that you have handled each series correctly by plotting the resulting datetimes, achieving a perfectly straight line.