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 @ 2021-12-29 18:01  会飞的斧头  阅读(47)  评论(0编辑  收藏  举报