原文地址: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. 用eclipse来制作并使用可执行的jar文件

    我近来用java写了一个股票收益分析的小程序,用于计算我的股票操作所带来的的收益.这里,记录了如何将源代码打包成可执行的命令的一个过程. 1:生成可执行的jar文件 选中工程,选择菜单中的export ...

  2. vscode: Visual Studio Code 常用快捷键【轉】

    主命令框 F1 或 Ctrl+Shift+P: 打开命令面板.在打开的输入框内,可以输入任何命令,例如: 按一下 Backspace 会进入到 Ctrl+P 模式 在 Ctrl+P 下输入 >  ...

  3. [转]HTTP 协议中的 Transfer-Encoding

    本文作为我的博客「HTTP 相关」专题新的一篇,主要讨论 HTTP 协议中的 Transfer-Encoding.这个专题我会根据自己的理解,以尽量通俗的讲述,结合代码示例和实际场景来说明问题,欢迎大 ...

  4. linux 命令详解 sort

    转自:https://www.cnblogs.com/51linux/archive/2012/05/23/2515299.html sort是在Linux里非常常用的一个命令,管排序的,集中精力,五 ...

  5. javascript中原型学习

    学习地址:http://www.cnblogs.com/wangfupeng1988/tag/%E5%8E%9F%E5%9E%8B/

  6. php时间 显示刚刚 几分钟前等

    功能示例: $now = time();foreach($sys_res as $k => $v){ $day = intval(floor(($now - $v->system_time ...

  7. Oracle中遇到的错误

    1. ORA-00937: 不是单组分组函数  和  不是group by表达式 --select count(corp_tn),state_code from t_oa_main where cor ...

  8. 【转】 Pro Android学习笔记(九二):AsyncTask(1):AsyncTask类

    文章转载只能用于非商业性质,且不能带有虚拟货币.积分.注册等附加条件.转载须注明出处:http://blog.csdn.net/flowingflying/ 在Handler的学习系列中,学习了如何h ...

  9. AJAX验证此ID是否有对应的name

    在表格输入一个ID,然后自动根据ID在数据库中查找是否有对应name 这是javascript部分,利用ajax验证 $(document).ready(function() { $("#c ...

  10. Python中调用设置环境变量的bat

    工作中用到一个python脚本,自动化运行某目录下的多个vc工程代码. 编译工程代码的命令如下,直接运行会失败,系统找不到devenv,我们需要添加devenv.exe所在的目录到环境变量中. os. ...