1. Home
  2. Training Library
  3. Programming
  4. Programming Courses
  5. Smart Contract Development with Solidity

The Concept of Time

Start course

This course provides a deep dive into the Solidity programming language which is used to implement smart contracts on blockchain platforms. This course will give you a practical understanding of Solidity and how to develop your own smart contracts.


In the previous lecture, we were talking about the view and pure functions. And in this lecture, we are talking about the concept of time, which is another very powerful thing in solidity, but is very often wrongly used, and I want to discuss this a little bit in this lecture. Not only give you the instructions how to use time and time constraints, but also talk about how to use it or how not to use it. And for this purpose for the first time, I am going to open the solidity documentation, and I'm here in the section Solidity in Depth, Units and Globally Available Variables, Special Variables and Functions. And here I'm at the Block and Transaction Properties. And we have used this now keyword already before. It gives you the current block timestamp. But unfortunately, very often it's used in a wrong way. And let me tell you why, I just have to scroll down a little bit, and I want to read that out loud to you. So, you just heard it once at least, "Do not rely on block.timestamp, now and blockhash as a source of randomness unless you know what you're doing. Both the block timestamp and the block hash can be influenced by miners to some degree. Bad actors in the mining community can, for example, run a casino payout function on a chosen hash and just retry a different hash if they did not receive any money. The current block timestamps must be strictly larger than the timestamp of the last block, but the only guarantee is that it will be somewhere between the timestamp of two consecutive blocks in their canonical chain." What does that mean? The block timestamp where now as a timestamp in seconds since 1970 in the UTC time zone, so it's always the same no matter where the miner is, but the block timestamp is set by the miner. Now, if block is mined, the miner can set the timestamp to a certain degree between the last block's timestamp and some seconds to the next block timestamp. When the next block is mined, it has to be a little bit faster than that. So, it usually has a couple of seconds to set the timestamp. Now, if you rely in your solidity code, maybe something that the timestamp is an even number or an odd number, then the miner can influence that. Now, if your smart contract is not very large, then usually it doesn't matter. But if your contract becomes very large with a lot of ethers in it, if the scale swell and you run something like a lottery, where you rely on somebody having the even number or if it's an even number and somebody gets paid out, then this becomes a problem. So, don't use the block timestamp for any means of randomness in your smart contracts. Randomness is essentially a very, very hard topic on the deterministic system, like a blockchain. There are solutions. One of them is for example a so-called randao smart contract; I don't have a URL for this now. But if you need randomness, we don't need it for our project, but if you need randomness have a look into randao, R-A-N-D-A-O. This is a smart contract where several people are contributing to a random number and that ultimately leads to a random number. So, it's a little bit harder than just using a variable, but our topic for this lecture is the concept of time. Now, we already know now or block timestamp, and we can use this in several different ways. And one of the ways this is used very often is to define a certain range where the specific functionality is available. For example, if you have an auction smart contract then you maybe have several hours or days where a bidding period is open. Now, let's just model this in a very simplistic way in our time or C10 times smart contract here, which you can also find in the course repository. We would have an unsigned integer that is biddingStartTimestamp, and let's say for simplicity's sake that this is now as soon as the smart contract is deployed, the bidding period starts and we have another unsigned integer, biddingDays, and you can bid for 10 days. And there's several ways how to model this and the concept of days, hours and seconds, and so on. There are actually ways of doing this quite elegantly in solidity. I go back to the solidity documentation, you can find it in solidity.readthedocs.io, and then I'm here in Solidity in Depth, Units and Globally Available Variables, Special Variables and Functions, and you can find this Time Units, not Special Variables Functions, Time Units. And then you come to this headline and this paragraph where it says 1 == 1 second, 1 minute == 60 seconds, 1 hour == 60 minutes, 1 day == 24 hours, 1 week == 7 days. Now, if you want to have 10 days then you can for example, write just biddingDays == 10 days and this will multiply the number 10 by 60, 60, and that's it. Twenty-four, 24 * 60 * 60, and this will give you the amount in seconds. And then you can say if now is smaller than now plus  the biddingDays, then you are inside the open bidding period. Before we do that, let me just simply return the keyword now in order to give you an idea how this works. So, I'm going to write the simple function, getCurrentTimestamp. This is a public view function because now is a state variable, interesting. Returns an unsigned integer and this return just now. And if I'm going to deploy this and I get the timestamp, then you can see that every second we have a new timestamp. So, this is really the seconds not the milliseconds and not anything else. And the coding challenge for now, it's going to be a little bit of a hard coding challenge, but I really want you to try that. We are slowly reaching a point where you know a lot in solidity, and I think you can solve it, and I will solve it for you after a small pause. The coding challenge for this here is I'm going to write the function, isBiddingStillOpen, and this is a public view function, and returns a Boolean. And based on these two variables, you would have to determine if the bidding period is still open. If you want to do this coding challenge, then just pause the video now and I will show you in a second how this is done. All right. So, what we want is we want to see if the current timestamp is larger than the biddingStartTimestamp, but smaller than the bidding period start timestamp plus the biddingDays. So, what we want to is return getCurrentTmestamp)  is larger than biddingStartTimestamp and current timestamp is smaller or equals than the biddingStartTimestamp plus the biddingDays. And that should be just fine because obviously, we have 10 days. So, this is true for 10 days since the deployment of the smart contract, but let's just change this to seconds just to give this a try. We deploy the smart contract and bidding is open for 10 seconds and then it should be false. Now, it's false. So, the 10 seconds passed and the bidding period is not open anymore. All right. You can find this also in the course repository. If you have any questions or suggestions or run into any problems, feel free to reach out to us in the course Q&A. And I'll see you in the next lecture where we are talking about exceptions, require, assert, and revert, which is a very powerful concept in solidity. Talking about this a little bit and then we are actually done with this section, where you have all the basics of smart contract development hopefully now in your brain. That's very cool. See you in the next section and the next lecture.


About the Author

Tom is a CTO, senior back-end developer, and systems architect with over twenty years of hands-on development experience in a variety of languages and systems. He has a CS master's degree and has been working with Ethereum and blockchain technologies since 2016.

Covered Topics