Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Parse Go time stamp using Go

Tags:

time

parsing

go

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

like image 498
Max Avatar asked Dec 30 '12 20:12

Max


People also ask

How do you parse time in go?

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.

How do you add time on go?

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.

What is the type of time now () in Golang?

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.

Which is valid go time format literal?

Golang Time Format YYYY-MM-DD.


2 Answers

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.

like image 169
Alexander Bauer Avatar answered Sep 30 '22 23:09

Alexander Bauer


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

like image 37
topskip Avatar answered Sep 30 '22 23:09

topskip