Dates, Time Zones, and ISO 8601
Ask a human "What time is it?" and they say "2:00 PM."
Ask a programmer, and they start sweating.
Time is complex because of geography, politics, and history.
The Problem with Local Time
If you save a file timestamp as "2024-01-01 10:00:00", you have created a mystery.
- 10:00 AM in New York?
- 10:00 AM in Tokyo?
- 10:00 AM in London?
Without a Time Zone, a timestamp is meaningless data.
UTC: The Universal Standard
To solve this, computers use UTC (Coordinated Universal Time). It is the time at 0° longitude (the Prime Meridian). It never observes Daylight Savings Time.
The Golden Rule: Always store dates in UTC. Only convert them to local time when showing them to a user.
ISO 8601: The Format
This is the international standard for writing dates.YYYY-MM-DDTHH:mm:ssZ
Example: 2024-02-15T14:30:00Z
- T: Separator between date and time.
- Z: "Zulu" time (which means UTC).
If you see 2024-02-15T14:30:00-05:00, the -05:00 is the Offset. It means "This time is 5 hours behind UTC."
Unix Epoch
Computers often store time as a single integer: The Unix Timestamp.
This is the number of seconds that have passed since January 1, 1970 (UTC).
0= Jan 1, 1970.1700000000= Nov 14, 2023.
This is very efficient for computers to compare (is A > B?), but humans can't read it without a converter.