ruk·si

Go
Time

Updated at 2014-01-23 08:20

Basic time manipulation.

package main

import (
    "fmt"
    "time"
)

var p = fmt.Println

func main() {
    now := time.Now()
    p(now) // 2009-11-10 23:00:00 +0000 UTC

    then := time.Date(2009, 11, 17, 20, 34, 58, 651387237, time.UTC)
    p(then) // 2009-11-17 20:34:58.651387237 +0000 UTC

    p(then.Year())       // 2009
    p(then.Month())      // November
    p(then.Day())        // 17
    p(then.Hour())       // 20
    p(then.Minute())     // 34
    p(then.Second())     // 58
    p(then.Nanosecond()) // 651387237
    p(then.Location())   // UTC
    p(then.Weekday())    // Tuesday

    p(then.Before(now)) // false
    p(then.After(now))  // true
    p(then.Equal(now))  // false

    diff := now.Sub(then)
    p(diff)               // -165h34m58.651387237s
    p(diff.Hours())       // -165.58295871867693
    p(diff.Minutes())     // -9934.977523120617
    p(diff.Seconds())     // -596098.651387237
    p(diff.Nanoseconds()) // -596098651387237
    p(then.Add(diff))     // 2009-11-10 23:00:00 +0000 UTC
    p(then.Add(-diff))    // 2009-11-24 18:09:57.302774474 +0000 UTC
}

Durations.

package main

import (
    "fmt"
    "time"
)

func main() {
    now := time.Now()
    soon := now.Add(10 * time.Minute)
    fmt.Println(now)  // => 2009-11-10 23:00:00 +0000 UTC
    fmt.Println(soon) // => 2009-11-10 23:10:00 +0000 UTC
}

Unix epoch.

package main

import (
    "fmt"
    "time"
)

func main() {
    now := time.Now()
    secs := now.Unix()
    fmt.Println(now)  // 2009-11-10 23:00:00 +0000 UTC
    fmt.Println(secs) // 1257894000
    nanos := now.UnixNano()
    millis := nanos / 1000000
    fmt.Println(millis) // 1257894000000
    fmt.Println(nanos)  // 1257894000000000000

    // Epoch integer to time.    // (seconds, nanoseconds)
    fmt.Println(time.Unix(0, 0)) // 1970-01-01 00:00:00 +0000 UTC
    fmt.Println(time.Unix(1, 1)) // 1970-01-01 00:00:01.000000001 +0000 UTC
}

Time formatting.

package main

import (
    "fmt"
    "time"
)

var p = fmt.Println

func main() {
    t := time.Now()
    p(t) // 2009-11-10 23:00:00 +0000 UTC

    // time.Format() works by giving an example.
    p(t.Format("2006-01-02T15:04:05Z"))             // 2009-11-10T23:00:00Z
    p(t.Format("2006-01-02T15:04:05.999999-07:00")) // 2009-11-10T23:00:00+00:00
    p(t.Format("Mon Jan _2 15:04:05 2006"))         // Tue Nov 10 23:00:00 2009
    p(t.Format("15:04:05"))                         // 23:00:00

    // Custom formatting.
    fmt.Printf("%d-%02d-%02dT%02d:%02d:%02d-00:00\n",
        t.Year(), t.Month(), t.Day(),
        t.Hour(), t.Minute(), t.Second()) // 2009-11-10T23:00:00-00:00

    // Parse by giving first the format, then the string.
    withNanos := "2006-01-02T15:04:05.999999999-07:00"
    t1, errParse := time.Parse(withNanos, "2012-11-01T22:08:41.117442+00:00")
    if errParse != nil {
        panic(errParse)
    }
    p(t1) // 2012-11-01 22:08:41.117442 +0000 UTC
    kitchen := "3:04PM"
    t2, errParse := time.Parse(kitchen, "8:41PM")
    if errParse != nil {
        panic(errParse)
    }
    p(t2) // 0000-01-01 20:41:00 +0000 UTC
    ansic := "Mon Jan _2 15:04:05 2006"
    _, errWillHappen := time.Parse(ansic, "8:41PM")
    p(errWillHappen) // ... cannot parse "8:41PM" as "Mon"

    // There are some predefined formats.
    p(t.Format(time.RFC3339)) // 2009-11-10T23:00:00Z
}

Time zones.

package main

import (
    "fmt"
    "time"
)

func main() {
    const shortForm = "2006-01-02"
    loc, _ := time.LoadLocation("America/Chicago")
    t, _ := time.ParseInLocation(shortForm, "2015-01-19", loc)
    fmt.Println(t.Format(time.RFC3339)) // => 2015-01-19T00:00:00-06:00
    fmt.Println(t.Format(time.RFC822))  // => 19 Jan 15 00:00 CST
}

Sources