Time in Computing
Clocks, Timestamps, e Synchronization
Impara Computer TimeComputers need accurate time per logging, scheduling, security certificates, database transactions, e countless other operations. Comprendere how systems misurare, store, e synchronize time e essential per developers, administrators, e anyone troubleshooting time-related issues.
How Computers Keep Time
Hardware Clocks
- RTC (Real-Time Clock): Battery-backed chip that keeps time quando power e off
- Crystal oscillator: Quartz crystal providing stable frequency (typically 32.768 kHz)
- Drift: Hardware clocks drift secondi un minuti per mese
System Clock
- Software clock maintained da il operating system
- Initialized da RTC at boot
- Synchronized via network protocols (NTP)
- Can be adjusted more frequently than RTC
Network Time Protocol (NTP)
NTP synchronizes computer clocks over un network, achieving millisecond accuracy over il internet e sub-millisecond on LANs.
Stratum Levels
| Stratum | Source | Accuracy |
|---|---|---|
| 0 | Atomic clock, GPS receiver | Nanoseconds |
| 1 | Directly connected un stratum 0 | Microseconds |
| 2 | Synced un stratum 1 | Milliseconds |
| 3-15 | Each level syncs un one above | Degrades slightly |
How NTP Works
- Client sends request con timestamp
- Server responds con its timestamps
- Client calculates network delay e offset
- Clock e gradually adjusted (slewed) un avoid jumps
Time Storage Formats
| Format | Esempio | Usa 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 hanno slightly different times, causing:
- Out-di-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 per ordering events
- TrueTime (Google): GPS-synchronized con bounded uncertainty
Comuni Time Issues
Y2K Problem (1999-2000)
Two-digit anni couldn't represent 2000. Cost billions un fix worldwide.
Anno 2038 Problem
32-bit Unix timestamps overflow on January 19, 2038. Migration un 64-bit ongoing.
Leap Secondo Bugs
Some systems crash quando clocks show 23:59:60. Leap secondi being phased out da 2035.
DST Transition Bugs
Scheduled tasks puo run twice o skip during DST changes.
Best Practices
- Usa UTC internally: Store e process in UTC; convertire per display
- Usa ISO 8601: Unambiguous, sortable format
- Include timezone info: 2024-01-15T14:30:00+05:30, not just "14:30"
- Synchronize via NTP: Keep all servers within milliseconds
- Test edge cases: DST transitions, leap anni, anno boundaries
- Usa 64-bit timestamps: Avoid 2038 problem
Conclusione
Computer time involves hardware clocks, operating system synchronization, e network protocols working together. NTP keeps clocks accurate across networks. Store times in UTC con timezone information; usare ISO 8601 format per interchange. Be aware di edge cases like DST transitions, leap secondi, e il 2038 problem. In distributed systems, clock synchronization e fundamental un correctness.