Is there a library or function in the included libraries that converts an RFC timestamp to Unix time (or another format that I can then format into Unix time?)
For example, I'd like to change this Tue Sep 16 21:58:58 +0000 2014
to a Unix timestamp.
For example,
package main
import (
"fmt"
"time"
)
func main() {
s := "Tue Sep 16 21:58:58 +0000 2014"
const rfc2822 = "Mon Jan 02 15:04:05 -0700 2006"
t, err := time.Parse(rfc2822, s)
if err != nil {
fmt.Println(err)
return
}
u := t.Unix()
fmt.Println(u)
f := t.Format(time.UnixDate)
fmt.Println(f)
}
Output:
1410904738
Tue Sep 16 21:58:58 +0000 2014
References:
RFC2822: 3.3. Date and Time Specification
NOTE:
There is a package time
format constant named RubyDate
; it's a misnomer.
The Go authors were misled by Go Issue 518 which claimed that Ruby Time.now
outputs Tue Jan 12 02:52:59 -0800 2010
. However,
#!/usr/bin/env ruby
print Time.now
print "\n"
Output:
2014-09-20 19:40:32 -0400
Later, the issue was revised to say that Tue Jan 12 02:52:59 -0800 2010
was the date format used by the Twitter API. In the beginning, in the "Fail Whale" days, Twitter used Ruby-on-Rails, which may be why they assumed it was a Ruby date format.
I didn't use the time.RubyDate
constant in my example since it's misleading. A constant named rfc2822
provides better documentation.
References:
Go: Issue 518: time.Parse - numeric time zones and spacing