当前位置:网站首页>solidity获取季度时间
solidity获取季度时间
2022-06-25 18:21:00 【破 风】
pragma solidity ^0.5.1;
//时间工具
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;
//每月天数
uint8[] monthDays = [31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31];
//时间戳转日期
function daysToDate(int timestamp, int8 timezone) public pure returns (uint year, uint month, uint day){
return _daysToDate(timestamp + timezone * int(SECONDS_PER_HOUR));
}
//当月总天数
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;
}
//时间戳转日期,UTC时区
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;
}
//获取当个季度总天数和剩余多少天数
function getQuarterlyDay(int timestamp) public view returns(int){
(uint year, uint month, uint day) = _daysToDate(timestamp);
//年 月 日 季度 总天数 时间戳
return int(getQuarterly(uint16(year),uint8(month),uint8(day),getQuarterly(timestamp),0,timestamp));
}
//当日是否为季度的第一天
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;
}
}
//输入年year 月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;
}
}
//根据年月日获取时间戳
function toTimestamp(uint16 year, uint8 month, uint8 day) public pure returns (uint timestamp) {
return toTimestamp(year, month, day, 0, 0, 0);
}
//根据年月日时分秒获取时间戳
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;
}
//判断输入的年份是不是闰年
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;
}
//传入时间搓获取当前到了第几个季度
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;
}
}
//年月日 季度 时间戳
function getQuarterly(uint16 year, uint8 month, uint8 day,uint quarterly,uint countNumber,int timestamp) public view returns(uint){
//获取当前日期
(uint yearPs, uint monthPs, uint dayPs) = _daysToDate(timestamp);
if(countNumber == 0){
//获取当月天数
countNumber = monthTotalDay(timestamp,8);
//当月天数-号数=剩余当月天数
countNumber = countNumber - day;
return getQuarterly(uint16(year), uint8(month), uint8(day),quarterly,countNumber,timestamp);
}
if(month > 12){
month = 1;
year++;
}else{
month++;
}
//根据年月日获取时间戳
int time = int(toTimestamp(year,month,day));
//获取季度
uint8 jidu = getQuarterly(time);
if(jidu != uint8(quarterly)){
return countNumber;
}else{
//根据时间搓获取当月天数
uint dayTime = monthTotalDay(time,8);
countNumber = countNumber + dayTime;
return getQuarterly(uint16(year), uint8(month), uint8(day),quarterly,countNumber,timestamp);
}
}
}
边栏推荐
- 存在重复元素III[利用排序后的有序性进行剪枝]
- Sword finger offer double pointer
- RMAN备份数据库_跳过脱机,只读和不可访问的文件
- Problems encountered during the use of pychar
- 【深入理解TcaplusDB技术】单据受理之事务执行
- Some recursive and iterative problem solving ideas of binary tree (clear and easy to understand)
- Kwai 616 war report was launched, and the essence was thrown away for the second time to lead the new wave. Fast brand jumped to the top 3 of the hot list
- QT中QString包含“\u0000“的处理方式
- RMAN备份数据库_双重备份备份集(Duplexing Backup Sets)
- What is the ranking of the top ten securities companies? Is it safe to open a mobile account?
猜你喜欢

Under what circumstances do you need to manually write the @bean to the container to complete the implementation class

Optimization of lazyagg query rewriting in parsing data warehouse

04 program counter (PC register)

安装spark + 用命令运行scala相关项目 + crontab定时执行

JVM problem replication
![[deeply understand tcapulusdb technology] create a game area for document acceptance](/img/7b/8c4f1549054ee8c0184495d9e8e378.png)
[deeply understand tcapulusdb technology] create a game area for document acceptance

Anaconda download Tsinghua source

Dell r530 built in hot spare status change description

【深入理解TcaplusDB技术】TcaplusDB构造数据

Idea annotation color modification method (clear)
随机推荐
Training of long and difficult sentences in postgraduate entrance examination day92
RMAN backup database_ catalogue
Deep learning network model
connect to address IP: No route to host
Training of long and difficult sentences in postgraduate entrance examination day85
存在重复元素III[利用排序后的有序性进行剪枝]
【深入理解TcaplusDB技术】单据受理之事务执行
Pycharm 使用过程中碰到问题
【深入理解TcaplusDB技术】单据受理之建表审批
Slam visuel Leçon 14 leçon 9 filtre Kalman
华为云SRE确定性运维专刊(第一期)
Training of long and difficult sentences in postgraduate entrance examination day84
[in depth understanding of tcapulusdb technology] business guide for creating doc acceptance
Sword finger offer double pointer
anaconda下载清华源
IVX 启航
【深入理解TcaplusDB技术】TcaplusDB运维单据
Handling method of qstring containing "\u0000" in QT
What is an operator?
C ASP, net core framework value transfer method and session use