This is a simple example that shows us how we could check if the time we have expired or not by comparing it to the current time. It assumes the time is in RFC3339 or ISO 8601 format.


Code


package main

import (
"fmt"
"time"
)

// expired accepts a `RFC3339`/`ISO 8601` formatted time and
// compares it to the current time to tell if it expired or not.
func expired(now time.Time, v string) (bool, error) {
fmt.Println("NOW:", now.Format(time.RFC3339))
fmt.Println("YOU:", v)

exp, err := time.ParseInLocation(time.RFC3339, v, now.Location())
if err != nil {
return false, err
}

if now.Sub(exp).Seconds() <= 0 {
return false, nil
}

return true, nil
}

func main() {
fmt.Println(expired(time.Now().UTC(), "2002-10-02T15:00:00Z"))
fmt.Println("------")
fmt.Println(expired(time.Now().UTC(), "2021-12-31T03:11:00Z"))
fmt.Println("------")
fmt.Println(expired(time.Now().UTC(), "2020-07-06T20:20:01+00:00"))
fmt.Println("------")
fmt.Println(expired(time.Now().UTC(), "2020-08-06T20:20:01+00:00"))
fmt.Println("------")
fmt.Println(expired(time.Now().UTC(), time.Now().Format(time.RFC3339)))
fmt.Println("------")
fmt.Println(expired(time.Now().UTC(), time.Now().UTC().Format(time.RFC3339)))
fmt.Println("------")
fmt.Println(expired(time.Now().UTC(), time.Now().Add(1*time.Minute).UTC().Format(time.RFC3339)))
fmt.Println("------")
fmt.Println(expired(time.Now(), time.Now().Add(1*time.Minute).UTC().Format(time.RFC3339)))
fmt.Println("------")
fmt.Println(time.Now().Zone())
fmt.Println(time.Now().Location())
fmt.Println(time.Now().UTC().Location())
}

Test


NOW: 2020-07-06T20:47:11Z
YOU: 2002-10-02T15:00:00Z
true
------
NOW: 2020-07-06T20:47:11Z
YOU: 2021-12-31T03:11:00Z
false
------
NOW: 2020-07-06T20:47:11Z
YOU: 2020-07-06T20:20:01+00:00
true
------
NOW: 2020-07-06T20:47:11Z
YOU: 2020-08-06T20:20:01+00:00
false
------
NOW: 2020-07-06T20:47:11Z
YOU: 2020-07-06T21:47:11+01:00
true
------
NOW: 2020-07-06T20:47:11Z
YOU: 2020-07-06T20:47:11Z
true
------
NOW: 2020-07-06T20:47:11Z
YOU: 2020-07-06T20:48:11Z
false
------
NOW: 2020-07-06T21:47:11+01:00
YOU: 2020-07-06T20:48:11Z
false
------
BST 3600
Local
UTC