From 9edc2cb215b6fe85aa9b0a58b4cc8c8e14ceb389 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E7=AB=8B=E5=85=9A=20Lidang?= Date: Mon, 5 Jun 2023 00:31:11 -0500 Subject: [PATCH] update --- .../MachineState/TimeStateFunction.sol | 56 +++++++++++++++++++ .../CertainTokenNumberExpressionFunction.sol | 0 ...ertainTokenPermyriadExpressionFunction.sol | 0 .../TotalTokenNumberExpressionFunction.sol | 0 .../TotalTokenPermyriadExpressionFunction.sol | 0 5 files changed, 56 insertions(+) create mode 100644 darc-protocol/contracts/protocol/Plugin/Conditions/MachineState/TimeStateFunction.sol rename darc-protocol/contracts/protocol/Plugin/Conditions/{TokenStateExpressionFunction => TokenState}/CertainTokenNumberExpressionFunction.sol (100%) rename darc-protocol/contracts/protocol/Plugin/Conditions/{TokenStateExpressionFunction => TokenState}/CertainTokenPermyriadExpressionFunction.sol (100%) rename darc-protocol/contracts/protocol/Plugin/Conditions/{TokenStateExpressionFunction => TokenState}/TotalTokenNumberExpressionFunction.sol (100%) rename darc-protocol/contracts/protocol/Plugin/Conditions/{TokenStateExpressionFunction => TokenState}/TotalTokenPermyriadExpressionFunction.sol (100%) diff --git a/darc-protocol/contracts/protocol/Plugin/Conditions/MachineState/TimeStateFunction.sol b/darc-protocol/contracts/protocol/Plugin/Conditions/MachineState/TimeStateFunction.sol new file mode 100644 index 0000000..70a1d29 --- /dev/null +++ b/darc-protocol/contracts/protocol/Plugin/Conditions/MachineState/TimeStateFunction.sol @@ -0,0 +1,56 @@ +// SPDX-License-Identifier: BUSL-1.1 +pragma solidity ^0.8.9; +/** + * @title ConditionExpressionFunctions + * @author DARC Team + * @notice Functions of criteria expression related to Machine State + */ + + +import "../../../MachineState.sol"; +import "../../../MachineStateManager.sol"; +import "../../../Utilities/StringUtils.sol"; +import "../../../Utilities/OpcodeMap.sol"; + +contract TimeStateFunction is MachineStateManager { + function getDateTime(uint256 timestamp) public pure returns (uint256 year, uint256 month, uint256 day, uint256 hour, uint256 minute, uint256 second) { + // Convert timestamp to readable format + uint256 unixTimestamp = timestamp; + uint256 _year = (unixTimestamp / 31556926) + 1970; + uint256 _leapYears = (_year - 1969) / 4 - (_year - 1901) / 100 + (_year - 1601) / 400; + uint256 _secondsAccountedFor = (_year - 1970) * 31556926 + _leapYears * 86400; + uint256 _remainingSeconds = unixTimestamp - _secondsAccountedFor; + bool _isLeapYear = (_year % 4 == 0 && (_year % 100 != 0 || _year % 400 == 0)); + + uint256[] memory _daysPerMonth = new uint256[](12); + + _daysPerMonth[0] = 31; + _daysPerMonth[1] = _isLeapYear ? 29 : 28; + _daysPerMonth[2] = 31; + _daysPerMonth[3] = 30; + _daysPerMonth[4] = 31; + _daysPerMonth[5] = 30; + _daysPerMonth[6] = 31; + _daysPerMonth[7] = 31; + _daysPerMonth[8] = 31; + _daysPerMonth[9] = 30; + _daysPerMonth[10] = 31; + _daysPerMonth[11] = 30; + _daysPerMonth[12] = 31; + + uint256 _month; + for (_month = 0; _month < 12; _month++) { + if (_remainingSeconds < _daysPerMonth[_month] * 86400) + break; + _remainingSeconds -= _daysPerMonth[_month] * 86400; + } + uint256 _day = _remainingSeconds / 86400 + 1; + + // Determine the hour, minute, and second + uint256 _hour = (_remainingSeconds % 86400) / 3600; + uint256 _minute = (_remainingSeconds % 3600) / 60; + uint256 _second = _remainingSeconds % 60; + + return (_year, _month + 1, _day, _hour, _minute, _second); + } +} \ No newline at end of file diff --git a/darc-protocol/contracts/protocol/Plugin/Conditions/TokenStateExpressionFunction/CertainTokenNumberExpressionFunction.sol b/darc-protocol/contracts/protocol/Plugin/Conditions/TokenState/CertainTokenNumberExpressionFunction.sol similarity index 100% rename from darc-protocol/contracts/protocol/Plugin/Conditions/TokenStateExpressionFunction/CertainTokenNumberExpressionFunction.sol rename to darc-protocol/contracts/protocol/Plugin/Conditions/TokenState/CertainTokenNumberExpressionFunction.sol diff --git a/darc-protocol/contracts/protocol/Plugin/Conditions/TokenStateExpressionFunction/CertainTokenPermyriadExpressionFunction.sol b/darc-protocol/contracts/protocol/Plugin/Conditions/TokenState/CertainTokenPermyriadExpressionFunction.sol similarity index 100% rename from darc-protocol/contracts/protocol/Plugin/Conditions/TokenStateExpressionFunction/CertainTokenPermyriadExpressionFunction.sol rename to darc-protocol/contracts/protocol/Plugin/Conditions/TokenState/CertainTokenPermyriadExpressionFunction.sol diff --git a/darc-protocol/contracts/protocol/Plugin/Conditions/TokenStateExpressionFunction/TotalTokenNumberExpressionFunction.sol b/darc-protocol/contracts/protocol/Plugin/Conditions/TokenState/TotalTokenNumberExpressionFunction.sol similarity index 100% rename from darc-protocol/contracts/protocol/Plugin/Conditions/TokenStateExpressionFunction/TotalTokenNumberExpressionFunction.sol rename to darc-protocol/contracts/protocol/Plugin/Conditions/TokenState/TotalTokenNumberExpressionFunction.sol diff --git a/darc-protocol/contracts/protocol/Plugin/Conditions/TokenStateExpressionFunction/TotalTokenPermyriadExpressionFunction.sol b/darc-protocol/contracts/protocol/Plugin/Conditions/TokenState/TotalTokenPermyriadExpressionFunction.sol similarity index 100% rename from darc-protocol/contracts/protocol/Plugin/Conditions/TokenStateExpressionFunction/TotalTokenPermyriadExpressionFunction.sol rename to darc-protocol/contracts/protocol/Plugin/Conditions/TokenState/TotalTokenPermyriadExpressionFunction.sol