Unix Timestamps Explained
Understanding Epoch Tempo
Learn Sobre TimestampsA 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
| Timestamp | Human-Readable (UTC) |
|---|---|
| 0 | January 1, 1970 00:00:00 |
| 86400 | January 2, 1970 00:00:00 |
| 1000000000 | September 9, 2001 01:46:40 |
| 1234567890 | February 13, 2009 23:31:30 |
| 1704067200 | January 1, 2024 00:00:00 |
| 2000000000 | May 18, 2033 03:33:20 |
| 2147483647 | January 19, 2038 03:14:07 (32-bit limit) |
Useful Tempo Intervals
| Period | Seconds |
|---|---|
| 1 minute | 60 |
| 1 hour | 3,600 |
| 1 day | 86,400 |
| 1 week | 604,800 |
| 1 year (approx) | 31,536,000 |
Millisecond Timestamps
Muitos systems use milliseconds instead of seconds:
| Format | Exemplo (mesmo moment) | Usado By |
|---|---|---|
| Seconds | 1704067200 | Unix, PHP, Python |
| Milliseconds | 1704067200000 | JavaScript, Java, JSON APIs |
| Microseconds | 1704067200000000 | Alguns databases |
| Nanoseconds | 1704067200000000000 | Go, 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.