This page is a placeholder. All examples on this page are currently AI-generated and are not correct. This documentation will be completed in the future with accurate, tested examples.
Overview
Opcode: 0x42
Introduced: Frontier (EVM genesis)
TIMESTAMP retrieves the Unix timestamp of the current block in seconds since epoch (January 1, 1970 00:00:00 UTC). Block producers set this value when creating blocks, subject to consensus rules.
Specification
Stack Input:
Stack Output:
Gas Cost: 2 (GasQuickStep)
Operation:
Behavior
TIMESTAMP pushes the block timestamp as a 256-bit unsigned integer representing seconds since Unix epoch:
Consensus Rules
Pre-Merge (PoW):
- Miners could manipulate ±15 seconds
- Must be greater than parent timestamp
- Limited by network propagation
Post-Merge (PoS):
- Strictly enforced to
12 * slot_number + genesis_time
- More predictable and regular
- 12-second slot times on Ethereum mainnet
Examples
Basic Usage
Time-Based Calculations
Timestamp Comparison
Gas Cost
Cost: 2 gas (GasQuickStep)
TIMESTAMP is one of the cheapest operations in the EVM.
Comparison:
TIMESTAMP: 2 gas
NUMBER, COINBASE, DIFFICULTY, GASLIMIT: 2 gas
BLOCKHASH: 20 gas
SLOAD (cold): 2100 gas
Common Usage
Time Locks
Vesting Schedules
Auction Deadlines
Cooldown Mechanisms
Time-Window Operations
Security Considerations
Timestamp Manipulation
Block producers can manipulate timestamps within bounds:
Not Suitable for Randomness
Timestamps are predictable and should not be used for randomness:
Short Time Windows
Avoid time windows shorter than block time:
Overflow Considerations
Timestamps will overflow u256 in ~10^60 years (not a practical concern):
Backwards Time Travel
While rare, timestamp must be > parent timestamp:
Pre vs Post-Merge Differences
Implementation
Edge Cases
Zero Timestamp
Far Future Timestamp
Post-Merge Slot Calculation
Stack Overflow
Out of Gas
Practical Patterns
Safe Time Ranges
Relative Time Checks
Timestamp to Date Conversion
Benchmarks
Performance:
- Stack push: O(1)
- No computation required
Gas efficiency:
- 2 gas per query
- ~500,000 queries per million gas
References