当前位置:网站首页>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);
}
}
}
边栏推荐
- Skills to master in advanced development
- SQL is used for field data types in various databases
- Analysis on employment compensation of 2021 college graduates: the average monthly starting salary of doctors, masters, undergraduates and junior colleges is 14823 yuan, 10113 yuan, 5825 yuan and 3910
- Ruffian Heng embedded semimonthly issue 57
- mysql事务讲解
- 为什么生命科学企业都在陆续上云?
- 最新数据挖掘赛事方案梳理!
- [elt.zip] openharmony paper Club - witness file compression system erofs
- [today in history] June 25: the father of notebook was born; Windows 98 release; First commercial use of generic product code
- [deeply understand tcapulusdb technology] create a game zone
猜你喜欢
Analysis of global tea production, consumption and import and export trade: China's tea production ranks first in the world [figure]
Sorting out the latest data mining competition scheme!
Redis configuration (Internet access, password)
[deeply understand tcapulusdb technology] table management of document acceptance
正则表达式总结
[in depth understanding of tcapulusdb technology] tcapulusdb operation and maintenance doc
揭秘GES超大规模图计算引擎HyG:图切分
《痞子衡嵌入式半月刊》 第 57 期
Overview and trend analysis of China's foreign direct investment industry in 2020 [figure]
Regular expression summary
随机推荐
TCP/IP 测试题(三)
LeetCode-78-子集
QQ机器人:群成员自我禁言管理【最新beta2版本】
[in depth understanding of tcapulusdb technology] tcapulusdb operation and maintenance doc
什么是算子?
06 local method interface
Leetcode-78-subset
Addition, deletion, modification and query of mysql~ tables (detailed and easy to understand)
云上弹性高性能计算,支持生命科学产业高速发展、降本增效
Skills to master in advanced development
C generic class case
华为发布两大昇腾计划 推动AI人才发展和科研创新
Analysis on development status and development suggestions of e-commerce industry in Xinjiang in 2020 [figure]
2021 development status of China's cloud game industry and analysis of major service providers: Although cloud games are still in their infancy, the market prospect is huge [figure]
Dell r530 built in hot spare status change description
158_模型_Power BI 使用 DAX + SVG 打通制作商業圖錶幾乎所有可能
最新数据挖掘赛事方案梳理!
TCP/IP 测试题(二)
03 runtime data area overview and threads
[in depth understanding of tcapulusdb technology] form creation and approval of document acceptance