Displaying Dates
Displaying Dates
We haven't discussed time series yet, when do, we will need to describe dates. There are countless ways to format a date but there is only one way we will do it, ISO8601
1. In the ISO8601 we represent dates like so:
2006-01-02T15:04:05Z07:00
This corresponds to:
Mon Jan 2 15:04:05 MST 2006
Displaying Dates
strftime
The more common way to format time is strftime
. This approach uses a collection of symbols to represent components of time, for a list of these symbols see man strftime
2, R's ?strftime
and Python's help(time.strftime)
:
man strftime
The characters of ordinary character sequences (including the null byte) are copied verbatim from format to s. However, the characters of conversion specifications are replaced as shown in
the list below. In this list, the field(s) employed from the tm structure are also shown.
`%a` The abbreviated name of the day of the week according to the current locale. (Calculated from tm_wday.) (The specific names used in the current locale can be obtained by calling
nl_langinfo(3) with ABDAY_{1–7} as an argument.)
`%A` The full name of the day of the week according to the current locale. (Calculated from tm_wday.) (The specific names used in the current locale can be obtained by calling nl_lang‐
info(3) with DAY_{1–7} as an argument.)
`%b` The abbreviated month name according to the current locale. (Calculated from tm_mon.) (The specific names used in the current locale can be obtained by calling nl_langinfo(3) with
ABMON_{1–12} as an argument.)
`%B` The full month name according to the current locale. (Calculated from tm_mon.) (The specific names used in the current locale can be obtained by calling nl_langinfo(3) with
MON_{1–12} as an argument.)
The strftime
approach was named after the C library that popularized it, it's used in C++, Rust, R, Python, PHP, Ruby and many more.
To represent in ISO8601 via strftime
, the format would be %Y-%m-%dT%H:%M:%S
, so e.g.:
-
R
format(Sys.time(), "%Y-%m-%dT%H:%M:%S")
-
Python
from datetime import datetime now = datetime.now() formatted = now.strftime("%Y-%m-%dT%H:%M:%S") print(formatted)
-
Shell
# ISO8601 date "+%Y-%m-%dT%H:%M:%S" # Unix date %+s
More Languages
-
Rust
extern crate chrono; use chrono::offset::Local; use chrono::DateTime; fn main() { let now: DateTime<Local> = Local::now(); println!("{}", now.format("%Y-%m-%dT%H:%M:%S").to_string()); }
-
Julia
using Dates println(Dates.format(now(), "yyyy-mm-ddTHH:MM:SS"))
-
C
#include <stdio.h> #include <time.h> int main() { char buffer[80]; time_t t = time(NULL); struct tm *tm_info; tm_info = localtime(&t); strftime(buffer, 80, "%Y-%m-%dT%H:%M:%S", tm_info); printf("%s\n", buffer); return 0; }
You'll notice that each language (except Go and Julia) uses a similar approach to format the time, standardized by the strftime
approach.
Reference Time
In this approach the language requires a specific date to format time, the specific date (Mon Jan 2 15:04:05 MST 2006
) was a design decision made by the development team because they are distinct in the context of a date -- they used each number from 0 to 6 only once:
2006
: Represents the full year format.01
: Stands for the numerical month format.02
: Represents the day of the month.15
: Stands for the 24-hour format time.04
: Represents the minutes.05
: Represents the seconds.MST
: Time zoneMon
andJan
: Are used for the abbreviated day of the week and month respectively.
Reference time is not a common approach, it's more of a unique quirk of the Go language. We draw attention to it here because it is a convenient way to express time in a human readable way, this can be useful when looking up documentaion, researching or querying a LLM. For example, to print the current time in ISO8601 format we would write it like this:
package main
import (
"fmt"
"time"
)
func main() {
t := time.Now()
fmt.Println(t.Format("2006-01-02T15:04:05-0700"))
}