英文:
Formatting of Time in Go
问题
我有一个像这样的Go程序,
package main
import "fmt"
import "time"
func main() {
s := strconv.Itoa64(time.Nanoseconds())
fmt.Println(s)
}
在我的系统中,输出是19位数字的纳秒。现在,我想要获取这些纳秒的第7到12位数字的时间。有人可以帮忙吗?
注意:我想要的是从第7到12位的数字,因为在我的系统中,它们之间的时间差异是相同的,所以其他位数对我来说不重要。并且在println中不需要格式化,因为我只是为了澄清我的代码而给出了示例。实际上,我将使用格式化后的时间进行其他用途。我需要它在s :=中。
英文:
I have a go program like this,
package main
import "fmt"
import "time"
func main() {
s := strconv.Itoa64(time.Nanoseconds())
fmt.Println(s)
}
Here, in my system, the output is 19 digits nanoseconds. Now, I want to get the time like digits after 7 to 12 of those nanoseconds. Can anybody help how can it be possible in Go ?
NB: I want digits from 7 to 12 because in my system, time differs between them means other digits are same, so for me not needed. And formatting inside println is not required because I give example just to clarify my code. Actually, I will use formatted time for another purpose. I need it in s :=.
答案1
得分: 3
例如,
package main
import (
"fmt"
"strconv"
"time"
)
func main() {
s := strconv.Itoa64(time.Nanoseconds() / 1e7 % 1e6)
fmt.Println(s)
}
该算法从右边开始计数,将数字8到13隔离出来。对于一个19位数,这相当于从左边开始计数的第7到12位数字。对于19位数1323154238948677000,这些数字是423894。
func Nanoseconds() int64
Nanoseconds函数报告自Unix纪元(1970年1月1日00:00:00 UTC)以来的纳秒数。
在某个时间点,纳秒数的计数将从19位增加到20位有效数字。因此,从左边切片有效数字的算法,例如[6:12],是错误的。
英文:
For example,
package main
import (
"fmt"
"strconv"
"time"
)
func main() {
s := strconv.Itoa64(time.Nanoseconds() / 1e7 % 1e6)
fmt.Println(s)
}
This algorithm isolates digits 8 through 13 counting from the right. For a 19 digit integer, this is equivalent to digits 7 through 12 counting from the left. For the 19 digit number 1323154238948677000, these are the digits 423894.
> func Nanoseconds
>
> func Nanoseconds() int64
>
> Nanoseconds reports the number of nanoseconds since the Unix epoch,
> January 1, 1970 00:00:00 UTC.
At some point in time, the count of nanoseconds will increase from 19 to 20 significant digits. Therefore, algorithms that slice significant digits from the left, for example [6:12], are in error.
答案2
得分: 1
提取从7到12的数字很容易,使用字符串切片就可以解决:
s := strconv.Itoa64(time.Nanoseconds())
fmt.Println(s[6:12])
这样你就得到了。
英文:
to extract digits from 7 to 12 is easy, string slice will do the trick:
s := strconv.Itoa64(time.Nanoseconds())
fmt.Println(s[6:12])
so you get it.
答案3
得分: 0
s := strconv.Itoa64(time.Nanoseconds())[6:12]
英文:
s := strconv.Itoa64(time.Nanoseconds())[6:12]
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论