原文地址: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);
}
}

Solidity 合约调用合约的更多相关文章

  1. Solidity智能合约调用智能合约

    来源:https://medium.com/@blockchain101/calling-the-function-of-another-contract-in-solidity-f9edfa921f ...

  2. Solidity truffle,部署合约到Ropsten测试链或主链,调用合约(转)

    Solidity truffle,部署合约到Ropsten测试链或主链,调用合约 转 https://blog.csdn.net/houyanhua1/article/details/89010896 ...

  3. web3 编译部署调用合约

    //导入solc 编译器 let solc = require('solc') let fs = require('fs') //读取合约 let sourceCode = fs.readFileSy ...

  4. 区块链入门(5)Truffle 项目实战,Solidity IDE, 智能合约部署

    在上一张我们学习了Truffle项目的创建,部署等相关内容,今天我们就来实战一下. 今天我们要做3件事: 1) 学习搭建一个Solidity IDE(Remix). 2) 使用这个Solidity I ...

  5. 智能合约调用另一合约中的payable方法

    参考链接: https://ethereum.stackexchange.com/questions/9705/how-can-you-call-a-payable-function-in-anoth ...

  6. Remix+Geth 实现智能合约部署和调用详解

    Remix编写智能合约 编写代码 在线调试 实现部署 调用接口 Geth实现私有链部署合约和调用接口 部署合约 调用合约 获得合约实例 通过实例调用合约接口 Remix编写智能合约 编写代码 Remi ...

  7. 以太坊系列之十七: 使用web3进行合约部署调用以及监听

    以太坊系列之十七: 使用web3进行智能合约的部署调用以及监听事件(Event) 上一篇介绍了使用golang进行智能合约的部署以及调用,但是使用go语言最大的一个问题是没法持续监听事件的发生. 比如 ...

  8. 以太坊系列之十六: 使用golang与智能合约进行交互

    以太坊系列之十六: 使用golang与智能合约进行交互 以太坊系列之十六: 使用golang与智能合约进行交互 此例子的目录结构 token contract 智能合约的golang wrapper ...

  9. 以太坊系列之十六:golang进行智能合约开发

    以太坊系列之十六: 使用golang与智能合约进行交互 以太坊系列之十六: 使用golang与智能合约进行交互 此例子的目录结构 token contract 智能合约的golang wrapper ...

随机推荐

  1. Python之matplotlib库

    知识结构 pyplot.plot()流程 1. _axes.py中plot()函数说明 a. 调用说明 plot([x], y, [fmt], data=None, **kwargs)       p ...

  2. Angular2常用命令

    一.常用命令 1.1 npm config list配置项目 可进行相关代理配置,通常可以配置在网络环境较差的情况下,配置相关代理.相关的设置命令如图: 1.2 ng 新建启动项目 ng new pr ...

  3. Maven构建跳过测试步骤

    有时候我们不想再执行maven的package或者install命令时每次都执行test,那么可以在pom.xml里的build->pluginManagement->plugins新增如 ...

  4. sql中case when的简单使用

    这是一个很多博客都引用的博客,作者未知,但是我第一次看到的就是这个,所以置顶这个吧, 这里有两个我刚才使用的列子: --查询同一机构的签约数和解约数: select t.sgn_acct_issr_i ...

  5. (转)Oracle 临时表用法

    本文转载自:http://www.iteye.com/topic/371390 ORACLE的临时表在应用系统中有很大的作用,它可以让用户只能够操作各自的数据中而互不干扰,不用担心会破坏或影响其他SE ...

  6. shell脚本,每5个字符之间插入"|",行末不插入“|”

    文本aaaaabbbbbcccccddddeeeeefffffkkkkkvvvvnnnnnggggg 希望得到的结果如下:aaaaa|bbbbb|ccccc|ddddeeeee|fffff|kkkkk ...

  7. 在mac上 使用jenkins 执行python文件

    1.要选择 [执行 shell]构建

  8. 【转】使用JMeter测试你的EJB

    对EJB进行一些性能基准测试是非常有必要和有帮助的,测试的方法和工具有很多,不过我最近发现,Apache JMeter是进行基准测试的一个优秀工具.可惜的是,JMeter没有提供一个可测试任意EJB的 ...

  9. zabbix增加手机短信、邮件监控的注意要点,SSL邮件发送python脚本

    1.短信接口文档: URL http://xxx.com/interfaces/sendMsg.htm Method POST Description 文字短信调用接口 Request Param L ...

  10. 生成器+列表生成式,生成器可以节省内存,随时调取函数运行,以及实现多线程运行函数,__next__()和.send(参数)的区别,a,b=b,a+b其实是元祖的用法,出现异常状态用try...except StopIteration来处理

    列表生成式:是代码更简洁. 也可以是函数,比如func(i) 生成器:generator 列表生成式,是中括号,改成小括号,就是生成器: 如果你用列表生成式,生成一亿个数据:这里会卡好久,会生成一亿个 ...