-2

I want to show some RFC3339 time as seconds. I found how to parse times string, but it not that

t, _ := time.Parse(time.RFC3339, "2012-11-01T22:08:41+00:00") 
Ihor Bondartcov
  • 174
  • 1
  • 7
  • 1
    If the time is parsed correctly, what is the problem you're having? – JimB Oct 31 '17 at 14:15
  • 5
    Possible duplicate of [Obtaining a Unix Timestamp in Go Language (current time in seconds since epoch)](https://stackoverflow.com/questions/9539108/obtaining-a-unix-timestamp-in-go-language-current-time-in-seconds-since-epoch) – Jonathan Hall Oct 31 '17 at 14:15
  • @Flimzy: It has to be an exact duplicate. It has to be correct. `int32(time.Now().Unix())` is a bug since `Unix()` returns `int64`. See [Year 2038 problem](https://en.wikipedia.org/wiki/Year_2038_problem). – peterSO Oct 31 '17 at 14:36

1 Answers1

3

For example,

package main

import (
    "fmt"
    "time"
)

func main() {
    t, err := time.Parse(time.RFC3339, "2012-11-01T22:08:41+00:00")
    if err != nil {
        fmt.Println(err)
        return
    }
    fmt.Println(t)
    // Unix returns t as a Unix time,
    // the number of seconds elapsed since January 1, 1970 UTC.
    fmt.Println(t.Unix())
}

Playground: https://play.golang.org/p/LG6G4lMIWt

Output:

2012-11-01 22:08:41 +0000 UTC
1351807721
peterSO
  • 158,998
  • 31
  • 281
  • 276