Solidity

如何使用我的狀態變數的更新值?

  • November 10, 2022
uint constant SECONDS_PER_DAY = 24 * 60 * 60;
uint constant SECONDS_PER_HOUR = 60 * 60;
uint constant SECONDS_PER_MINUTE = 60;
uint public startingDate;
uint public endingDate;
int constant OFFSET19700101 = 2440588;
uint public secondsBetween;

function _daysFromDate(uint year, uint month, uint day) internal pure returns (uint _days) {
       require(year >= 1970);
       int _year = int(year);
       int _month = int(month);
       int _day = int(day);

       int __days = _day
         - 32075
         + 1461 * (_year + 4800 + (_month - 14) / 12) / 4
         + 367 * (_month - 2 - (_month - 14) / 12 * 12) / 12
         - 3 * ((_year + 4900 + (_month - 14) / 12) / 100) / 4
         - OFFSET19700101;

       _days = uint(__days);
   }

function startDate(uint _year, uint _month, uint _day, uint _hour, uint _minute, uint _second) public {
       startingDate = _daysFromDate(_year, _month, _day) * SECONDS_PER_DAY + _hour * SECONDS_PER_HOUR + _minute * SECONDS_PER_MINUTE + _second;
   } 
// startDate function takes arguments ( year , month , day , hour , minute , second ) and converts a standard date in UTC format to unix format.

function endDate(uint _year, uint _month, uint _day, uint _hour, uint _minute, uint _second) public {
       endingDate = _daysFromDate(_year, _month, _day) * SECONDS_PER_DAY + _hour * SECONDS_PER_HOUR + _minute * SECONDS_PER_MINUTE + _second;
   }  
// endDate function takes arguments ( year , month , day , hour , minute , second ) and converts a standard date in UTC format to unix format.

function dateDifference() public returns(uint) {
  secondsBetween = endingDate - startingDate;
}
//secondsBetween should return difference between endingDate and startingDate in seconds .

我仔細閱讀了您的程式碼。目標是什麼?問題是什麼?不知道如何幫助你。

我不知道您的問題是什麼(順便說一下,我編輯了您的問題以提高程式碼的可讀性)但我在這裡看到的最明顯的問題是 dateDifference 將始終返回 0 而不是返回 secondsBetween (實際上,此程式碼甚至無法編譯,我相信)。你可以像這樣修復它:

function dateDifference() public returns(uint) {
  uint secondsBetween = endingDate - startingDate;
  return(secondsBetween);
}

或者 :

function dateDifference() public returns(uint secondsBetween) {
  secondsBetween = endingDate - startingDate;
}

如果那不是您的問題,您可以使用有關您所面臨的確切問題的詳細資訊對其進行編輯嗎?

引用自:https://ethereum.stackexchange.com/questions/139149