Cette page est provisoirement disponible en anglais. La traduction française est en préparation.
Introduction
Epoch time (also called Unix timestamp or POSIX time) is a system for describing a point in time as the number of seconds that have elapsed since a specific reference date and time. Understanding epoch time is fundamental for developers, system administrators, and anyone working with timestamps in computing.
What is Epoch Time
Definition
Epoch time represents time as a continuous count of time units (usually seconds) from a fixed reference point called the "epoch":
TEXT1Epoch Timestamp = Current Time - Epoch Start Time 2 3Example (Unix Epoch): 4 Epoch Start: January 1, 1970, 00:00:00 UTC 5 Current Time: January 8, 2024, 00:00:00 UTC 6 Timestamp: 1704672000 seconds since epoch
Unix Epoch (POSIX Time)
The most widely used epoch system is Unix epoch (POSIX time):
TEXT1Unix Epoch Reference: 2 Start Date: January 1, 1970, 00:00:00 UTC 3 Time Units: Seconds (some systems use milliseconds) 4 Notation: 32-bit signed integer (common) or 64-bit 5 Max Range (32-bit): 1901-12-13 to 2038-01-19 6 Current Use: Linux, Unix, macOS, most programming languages
Interesting Fact: Unix epoch was chosen because it was a round number at the time Unix was being developed, making calculations simple and avoiding negative timestamps for most practical uses.
History of Unix Epoch
Unix epoch was established in the early 1970s:
- 1970: Unix epoch date chosen (January 1, 1970)
- 1971-1973: Initial Unix systems used time_t type (32-bit)
- 1988: POSIX standard formally adopted Unix time
- 2000s: Most systems transitioned to 64-bit time_t to avoid year 2038 problem
- 2020s: Modern systems use 64-bit epoch timestamps
Why 1970? It was approximately the time Unix was being developed and represents a convenient "zero point" before most computer systems existed, avoiding negative timestamps for early computing history.
Different Epoch Systems
Common Epoch References
TEXT11. Unix Epoch (POSIX) 2 Start: January 1, 1970, 00:00:00 UTC 3 Units: Seconds (or milliseconds) 4 Usage: Unix, Linux, macOS, most programming languages 5 62. Windows FILETIME 7 Start: January 1, 1601, 00:00:00 UTC 8 Units: 100-nanosecond intervals 9 Usage: Windows file systems, NTFS 10 113. Mac HFS/HFS+ Epoch 12 Start: January 1, 1904, 00:00:00 UTC 13 Units: Seconds 14 Usage: Classic Mac OS (pre-OS X) 15 164. GPS Epoch 17 Start: January 6, 1980, 00:00:00 UTC 18 Units: Seconds 19 Usage: GPS systems, navigation 20 215. NTP Timestamp 22 Start: January 1, 1900, 00:00:00 UTC 23 Units: Seconds (64-bit) 24 Usage: Network Time Protocol 25 266. WebKit/Chrome Epoch 27 Start: January 1, 1601, 00:00:00 UTC 28 Units: Microseconds 29 Usage: Web browsers (Date object)
Important: Always verify which epoch system your system or API uses before converting timestamps.
JavaScript Epoch
JavaScript uses Unix epoch in milliseconds:
JAVASCRIPT1// JavaScript Date uses Unix epoch in milliseconds 2const now = Date.now(); 3console.log(now); // e.g., 1704672000000 (milliseconds) 4 5// Convert to seconds 6const nowSec = Math.floor(Date.now() / 1000); 7console.log(nowSec); // e.g., 1704672000 (seconds) 8 9// Create from Unix epoch seconds 10const date = new Date(1704672000 * 1000); 11console.log(date.toISOString()); // "2024-01-08T00:00:00.000Z"
Python Epoch
Python's time.time() uses Unix epoch in seconds:
PYTHON1import time 2from datetime import datetime 3 4# Get current Unix timestamp (seconds) 5now_ts = time.time() 6print(now_ts) # e.g., 1704672000.123 7 8# Create datetime from Unix timestamp 9dt = datetime.fromtimestamp(1704672000) 10print(dt) # 2024-01-08 00:00:00 11 12# Get UTC timestamp 13now_utc = datetime.now(timezone.utc).timestamp() 14print(now_utc) # e.g., 1704672000
Java Epoch
Java uses Unix epoch in milliseconds (consistent with JavaScript):
JAVA1import java.time.Instant; 2 3// Get current timestamp (milliseconds) 4long nowMs = System.currentTimeMillis(); 5System.out.println(nowMs); // e.g., 1704672000000 6 7// Get from Instant (Java 8+) 8Instant now = Instant.now(); 9long ts = now.getEpochSecond(); 10System.out.println(ts); // e.g., 1704672000 11 12// Create Instant from Unix timestamp 13Instant instant = Instant.ofEpochSecond(1704672000); 14System.out.println(instant); // 2024-01-08T00:00:00Z
Timestamp Limits and Ranges
32-bit Signed Integer Limits
Traditional Unix timestamps use 32-bit signed integers:
TEXT132-bit Signed Integer Range: 2 Min Value: -2147483648 (seconds) 3 Max Value: 2147483647 (seconds) 4 Min Date: December 13, 1901, 20:45:52 UTC 5 Max Date: January 19, 2038, 03:14:07 UTC 6 Current Date: January 8, 2024 7 Time to Overflow: ~14 years
Critical: January 19, 2038, 03:14:07 UTC is known as the "Year 2038 problem" - 32-bit Unix timestamps will overflow and wrap to negative values, breaking many systems.
64-bit Timestamps
Modern systems use 64-bit timestamps to solve overflow issues:
TEXT164-bit Signed Integer Range: 2 Min Value: -9223372036854775808 (seconds) 3 Max Value: 9223372036854775807 (seconds) 4 Min Date: ~293 billion years BCE 5 Max Date: ~293 billion years CE 6 Practical Limit: Effectively infinite for all practical purposes 7 Current Date: January 8, 2024
Best Practice: Always use 64-bit timestamps in new systems to avoid year 2038 problem and other overflow issues.
Millisecond vs Second Precision
Different systems use different precision:
TEXT1Second Precision (Traditional Unix): 2 Units: Seconds since epoch 3 Range (32-bit): 136 years 4 Use: Unix/Linux systems, APIs, databases 5 Example: 1704672000 6 7Millisecond Precision (Modern Systems): 8 Units: Milliseconds since epoch 9 Range (32-bit): ~49 days (not practical) 10 Range (64-bit): ~293 million years 11 Use: JavaScript, Java, most modern APIs 12 Example: 1704672000000
Conversion Formula:
TEXT1Seconds to Milliseconds: seconds * 1000 2Milliseconds to Seconds: milliseconds / 1000
The Year 2038 Problem
What is the Year 2038 Problem?
The Year 2038 problem is a time computing bug where 32-bit signed Unix timestamps will overflow on January 19, 2038, at 03:14:07 UTC.
TEXT1Overflow Timestamp: 2147483647 2Date: January 19, 2038, 03:14:07 UTC 3 4After Overflow (Signed 32-bit wraps to negative): 5Next Second: -2147483648 6Date: December 13, 1901, 20:45:52 UTC
Critical Impact: Systems using 32-bit timestamps will fail or produce incorrect dates after 2038-01-19.
Affected Systems
Systems potentially affected by year 2038 problem:
- Legacy Unix systems still using 32-bit time_t
- Embedded systems with limited memory
- Older database systems
- File systems using 32-bit timestamps
- Network protocols with 32-bit time fields
- Some APIs still using 32-bit integers
Solutions
TEXT1Solution 1: Upgrade to 64-bit 2 - Use time64_t or equivalent 3 - Increases range to ~293 billion years 4 - Required for long-term systems 5 6Solution 2: Offset Timestamps 7 - Store timestamps with epoch offset 8 - Example: Store "years since 2000" instead of seconds since 1970 9 - Requires conversion on read/write 10 11Solution 3: Use Alternative Formats 12 - Use ISO 8601 strings (no overflow) 13 - Use datetime types with larger range 14 - Store year, month, day separately
Prevention: When designing new systems, always use 64-bit timestamps or datetime objects with larger ranges.
Converting Between Epochs
Unix to Windows FILETIME
JAVASCRIPT1function unixToWindowsFiletime(unixTimestamp) { 2 // Unix to FILETIME: 100-nanosecond intervals since 1601-01-01 3 const WINDOWS_EPOCH = 0x019DB1DED53E8000; // FILETIME of Unix epoch 4 const NANOSECONDS_PER_MILLISECOND = 10000; 5 6 const filetime = (unixTimestamp * 1000 + 11644473600000) * NANOSECONDS_PER_MILLISECOND; 7 8 // For large numbers, use BigInt 9 return BigInt(unixTimestamp) * 10000n + 116444736000000000n; 10} 11 12// Example 13const filetime = unixToWindowsFiletime(1704672000); 14console.log(filetime); // 1336477752000000000
Windows FILETIME to Unix
JAVASCRIPT1function windowsFiletimeToUnix(filetime) { 2 const NANOSECONDS_PER_MILLISECOND = 10000; 3 4 // FILETIME to Unix: subtract Windows epoch 5 const unixMs = (filetime - 116444736000000000) / 10000n; 6 7 return Number(unixMs / 1000n); 8} 9 10// Example 11const filetime = 133647752000000000n; 12const unixTs = windowsFiletimeToUnix(filetime); 13console.log(unixTs); // 1704672000
Unix to GPS Time
PYTHON1import datetime 2 3def unix_to_gps(unix_timestamp): 4 """Convert Unix timestamp to GPS timestamp""" 5 # GPS epoch: January 6, 1980 6 # Unix epoch: January 1, 1970 7 # Difference: 315964800 seconds 8 GPS_EPOCH_OFFSET = 315964800 9 10 return unix_timestamp - GPS_EPOCH_OFFSET 11 12def gps_to_unix(gps_timestamp): 13 """Convert GPS timestamp to Unix timestamp""" 14 GPS_EPOCH_OFFSET = 315964800 15 return gps_timestamp + GPS_EPOCH_OFFSET 16 17# Example 18unix_ts = 1704672000 19gps_ts = unix_to_gps(unix_ts) 20print(f"GPS Timestamp: {gps_ts}") # 5355687200
Unix to Mac HFS Time
PYTHON1import datetime 2 3def unix_to_mac_hfs(unix_timestamp): 4 """Convert Unix timestamp to Mac HFS timestamp""" 5 # Mac HFS epoch: January 1, 1904 6 # Unix epoch: January 1, 1970 7 # Difference: -2082844800 seconds (Mac is earlier) 8 MAC_EPOCH_OFFSET = -2082844800 9 10 return unix_timestamp - MAC_EPOCH_OFFSET 11 12def mac_hfs_to_unix(mac_timestamp): 13 """Convert Mac HFS timestamp to Unix timestamp""" 14 MAC_EPOCH_OFFSET = -2082844800 15 return mac_timestamp + MAC_EPOCH_OFFSET 16 17# Example 18unix_ts = 1704672000 19mac_ts = unix_to_mac_hfs(unix_ts) 20print(f"Mac HFS Timestamp: {mac_ts}") # 3787516800
Practical Applications
Calculating Duration
PYTHON1import time 2 3def calculate_duration(start_ts, end_ts): 4 """Calculate duration between two Unix timestamps""" 5 duration_seconds = end_ts - start_ts 6 7 hours = duration_seconds // 3600 8 minutes = (duration_seconds % 3600) // 60 9 seconds = duration_seconds % 60 10 11 return f"{hours}h {minutes}m {seconds}s" 12 13# Example 14start = 1704587200 # 2024-01-07 00:00:00 15end = 1704673600 # 2024-01-08 00:00:00 16print(calculate_duration(start, end)) # "24h 0m 0s"
Future/Past Time Calculation
JAVASCRIPT1function timeUntil(targetTimestamp) { 2 const now = Date.now() / 1000; 3 const diff = targetTimestamp - now; 4 5 if (diff <= 0) { 6 return { type: 'past', text: 'Already passed' }; 7 } 8 9 const days = Math.floor(diff / 86400); 10 const hours = Math.floor((diff % 86400) / 3600); 11 const minutes = Math.floor((diff % 3600) / 60); 12 13 return { 14 type: 'future', 15 text: `${days} days, ${hours} hours, ${minutes} minutes` 16 }; 17} 18 19// Calculate time until end of 2037 20const target = 2155999999; // December 31, 2037 21console.log(timeUntil(target)); 22// "511 days, 2 hours, 33 minutes"
Date Range Validation
PYTHON1import time 2 3def is_valid_unix_timestamp(ts, allow_future=True): 4 """Validate Unix timestamp is within reasonable range""" 5 # Minimum: January 1, 1970 6 MIN_TIMESTAMP = 0 7 8 # Maximum: January 19, 2038 (32-bit limit) 9 MAX_TIMESTAMP = 2147483647 10 11 if ts < MIN_TIMESTAMP: 12 return False, "Timestamp is before Unix epoch" 13 14 if not allow_future and ts > time.time(): 15 return False, "Timestamp is in the future" 16 17 if ts > MAX_TIMESTAMP: 18 return False, "Timestamp exceeds 32-bit limit" 19 20 return True, "Valid" 21 22# Example 23print(is_valid_unix_timestamp(1704672000)) # (True, "Valid") 24print(is_valid_unix_timestamp(-1000)) # (False, "Timestamp is before Unix epoch") 25print(is_valid_unix_timestamp(3000000000)) # (False, "Timestamp exceeds 32-bit limit")
Batch Conversion
JAVASCRIPT1function convertUnixTimestamps(timestamps) { 2 return timestamps.map(ts => { 3 const date = new Date(ts * 1000); 4 return { 5 timestamp: ts, 6 isoString: date.toISOString(), 7 utcString: date.toUTCString(), 8 localString: date.toLocaleString() 9 }; 10 }); 11} 12 13// Example batch conversion 14const timestamps = [1704587200, 1704673600, 1704760000]; 15const results = convertUnixTimestamps(timestamps); 16console.log(results); 17/* 18[ 19 { 20 timestamp: 1704587200, 21 isoString: "2024-01-07T00:00:00.000Z", 22 utcString: "Sun, 07 Jan 2024 00:00:00 GMT", 23 localString: "1/7/2024, 12:00:00 AM" 24 }, 25 ... 26] 27*/
Best Practices
Timestamp Selection Guidelines
TEXT1When choosing timestamp precision: 2 ✅ Use seconds for Unix/Linux compatibility 3 ✅ Use milliseconds for JavaScript/Java/Web compatibility 4 ✅ Use 64-bit integers for future-proof systems 5 ✅ Use ISO 8601 strings for cross-system compatibility 6 ✅ Document epoch system in API specifications 7 ❌ Don't mix precision levels in same API 8 ❌ Don't use 32-bit timestamps for long-term systems 9 ❌ Don't assume all systems use Unix epoch
Storage Recommendations
TEXT1Database storage: 2 ✅ Store as TIMESTAMP or DATETIME (64-bit) 3 ✅ Store timezone information separately 4 ✅ Use UTC for all storage 5 ✅ Validate timestamp ranges on input 6 ❌ Don't store as VARCHAR or STRING 7 ❌ Don't store local times without timezone metadata 8 ❌ Don't use 32-bit integers for new systems
API Design
TEXT1REST API design: 2 ✅ Use ISO 8601 in JSON responses (e.g., "2024-01-08T00:00:00Z") 3 ✅ Include timezone information (Z or offset) 4 ✅ Document timestamp precision (seconds or milliseconds) 5 ✅ Support both input formats for flexibility 6 ✅ Return descriptive errors for invalid timestamps 7 ❌ Don't use locale-specific formats in APIs 8 ❌ Don't assume client timezone 9 ❌ Don't silently correct invalid timestamps
Testing Strategies
PYTHON1import datetime 2 3def test_epoch_conversions(): 4 """Test epoch conversion edge cases""" 5 test_cases = [ 6 ("Unix epoch start", 0, datetime.datetime(1970, 1, 1)), 7 ("Current time", 1704672000, datetime.datetime(2024, 1, 8)), 8 ("Year 2038 limit", 2147483647, datetime.datetime(2038, 1, 19, 3, 14, 7)), 9 ("Negative timestamp", -86400, datetime.datetime(1969, 12, 31, 0, 0, 0)), 10 ] 11 12 for name, timestamp, expected in test_cases: 13 result = datetime.datetime.fromtimestamp(timestamp, tz=datetime.timezone.utc) 14 assert result == expected, f"Failed: {name}" 15 16 print("All epoch conversion tests passed!") 17 18test_epoch_conversions()
Related Tools
- Unix Timestamp Converter - Convert Unix timestamps to dates
- Timestamp Format Converter - Convert between multiple epoch formats
- Current Timestamp - Get current timestamp in multiple formats
- Timestamp Validator - Validate timestamp ranges and formats
- Excel Timestamp Converter - Convert Excel serial dates to Unix timestamps
Additional Resources
For most applications, Unix epoch (January 1, 1970) is the de facto standard. However, always verify the epoch system when integrating with external systems, APIs, or legacy data, as different systems may use different reference dates.
Quick Reference
TEXT1Common Epoch Timestamps: 2 0: January 1, 1970, 00:00:00 UTC (Unix epoch start) 3 946684800: January 1, 2000, 00:00:00 UTC 4 1577836800: January 1, 2020, 00:00:00 UTC 5 1704067200: January 1, 2024, 00:00:00 UTC 6 2147483647: January 19, 2038, 03:14:07 UTC (32-bit max) 7 253402300799: December 31, 9999, 23:59:59 UTC (Common limit)