1. Block Time, Not Real Time
- In Solidity,
block.timestamp
gives the timestamp of the block that included your transaction, ** not the exact submission time** - All transactions in the same block share the same
block.timestamp
- This means "first come, first served" logic cannot rely on precise timestamps.
- The old keyword
now
was just an alias forblock.timestamp
, but it's deprecated now.
Get Seconds Since 1970
-
block.timestamp
returns a Unix timestamp, i.e., the number of seconds since January 1, 1970(Uinx epoch) - It's a standard across computer science, not unique to Solidity.
Solidity Makes Time Arithmetic Simple
- Solidity provides time units:
seconds
,minutes
,hours
,days
,weeks
. - These can be used directly in expressions:
block.timestamp + 1 days
block.timestamp - 7 days
- This improves readablity and prevents errors from using "magic numbers".
Building Contracts With Lifecycles
- You can implement expiry logic by setting a deadline in the constructor:
uint public expiry;
constructor() {
expiry = block.timestamp + 1 minutes;
}
function addOne() public {
require(block.timestamp < expiry, "Contract has expired");
count++;
}
This pattern enables time-based features like token vesting, limited auctions, or timed voting.
Conclusion
- Correct time handling = safer and more predictable smart contracts.
- Four pillars:
- Block time consensus model.
- Unix timestamp standard.
- Readable time units in Solidity
- Lifecycle enforcement via expiry checks.
Top comments (0)