Time Handling

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 “fir…


This content originally appeared on DEV Community and was authored by Loading Blocks

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 for block.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.


This content originally appeared on DEV Community and was authored by Loading Blocks


Print Share Comment Cite Upload Translate Updates
APA

Loading Blocks | Sciencx (2025-09-21T13:32:29+00:00) Time Handling. Retrieved from https://www.scien.cx/2025/09/21/time-handling/

MLA
" » Time Handling." Loading Blocks | Sciencx - Sunday September 21, 2025, https://www.scien.cx/2025/09/21/time-handling/
HARVARD
Loading Blocks | Sciencx Sunday September 21, 2025 » Time Handling., viewed ,<https://www.scien.cx/2025/09/21/time-handling/>
VANCOUVER
Loading Blocks | Sciencx - » Time Handling. [Internet]. [Accessed ]. Available from: https://www.scien.cx/2025/09/21/time-handling/
CHICAGO
" » Time Handling." Loading Blocks | Sciencx - Accessed . https://www.scien.cx/2025/09/21/time-handling/
IEEE
" » Time Handling." Loading Blocks | Sciencx [Online]. Available: https://www.scien.cx/2025/09/21/time-handling/. [Accessed: ]
rf:citation
» Time Handling | Loading Blocks | Sciencx | https://www.scien.cx/2025/09/21/time-handling/ |

Please log in to upload a file.




There are no updates yet.
Click the Upload button above to add an update.

You must be logged in to translate posts. Please log in or register.