Go——关于Time包

需要转换毫秒值 Milliseconds

但包里面单单没有提供毫秒值的装换呢? 

Go 语言的设计者希望我有更多的选择,而不只是将毫秒值转换成某种单独的内建类型。

在 Time 包中,定义有一个名为 Duration 的类型和一些辅助的常量。

当我们再次查看Duration类型的定义,可以发现Duration类型汇总基本单位时间是纳秒(Nanosecond),所以讲一个表示10毫秒的Durantion类型对象转换为int64类型时,实际上得到的是10,000,000。

复制代码
// Milliseconds returns the duration as a floating point number of Milliseconds.
// add by chong
func (d Duration) Milliseconds() float64 {
    milli := d / Millisecond
    nsec := d % Millisecond
    return float64(milli) + float64(nsec)/1e6
}
// Seconds returns the duration as a floating point number of seconds.
func (d Duration) Seconds() float64 {
    sec := d / Second
    nsec := d % Second
    return float64(sec) + float64(nsec)/1e9
}

// Minutes returns the duration as a floating point number of minutes.
func (d Duration) Minutes() float64 {
    min := d / Minute
    nsec := d % Minute
    return float64(min) + float64(nsec)/(60*1e9)
}

// Hours returns the duration as a floating point number of hours.
func (d Duration) Hours() float64 {
    hour := d / Hour
    nsec := d % Hour
    return float64(hour) + float64(nsec)/(60*60*1e9)
}
复制代码

 

posted @   会飞的斧头  阅读(50)  评论(0编辑  收藏  举报
编辑推荐:
· 基于Microsoft.Extensions.AI核心库实现RAG应用
· Linux系列:如何用heaptrack跟踪.NET程序的非托管内存泄露
· 开发者必知的日志记录最佳实践
· SQL Server 2025 AI相关能力初探
· Linux系列:如何用 C#调用 C方法造成内存泄露
阅读排行:
· 震惊!C++程序真的从main开始吗?99%的程序员都答错了
· 【硬核科普】Trae如何「偷看」你的代码?零基础破解AI编程运行原理
· 单元测试从入门到精通
· 上周热点回顾(3.3-3.9)
· winform 绘制太阳,地球,月球 运作规律
历史上的今天:
2020-12-29 数据结构——(转)如何存储微博、微信等社交网络中的好友关系?
点击右上角即可分享
微信分享提示