Vissza a bloghoz

Unix Timestamp Explained: What Is Epoch Time?

·6 perc olvasás

If you've ever seen a number like 1749673200 and been told it represents a date and time, you've encountered a Unix timestamp. Also known as "epoch time" or "Unix time," this system is the backbone of how computers track time. Let's break it down.

What Is a Unix Timestamp?

A Unix timestamp is the number of seconds that have elapsed since January 1, 1970, 00:00:00 UTC — a moment known as the "Unix epoch." This starting point was chosen somewhat arbitrarily in the early 1970s when the Unix operating system was being developed.

For example:

  • 0 = January 1, 1970, 00:00:00 UTC
  • 1000000000 = September 9, 2001, 01:46:40 UTC
  • 1749673200 = June 12, 2026, 01:00:00 UTC
  • Why Is It Useful?

    Unix timestamps have several advantages for computing:

  • Timezone-independent. A Unix timestamp represents the same moment everywhere on Earth. There's no ambiguity about which timezone it refers to.
  • Easy arithmetic. Want to know what time it will be in 3600 seconds? Just add 3600 to the timestamp. Need the difference between two moments? Subtract the timestamps.
  • Compact storage. A single integer stores the complete date and time.
  • Universal compatibility. Every programming language and database can work with Unix timestamps.
  • Unix Timestamps in Practice

    JavaScript

    ``javascript

    // Get current timestamp

    Date.now() // Returns milliseconds

    Math.floor(Date.now() / 1000) // Returns seconds

    // Convert timestamp to date

    new Date(1749673200 * 1000)

    `

    Python

    `python

    import time

    time.time() # Current timestamp (seconds)

    time.gmtime(1749673200) # Convert to struct_time

    `

    Databases

    Most databases store timestamps internally as Unix time or similar representations:

  • PostgreSQL: EXTRACT(EPOCH FROM NOW())
  • MySQL: UNIX_TIMESTAMP()
  • MongoDB: Dates stored as milliseconds since epoch
  • The Year 2038 Problem

    Here's a fascinating issue: many systems store Unix timestamps as 32-bit signed integers, which can represent values up to 2,147,483,647. This corresponds to January 19, 2038, at 03:14:07 UTC.

    After that moment, the timestamp will overflow and wrap around to a negative number, potentially causing systems to interpret dates as being in 1901. This is similar to the Y2K problem but for Unix systems.

    The solution is straightforward: use 64-bit integers, which can represent dates billions of years into the future. Most modern systems have already made this transition, but legacy systems may still be at risk.

    Millisecond Timestamps

    Some systems use milliseconds instead of seconds for higher precision:

  • JavaScript's Date.now() returns milliseconds
  • Java's System.currentTimeMillis() uses milliseconds
  • Multiply a seconds timestamp by 1000 to get milliseconds
  • Fun Timestamps

  • 1234567890 = February 13, 2009, 23:31:30 UTC — celebrated by geeks worldwide
  • 1111111111 = March 18, 2005, 01:58:31 UTC
  • 1500000000` = July 14, 2017, 02:40:00 UTC
  • Vissza a bloghoz🔗 Megosztás