solidity--time

发布时间 2023-11-19 10:29:51作者: zhihua
 1 // SPDX-License-Identifier: GPL-3.0-or-later
 2 
 3 pragma solidity >=0.8.0;
 4 
 5 contract datetime{
 6 
 7      constructor(){
 8         
 9      }
10 
11     
12     function getBlockTime() public view returns(uint256){
13 
14         return block.timestamp;
15     }
16 
17        //获取当天0点的时间戳
18     function getCurrentDayZeroTimestamp(uint256 timestamp) public   pure returns (uint256) {
19         uint256 time = (timestamp / (60 * 60 * 24)) *
20             60 *
21             60 *
22             24 - 8 hours
23            ;
24 
25         if ((timestamp - time) >= 1 days) {
26             return time + 1 days;
27         }
28         return time;
29     }
30 
31      //获取当天0点的时间戳
32     function getCurrentDayTimestamp() public view returns (uint256) {
33         uint256 time = (block.timestamp / (60 * 60 * 24)) *
34             60 *
35             60 *
36             24 - 8 hours
37             ;
38 
39         if ((block.timestamp - time) >= 1 days) {
40             return time + 1 days;
41         }
42         return time;
43     }
44 
45     //获取明天0点的时间戳
46     function getNextDayTimestamp() public view returns (uint256) {
47         return getCurrentDayTimestamp() + 1 days;
48     }
49      
50 }