Time and timestamps
Tokens, cookies and certificates all depend on time. You will learn how computers record a moment, how people read it, and why mismatched units or clocks can make a valid credential look expired.
Counting seconds
A ticket that says "valid until 6 pm" works as long as everyone agrees which 6 pm it means. The photo app Ava uses has users in Lisbon and Tokyo, servers in two other countries, and tokens that must stop working at one exact moment everywhere. Writing "6 pm" would leave every one of those computers guessing at the time zone.
Computers avoid the question by counting. Unix time is the number of seconds since midnight at the start of 1 January 1970, in UTC, a moment called the epoch. UTC is the world's reference time, explained in the next section. Unix time is the same number on every computer at the same moment, whatever time zone the computer is set to. date +%s prints it:
$ date +%s
1791216000Your number will be larger, because time has moved on since this lesson was written. Unix time counts every day as exactly 86,400 seconds and ignores the occasional leap second, which keeps the arithmetic simple.
You will meet these numbers often. The curl cookieA small value that a website asks the browser to store and send back with later matching requests. Cookies commonly carry a session identifier, so the site can recognize an ongoing sign-in without your password. jar in What browsers add stores each cookie's expiry this way, and the tokens in later lessons carry the times they were issued and when they expire:
{
"sub": "ava",
"iat": 1791212400,
"exp": 1791216000
}iat is when the tokenA piece of data issued by a trusted service that a client presents as a credential, for example to call an API or to report a sign-in. Some tokens carry readable claims; others are random strings that only the issuer can interpret. was issued and exp is when it expires. The difference is 3,600 seconds, so this token lasts one hour. Watch the number of digits. A Unix time in seconds has 10 digits for the rest of this century, while JavaScript, the language web pages run, counts in milliseconds: its Date.now() gives 13 digits. Comparing a millisecond value with a seconds value is a classic bug. A current time of 1791214200000 milliseconds is far larger than an exp of 1791216000 seconds, so the token always looks expired; mix the units the other way round and it looks valid for tens of thousands of years.
UTC and ISO 8601
Numbers suit computers, but people need dates. The reference for writing them is UTC, Coordinated Universal Time. Every time zone is an offset from it: Lisbon is UTC in winter and one hour ahead in summer, and Tokyo is always nine hours ahead.
Because offsets change with summer time, and sometimes by law, a computer does not store a zone as one fixed offset. It uses a name from the IANA time zone database, a list kept by the Internet Assigned Numbers Authority and shipped with every operating system. Each name is a region and a city, such as Europe/Lisbon or Asia/Tokyo, and stands for that zone's whole history of offsets, so the computer can work out the right offset for any date.
The TZ environment variable tells a program which zone to use. Written in front of a command, with no semicolon between them, an assignment applies to that one command and leaves your shell as it was:
$ TZ=Asia/Tokyo date +%H:%M%z
01:00+0900%H and %M are the hour and minute, and %z prints the zone's offset from UTC, here +0900, written without a colon.
ISO 8601 is the standard way to write a date and time as text. The moment from the token's exp is:
2026-10-05T16:00:00ZThe date comes first, from the largest unit to the smallest, then a T, then the time. The final Z means the time is in UTC. A time in another zone carries its offset instead, so 2026-10-05T18:00:00+02:00 is the same moment, written by a clock two hours ahead of UTC. Strings in this form, all in UTC, sort into time order with an ordinary sort, because the largest unit comes first.
date writes ISO 8601 when you give it the format. The -u option makes it use UTC:
date -u +%Y-%m-%dT%H:%M:%SZ%Y, %m and %d are the year, month and day, %S is the second, and the T and Z are typed as they are. Always pair that Z with -u. Without it, date prints the local time of whatever zone the computer or TZ names, and the Z claims it is UTC when it may not be.
Other formats turn up too, and each means a moment in UTC. HTTPThe Hypertext Transfer Protocol, the rules browsers, apps, and servers use to send requests and responses on the web. responses carry a Date header such as Date: Mon, 05 Oct 2026 16:00:00 GMT. OpenSSL shows a certificateA signed statement that a public key belongs to a name, such as a website's hostname, for stated purposes and a limited period. A certificate authority signs it, so software that trusts the authority can connect the key to the name.'s dates as Oct 5 16:00:00 2026 GMT. Some sign-in protocols write ISO 8601 directly into their messages, as in NotOnOrAfter="2026-10-05T16:05:00Z". GMT and UTC differ by less than a second, and these formats treat them as the same.
Converting between the two
To read a Unix time, ask date to show that moment instead of the current one. Here Ubuntu and macOS differ. Ubuntu's date comes from the GNU project, the source of most Linux command-line tools, and macOS has the BSD version, which descends from a different family of Unix. The two agree on +%s and -u, but spell the option for "show this moment" differently. Always give a format too: without one, date chooses a layout from your language settings, which differs from one computer to the next.
Ubuntu 24.04 (Windows with WSL, or Linux)
-d names the date to show, and @ in front of a number means "this many seconds after the epoch":
$ date -u -d @1791216000 +%Y-%m-%dT%H:%M:%SZ
2026-10-05T16:00:00Z
$ date -u -d 2026-10-05T16:00:00Z +%s
1791216000The second command goes the other way: -d also reads an ISO 8601 time, and +%s prints it as Unix time.
macOS 14 or later with Homebrew
-r takes a number of seconds after the epoch:
$ date -u -r 1791216000 +%Y-%m-%dT%H:%M:%SZ
2026-10-05T16:00:00Z
$ date -j -u -f %Y-%m-%dT%H:%M:%SZ 2026-10-05T16:00:00Z +%s
1791216000The second command goes the other way. -f describes the layout of the text that follows, and -j tells date only to convert it, never to try to set the computer's clock.
Notes written for one system often fail on the other with an illegal option or invalid date message. When that happens, check which date the instructions assumed.
A function keeps the difference in one place. Define iso once with the body your system needs, and every later command can call iso 1791216000 without caring which date runs underneath:
Ubuntu 24.04 (Windows with WSL, or Linux)
iso() {
date -u -d "@$1" +%Y-%m-%dT%H:%M:%SZ
}macOS 14 or later with Homebrew
iso() {
date -u -r "$1" +%Y-%m-%dT%H:%M:%SZ
}Arithmetic with timestamps
Because a Unix time is just a number, the arithmetic expansion $(( )) works on it directly. How long until the example token expires, or how long ago did it?
now=$(date +%s)
exp=1791216000
echo $(( exp - now ))A positive answer is the number of seconds left. A negative one means the moment has passed. Divide by 60 for minutes, remembering that bash keeps whole numbers only and drops any remainder. Lifetimes work the same way: $(( exp - iat )) for the token above is 3600. A few durations come up so often that they are worth knowing on sight: 300 is five minutes, 3600 an hour and 86400 a day.
Going forward is addition. An hour from now is $(( now + 3600 )), which iso turns into ISO 8601. A test compares two times:
if [[ $now -ge $exp ]]; then
echo "expired"
else
echo "still valid for $(( (exp - now) / 60 )) minutes"
fiA sum you need more than once is worth a name of its own, like iso. This one prints the whole minutes between two Unix times:
$ minutes_between() { echo $(( ($2 - $1) / 60 )); }
$ minutes_between 1791212400 1791216000
60When clocks disagree
All of this assumes that computers agree on the current time, and they only roughly do. Each keeps its own clock, and clocks drift. Most stay close to the right time by asking time servers on the internet, using the Network Time ProtocolAn agreed set of rules for the messages two systems exchange and what each message means. HTTP, OAuth 2.0, OpenID Connect and SAML are all protocols. (NTP), but a laptop that has slept, a virtual machine that was paused or a server with a broken time service can be minutes out.
The difference is called clock skew, and identity systems feel it first. A service that receives the example token compares three numbers: iat, its own clock, and exp. With a correct clock reading 1791214200, half an hour after the token was issued, the token is still valid with 1,800 seconds left. Now suppose the photo APIAn application programming interface: a defined way for software to interact with a service. A web API offers endpoints where other programs send HTTP requests. that receives it runs three minutes slow and reads 1791212220 when the token arrives. To the API, the token was issued 180 seconds in its future, so a careful API refuses it as not valid yet. An API whose clock runs fast has the opposite problem: it treats tokens as expired before their time.
A correct clock
- iat
- 1791212400
- Clock
- 1791214200
- exp
- 1791216000
Still valid with 1,800 seconds left
Runs three minutes slow
- iat
- 1791212400
- Clock
- 1791212220
- exp
- 1791216000
Not valid yet: issued 180 seconds in its future
Milliseconds compared with seconds
- iat
- 1791212400
- Clock
- 1791214200000
- exp
- 1791216000
Always looks expired
A 60-second tolerance absorbs a clock a few seconds out, but not one 180 seconds out. The fix for skew is a correct clock, not a bigger allowance.
Systems that check times therefore allow a small tolerance, often a minute or two, on each side. A 60-second tolerance absorbs a clock a few seconds out, but not one 180 seconds out. A larger tolerance would hide broken clocks and keep expired tokens working for longer, so the fix for skew is a correct clock, not a bigger allowance. No tolerance rescues the third case: a unit mistake is off by a factor of a thousand, and only the code can fix it.
To check your own clock, compare it with the Date header of a server you trust:
date -u '+%a, %d %b %Y %H:%M:%S GMT'
curl -sSI https://www.example.com/ | grep -i '^date:'%a and %b are the short day and month names the header uses, and -I asks curl for the response headers only. The two times should agree to within a second or two. On Ubuntu, timedatectl shows whether the system clock is synchronized. Under WSL the Linux clock can fall behind after Windows sleeps, and running wsl --shutdown in Windows before you open Ubuntu again brings it back in line. On macOS, keep "Set time and date automatically" turned on in the Date and Time settings.
Check your understanding
Try these questions before moving on. If an answer isn't right, use the feedback and try again.
Try it in the Lab
- Convert timestamps, work out a token's lifetime and check your clockRead Unix time and ISO 8601, write small functions that convert and subtract timestamps on your system, compare your clock with a server, then send the lifetime and expiry of your own token times.