英文:
Creating a time.Duration from float64 seconds
问题
我有一个包含以秒为单位的float64
类型的变量。我正在寻找一种将这个值转换为time.Duration
类型的方法。我能够进行这种转换,但我想知道是否有更简洁的方法。
我目前的方法是这样的:
var timeout float64 // 输入的浮点数类型的值
var res time.Duration // 结果的time.Duration类型的值
res += time.Duration(math.Round(timeout)) * time.Second
timeout -= math.Round(timeout)
timeout *= 1000
res += time.Duration(math.Round(timeout)) * time.Millisecond
timeout -= math.Round(timeout)
timeout *= 1000
res += time.Duration(math.Round(timeout)) * time.Microsecond
timeout -= math.Round(timeout)
timeout *= 1000
res += time.Duration(math.Round(timeout)) * time.Nanosecond
return res
我不喜欢这种方法的原因是它很繁琐且不可靠。我期望Go语言能够提供类似的功能,并以一种能够检测溢出和类似范围违规的方式进行转换。看起来Go语言还没有达到这个目标,但也许我错过了一些明显的东西,所以才有这个问题。
注意:
-
这个问题并不能满足我的需求,因为它与相反的转换方式有关。那种转换实际上非常简单,这让我更加惊讶的是我需要的这种转换居然没有提供。
-
为什么我不使用毫秒呢?原因很简单:一致性、KISS原则和最小惊讶原则。时间的国际单位是秒,其他单位都是从秒导出的,所以我使用秒作为默认单位。
-
对于前面的陈述,有一个小问题:Go语言本身说“没有定义大于一天的单位,以避免在夏令时转换时引起混淆。”。他们忽略了一点,即使有58到61秒的分钟,他们仍然有分钟和小时。这不是什么大问题,只是为了完整性而提一下。
英文:
I have a float64
containing a duration in seconds. I'm looking for a way to convert this value to a time.Duration
. I'm able to perform this conversion, but I'm wondering if there is not a more elegant way.
The approach I have is this:
var timeout float64 // input value of float type
var res time.Duration // result value of time.Duration type
res += time.Duration(math.Round(timeout)) * time.Second
timeout -= math.Round(timeout)
timeout *= 1000
res += time.Duration(math.Round(timeout)) * time.Millisecond
timeout -= math.Round(timeout)
timeout *= 1000
res += time.Duration(math.Round(timeout)) * time.Microsecond
timeout -= math.Round(timeout)
timeout *= 1000
res += time.Duration(math.Round(timeout)) * time.Nanosecond
return res
What I dislike about this is that it is cumbersome and not reliable. I'd expect Go to supply something like this out of the box and to perform these conversions in a way that detects overflows and similar range violations. It seems that Go is not there yet, but maybe I missed something obvious, hence my question.
Notes:
- This question doesn't address my needs, because it is rather related to the opposite way of conversion. That conversion is actually pretty painless, which makes the it even more surprising that the one I need isn't there.
- Why don't I use milliseconds instead? Simple reason: Consistency, KISS principle, principle of least surprise. The SI unit for time is the second. Everything else is only derived from this, so I use this as a default.
- Nitpick concerning the previous statement: Go itself says "There is no definition for units of Day or larger to avoid confusion across daylight savings time zone transitions.". They missed the point, because they still have minutes and hours, even though there are minutes with 58..61 seconds. Not a big deal, just mentioning it for completeness.
答案1
得分: 23
根据JimB的评论所示,将秒数乘以每秒的持续时间单位数。持续时间以纳秒为单位,但你的代码不需要知道这个细节。
return time.Duration(timeout * float64(time.Second))
将其转换为浮点数进行乘法运算,然后将其转换为持续时间以获得结果。
英文:
As JimB's comment shows, multiply the number of seconds by the number of duration units per second. Durations are measured in nanoseconds, but your code does not need to know that detail.
return time.Duration(timeout * float64(time.Second))
Convert to floating point for the multiplication and convert to duration to get the result.
答案2
得分: 1
我不确定问题出在哪里。您的请求非常简单实现:
package main
import (
"fmt"
"time"
)
func duration(f float64) time.Duration {
return time.Duration(f * 1e9)
}
func main() {
t := duration(9)
fmt.Println(t) // 9s
}
这只是一行代码,有什么不优雅的呢?唯一更优雅的方式是如果time.Duration
本身就是float64
类型。但这并不合理,因为Go语言不追踪比纳秒更小的时间单位。
https://golang.org/pkg/time#Duration
英文:
I'm not sure what the issue is here. Your request is very simple to implement:
package main
import (
"fmt"
"time"
)
func duration(f float64) time.Duration {
return time.Duration(f * 1e9)
}
func main() {
t := duration(9)
fmt.Println(t) // 9s
}
It's literally one line of code, what is not elegant about that? The only way it could be more elegant, is if time.Duration
was float64
natively. And that just doesn't make sense, as Go doesn't track anything smaller than a nanosecond.
答案3
得分: 0
根据https://pkg.go.dev/time#Second上的文档,time.Duration(timeout) * time.Second表示将timeout转换为以秒为单位的时间间隔。
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论