原文地址:https://medium.com/@k3no/making-a-birthday-contract-858fd3f63618

先将datetime合约部署:https://github.com/pipermerriam/ethereum-datetime

pragma solidity ^0.4.;

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 = ;
uint constant YEAR_IN_SECONDS = ;
uint constant LEAP_YEAR_IN_SECONDS = ; uint constant HOUR_IN_SECONDS = ;
uint constant MINUTE_IN_SECONDS = ; uint16 constant ORIGIN_YEAR = ; function isLeapYear(uint16 year) public pure returns (bool) {
if (year % != ) {
return false;
}
if (year % != ) {
return true;
}
if (year % != ) {
return false;
}
return true;
} function leapYearsBefore(uint year) public pure returns (uint) {
year -= ;
return year / - year / + year / ;
} function getDaysInMonth(uint8 month, uint16 year) public pure returns (uint8) {
if (month == || month == || month == || month == || month == || month == || month == ) {
return ;
}
else if (month == || month == || month == || month == ) {
return ;
}
else if (isLeapYear(year)) {
return ;
}
else {
return ;
}
} function parseTimestamp(uint timestamp) internal pure returns (_DateTime dt) {
uint secondsAccountedFor = ;
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 = ; i <= ; i++) {
secondsInMonth = DAY_IN_SECONDS * getDaysInMonth(i, dt.year);
if (secondsInMonth + secondsAccountedFor > timestamp) {
dt.month = i;
break;
}
secondsAccountedFor += secondsInMonth;
} // Day
for (i = ; 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 = ;
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 - ))) {
secondsAccountedFor -= LEAP_YEAR_IN_SECONDS;
}
else {
secondsAccountedFor -= YEAR_IN_SECONDS;
}
year -= ;
}
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 / / ) % );
} function getMinute(uint timestamp) public pure returns (uint8) {
return uint8((timestamp / ) % );
} function getSecond(uint timestamp) public pure returns (uint8) {
return uint8(timestamp % );
} function getWeekday(uint timestamp) public pure returns (uint8) {
return uint8((timestamp / DAY_IN_SECONDS + ) % );
} function toTimestamp(uint16 year, uint8 month, uint8 day) public pure returns (uint timestamp) {
return toTimestamp(year, month, day, , , );
} function toTimestamp(uint16 year, uint8 month, uint8 day, uint8 hour) public pure returns (uint timestamp) {
return toTimestamp(year, month, day, hour, , );
} function toTimestamp(uint16 year, uint8 month, uint8 day, uint8 hour, uint8 minute) public pure returns (uint timestamp) {
return toTimestamp(year, month, day, hour, minute, );
} 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[] memory monthDayCounts;
monthDayCounts[] = ;
if (isLeapYear(year)) {
monthDayCounts[] = ;
}
else {
monthDayCounts[] = ;
}
monthDayCounts[] = ;
monthDayCounts[] = ;
monthDayCounts[] = ;
monthDayCounts[] = ;
monthDayCounts[] = ;
monthDayCounts[] = ;
monthDayCounts[] = ;
monthDayCounts[] = ;
monthDayCounts[] = ;
monthDayCounts[] = ; for (i = ; i < month; i++) {
timestamp += DAY_IN_SECONDS * monthDayCounts[i - ];
} // Day
timestamp += DAY_IN_SECONDS * (day - ); // Hour
timestamp += HOUR_IN_SECONDS * (hour); // Minute
timestamp += MINUTE_IN_SECONDS * (minute); // Second
timestamp += second; return timestamp;
}
}

然后获取datetime utility的地址,再部署另一个合约:

pragma solidity ^ 0.4.;
contract DateTime {
function getYear(uint timestamp) public constant returns (uint16);
function getMonth(uint timestamp) public constant returns (uint8);
function getDay(uint timestamp) public constant returns (uint8);
}
contract BirthDay {
uint public bday;
address public dateTimeAddr = 0xb23e0bfaeedcfce82ea2011f2a2726584e611e57;
DateTime dateTime = DateTime(dateTimeAddr);
function BirthDay() public {
bday = now;
}
function getBirthYear() view public returns (uint16){
return dateTime.getYear(bday);
}
function getBirthMonth() view public returns (uint8){
return dateTime.getMonth(bday);
}
function getBirthDay() view public returns (uint8){
return dateTime.getDay(bday);
}
}
05-27 18:26