File 1 of 1: dailySign.sol
pragma solidity ^0.7.0;
contract DailySign {
uint256 constant SECONDS_PER_DAY = 24 * 60 * 60;
int256 constant OFFSET19700101 = 2440588;
uint256 TIMEZONE_OFFSET = 28800;
mapping(address => mapping(uint256 => bool)) public signRecords;
function timestampToDate(uint256 timestamp)
internal
pure
returns (
uint256 year,
uint256 month,
uint256 day
)
{
(year, month, day) = _daysToDate(timestamp / SECONDS_PER_DAY);
}
function _daysToDate(uint256 _days)
internal
pure
returns (
uint256 year,
uint256 month,
uint256 day
)
{
int256 __days = int256(_days);
int256 L = __days + 68569 + OFFSET19700101;
int256 N = (4 * L) / 146097;
L = L - (146097 * N + 3) / 4;
int256 _year = (4000 * (L + 1)) / 1461001;
L = L - (1461 * _year) / 4 + 31;
int256 _month = (80 * L) / 2447;
int256 _day = L - (2447 * _month) / 80;
L = _month / 11;
_month = _month + 2 - 12 * L;
_year = 100 * (N - 49) + _year + L;
year = uint256(_year);
month = uint256(_month);
day = uint256(_day);
}
function signIn() external {
(uint256 year, uint256 month, uint256 day) = timestampToDate(
block.timestamp + TIMEZONE_OFFSET
);
uint256 nowDate = year * 10000 + month * 100 + day;
require(signRecords[msg.sender][nowDate] == false, "already signed");
signRecords[msg.sender][nowDate] = true;
emit SignIn(msg.sender, nowDate);
}
function getDate(uint256 timestamp) public view returns (uint256 date) {
if (timestamp == 0) {
timestamp = block.timestamp;
}
timestamp += TIMEZONE_OFFSET;
(uint256 year, uint256 month, uint256 day) = timestampToDate(timestamp);
uint256 nowDate = year * 10000 + month * 100 + day;
return nowDate;
}
event SignIn(address user, uint256 date);
}
{
"compilationTarget": {
"contracts/dailySign.sol": "DailySign"
},
"evmVersion": "istanbul",
"libraries": {},
"metadata": {
"bytecodeHash": "ipfs"
},
"optimizer": {
"enabled": true,
"runs": 200
},
"remappings": []
}