How Linux keeps time

0

Knowing the exact time is important to computers for many reasons. A GPS, which is a computer, needs to know the exact time so it can determine your exact location. Financial systems require the exact time so that transactions can be accurately timestamped and thus sorted into the correct time-sequence. Most of us just need to display an accurate time on our desktop or use the timestamps on our files so we can find the most recent versions.

The Epoch

The term “epoch” refers to the reference point for timekeeping of any sort. In computing, epoch refers to a specific, fixed moment in time from which system time is measured. Time is measured in seconds, i.e., ticks, since the epoch. I usually refer to this as Unix or Linux time.

Different computer systems use different dates, but the Epoch for Unix and Linux is defined as 1970-01-01 00:00:00 GMT. Time is measured in seconds since that Epoch.

Working with dates

Here are a few examples using the date command to illustrate the relationship between the Linux time and the date and time strings we humans can easily interpret. The Linux date command can tell us the current time in a format defined by the locale.

$ date
Tue Jan 21 10:18:41 AM EST 2025

We can also use the date command to display the number of seconds since the Epoch. This is a universal number and independent of local time zone and locale formats.

$ date +%s
1737491563

Then we can use it to convert the number of seconds displayed in the previous command back into a human-readable date. This converts the seconds into a date and time string based on the locale and time zone specified in the operating system’s configuration.

$ date --date='@1737491563'
Tue Jan 21 03:32:43 PM EST 2025

It can also give us the number of seconds since the Epoch for a specified date and time.

$ date --date='2025-01-25 15:02:21 0500' +%s
1737817341

The date command has many interesting options you can use for display and manipulation of dates. The man page for the date command has enough information for most users, but the info page ( info date) contains much more information and many examples illustrating the use of the date command.

Leave a Reply