英文:
Subtracting time to get age
问题
我的目标是通过计算“当前时间 - Pod 创建时间”的差值来计算 Pod 的年龄,我从元数据中获取创建时间,但它的格式是“2021-07-13 16:34:22 +0530 IST”,所以当我尝试从 time.Now()
中减去它时,我得到了如下的解析错误:
无效操作:“t2 : ” + t2(不匹配的类型 string 和 time.Time)
请问有人可以帮忙解决如何从元数据中获取格式为“2021-07-13 16:34:22 +0530 IST”的创建时间,以便我可以进行“time.Now - (创建时间)”的计算吗?
我尝试了一些解决方法,如下所示:
creatTime, err := time.Parse("2006-01-02 15:04:05 -0700 MST",
pod.ObjectMeta.CreationTimestamp.String())
然后从当前时间减去创建时间。这个方法可以工作,但我认为这不是正确的方法。
英文:
My aim is to calculate the age of the pod by doing the subtraction of "current_time - pod_creation_time" so that I will get the age, I am getting creation time from metadata but it's in the format "2021-07-13 16:34:22 +0530 IST", so when I trying to subtract it from time.Now()
, I am getting parsing error like below:
invalid operation: "t2 : " + t2 (mismatched types string and time.Time)
Anyone could please help how to have creation time "2021-07-13 16:34:22 +0530 IST" from metadata in the proper format so that I can do "time.Now - (creation time)"
I tried some workaround like below:
creatTime, err := time.Parse("2006-01-02 15:04:05 -0700 MST",
pod.ObjectMeta.CreationTimestamp.String())
and then subtracted creationTime from Current Time. It works, but I think this is not the right way.
答案1
得分: 1
time.Now()
返回的是存储在time.Time
类型中的当前时间,而2021-07-13 16:34:22 +0530 IST
是一个字符串,所以存在类型不匹配的问题。你可以对不匹配的类型进行所需的减法操作,即time.Time
和string
。
你需要通过指定格式来解析字符串。我建议阅读time包的文档。
我在下面的示例代码中解释了每个操作,希望对你有所帮助。如果你理解了这个,你还可以查看像time.Since
这样的辅助函数,它可以帮助你用更少的代码编写相同的程序。
package main
import (
"fmt"
"time"
)
func main() {
// K8s timestamp
t := "2021-07-13 16:34:22 +0530 IST"
// K8s时间戳的格式
format := "2006-01-02 15:04:05 -0700 MST" // Mon Jan 2 15:04:05 -0700 MST 2006
// 解析时间戳,使其存储在time.Time中
cur, err := time.Parse(format, t)
if err != nil {
panic(err)
}
// 当前时间
now := time.Now()
// 由于两者都是time.Time类型,可以进行减法运算
dur := now.Sub(cur)
// 打印持续时间
fmt.Println(dur)
// 打印持续时间(以秒为单位)
fmt.Println(dur.Seconds())
}
另外,我希望你学会如何在StackOverflow上提问。你的问题格式非常糟糕。当寻求好的解决方案时,提问者首先应该正确地发布问题,以便每个人都能理解,并期望得到答案。
阅读:https://stackoverflow.com/help/how-to-ask
英文:
There's a type mismatch as time.Now()
return the current time stored in the type time.Time
whereas 2021-07-13 16:34:22 +0530 IST
is a string. You can perform the required subtraction operation on mismatched types i.e., time.Time
and string
.
You have to parse the string by specifying the layout. I'd recommend reading the time package's doc.
I've explained every operation in the sample code below; I hope it helps. If you understand this, you can also then look at helper functions like time.Since
that can help you write the same program in fewer lines.
package main
import (
"fmt"
"time"
)
func main() {
// K8s timestamp
t := "2021-07-13 16:34:22 +0530 IST"
// Format of K8s timestamp
format := "2006-01-02 15:04:05 -0700 MST" // Mon Jan 2 15:04:05 -0700 MST 2006
// Parse the timestamp so that it's stored in time.Time
cur, err := time.Parse(format, t)
if err != nil {
panic(err)
}
// Current time
now := time.Now()
// As both are of type time.Time, it's subtractable
dur := now.Sub(cur)
// Print duration
fmt.Println(dur)
// Print duration (in seconds)
fmt.Println(dur.Seconds())
}
Also, I'd like you to learn how to write questions on StackOverflow. The formatting of your question is pretty bad. When seeking good solutions; it is the OP's duty to post the question correctly first so that everybody could understand it and then expect answers.
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论