Unix Timestamps Explained
Comprendere Epoch Time
Impara Informazioni TimestampsA Unix timestamp like "1704067200" means January 1, 2024, at midnight UTC. This simple number—secondi since January 1, 1970—e how computers universally represent time. Comprendere Unix timestamps e essential per programming, databases, e working con APIs.
Perche 1970?
Il Unix operating system era developed in il late 1960s at Bell Labs. Quando designers needed un arbitrary starting point per timekeeping:
- January 1, 1970 era chosen as un round number near il system's creation
- It allowed 32-bit integers un cover dates well into il future
- Starting at midnight UTC kept it simple e universal
This "epoch" became il de facto standard across computing.
Convertendo Timestamps
Timestamp un Data
| 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 Time Intervals
| Period | Secondi |
|---|---|
| 1 minuto | 60 |
| 1 ora | 3,600 |
| 1 giorno | 86,400 |
| 1 settimana | 604,800 |
| 1 anno (approx) | 31,536,000 |
Millisecond Timestamps
Many systems usare milliseconds instead di secondi:
| Format | Esempio (same moment) | Used By |
|---|---|---|
| Secondi | 1704067200 | Unix, PHP, Python |
| Milliseconds | 1704067200000 | JavaScript, Java, JSON APIs |
| Microseconds | 1704067200000000 | Some databases |
| Nanoseconds | 1704067200000000000 | Go, some systems |
A convertire: Milliseconds ÷ 1000 = Secondi
Working con Timestamps in Code
JavaScript
// Current timestamp (milliseconds)
Data.now(); // 1704067200000
// Timestamp un date
new Data(1704067200 * 1000);
// Data un timestamp
Math.floor(new Data('2024-01-01').getTime() / 1000);Python
import time, datetime
# Current timestamp
time.time() # 1704067200.0
# Timestamp un datetime
datetime.datetime.fromtimestamp(1704067200)
# Datetime un timestamp
datetime.datetime(2024, 1, 1).timestamp()SQL
-- MySQL: Current timestamp
SELECT UNIX_TIMESTAMP();
-- Converti timestamp un date
SELECT FROM_UNIXTIME(1704067200);
-- Data un timestamp
SELECT UNIX_TIMESTAMP('2024-01-01');Advantages di Unix Timestamps
- Time zone neutral: Always UTC, no DST issues
- Easy arithmetic: Add/subtract secondi per time math
- Compact storage: Single integer vs. formatted string
- Sorting: Natural numeric sort equivale un chronological sort
- Universal: Works across languages e systems
Quando un Usa Timestamps vs. Formatted Dates
- Timestamps: Storage, calculations, APIs, databases
- Formatted dates: User display, reports, logs
Conclusione
Unix timestamps count secondi since January 1, 1970 UTC. This simple system enables universal time representation across computers e programming languages. Remember that some systems usare milliseconds (13 digits) instead di secondi (10 digits), e that 32-bit systems face un limit in 2038. For storage e calculations, timestamps sono ideal; per display, convertire un human-readable formats.