Tempo in Computing
Clocks, Timestamps, e Synchronization
Learn Computer TempoComputers need preciso time for logging, scheduling, security certificates, database transactions, e countless outro operations. Understanding como systems measure, store, e synchronize time é essential for developers, administrators, e anyone troubleshooting time-related issues.
Como Computers Keep Tempo
Hardware Clocks
- RTC (Real-Tempo Clock): Battery-backed chip esse keeps time quando power é off
- Crystal oscillator: Quartz crystal providing stable frequency (typically 32.768 kHz)
- Drift: Hardware clocks drift seconds to minutes per month
Sistema Clock
- Software clock maintained by o operating sistema
- Initialized from RTC at boot
- Synchronized via network protocols (NTP)
- Can be adjusted mais frequently than RTC
Network Tempo Protocol (NTP)
NTP synchronizes computer clocks over a network, achieving millisecond accuracy over o internet e sub-millisecond on LANs.
Stratum Levels
| Stratum | Source | Accuracy |
|---|---|---|
| 0 | Atomic clock, GPS receiver | Nanoseconds |
| 1 | Directly connected to stratum 0 | Microseconds |
| 2 | Synced to stratum 1 | Milliseconds |
| 3-15 | Cada level syncs to one above | Degrades slightly |
Como NTP Works
- Client sends request com timestamp
- Server responds com its timestamps
- Client calculates network delay e offset
- Clock é gradually adjusted (slewed) to avoid jumps
Tempo Storage Formats
| Format | Exemplo | Usar 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
Diferente machines têm 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 com bounded uncertainty
Comuns Tempo Issues
Y2K Problem (1999-2000)
Two-digit years couldn't represent 2000. Cost billions to fix worldwide.
Year 2038 Problem
32-bit Unix timestamps overflow on January 19, 2038. Migration to 64-bit ongoing.
Leap Segundo Bugs
Alguns systems crash quando clocks show 23:59:60. Leap seconds being phased out by 2035.
DST Transition Bugs
Scheduled tasks may run twice ou skip during DST changes.
Melhor Practices
- Usar UTC internally: Store e process in UTC; converter for display
- Usar ISO 8601: Unambiguous, sortable format
- Include timezone info: 2024-01-15T14:30:00+05:30, not apenas "14:30"
- Synchronize via NTP: Keep todos servers within milliseconds
- Test edge cases: DST transitions, leap years, year boundaries
- Usar 64-bit timestamps: Avoid 2038 problem
Conclusão
Computer time involves hardware clocks, operating sistema synchronization, e network protocols trabalhando together. NTP keeps clocks preciso across networks. Store times in UTC com timezone information; use ISO 8601 format for interchange. Be aware of edge cases like DST transitions, leap seconds, e o 2038 problem. In distributed systems, clock synchronization é fundamental to correctness.