How to parse a RFC3339 in Go? - go

My date looks like this
2019-03-29T09:32:52Z
Please help me parse it using Go's standard time package

Nevermind, I just had the wrong layout
package main
import (
"fmt"
"time"
)
func main() {
layout := "2006-01-02T15:04:05Z07:00"
t, err := time.Parse(layout, "2019-03-29T09:32:52Z")
fmt.Println(t, err)
}

Related

golang timestamp in RFC3339 format

how do we convert time.now() in time.Time( RFC3339) format?
Eg:
var t time.Time
timeNow= time.Now()
I want to assign timeNow to t
Golang has support for various time formats.
package main
import (
"fmt"
"time"
)
func main() {
t := time.Now()
r := t.Format(time.RFC3339)
fmt.Println("time.Now() ", t)
fmt.Println("RFC3339 ", r)
}

how to time.Parse() comma separate milliseconds in go

I have logs with timestamps that look like "2020-05-08 22:02:00,845". They have comma separated milliseconds, which is what is giving time.Parse issues. I can't seem to figure out how to make time.Parse happy with it. Here is sample code that produces the error in go version go1.13.4 darwin/amd64 (and in the playground linked below);
package main
import (
"time"
)
func main() {
ts := "2020-05-08 22:02:00,845"
_, err := time.Parse("2006-01-02 15:04:05,000", ts)
print(err.Error())
}
Running that code produces this error
parsing time "2020-05-08 22:02:00,845" as "2006-01-02 15:04:05,000": cannot parse "845" as ",000"
Here a link to the code in the go playground
So what would a format look like to parse this? Thanks for your help.
It's a bug already filed here
The behaviour is documented as such: "A fractional second is represented by adding a period and zeros to the end of the seconds section of layout string, as in "15:04:05.000" to format a time stamp with millisecond precision.
Workaround for that would be replacing "," with "."
package main
import (
"time"
"fmt"
"strings"
)
func main() {
ts := "2020-05-08 22:02:00,845"
ts = strings.Replace(ts, ",", ".", -1)
d, err := time.Parse("2006-01-02 15:04:05.000", ts)
if err != nil{
print(err.Error())
}
fmt.Println(d)
}
here is the Playground

How to parse this date 2018-10-22T2250?

How to parse this strange datetime 2018-10-22T2250 in golang?
I couldn't find date layout
You can create your own custom format. In production, you should also handle the error.
package main
import (
"fmt"
"time"
)
func main() {
timeString := "2018-10-22T2250"
timeFormat := "2006-01-02T1504"
t, _ := time.Parse(timeFormat, timeString)
fmt.Println(t)
}
Playground link
This returns the time in UTC. You may need to adjust to another timezone, depending on your source.
//init the location
loc, _ := time.LoadLocation("Asia/Shanghai")
//localize the time
localTime := t.In(loc)

Go time.Parse() getting "month out of range" error

I'm new to Go and I was creating a little console script. You can check my code here:
package main
import (
"bufio"
"fmt"
"os"
"time"
)
func main() {
reader := bufio.NewReader(os.Stdin)
fmt.Println("Calculate")
fmt.Print("Hours and minutes: ")
start, _, _ := reader.ReadLine()
begin, err := time.Parse("2016-12-25 00:00:00", "2016-12-25 "+string(start)+":00")
if err != nil {
fmt.Println(err)
}
fmt.Println(begin)
}
I've seen a related question but I couldn't understand why.
This is the error I'm getting after running my code:
parsing time "2016-12-25 22:40:00": month out of range
0001-01-01 00:00:00 +0000 UTC
Any ideas on what am I doing wrong?
Thanks
You're using the wrong reference time in the layout parameter of time.Parse which should be Jan 2, 2006 at 3:04pm (MST)
Change your begin line to the following and it will work:
begin, err := time.Parse("2006-01-02 15:04:05", "2016-12-25 "+string(start)+":00")
func Parse
To avoid having to remember the special date, I usually wrap the logic in a
function:
package main
import (
"fmt"
"time"
)
func parseDate(value string) (time.Time, error) {
layout := time.RFC3339[:len(value)]
return time.Parse(layout, value)
}
func main() {
start := "15:04"
d, e := parseDate("2016-12-25T" + start)
if e != nil {
panic(e)
}
fmt.Println(d)
}

How to scan a big.Int from standard input in Go

Is there a way to scan a big.Int directly from the standard input in Go? Right now I'm doing this:
package main
import (
"fmt"
"math/big"
)
func main() {
w := new(big.Int)
var s string
fmt.Scan(&s)
fmt.Sscan(s, w)
fmt.Println(w)
}
I also could have used .SetString. But, is there a way to Scan the big.Int directly from the standard input without scanning a string or an integer first?
For example,
package main
import (
"fmt"
"math/big"
)
func main() {
w := new(big.Int)
n, err := fmt.Scan(w)
fmt.Println(n, err)
fmt.Println(w.String())
}
Input (stdin):
295147905179352825857
Output (stdout):
1 <nil>
295147905179352825857
As far as I know - no, there's no other way. In fact, what you've got is the default example they have for scanning big.Int in the documentation.
package main
import (
"fmt"
"log"
"math/big"
)
func main() {
// The Scan function is rarely used directly;
// the fmt package recognizes it as an implementation of fmt.Scanner.
i := new(big.Int)
_, err := fmt.Sscan("18446744073709551617", i)
if err != nil {
log.Println("error scanning value:", err)
} else {
fmt.Println(i)
}
}
You can see the relevant section here - http://golang.org/pkg/math/big/#Int.Scan

Resources