Go语言学习#5-time包的学习

本文最后更新于2018年10月25日,已超过 1 年没有更新,如果文章内容失效,还请反馈给我,谢谢!

=Start=

缘由:

在一些日志分析程序中,日期/时间字符串的解析操作要用到的地方有很多,之前在用Python的时候就这类问题整理了好几篇文章,现在学习Go语言了,也整理一篇相关文章,方便以后参考。

正文:

参考解答:
如何获取当前时间(字符串、年、月、日)?
package main

import (
    "fmt"
    "time"
)

func main() {
    start_time := time.Now()
    y, m, d := start_time.Date()
    today := start_time.Format("2006-01-02")
    datetime := start_time.Format("2006-01-02 15:04:05") // 后面的参数是固定的,否则将无法正常输出

    fmt.Printf("time is: %v, (%T)\n", start_time, start_time) // time is: 2018-10-21 17:24:29.307336 +0800 CST, (time.Time)
    fmt.Printf("y, m, d: %v, %v, %v\n", y, m, d)              // y, m, d: 2018, October, 21
    fmt.Printf("y[%T], m[%T], d[%T]\n", y, m, d)              // y[int], m[time.Month], d[int]
    fmt.Printf("today is: %q, (%T)\n", today, today)          // today is: "2018-10-21", (string)
    fmt.Printf("datetime is: %q, (%T)\n", datetime, datetime) // datetime is: "2018-10-21 17:24:29", (string)
}
Go 语言中如何解析日期/时间字符串?(使用time.Parse()方法)
datetime_str := "2018-10-21 12:24:51"
dt, _ := time.Parse("2006-01-02 15:04:05", datetime_str)
fmt.Printf("value: %s, type: %T\n", dt, dt) // value: 2018-10-21 12:24:51 +0000 UTC, type: time.Time
fmt.Printf("date: %s, datetime: %s\n", dt.Format("2006-01-02"), dt.Format("2006-01-02 15:04:05")) // date: 2018-10-21, datetime: 2018-10-21 12:24:51
如何获取下一小时、明天的信息?
package main

import (
    "fmt"
    "time"
)

func main() {
    start_time := time.Now()

    duration, _ := time.ParseDuration("1h")
    fmt.Printf("duration is: %q, (%T)\n", duration, duration) // duration is: "1h0m0s", (time.Duration)

    next_hour := start_time.Add(duration)
    fmt.Printf("1h later is: %q, (%T)\n", next_hour.Format("2006-01-02 15:04:05"), next_hour)

    last_hour := start_time.Add(-duration) // 这里用了一个「负号」
    fmt.Printf("1h before is: %q, (%T)\n", last_hour.Format("2006-01-02 15:04:05"), last_hour)

    yesterday := start_time.AddDate(0, 0, -1) // yesterday is: 2018-10-20, (time.Time)
    fmt.Printf("yesterday is: %s, (%T)\n", yesterday.Format("2006-01-02"), yesterday)

    tomorrow := start_time.AddDate(0, 0, 1) // tomorrow is: 2018-10-22, (time.Time)
    fmt.Printf("tomorrow is: %s, (%T)\n", tomorrow.Format("2006-01-02"), tomorrow)

}
如何暂停(sleep)执行?
// 暂停2秒
time.Sleep(time.Duration(2) * time.Second)
// https://stackoverflow.com/questions/17573190/how-to-multiply-duration-by-integer
如何获取程序的执行耗时?
package main

import (
    "fmt"
    "time"
)

func main() {
    start_time := time.Now()

    // 将操作放在这里,运行结束时会打印出耗时(以「秒」为单位)

    // Seconds() 返回的是 float64 类型的变量
    fmt.Printf("Spend %v seconds.\n", time.Since(start_time).Seconds()) // Spend 0.000141 seconds.
}

 

func ParseDuration(s string) (Duration, error)
`
ParseDuration parses a duration string. A duration string is a possibly signed sequence of decimal numbers, each with optional fraction and a unit suffix, such as "300ms", "-1.5h" or "2h45m". Valid time units are "ns", "us" (or "µs"), "ms", "s", "m", "h".

ParseDuration 用于解析一个表示持续时间的字符串。持续时间字符串可能是一个有符号的十进制数字序列,每个数字都有可选的分数和单位后缀,例如“300ms”、“-1.5h”或“2h45m”。有效的时间单位是“ns”,“us”(或“µs”),“ms”,“s”,“m”,“h”。
`

&

func ParseInLocation(layout, value string, loc *Location) (Time, error)
`
ParseInLocation is like Parse but differs in two important ways. First, in the absence of time zone information, Parse interprets a time as UTC; ParseInLocation interprets the time as in the given location. Second, when given a zone offset or abbreviation, Parse tries to match it against the Local location; ParseInLocation uses the given location.

ParseInLocation 与 Parse 类似,但有两种重要的区别。首先,在没有时区信息的情况下,Parse将时间解释为UTC,而ParseInLocation用给定位置来进行解释。其次,当给定一个区域偏移量或缩写时,Parse试图将它与本地位置进行匹配;而ParseInLocation使用给定的位置。
`

 

参考链接:

=END=

声明: 除非注明,ixyzero.com文章均为原创,转载请以链接形式标明本文地址,谢谢!
https://ixyzero.com/blog/archives/4134.html

One thought on “Go语言学习#5-time包的学习”

发表评论

电子邮件地址不会被公开。 必填项已用*标注