Purpose of Time Units

The entire purpose of time units is to write time duration in human-readable form since a large duration of time represented in seconds would be illegible. Time units make the code look more readable.

Let’s explain the concept and usage of time units via a Solidity Program. The contract named Timeunits_Example will get us the number of seconds and minutes that elapsed after the calling of the setStartTime() function. 

Solidity




// SPDX-License-Identifier: GPL-3.0
pragma solidity >= 0.5.0 < 0.9.0;
  
contract Timeunits_Example {
      
  // Declaring a  state variable that will 
  // store the current block timestamp
  // as seconds since unix epoch(January 1, 1970)
  uint256 public startTime;
  
  // setting the startTime variable
  function setStartTime() public {
    startTime = block.timestamp;
  }
    
  // calculates numbers of seconds that 
  // have been elapsed since the startTime 
  // variable was set
  function elapsedSeconds() public view returns (uint256) {
    return (block.timestamp - startTime);
  }
    
  // calculates numbers of minutes that have been 
  // elapsed since the startTime variable was set
  function elapsedMinutes() public view returns (uint256) {
     
    // Both block.timestamp and startTime are in seconds 
    // Dividing them by 1 minutes == 60 seconds to find 
    // equivalent number of minutes
    return (block.timestamp - startTime) / 1 minutes;
  }
}


Output :

 

Similarly, we can add more functions to the above contract to find elapsedDays, and elapsedWeeks since the startTime was set. We can edit the above contract and use a single function to get the time elapsed.

Solidity




// SPDX-License-Identifier: GPL-3.0
pragma solidity >= 0.5.0 < 0.9.0;
  
contract Timeunits_Example {
    
  // Declaring a  state variable that will 
  // store the current block timestamp as 
  // seconds since unix epoch(January 1, 1970)
  uint256 public startTime;
    
  // setting the startTime variable
  function setStartTime() public {
    startTime = block.timestamp;
  }
  
  // calculates the time in dd:hh:mm:ss format. 
  // For example, 3702 seconds will be 0 dd, 1 hh, 
  // 1 mm, 42 ss that is 0 days, 1 hours, 1 minutes 
  // and 42 seconds
  function elapsedTime() public view returns (uint256 dd, 
                                              uint256 hh, 
                                              uint256 mm, 
                                              uint256 ss) {
    dd = (block.timestamp - startTime)/1 days;
    hh = (block.timestamp - startTime)/1 hours - (dd * 24);
    mm = (block.timestamp - startTime)/1 minutes - (hh * 60) - 
         (dd * 24 * 60);
    ss = (block.timestamp - startTime)/1 seconds - (mm * 60) - (hh * 60 * 60) - 
         (dd * 24 * 60 * 60);
  }
}


Output :

The time elapsed is 3 minutes 12 seconds, i.e., 192 seconds



Time Units in Solidity

Solidity is a programming language to write smart contracts on the Ethereum blockchain. Precise timings have an important role to play in smart contracts since on many occasions it has to ensure the execution of the code based on conditions that include time constraints. 
Solidity has provided developers with many time units which are used as suffixes for better code readability and can be used to denote various time duration.

Similar Reads

Time Units in Solidity

There are 5 valid time units in Solidity. These are seconds, minutes, hours, days, and weeks....

Syntax Rules and Properties of Time Units

1. These time units are used as suffixes after integer numbers, i.e., you have to append the time unit to the integer value....

Purpose of Time Units

The entire purpose of time units is to write time duration in human-readable form since a large duration of time represented in seconds would be illegible. Time units make the code look more readable....