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:0x42Introduced: 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:
(none)
Stack Output:
timestamp (Unix seconds as u256)
Gas Cost: 2 (GasQuickStep)Operation:
stack.push(block.timestamp)
Behavior
TIMESTAMP pushes the block timestamp as a 256-bit unsigned integer representing seconds since Unix epoch:
contract TimeWindow { uint256 public windowStart; uint256 public windowEnd; function isInWindow() public view returns (bool) { return block.timestamp >= windowStart && block.timestamp < windowEnd; } modifier onlyDuringWindow() { require(isInWindow(), "Outside time window"); _; } function limitedOperation() external onlyDuringWindow { // Only callable during window }}
Security Considerations
Timestamp Manipulation
Block producers can manipulate timestamps within bounds:
// VULNERABLE: Exact timestamp checksrequire(block.timestamp == expectedTime); // Can be gamed by ±15 seconds// SAFER: Range checks with marginrequire( block.timestamp >= startTime && block.timestamp <= endTime);
Not Suitable for Randomness
Timestamps are predictable and should not be used for randomness:
// DANGEROUS: Predictable randomnessfunction badRandom() public view returns (uint256) { return uint256(keccak256(abi.encodePacked(block.timestamp)));}// BETTER: Use Chainlink VRF or commit-reveal
Short Time Windows
Avoid time windows shorter than block time:
// PROBLEMATIC: 1-second windowrequire(block.timestamp == targetTime); // Only one block can match// BETTER: Reasonable window (minutes/hours)require( block.timestamp >= targetTime && block.timestamp < targetTime + 1 hours);
Overflow Considerations
Timestamps will overflow u256 in ~10^60 years (not a practical concern):
// No overflow risk in practiceuint256 futureTime = block.timestamp + 100 years;
Backwards Time Travel
While rare, timestamp must be > parent timestamp:
// Generally safe, but be aware edge cases existcontract TimeSensitive { uint256 public lastSeen; function update() external { // Could theoretically fail if timestamp < lastSeen // (e.g., chain reorg with earlier timestamp) require(block.timestamp > lastSeen, "Time went backwards"); lastSeen = block.timestamp; }}
Pre vs Post-Merge Differences
contract TimingAware { // Pre-merge: ±15 second manipulation possible // Post-merge: Predictable 12-second slots function checkTiming() external view { // Post-merge: Can predict timestamp from slot // slot = (timestamp - genesis_time) / 12 // Pre-merge: Less predictable }}
Implementation
TypeScript
/** * TIMESTAMP opcode (0x42) - Get block timestamp */export function timestamp(frame: FrameType): EvmError | null { // Consume gas (GasQuickStep = 2) frame.gasRemaining -= 2n; if (frame.gasRemaining < 0n) { frame.gasRemaining = 0n; return { type: "OutOfGas" }; } // Push timestamp to stack if (frame.stack.length >= 1024) return { type: "StackOverflow" }; frame.stack.push(frame.evm.blockContext.block_timestamp); frame.pc += 1; return null;}