You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

在Remix中使用DateTime库转换时间戳为日期时间格式遇阻求助

问题分析与解决

你的函数返回全0的核心原因是:没有调用DateTime库的解析方法,只是返回了未赋值的默认变量。

Solidity中未初始化的uint类型变量默认值为0,你写的函数只是声明了返回值,但没有对这些变量进行任何赋值操作,自然会返回全0。

修正后的代码

因为DateTime是library类型合约,必须通过库名.方法名()的方式调用其内部方法。修正后的函数如下:

// 导入库
import "./DateTime.sol";

contract Escrow {
    function timestampToDateTime(uint timestamp) public pure returns (uint year, uint month, uint day, uint hour, uint minute, uint second) {
        // 调用DateTime库的timestampToDateTime方法解析时间戳
        (year, month, day, hour, minute, second) = DateTime.timestampToDateTime(timestamp);
    }
}

额外说明

  • 确保你复制的DateTime.sol内容完整,包含library DateTime的定义以及timestampToDateTime方法的实现。
  • 编译时要同时确保DateTime.sol和Escrow.sol都被正确编译,Remix中默认会自动处理依赖文件,但可以检查编译面板的文件列表确认。

内容的提问来源于stack exchange,提问作者Blockpain

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.08.01 02:31:15