Unix Timestamps Explained

Understanding Epoch Tempo

Learn Sobre Timestamps

A Unix timestamp like "1704067200" means January 1, 2024, at midnight UTC. Este simples number—seconds since January 1, 1970—é como computers universally represent time. Understanding Unix timestamps é essential for programming, databases, e trabalhando com APIs.

Por que 1970?

O Unix operating sistema foi developed in o late 1960s at Bell Labs. Quando designers needed an arbitrary starting point for timekeeping:

  • January 1, 1970 foi chosen as a round number near o sistema's creation
  • It allowed 32-bit integers to cover dates bem into o future
  • Starting at midnight UTC kept it simples e universal

Este "epoch" became o de facto padrão across computing.

Converting Timestamps

Timestamp to Date

TimestampHuman-Readable (UTC)
0January 1, 1970 00:00:00
86400January 2, 1970 00:00:00
1000000000September 9, 2001 01:46:40
1234567890February 13, 2009 23:31:30
1704067200January 1, 2024 00:00:00
2000000000May 18, 2033 03:33:20
2147483647January 19, 2038 03:14:07 (32-bit limit)

Useful Tempo Intervals

PeriodSeconds
1 minute60
1 hour3,600
1 day86,400
1 week604,800
1 year (approx)31,536,000

Millisecond Timestamps

Muitos systems use milliseconds instead of seconds:

FormatExemplo (mesmo moment)Usado By
Seconds1704067200Unix, PHP, Python
Milliseconds1704067200000JavaScript, Java, JSON APIs
Microseconds1704067200000000Alguns databases
Nanoseconds1704067200000000000Go, alguns systems

Para converter: Milliseconds ÷ 1000 = Seconds

Trabalhando com Timestamps in Code

JavaScript

// Corrente timestamp (milliseconds)
Date.agora(); // 1704067200000

// Timestamp to date
novo Date(1704067200 * 1000);

// Date to timestamp
Math.floor(novo Date('2024-01-01').getTime() / 1000);

Python

import time, datetime

# Corrente timestamp
time.time()  # 1704067200.0

# Timestamp to datetime
datetime.datetime.fromtimestamp(1704067200)

# Datetime to timestamp
datetime.datetime(2024, 1, 1).timestamp()

SQL

-- MySQL: Corrente timestamp
SELECT UNIX_TIMESTAMP();

-- Converter timestamp to date
SELECT FROM_UNIXTIME(1704067200);

-- Date to timestamp
SELECT UNIX_TIMESTAMP('2024-01-01');

Advantages of Unix Timestamps

  • Tempo zone neutral: Always UTC, no DST issues
  • Fácil arithmetic: Add/subtract seconds for time math
  • Compact storage: Single integer vs. formatted string
  • Sorting: Natural numeric sort é igual a chronological sort
  • Universal: Works across languages e systems

Quando to Usar Timestamps vs. Formatted Dates

  • Timestamps: Storage, calculations, APIs, databases
  • Formatted dates: User display, reports, logs

Conclusão

Unix timestamps count seconds since January 1, 1970 UTC. Este simples sistema enables universal time representation across computers e programming languages. Remember esse alguns systems use milliseconds (13 digits) instead of seconds (10 digits), e esse 32-bit systems face a limit in 2038. For storage e calculations, timestamps são ideal; for display, converter to human-readable formats.

Artigos Relacionados

Unix Timestamps Explained: Epoch Tempo for Developers | YounitConverter