当前位置:网站首页>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;
//每月天数
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;
}
}
pragma solidity ^0.4.16;
contract DateTime {
/*
* Date and Time utilities for ethereum contracts
*
*/
struct _DateTime {
uint16 year;
uint8 month;
uint8 day;
uint8 hour;
uint8 minute;
uint8 second;
uint8 weekday;
}
uint constant DAY_IN_SECONDS = 86400;
uint constant YEAR_IN_SECONDS = 31536000;
uint constant LEAP_YEAR_IN_SECONDS = 31622400;
uint constant HOUR_IN_SECONDS = 3600;
uint constant MINUTE_IN_SECONDS = 60;
uint16 constant ORIGIN_YEAR = 1970;
//判断输入的年份是不是闰年
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 leapYearsBefore(uint year) public pure returns (uint) {
year -= 1;
return year / 4 - year / 100 + year / 400;
}
//输入年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 parseTimestamp(uint timestamp) internal pure returns (_DateTime dt) {
uint secondsAccountedFor = 0;
uint buf;
uint8 i;
// Year
dt.year = getYear(timestamp);
buf = leapYearsBefore(dt.year) - leapYearsBefore(ORIGIN_YEAR);
secondsAccountedFor += LEAP_YEAR_IN_SECONDS * buf;
secondsAccountedFor += YEAR_IN_SECONDS * (dt.year - ORIGIN_YEAR - buf);
// Month
uint secondsInMonth;
for (i = 1; i <= 12; i++) {
secondsInMonth = DAY_IN_SECONDS * getDaysInMonth(i, dt.year);
if (secondsInMonth + secondsAccountedFor > timestamp) {
dt.month = i;
break;
}
secondsAccountedFor += secondsInMonth;
}
// Day
for (i = 1; i <= getDaysInMonth(dt.month, dt.year); i++) {
if (DAY_IN_SECONDS + secondsAccountedFor > timestamp) {
dt.day = i;
break;
}
secondsAccountedFor += DAY_IN_SECONDS;
}
// Hour
dt.hour = getHour(timestamp);
// Minute
dt.minute = getMinute(timestamp);
// Second
dt.second = getSecond(timestamp);
// Day of week.
dt.weekday = getWeekday(timestamp);
}
//根据时间戳获取年份
function getYear(uint timestamp) public pure returns (uint16) {
uint secondsAccountedFor = 0;
uint16 year;
uint numLeapYears;
// Year
year = uint16(ORIGIN_YEAR + timestamp / YEAR_IN_SECONDS);
numLeapYears = leapYearsBefore(year) - leapYearsBefore(ORIGIN_YEAR);
secondsAccountedFor += LEAP_YEAR_IN_SECONDS * numLeapYears;
secondsAccountedFor += YEAR_IN_SECONDS * (year - ORIGIN_YEAR - numLeapYears);
while (secondsAccountedFor > timestamp) {
if (isLeapYear(uint16(year - 1))) {
secondsAccountedFor -= LEAP_YEAR_IN_SECONDS;
}
else {
secondsAccountedFor -= YEAR_IN_SECONDS;
}
year -= 1;
}
return year;
}
//根据时间戳获取月份
function getMonth(uint timestamp) public pure returns (uint8) {
return parseTimestamp(timestamp).month;
}
//根据时间戳获取当前天数
function getDay(uint timestamp) public pure returns (uint8) {
return parseTimestamp(timestamp).day;
}
function getHour(uint timestamp) public pure returns (uint8) {
return uint8((timestamp / 60 / 60) % 24);
}
//获取分钟
function getMinute(uint timestamp) public pure returns (uint8) {
return uint8((timestamp / 60) % 60);
}
function getSecond(uint timestamp) public pure returns (uint8) {
return uint8(timestamp % 60);
}
//获取星期几
function getWeekday(uint timestamp) public pure returns (uint8) {
return uint8((timestamp / DAY_IN_SECONDS + 4) % 7);
}
//根据年月日获取时间戳
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) public pure returns (uint timestamp) {
return toTimestamp(year, month, day, hour, 0, 0);
}
//根据年月日时分获取时间戳
function toTimestamp(uint16 year, uint8 month, uint8 day, uint8 hour, uint8 minute) public pure returns (uint timestamp) {
return toTimestamp(year, month, day, hour, minute, 0);
}
//根据年月日时分秒获取时间戳
function toTimestamp(uint16 year, uint8 month, uint8 day, uint8 hour, uint8 minute, uint8 second) public pure returns (uint timestamp) {
uint16 i;
// Year
for (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;
}
}边栏推荐
- [elt.zip] openharmony paper Club - witness file compression system erofs
- [elt.zip] openharmony paper Club - memory compression for data intensive applications
- 踩坑记录---线程池拒绝策略引起的一系列巧合
- Huawei cloud SRE deterministic operation and maintenance special issue (the first issue)
- 【深入理解TcaplusDB技术】TcaplusDB新增机型
- Handling method of qstring containing "\u0000" in QT
- C ASP, net core framework value transfer method and session use
- Analysis of China's medical device industry development environment (PEST) in 2021: the awareness of medical care is enhanced, and the demand for medical device products is also rising [figure]
- 解决sublime Text3 package control 无法安装插件问题
- RMAN备份数据库_管理备份窗口(Backup Window)
猜你喜欢

【ELT.ZIP】OpenHarmony啃论文俱乐部—见证文件压缩系统EROFS

LeetCode力扣(剑指offer 26-30)26. 树的子结构 27. 二叉树的镜像 28. 对称的二叉树 29. 顺时针打印矩阵 30. 包含min函数的栈
![[deeply understand tcapulusdb technology] tmonitor system upgrade](/img/7b/8c4f1549054ee8c0184495d9e8e378.png)
[deeply understand tcapulusdb technology] tmonitor system upgrade

Use pagoda to set up mqtt server

一晚上做了一个xpath终结者:xpath-helper-plus
![[deeply understand tcapulusdb technology] create a game zone](/img/91/cf4eae9a4336ca407c0da805b9d909.png)
[deeply understand tcapulusdb technology] create a game zone

【深入理解TcaplusDB技术】一键安装Tmonitor后台
![[deeply understand tcapulusdb technology] cluster management operation](/img/5f/f501f557171d438f7cc9cc4427f234.png)
[deeply understand tcapulusdb technology] cluster management operation

Dell R530内置热备盘状态变化说明

GenICam GenTL 标准 ver1.5(1)
随机推荐
.NET Worker Service 如何优雅退出
06 local method interface
[in depth understanding of tcapulusdb technology] how to realize single machine installation of tmonitor
[deeply understand tcapulusdb technology] tmonitor system upgrade
GNU nano
Addition, deletion, modification and query of mysql~ tables (detailed and easy to understand)
Analysis of China's medical device industry development environment (PEST) in 2021: the awareness of medical care is enhanced, and the demand for medical device products is also rising [figure]
PHP数据库连接version1.1
Boiled peanuts
解决sublime Text3 package control 无法安装插件问题
Training of long and difficult sentences in postgraduate entrance examination English Day82
【深入理解TcaplusDB技术】Tmonitor系统升级
GNU nano
RMAN备份数据库_双重备份备份集(Duplexing Backup Sets)
Basic operations and basic data types of MySQL database
【深入理解TcaplusDB技术】TcaplusDB导入数据
solidity获取季度时间
Training of long and difficult sentences in postgraduate entrance examination day91
[deeply understand tcapulusdb technology] table management of document acceptance
【深入理解TcaplusDB技术】 Tmonitor模块架构