当前位置:网站首页>Solidity get quarterly time

Solidity get quarterly time

2022-06-25 18:53:00 Wind breaking

pragma solidity ^0.5.1;

// Time tools 
contract DateUtil {

    uint constant internal SECONDS_PER_DAY = 24 * 60 * 60;
    uint constant internal SECONDS_PER_HOUR = 60 * 60;
    uint constant internal SECONDS_PER_MINUTE = 60;
    uint constant internal OFFSET19700101 = 2440588;
    uint16 constant ORIGIN_YEAR = 1970;
    uint constant LEAP_YEAR_IN_SECONDS = 31622400;
    uint constant YEAR_IN_SECONDS = 31536000;
    uint constant DAY_IN_SECONDS = 86400;

    uint constant HOUR_IN_SECONDS = 3600;
    uint constant MINUTE_IN_SECONDS = 60;


    // Days per month 
    uint8[] monthDays = [31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31];

    // Time stamp date 
    function daysToDate(int timestamp, int8 timezone) public pure returns (uint year, uint month, uint day){
        return _daysToDate(timestamp + timezone * int(SECONDS_PER_HOUR));
    }

    // Total days of the month 
    function monthTotalDay(int timestamp, int8 timezone) public view returns (uint){
        (uint year, uint month,) = daysToDate(timestamp, timezone);
        if (month != 2) {
            return monthDays[month - 1];
        }
        return year % 4 == 0 ? 29 : 28;
    }

    // Time stamp date ,UTC The time zone 
    function _daysToDate(int timestamp) private pure returns (uint year, uint month, uint day) {
        uint _days = uint(timestamp) / SECONDS_PER_DAY;

        uint L = _days + 68569 + OFFSET19700101;
        uint N = 4 * L / 146097;
        L = L - (146097 * N + 3) / 4;
        year = 4000 * (L + 1) / 1461001;
        L = L - 1461 * year / 4 + 31;
        month = 80 * L / 2447;
        day = L - 2447 * month / 80;
        L = month / 11;
        month = month + 2 - 12 * L;
        year = 100 * (N - 49) + year + L;
    }



    // Get the total number of days and the remaining number of days in the current quarter 
    function getQuarterlyDay(int timestamp) public view returns(int){
        (uint year, uint month, uint day) = _daysToDate(timestamp);
        // year   month    Japan    quarter    The total number of days    Time stamp 
        return int(getQuarterly(uint16(year),uint8(month),uint8(day),getQuarterly(timestamp),0,timestamp));
    }


    // Whether the current day is the first day of the quarter 
    function getQuarterlyFirst(int timestamp) public pure returns(bool){
        (uint year, uint month, uint day) = _daysToDate(timestamp);
        if((month == 1 || month == 4 || month == 7 || month == 10) && day == 1){
            return true;
        }else{
            return false;
        }
    }

    // Enter year year    month month   Get the number of days of the month 
    function getDaysInMonth(uint8 month, uint16 year) public pure returns (uint8) {
        if (month == 1 || month == 3 || month == 5 || month == 7 || month == 8 || month == 10 || month == 12) {
            return 31;
        }
        else if (month == 4 || month == 6 || month == 9 || month == 11) {
            return 30;
        }
        else if (isLeapYear(year)) {
            return 29;
        }
        else {
            return 28;
        }
    }

    // Obtain the timestamp according to the month, year and day 
    function toTimestamp(uint16 year, uint8 month, uint8 day) public pure returns (uint timestamp) {
        return toTimestamp(year, month, day, 0, 0, 0);
    }


    // Obtain the timestamp according to the hour, minute and second of month, day and year 
    function toTimestamp(uint16 year, uint8 month, uint8 day, uint8 hour, uint8 minute, uint8 second) public pure returns (uint timestamp) {
        uint16 i;

        // Year
        for (uint16 i = ORIGIN_YEAR; i < year; i++) {
            if (isLeapYear(i)) {
                timestamp += LEAP_YEAR_IN_SECONDS;
            }
            else {
                timestamp += YEAR_IN_SECONDS;
            }
        }
        // Month
        uint8[12] memory monthDayCounts;
        monthDayCounts[0] = 31;
        if (isLeapYear(year)) {
            monthDayCounts[1] = 29;
        }
        else {
            monthDayCounts[1] = 28;
        }
        monthDayCounts[2] = 31;
        monthDayCounts[3] = 30;
        monthDayCounts[4] = 31;
        monthDayCounts[5] = 30;
        monthDayCounts[6] = 31;
        monthDayCounts[7] = 31;
        monthDayCounts[8] = 30;
        monthDayCounts[9] = 31;
        monthDayCounts[10] = 30;
        monthDayCounts[11] = 31;
        for (i = 1; i < month; i++) {
            timestamp += DAY_IN_SECONDS  * monthDayCounts[i - 1];
        }
        // Day
        timestamp += DAY_IN_SECONDS * (day - 1);
        // Hour
        timestamp += HOUR_IN_SECONDS * (hour);
        // Minute
        timestamp += MINUTE_IN_SECONDS * (minute);
        // Second
        timestamp += second;
        return timestamp;
    }

    // Judge whether the entered year is a leap year 
    function isLeapYear(uint16 year) public pure returns (bool) {
        if (year % 4 != 0) {
            return false;
        }
        if (year % 100 != 0) {
            return true;
        }
        if (year % 400 != 0) {
            return false;
        }
        return true;
    }



    // Pass in the time to get the current quarter 
    function getQuarterly(int timestamp) public pure returns(uint8){
        (uint year, uint month, uint day) = _daysToDate(timestamp);
        if(month >= 1 && month <= 3){
            return 1;
        }else if(month >= 4 && month <= 6){
            return 2;
        }else if(month >= 7 && month <= 9){
            return 3;
        }else if(month >= 10 && month <= 12){
            return 4;
        }
    }


    // Specific date      quarter     Time stamp 
    function getQuarterly(uint16 year, uint8 month, uint8 day,uint quarterly,uint countNumber,int timestamp) public view returns(uint){
        // Get current date 
        (uint yearPs, uint monthPs, uint dayPs)  = _daysToDate(timestamp);
        if(countNumber == 0){
            // Get the number of days in the current month 
            countNumber = monthTotalDay(timestamp,8);
            // Days of the month - Number = Remaining days of the current month 
            countNumber = countNumber - day;
            return getQuarterly(uint16(year), uint8(month), uint8(day),quarterly,countNumber,timestamp);
        }
        if(month > 12){
            month = 1;
            year++;
        }else{
            month++;
        }
        // Obtain the timestamp according to the month, year and day 
        int time = int(toTimestamp(year,month,day));
        // Get Quarterly 
        uint8 jidu = getQuarterly(time);
        if(jidu != uint8(quarterly)){
            return countNumber;
        }else{
            // Get the days of the current month according to the time 
            uint dayTime = monthTotalDay(time,8);
            countNumber = countNumber + dayTime;
            return getQuarterly(uint16(year), uint8(month), uint8(day),quarterly,countNumber,timestamp);
        }
    }


}

原网站

版权声明
本文为[Wind breaking]所创,转载请带上原文链接,感谢
https://yzsam.com/2022/176/202206251821252934.html