Convert oracle time string to time.Time golang - go

I am new to golang and trying to convert string date received from oracle DB into time.Time in golang.
Here is the plaground link: https://play.golang.org/p/z3OyC4-DTFA
timeTest, err := time.Parse("22-JAN-06", "26-JAN-17")
if err != nil {
fmt.Printf("\n\npaymentDateAfter: %v\n\n", timeTest)
} else {
fmt.Printf("\n\npaymentDateErr: %v\n\n", err)
}
Can anybody help me understand the issue. I tried searching it and found many answers in stack overflow, but none of them for this format.
Thanks

As described in the docs, the layout time in time.Parse needs to represent the following year/month/day/etc...: Mon Jan 2 15:04:05 -0700 MST 2006
However, you are using Jan 22 2006.
If you change your code to the following, it works:
func main() {
test, err := time.Parse("2-Jan-2006", "26-MAR-2018")
if err != nil {
panic(err)
}
fmt.Println(test)
// Prints: 2018-03-26 00:00:00 +0000 UTC
}
Note: the month (Jan) in the layout is case sensitive, but the month (MAR) in the string to be parsed is case insensitive.

Related

Convert timestamp and timezone into RFC3339 format

I am taking timestamp from the user like this
2015-05-28T17:00:00
And a timezone "America/Los_Angeles"
Now I want convert the date into something like
2015-05-28T17:00:00-07:00
Is that possible in go ,Please help me out in this ,if you have any links which you can share
You can use ParseInLocation to parse datetime in specific location.
package main
import (
"fmt"
"time"
)
func main() {
loc, err := time.LoadLocation("America/Los_Angeles")
if err != nil {
panic(err)
}
// Note: without explicit zone, returns time in given location.
const shortForm = "2015-05-28T17:00:00"
t, err := time.ParseInLocation("2006-01-02T15:04:05", shortForm, loc)
if err != nil {
panic(err)
}
fmt.Println(t)
}
Its output is:
2015-05-28 17:00:00 -0700 PDT
"timezone" translates to time.Location in go. To load a location by name:
loc, err := time.LoadLocation("America/Los_Angeles")
Parsing:
to interpret the string as "that timestamp in that location":
t, err := time.ParseInLocation("2006-01-02T15:04:05", input, loc)
to interpret the string as "that timestamp in UTC":
t, err := time.Parse("2006-01-02T15:04:05", input)
Formatting:
to format t according to RFC3339 :
fmt.Println(t.Format(time.RFC3339))
t carries its own time.Location, you can also translate that timestamp to the timezone you see fit:
fmt.Println(t.In(loc).Format(time.RFC3339))
fmt.Println(t.UTC().Format(time.RFC3339))
https://go.dev/play/p/g2BgfdYGxU_I

Parsing a UTC ISO8601 time format in Golang

I'm being sent a date in the following format:
2021-05-09T12:10:00+01:00
Which is apparently a valid date format: https://en.wikipedia.org/wiki/ISO_8601
I'm attempting to parse that date in Go:
pt, err := time.Parse("2006-01-02T15:04:05+00:00", dt)
I've also tried to use time.RFC3339
But neither seem to pick up the timezone. In this case I get:
2021/05/10 21:02:02 http: panic serving [::1]:62125: parsing time "2021-05-09T12:10:00 01:00" as "2006-01-02T15:04:05+00:00": cannot parse " 01:00" as "+00:00"
The problem is your layout parameter,
"2006-01-02T15:04:05+00:00"
Instead of +00:00 you should have -07:00
This should help,
package main
import (
"fmt"
"time"
)
func main() {
date := "2021-05-09T12:10:00+01:00"
layout := "2006-01-02T15:04:05-07:00"
t, err := time.Parse(layout, date)
if err != nil {
fmt.Println(err)
}
fmt.Println(t)
}
Output: 2021-05-09 12:10:00 +0100 +0100
Playground: https://play.golang.org/p/UcrIDfJRcNV
Don't get confused as to why the timezone is showing up twice.
It's explained in this answer,
Golang time - time zone showing twice
The special layout parameter only accepts a certain set of valid numbers.
You can refer them here,
https://yourbasic.org/golang/format-parse-string-time-date-example/
You need to relay the timezone via the number -7:00 (not +1:00):
// pt, err := time.Parse("2006-01-02T15:04:05+1:00", dt) // not this
pt, err := time.Parse("2006-01-02T15:04:05-07:00", dt) // this
https://play.golang.org/p/n697vKUHSjD

