[Ethereum] How does time in the blockchain work

contract-developmentsoliditytimestamp

I hope this question is not bad one, but this has been something I have been having trouble understanding. How does time in the blockchain work, and how can I utilize variables like block.timestamp and the like? I am creating a faucet where the user can withdraw a set amount of ether, then they have to wait one hour before they can withdraw more.

Edit for duplication: I have looked at other explanations here on StackExchange, but it did not answer my question. I would like to know how time is passed on the blockchain, and with this knowledge utilize block.timestamp. I apologize if I was unclear. I am a beginner with creating Dapps, and want to learn! 🙂

Best Answer

The block.timestamp should indicate approximately when the block was mined. But it is only an approximation, it depends on the precision of the clock of the machines working on it, also a miner can modify it. So it is not a very secure measure of the time but for your use case should be good.

From solidity documentation

block.timestamp (uint): current block timestamp as seconds since unix epoch.

You can do something like this:

mapping(address => uint) startTime;

function start() {
    startTime[msg.sender] = now;
}

function hasElapsed() constant return (bool) {
    if (now >= startTime[msg.sender] + 60 minutes) {
        // 60 minutes has elapsed from startTime[msg.sender]
        return true;
    }
    return false;
}
Related Topic