Core timestamps and their differences explained

Core timestamps and their differences explained

Introduction

We'll go through some core timestamps when working with software systems.

TypeScript may not be your language, but it's mine. I'll use it to demonstrate examples.

Unix Timestamp

A Unix timestamp counts seconds. It starts from January 1, 1970, at 00:00:00 UTC. This start point is called the "Unix Epoch."

The word "Epoch" means the start of a system. Unix developers needed to track time across computers. They picked 1970 because Unix development started around then. Unix timestamps are powerful because they're simple. They're just numbers. This makes them easy to work with. You can store them efficiently. You can compare them easily.

Want to know if event A happened before event B? Just compare two numbers. That's it. This simplicity is why Unix timestamps are everywhere. You'll find them in databases. You'll see them in log files. They're used in caching systems too.

Note: For distributed systems, don't use these timestamps for event ordering. That's a different topic involving logical timestamps.

The original Unix timestamp had a problem. It used a 32-bit integer. This number will run out in 2038. We call this the Y2K38 problem. When it happens, systems will break.

Modern systems fixed this. They use 64-bit integers instead. This gives us much more time.

Unix timestamps have one more limitation. They don't handle leap seconds. Leap seconds are extra seconds we add to UTC time. We need them because Earth's rotation isn't perfect. The Earth actually slows down. It loses about 2 milliseconds every hundred years.

TypeScript example:

// Getting current Unix timestamp
const currentUnixTimestamp: number = Math.floor(Date.now() / 1000);

// Creating a Date from Unix timestamp
const unixTimestamp: number = 1635789600; // November 1, 2021 00:00:00 UTC

// Need to do * 1000 because Date expects milliseconds
const dateFromUnix: Date = new Date(unixTimestamp * 1000);

ISO 8601

ISO 8601 is a way to write dates and times as text. ISO stands for International Organization for Standardization. They create standards that everyone can follow.

The format looks like this: YYYY-MM-DDTHH:mm:ss.sssZ. Let's break that down:

  • YYYY is the year (2023)

  • MM is the month (01 for January)

  • DD is the day

  • T simply separates the date from time

  • HH:mm:ss.sss is hours, minutes, and seconds

  • Z means UTC time

What's UTC? It's the world's main time standard. When you see +01:00 instead of Z, it means "one hour ahead of UTC."

This format solves a common problem: dates can be confusing across cultures. Think about 03/04/2023. Is this March 4th or April 3rd? Americans would say March 4th. Europeans would say April 3rd. It's confusing!

ISO 8601 fixes this confusion. It arranges everything from biggest to smallest: year, then month, then day. There's no room for confusion.

The format is great for two reasons. First, humans can read it easily. Second, computers can process it without problems. APIs use this format a lot. APIs are how different computer systems talk to each other. You'll also see it in log files, which are records of what a system is doing.

TypeScript example:

const currentDate: Date = new Date();

// toISOString() returns a string in ISO 8601 format
// e.g. 2024-11-23T10:00:00.000Z -> this would be November 23rd, 2024 at 10:00:00 UTC
const isoString: string = currentDate.toISOString();

UTC (Coordinated Universal Time)

UTC is the world's main time standard. It's how we keep all clocks in sync. GMT was the old standard. UTC replaced it. GMT stands for Greenwich Mean Time, named after an observatory in Greenwich, London.

UTC is kept accurate using atomic clocks. Atomic clocks are super precise. They measure time by watching atoms vibrate. But here's something interesting: Earth's rotation isn't perfect. Sometimes UTC needs an extra second (called a leap second) to match Earth's actual rotation.

Note: Atomic clocks still aren't perfect. For ordering in distributed systems, use logical timestamps.

UTC acts like a central reference point. Think of it as the world's master clock. When computers in different time zones need to work together, they use UTC. It's like everyone agreeing to use the same language.

This is why computers often store time in UTC. Your phone might show local time, but behind the scenes, it's storing UTC. The computer just adds or subtracts hours to show your local time.

Let's say it's 3 PM in New York. The computer knows this is UTC-5 (5 hours behind UTC). So it stores 8 PM UTC. When someone in London looks at the same time, their computer knows London is UTC+0. The computer shows them 8 PM.

This is super important for distributed systems. Distributed systems are computer programs running on many machines worldwide. When these machines need to work together, they all speak UTC. This prevents confusion about when things happened at a point in time.

TypeScript example:

function getCurrentUTCComponents() {
  const now: Date = new Date();

  console.log(now.toUTCString()); // Example -> Sat, 23 Nov 2024 10:59:59 GMT

  return {
    // Example -> 2024
    year: now.getUTCFullYear(),
    // Example -> 11
    month: now.getUTCMonth() + 1, // Months are 0-based, hence the +1
    // Example -> 23
    day: now.getUTCDate(),
    // Example -> 10
    hours: now.getUTCHours(),
    // Example -> 59
    minutes: now.getUTCMinutes(),
    // Example -> 59
    seconds: now.getUTCSeconds(),
  };
}