Incorrect time-conversion in Go

I am trying to convert the time string "2020-02-01T12:30:00+01:00" (from the google calendar API) to time.Time format in Go, for some reason it keeps giving me "2020-01-01 12:30:00 +0000 UTC" as output (which is first of January, instead of first of February). Any idea what I'm doing wrong?
Thanks in advance!
package main
import (
"fmt"
"time"
"log"
)
func main() {
input := "2020-02-01T12:30:00+01:00"
output, err := StrToTime(input)
if err != nil{
log.Fatal(err)
}
fmt.Println(output)
}
func StrToTime(strDateTime string) (time.Time, error) {
layout := "2006-01-02T15:04:05+01:00"
t, err := time.Parse(layout, strDateTime)
if err != nil {
return time.Time{}, fmt.Errorf("could not parse datetime: %v", err)
}
return t, nil
}
It happens because you've specified the time offset portion wrong, it should be -07:00 not +01:00.
As of now it treats 01 as month portion, the second time, and overwrites the originally correctly parsed 02 as 01 (but not from the time offset part of the input).

Hour out of range on time.parse in golang

I am trying to parse a date time string in go. I pass the exact string as the format and get and error parsing time "01/31/2000 12:59 AM": hour out of range.
I am getting that string from an input. How can I make this work?
Here is the code (https://play.golang.org/p/Kg9KfFpU2z)
func main() {
layout := "01/31/2000 12:59 AM"
if t, err := time.Parse(layout, "01/31/2000 12:59 AM"); err == nil {
fmt.Println("Time decoded:", t)
} else {
fmt.Println("Failed to decode time:", err)
}
}
Based on your shared code, you should change the layout to 01/02/2006 03:04 AM to fix it:
Note:
If you have 24 hours format, you should change the hour part in layout to 15 instead of 03 and also to get rid of AM part e.g. 01/02/2006 15:04
package main
import (
"fmt"
"time"
)
func main() {
layout := "01/02/2006 03:04 AM"
if t, err := time.Parse(layout, "01/31/2000 12:59 AM"); err == nil {
fmt.Println("Time decoded:", t)
} else {
fmt.Println("Failed to decode time:", err)
}
}
Here is a good article that would help you to understand different layouts.
Your format needs to use a very specific date and time, see the docs:
https://golang.org/pkg/time/#example_Parse
Parse parses a formatted string and returns the time value it
represents. The layout defines the format by showing how the reference
time, defined to be
Mon Jan 2 15:04:05 -0700 MST 2006
So you need https://play.golang.org/p/c_Xc_R2OHb

Not able to parse time properly

I'm trying to parse time for values in a template like so:
"parseDate": func(timeStamp time.Time) string {
newTime, err := time.Parse("Jan 2 2006 # 15:04:05", fmt.Sprintf("%v", timeStamp))
if err != nil {
log.Println(err)
}
return fmt.Sprintf("%v", newTime)
},
which is one of my handler funcs, but I get this error:
parsing time "2015-12-13 06:49:52 +0000 UTC" as "Jan 2 2006 # 15:04:05": cannot parse "2015-12-13 06:49:52 +0000 UTC" as "Jan"
Not sure what I'm doing wrong
You have to parse it as
t, _ := time.Parse("2006-01-02 15:04:05 -0700 MST")
For parsing you have to give the format of the date you are receiving.
Then you can format the correctly parsed time using
t.Format("Jan 2 2006 # 15:04:05")

Resources