// utc life
loc, _ := time.LoadLocation("UTC")

// setup a start and end time
createdAt := time.Now().In(loc).Add(1 * time.Hour)
expiresAt := time.Now().In(loc).Add(4 * time.Hour)

// get the diff
diff := expiresAt.Sub(createdAt)
fmt.Printf("Lifespan is %+v", diff)

 

---------------------------------------------------------------

Feb 16, 2019 · 175 words · 1 minute read
#DATE #TIME #ZERO #ISSET #NIL #TIMESTAMP #STATE
ADS VIA CARBON
0001-01-01 00:00:00 +0000 UTC
package main

import (
    "fmt"
    "time"
)

func main() {

    var myDate time.Time

    // IsZero returns a bool of whether a date has been set, but as the printf shows it will
    // still print a zero-based date if it hasn't been set.
    if myDate.IsZero() {
        fmt.Printf("No date has been set, %s\n", myDate)
    }

    // Demonstrating that by setting a date, IsZero now returns false
    myDate = time.Date(2019, time.February, 1, 0, 0, 0, 0, time.UTC)
    if !myDate.IsZero() {
        fmt.Printf("A date has been set, %s\n", myDate)
    }
}

golang 时间的比较,time.Time的初始值?

 

Use the time package to work with time information in Go.

Time instants can be compared using the Before, After, and Equal methods. The Sub method subtracts two instants, producing a Duration. The Add method adds a Time and a Duration, producing a Time.

Play example:

package main

import (
    "fmt"
    "time"
)

func inTimeSpan(start, end, check time.Time) bool {
    return check.After(start) && check.Before(end)
}

func main() {
    start, _ := time.Parse(time.RFC822, "01 Jan 15 10:00 UTC")
    end, _ := time.Parse(time.RFC822, "01 Jan 16 10:00 UTC")

    in, _ := time.Parse(time.RFC822, "01 Jan 15 20:00 UTC")
    out, _ := time.Parse(time.RFC822, "01 Jan 17 10:00 UTC")

    if inTimeSpan(start, end, in) {
        fmt.Println(in, "is between", start, "and", end, ".")
    }

    if !inTimeSpan(start, end, out) {
        fmt.Println(out, "is not between", start, "and", end, ".")
    }
}