Tiempo in Computing
Clocks, Timestamps, y Synchronization
Learn Computer TiempoComputers necesitar preciso time for logging, scheduling, security certificates, database transactions, y countless otro operations. Understanding como sistemas medir, store, y synchronize time es esencial for developers, administrators, y anyone troubleshooting time-related issues.
How Computers Keep Tiempo
Hardware Clocks
- RTC (Real-Tiempo Clock): Battery-backed chip eso keeps time cuando power es off
- Crystal oscillator: Quartz crystal providing stable frequency (typically 32.768 kHz)
- Drift: Hardware clocks drift segundos un/una minutos per mes
System Clock
- Software clock maintained by el/la operating sistema
- Initialized desde RTC at boot
- Synchronized via network protocols (NTP)
- Can be adjusted mas frequently than RTC
Network Tiempo Protocol (NTP)
NTP synchronizes computer clocks over un/una network, achieving millisecond accuracy over el/la internet y sub-millisecond on LANs.
Stratum Levels
| Stratum | Source | Accuracy |
|---|---|---|
| 0 | Atomic clock, GPS receiver | Nanoseconds |
| 1 | Directly connected un/una stratum 0 | Microseconds |
| 2 | Synced un/una stratum 1 | Milliseconds |
| 3-15 | Each level syncs un/una one above | Degrades slightly |
How NTP Works
- Client sends request with timestamp
- Server responds with su timestamps
- Client calculates network delay y offset
- Clock es gradually adjusted (slewed) un/una avoid jumps
Tiempo Storage Formats
| Format | Ejemplo | Use Case |
|---|---|---|
| Unix timestamp (s) | 1705329000 | Databases, APIs |
| Unix timestamp (ms) | 1705329000000 | JavaScript, Java |
| ISO 8601 | 2024-01-15T14:30:00Z | JSON, APIs, logs |
| RFC 2822 | Mon, 15 Jan 2024 14:30:00 +0000 | Email headers |
| SQL DATETIME | 2024-01-15 14:30:00 | Databases |
| SQL TIMESTAMP | 2024-01-15 14:30:00 | Auto-updating fields |
Distributed Systems Challenges
Clock Skew
Different machines have slightly diferente times, causing:
- Out-of-order events in logs
- Race conditions in distributed transactions
- Certificate validation failures
Solutions
- NTP/PTP: Keep clocks synchronized
- Logical clocks: Vector clocks, Lamport timestamps
- Consensus protocols: Raft, Paxos for ordering events
- TrueTime (Google): GPS-synchronized with bounded uncertainty
Common Tiempo Issues
Y2K Problem (1999-2000)
Two-digit anos couldn't represent 2000. Cost billions un/una fix worldwide.
Year 2038 Problem
32-bit Unix timestamps overflow on January 19, 2038. Migration un/una 64-bit ongoing.
Leap Second Bugs
Some sistemas crash cuando clocks mostrar 23:59:60. Leap segundos being phased out by 2035.
DST Transition Bugs
Scheduled tasks may run twice o skip during DST changes.
Best Practices
- Use UTC internally: Store y process in UTC; convertir for mostrar
- Use ISO 8601: Unambiguous, sortable format
- Include timezone info: 2024-01-15T14:30:00+05:30, not solo "14:30"
- Synchronize via NTP: Keep todo servers within milliseconds
- Test edge cases: DST transitions, leap anos, ano boundaries
- Use 64-bit timestamps: Avoid 2038 problem
Conclusion
Computer time involves hardware clocks, operating sistema synchronization, y network protocols working together. NTP keeps clocks preciso across networks. Store times in UTC with timezone informacion; usar ISO 8601 format for interchange. Be aware of edge cases like DST transitions, leap segundos, y el/la 2038 problem. In distributed sistemas, clock synchronization es fundamental un/una correctness.