¿Qué es Epoch?
El concepto de la época Unix, también conocido como tiempo Unix, tiempo POSIX o timestamp Unix, denota la cantidad de
segundos transcurridos desde el 1 de enero de 1970 (medianoche UTC/GMT), excluyendo los segundos intercalares (ISO 8601:
1970-01-01T00:00:00Z). Aunque técnicamente señala el tiempo Unix 0 (medianoche del 1/1/1970), el término 'época' se usa
comúnmente como sinónimo de tiempo Unix.
Aquí hay algunas conversiones de intervalos de tiempo comunes a segundos y milisegundos.
| Tiempo | Segundos (s) | Milisegundos (ms) |
|---|
| 1 Hora | 3,600 Segundos | 3,600,000 Milisegundos |
| 1 Día | 86,400 Segundos | 86,400,000 Milisegundos |
| 1 Semana | 604,800 Segundos | 604,800,000 Milisegundos |
| 1 Año | 31,556,926 Segundos | 31,556,952,000 Milisegundos |
Aquí hay algunos ejemplos de cómo obtener el epoch actual en varios lenguajes de programación y herramientas.
| Lenguaje | Fragmento de código |
|---|
| JavaScript | Math.floor(new Date().getTime()/1000) |
| Python | import time; print(int(time.time())) |
| Java | System.out.println(System.currentTimeMillis() / 1000); |
| Go | fmt.Println(time.Now().Unix()) |
| Ruby | puts Time.now.to_i |
| PHP | echo time(); |
| C# | Console.WriteLine((DateTime.UtcNow - new DateTime(1970, 1, 1)).TotalSeconds); |
| Swift | print(Int(NSDate().timeIntervalSince1970)) |
| Kotlin | println(System.currentTimeMillis() / 1000) |
| Rust | println!("{}", SystemTime::now().duration_since(UNIX_EPOCH).unwrap().as_secs()); |
| C | printf("%ld\n", (long)time(NULL)); |
| C++ | std::cout << std::time(0) << std::endl; |
| Perl | print time; |
| Linux/Unix | date +%s |
| Postgres | SELECT extract(epoch from now()); |
| MySQL | SELECT UNIX_TIMESTAMP(); |
| Oracle | SELECT (SYSDATE - TO_DATE('1970-01-01', 'YYYY-MM-DD')) * 86400 FROM DUAL; |
| SQL Server | SELECT DATEDIFF(s, '1970-01-01 00:00:00', GETUTCDATE()); |
- Guía para principiantes sobre el tiempo Epoch
- Conversión del tiempo Epoch