0
1970-01-01T00:00:00Z
946684800
2000-01-01T00:00:00Z
2147483647
2038-01-19T03:14:07Z — max for 32-bit signed int
9223372036854775807
~292 billion years — no practical overflow
86400000
60 × 60 × 24 × 1000
31536000
365 × 24 × 60 × 60 (non-leap)
2024-01-15T12:30:00Z
YYYY-MM-DDTHH:MM:SSZ — widely recommended
2024-01-15T07:30:00-05:00
YYYY-MM-DDTHH:MM:SS±HH:MM
Mon, 15 Jan 2024 12:30:00 +0000
Used in email headers and HTTP headers
2024-01-15T12:30:00.000Z
Subset of ISO 8601 with milliseconds
Mon, 15 Jan 2024 12:30:00 GMT
Used in HTTP headers (Last-Modified, Date)
01/15/2024
Ambiguous — avoid in APIs
15/01/2024
Ambiguous — avoid in APIs
2024-01-15
SQL DATE type, BigQuery, Redshift
20240115123000
Lexicographically sortable
Date.now()
Returns milliseconds since epoch
Math.floor(Date.now() / 1000)
Returns whole seconds
new Date(ts * 1000).toISOString()
Multiply by 1000 if ts is in seconds
Math.floor(new Date('2024-01-15T12:30:00Z').getTime() / 1000)import time; time.time()
Returns float with sub-second precision
from datetime import datetime, timezone datetime.fromtimestamp(ts, tz=timezone.utc)
Always pass tz=timezone.utc to avoid local timezone
from datetime import datetime, timezone
datetime.fromisoformat('2024-01-15T12:30:00+00:00').timestamp()SELECT EXTRACT(EPOCH FROM NOW())::BIGINT;
Returns whole seconds
SELECT to_timestamp(1705319400);
SELECT UNIX_TIMESTAMP();
SELECT FROM_UNIXTIME(1705319400);
time.Now().UnixNano()
Nanoseconds precision
time.Now().Unix()
SystemTime::now().duration_since(UNIX_EPOCH).unwrap().as_secs()
time()
Returns seconds; microtime(true) for float
date +%s
GNU/BSD date; on macOS same command works
+00:00
Coordinated Universal Time — no DST
-05:00 / -04:00
EST in winter, EDT in summer (DST)
-06:00 / -05:00
-07:00 / -06:00
-08:00 / -07:00
+00:00 / +01:00
BST (British Summer Time) in summer
+01:00 / +02:00
Germany, France, Italy, Poland, etc.
+05:30
No DST — half-hour offset
+08:00
No DST — entire country one timezone
+09:00
No DST
+10:00 / +11:00
DST in southern hemisphere summer (Oct–Apr)
+12:00 / +13:00
| Unità | Cifre | Esempio | Intervallo appross. |
|---|---|---|---|
| Seconds (s) | 10 | 1705319400 | 1970 – 2286 |
| Milliseconds (ms) | 13 | 1705319400000 | 1970 – 2286 (×1000) |
| Microseconds (μs) | 16 | 1705319400000000 | Sub-second precision |
| Nanoseconds (ns) | 19 | 1705319400000000000 | Used by Go, Rust, Linux kernel |
I timestamp Unix sono sempre in UTC. Non contengono informazioni sul fuso orario. Questa è in realtà una caratteristica — fornisce un punto di riferimento universale.
La confusione nasce quando si convertono i timestamp in date leggibili dall'uomo:
const ts = 1700000000;
const date = new Date(ts * 1000);
date.toUTCString(); // "Tue, 14 Nov 2023 22:13:20 GMT"
date.toLocaleString(); // Dipende dal fuso orario locale dell'utente
date.toISOString(); // "2023-11-14T22:13:20.000Z"
Best practice: Archivia e trasmetti i timestamp in UTC. Converti all'ora locale solo al livello di presentazione, il più vicino possibile all'utente.
-- Timestamp corrente
SELECT EXTRACT(EPOCH FROM NOW());
-- Timestamp a data
SELECT TO_TIMESTAMP(1700000000);
-- Data a timestamp
SELECT EXTRACT(EPOCH FROM '2023-11-14'::timestamp);
Se una data mostra gennaio 1970, probabilmente hai passato secondi dove erano attesi millisecondi (o viceversa). Controlla sempre quale precisione si aspetta l'API.
Quando archivi i timestamp come numeri in virgola mobile, potresti perdere precisione oltre i millisecondi. Per la precisione a microsecondi o nanosecondi, usa interi con il moltiplicatore appropriato.
I timestamp Unix non tengono conto dei secondi intercalari. Un giorno Unix è sempre esattamente 86.400 secondi, anche se i giorni UTC effettivi occasionalmente hanno 86.401 secondi. Per la maggior parte delle applicazioni, questo è irrilevante. Per applicazioni scientifiche o satellitari, usa il TAI (Tempo Atomico Internazionale).
Archivia le date come timestamp (interi o tipi datetime nativi) per un ordinamento, confronto e aritmetica efficienti. Le stringhe formattate sono più difficili da interrogare e ordinare correttamente. La maggior parte dei database ha tipi datetime nativi che gestiscono bene questo aspetto. Riserva la formattazione delle stringhe per la visualizzazione e le risposte API.
Un timestamp Unix (detto anche epoch time) è il numero di secondi trascorsi dalla 00:00:00 UTC del 1° gennaio 1970, senza contare i secondi intercalari. È un singolo intero che identifica inequivocabilmente qualsiasi momento nel tempo, indipendente dal fuso orario.
I timestamp Unix tradizionali contano i secondi interi. I linguaggi e le API moderne (JavaScript Date.now(), Java System.currentTimeMillis()) usano i millisecondi — 1000× più grandi — per la precisione sub-secondo. Quando si interagisce, verificare sempre l'ordine di grandezza: un numero a 10 cifre sono secondi, a 13 cifre sono millisecondi.
Gli interi con segno a 32 bit possono rappresentare timestamp fino a 2.147.483.647, che corrisponde alle 03:14:07 UTC del 19 gennaio 2038. Dopo quel momento, i sistemi a 32 bit traboccheranno a valori negativi. I sistemi a 64 bit possono rappresentare date ben oltre l'anno 292 miliardi.
In JavaScript: new Date(ts * 1000).toISOString(). In Python: datetime.fromtimestamp(ts, tz=timezone.utc).isoformat(). In SQL (PostgreSQL): to_timestamp(ts). Il formato ISO 8601 è YYYY-MM-DDTHH:MM:SSZ per UTC, es. 2024-01-15T12:30:00Z.