Go
prints time with
time.Now().String()
as
2012-12-18 06:09:18.6155554 +0200 FLEST
or
2009-11-10 23:00:00 +0000 UTC
http://play.golang.org/p/8qwq9U_Ri5
How do I parse it?
I guess FLEST
is Finland Latvian Estonian Standard Time
I am not in these countries and I guess I can get all kind of time zones.
I can't find one unified way or pattern to parse it with time.Parse
We can parse any time by using time. Parse function which takes our time string and format in which our string is written as input and if there is no error in our format, it will return a Golang time object.
Add() function in Go language is used to add the stated time and duration. Moreover, this function is defined under the time package. Here, you need to import the “time” package in order to use these functions. Here, “t” is the stated time and “d” is the duration that is to be added to the time stated.
The Now function returns the current local time. The Format function returns a textual representation of the time value formatted according to layout. The layout is either a predefined constant value or a specific format of the reference datetime: Mon Jan 2 15:04:05-0700 MST 2006 . We use Go version 1.18.
Golang Time Format YYYY-MM-DD.
Though time.Parse()
accepts a format string such as 2006-01-02 15:04:05 -0700 MST
, it may be simpler to use one of the constants defined in time.
const (
ANSIC = "Mon Jan _2 15:04:05 2006"
UnixDate = "Mon Jan _2 15:04:05 MST 2006"
RubyDate = "Mon Jan 02 15:04:05 -0700 2006"
RFC822 = "02 Jan 06 15:04 MST"
RFC822Z = "02 Jan 06 15:04 -0700" // RFC822 with numeric zone
RFC850 = "Monday, 02-Jan-06 15:04:05 MST"
RFC1123 = "Mon, 02 Jan 2006 15:04:05 MST"
RFC1123Z = "Mon, 02 Jan 2006 15:04:05 -0700" // RFC1123 with numeric zone
RFC3339 = "2006-01-02T15:04:05Z07:00"
RFC3339Nano = "2006-01-02T15:04:05.999999999Z07:00"
Kitchen = "3:04PM"
// Handy time stamps.
Stamp = "Jan _2 15:04:05"
StampMilli = "Jan _2 15:04:05.000"
StampMicro = "Jan _2 15:04:05.000000"
StampNano = "Jan _2 15:04:05.000000000"
)
If you're using the strings as a way to store or encode time, (such as with a restrictive encoding format,) you may want to consider using Unix time. That way, you could just store an int64
(or two, if you keep the number of nanoseconds.
package main
import (
"fmt"
"time"
)
func main() {
fmt.Println(time.Now())
date := "2009-11-10 23:00:00 +0000 UTC"
t, err := time.Parse("2006-01-02 15:04:05 -0700 MST", date)
if err != nil {
fmt.Println("parse error", err.Error())
}
fmt.Println(t.Format(time.ANSIC))
}
Playground: http://play.golang.org/p/hvqBgtesLd
See the source code at http://golang.org/src/pkg/time/format.go?s=15404:15450#L607
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With