Golang Time Parse

先看看有哪些类型

Time

时间类型,包含了秒和纳秒以及Locationjson

Month

type Month int 月份.定义了十二个月的常量函数

Weekday

type Weekday int 周,定义了一周的七天编码

Duration

type Duration int64 持续时间.定义了如下持续时间类型.多用于时间的加减 须要传入Duration作为参数的时候.能够直接传入time.Secondspa

    const (
    Nanosecond  Duration = 1
    Microsecond          = 1000 * Nanosecond
    Millisecond          = 1000 * Microsecond
    Second               = 1000 * Millisecond
    Minute               = 60 * Second
    Hour                 = 60 * Minute
    )

Location

在time包里有两个时区变量:
time.UTC utc时间
time.Local 本地时间
FixedZone(name string, offset int) *Location  
设置时区名,以及与UTC0的时间误差.返回Locationcode

时间格式化

在其余语言通常格式化字符串是yyyy-MM-dd HH:mm:ss这种.
这个的Go语言的话是2006-01-02 15:04:05,这方式比较特别,按照123456来记忆吧:01月02号 下午3点04分05秒2006年.orm

Format(layout string) string

传入目标模板(Mon Jan 02 15:04:05 -0700 2006).时间以这个为准字符串

p(t.Format("3:04PM"))
p(t.Format("Mon Jan _2 15:04:05 2006"))
p(t.Format("2006-01-02T15:04:05.999999-07:00"))
p(t.Format("2006-01-02T15:04:05Z07:00"))
fmt.Printf("%d-%02d-%02dT%02d:%02d:%02d-00:00\n",
t.Year(), t.Month(), t.Day(),
t.Hour(), t.Minute(), t.Second())

Parse(layout, value string) (Time, error)

将字符窜转换为Time类型.string

    p := fmt.Println

    withNanos := "2006-01-02 15:04:05"
    t, _ := time.Parse(withNanos, "2013-10-05 18:30:50")
    p(t.Year())

ParseDuration(s string) (Duration, error)

将字duration符串(“ns”, “us” (or “ns”), “ms”, “s”, “m”, “h”.)转换为Duration类型.就是纳秒it

    p := fmt.Println
    t, _ := time.ParseDuration("1h")
    p(t.Seconds())

Time相关

time经常使用函数io

Now() Time

获取当前时间,返回Time类型

Unix(sec int64, nsec int64) Time

根据秒数和纳秒,返回Time类型

Date(year int, month Month, day, hour, min, sec, nsec int, loc *Location) Time

设置年月日返回,Time类型

Since(t Time) Duration

返回与当前时间的时间差

After(u Time) bool

时间类型比较,是否在Time以后

Before(u Time) bool

时间类型比较,是否在Time以前

Equal(u Time) bool

比较两个时间是否相等

IsZero() bool

判断时间是否为零值,若是sec和nsec两个属性都是0的话,则该时间类型为0

Date() (year int, month Month, day int)

返回年月日,三个参数

Year() int

返回年份

Month() Month

返回月份.是Month类型

Day() int

返回多少号

Weekday() Weekday

返回星期几,是Weekday类型

ISOWeek() (year, week int)

返回年份,和该填是在这年的第几周.

Clock() (hour, min, sec int)

返回小时,分钟,秒

Hour() int

返回小时

Minute() int

返回分钟

Second() int

返回秒数

Nanosecond() int

返回纳秒

Add(d Duration) Time

为一个时间,添加的时间类型为Duration.更精确到纳秒.比起AddDate

Sub(u Time) Duration

计算两个时间的差.返回类型Duration

AddDate(years int, months int, days int) Time

添加时间.以年月日为参数

UTC() Time

设置location为UTC,而后返回时间.就是utc为0.比中国晚了八个小时.

Local() Time

设置location为本地时间.就是电脑时间.

Location(loc *Location) Time

设置location为指定location

Location() *Location

获取时间的Location,若是是nic,返回UTC,若是为空,则表明本地

Zone() (name string, offset int)

返回时区,以及与utc的时间误差

Unix() int64

返回时间戳,自从1970年1月1号到如今

UnixNano() int64

返回时间戳.包含纳秒

 func main() { 

    now := time.Now()
    secs := now.Unix()
    nanos := now.UnixNano()
    fmt.Println(now)
    millis := nanos / 1000000


    fmt.Println(secs)
    fmt.Println(millis)
    fmt.Println(nanos)


    fmt.Println(time.Unix(secs, 0))
    fmt.Println(time.Unix(0, nanos))
}

GobEncode() ([]byte, error)

编码为gob

GobDecode(buf []byte) error

从gob解码

MarshalJSON() ([]byte, error)

编列为json

UnmarshalJSON(data []byte) (err error)

解码为json

    func main() {
        p := fmt.Println


        now := time.Now()
        p(now)


        d := time.Duration(7200 * 1000 * 1000 * 1000)
        p(d)


        then := time.Date(
        2013, 1, 7, 20, 34, 58, 651387237, time.UTC)


        p(then)
        p(then.Year())
        p(then.Month())
        p(then.Day())
        p(then.Hour())
        p(then.Minute())
        p(then.Second())
        p(then.Nanosecond())
        p(then.Location())
        p(then.Weekday())


        p(then.Before(now))
        p(then.After(now))
        p(then.Equal(now))


        p(then.Date())
        p(then.ISOWeek())
        p("----------")
        p(now.UTC())
        p(now.Local())
        p(now.Location())
        p(now.Zone())
        p(now.Unix())
        p(time.Unix(now.Unix(), 0))
        p(now.UnixNano())
        p(time.Unix(0, now.UnixNano()))
        p(now.GobEncode())
        p(now.MarshalJSON())
        p(time.Since(now))
        p("----------")
        diff := now.Sub(then)
        p(diff)


        p(diff.Hours())
        p(diff.Minutes())
        p(diff.Seconds())
        p(diff.Nanoseconds())
        p(then.Add(diff))
        p(then.Add(-diff))


        p(d)
        p(d.Hours())
        p(d.Minutes())
        p(d.Seconds())
        p(d.Nanoseconds())
        p(then.Add(d))
    }
相关文章
相关标签/搜索