time.ParseInLocation, location is nil - go

I am parsing a location like so:
estLocation, err := time.LoadLocation("America/New_York")
d, err := time.ParseInLocation(time.RFC3339, fmt.Sprintf("%sT%s:00.000Z", c.Date, c.Open), estLocation)
When I examine d after parsing, the time part looks fine (i.e. the string is parsed into the time I expect), but the location is nil.
Why is this? I need the time parsed as a new york time (so that I can call .UTC() on it and transform it into UTC time.

The Z at the end of your date string means "zulu", i.e. UTC time; so your time is explicitly indicating it's in UTC. See tools.ietf.org/html/rfc3339#section-2
If you pass a valid offset, you get a valid result: https://play.golang.org/p/QW8M3_eznDL
d, err := time.ParseInLocation(time.RFC3339, fmt.Sprintf("%sT%s:00.000+05:00", "2001-01-01", "12:34"), estLocation)
The docs indicate this accordingly (emphasis mine):
in the absence of time zone information, Parse interprets a time as UTC; ParseInLocation interprets the time as in the given location
https://golang.org/pkg/time/#ParseInLocation

Related

Work out if 2022-01-14T20:56:55Z is a valid date time in Go

I am attempting to create a function that tells me if a timestamp is valid or not.
My function looks like
// IsTimestamp checks if a string contains a timestamp.
func IsTimestamp(str string) bool {
_, err := time.Parse("2006-01-02 15:04:05.999", str)
if err != nil {
return false
}
return true
}
However, passing in 2022-01-14T20:56:55Z returns false when is it a valid timestamp.
I'm thinking this might be something to do with the layout I am using in time.Parse but I've tried just using the date with no luck.
Your layout doesn't match your input string, so it's expected that it isn't parsed successfully.
The docs say:
Parse parses a formatted string and returns the time value it represents. See the documentation for the constant called Layout to see how to represent the format. The second argument must be parseable using the format string (layout) provided as the first argument.
Therefore, you should use a layout that's matching your input. Below, I am using RFC3339, which is the layout of your input string.
if _, err := time.Parse(time.RFC3339, str); err != nil {
...
}
https://go.dev/play/p/_Q26NS2wwfy
2022-01-14T20:56:55Z does not match the layout 2006-01-02 15:04:05.999 because:
the layout expects a whitespace after day, not T
the layout expects exactly three digits for milliseconds (only two are given 55)
the layout does not expect the timezone to be specified. Z is not a valid.
You can match 2022-01-14T20:56:55Z with the layout 2006-01-02T15:04:05.99Z, or 2006-01-02T15:04:05Z. Or even better, use The Fool's answer.

Go parse time to string and back

I am trying to do something in Go that is very simple in languages like Java
I want to parse current time to string and then parse it back to time.
This is the code I tried but as can be seen here it gives unexpected results.
I am facing two problems
time.Now().String() gives a wrong date
If I cast the time to string
and cast it back to time, it gives a totally different date.
What is the right (and easy) way to do this?
p := fmt.Println
startStr := time.Now().String() //2009-11-10 23:00:00 +0000 UTC m=+0.000000001
p(startStr)
startTime, _ := time.Parse(
"2009-11-10 23:00:00 +0000 UTC m=+0.000000001",
startStr)
p(startTime) //0001-01-01 00:00:00 +0000 UTC
time.Now().String() is meant for debugging only (see go doc).
You should instead use time.Format().
For example:
p := fmt.Println
now := time.Now().Format(time.RFC3339)
p(now)
parsed, _ := time.Parse(time.RFC3339, now)
p(parsed.Format(time.RFC3339))
produces:
2009-11-10T23:00:00Z
2009-11-10T23:00:00Z
Your other concern regarding time.Now().String() gives a wrong date is likely due to where you're running the code. e.g. if you're running in "The Go Playgounrd", then the time won't be accurate. You should run it on your own computer, and assuming your computer has the correct time, then you should get the right time printed.
Unlike some other languages, Go does not treat String() as a de facto marshaling method -- instead, it's meant just to print the value out for debugging purposes. You could parse back from that format into a Time if you used a proper format string; however, a proper format string must be for the exact time of Mon Jan 2 15:04:05 MST 2006, not any time; but the format that String() prints out isn't captured by a constant within the Time package so it's probably not worth doing.
Instead, however, what you're trying to do may be better captured by the MarshalText and UnmarshalText methods:
startStr, _ := time.Now().MarshalText()
fmt.Println(string(startStr)) // 2009-11-10T23:00:00Z
startTime := new(time.Time)
startTime.UnmarshalText(startStr)
fmt.Println(startTime) // 2009-11-10 23:00:00 +0000 UTC
The time in the playground is fixed, it is always the date and time of
the Go announcement.
https://github.com/golang/go/issues/10663
So to play with time correctly, you need to run it on your local.
About the parsing time to string or back, you have to pass the format of time string:
For example:
package main
import (
"fmt"
"time"
)
func main() {
current := time.Now()
fmt.Println("Init Time:", current.String())
timeCustomFormatStr := current.Format("2006-01-02 15:04:05 -0700")
fmt.Println("Custom format", timeCustomFormatStr)
parsedTime, err := time.Parse("2006-01-02 15:04:05 -0700",timeCustomFormatStr)
if err != nil {
fmt.Println(err)
return
}
fmt.Println("parsedTime From Custom:", parsedTime)
timeFormatRFC3339 := current.Format(time.RFC3339)
fmt.Println("RFC3339 format", timeFormatRFC3339)
parsedTimeRFC3339, err := time.Parse(time.RFC3339,timeFormatRFC3339)
if err != nil {
fmt.Println(err)
return
}
fmt.Println("parsedTime From Custom:", parsedTimeRFC3339)
}
Ref:
1 https://golang.org/pkg/time/#Time.Format

Convert timestamp to ISO format in golang

I'm trying to convert the timestamp 2018-12-17T15:03:49.000+0000 to ISO format in golang, but am getting an error cannot parse "+0000" as "Z07:00"
This is what I tried
ts, err := time.Parse(time.RFC3339, currentTime)
Any ideas?
Beware, a long answer ahead
(tl;dr) use:
ts, err := time.Parse("2006-01-02T15:04:05-0700", currentTime)
ts.Format(time.RFC3339)
I really like go documentation, and you should do :)
All from https://golang.org/pkg/time/#pkg-constants
RFC3339 = "2006-01-02T15:04:05Z07:00"
Some valid layouts are invalid time values for time.Parse, due to
formats such as _ for space padding and Z for zone information
Which means you can't parse +0000 with layout Z07:00.
Also:
The reference time used in the layouts is the specific time:
Mon Jan 2 15:04:05 MST 2006
which is Unix time 1136239445. Since MST is GMT-0700, the reference
time can be thought of as
01/02 03:04:05PM '06 -0700
You can either parse numeric time zone offsets format as follows:
-0700 ±hhmm
-07:00 ±hh:mm
-07 ±hh
Or replacing the sign in the format with a Z:
Z0700 Z or ±hhmm
Z07:00 Z or ±hh:mm
Z07 Z or ±hh
fraction:
From this go example https://play.golang.org/p/V9ubSN6gTdG
// If the fraction in the layout is 9s, trailing zeros are dropped.
do("9s for fraction", "15:04:05.99999999", "11:06:39.1234")
So you can parse it like:
ts, err := time.Parse("2006-01-02T15:04:05.999-0700", currentTime)
Also, From the doc
A decimal point followed by one or more zeros represents a fractional
second, printed to the given number of decimal places. A decimal point
followed by one or more nines represents a fractional second, printed
to the given number of decimal places, with trailing zeros removed.
When parsing (only), the input may contain a fractional second field
immediately after the seconds field, even if the layout does not
signify its presence. In that case a decimal point followed by a
maximal series of digits is parsed as a fractional second.
Which means you can leave out the decimal points from the layout and it will parse correctly
ts, err := time.Parse("2006-01-02T15:04:05-0700", currentTime)
For getting the time in UTC simply write ts.UTC()
And for formatting it to RFC3339, you can use
ts.Format(time.RFC3339)
Example
currentTime := "2018-12-17T17:02:04.123+0530"
ts, err := time.Parse("2006-01-02T15:04:05-0700", currentTime)
if err != nil {
panic(err)
}
fmt.Println("ts: ", ts)
fmt.Println("ts in utc: ", ts.UTC())
fmt.Println("RFC3339: ", ts.Format(time.RFC3339))
// output
// ts: 2018-12-17 17:02:04.123 +0530 +0530
// ts in utc: 2018-12-17 11:32:04.123 +0000 UTC
// RFC3339: 2018-12-17T17:02:04+05:30
playground: https://play.golang.org/p/vfERDm_YINb
How about this?
ts, err := time.Parse("2006-01-02T15:04:05.000+0000", currentTime)
since time.RFC3339 is just 2006-01-02T15:04:05Z07:00

Parse time zone into a Location struct in Go

Given a time zone such as EDT or CEST is there a way to get a time.Location reference to use it to with func (t Time) In(loc *Location) Time?
It is possible to initialize the location for e.g. CEST with time.LoadLocation("Europe/Berlin") but how to do the same for the actual time zone notation?
Given the very insightful comment by #Svip is there any sensible way to return a list of representative location? That is for WET return e.g. [Europe/London, Atlantik/Reykjavík]. All other WET locations would follow the same time zone arrangements as one of those two.
There is option to parse time for given Location.
(https://golang.org/pkg/time/#LoadLocation)
type CustomTime struct {
time.Time
}
const ctLayout = "Jan 2, 2006 at 3:04pm (MST)"
func (ct *CustomTime) UnmarshalJSON(b []byte) (err error) {
s := strings.Trim(string(b), "\"")
if s == "null" {
ct.Time = time.Time{}
return
}
location, err := time.LoadLocation("Local")
if err != nil {
return err
}
ct.Time, err = time.ParseInLocation(ctLayout, s, location)
return err
}
There exists a package github.com/tkuchiki/go-timezone that provides mapping between zone abbreviations and locations. See its timezones.go.
However, as commenters also pointed out, abbreviated timezone names are ambiguous and it is better to avoid user input with such names at all. As mentioned in other questions (Why doesn't Go's time.Parse() parse the timezone identifier? and How to properly parse timezone codes), when parsing time, Go correctly parses abbreviated timezone when it matches local timezone of the machine running code and UTC timezone. All others are not parsed correctly in my experience.

Golang time error: month out of range

Here is my code:
time.Parse(time.Now().String()[0:19],time.Now().String()[0:19])
error:
parsing time "2016-09-20 16:50:08": month out of range
How to parse time string?
First param is layout, see:
func Parse(layout, value string) (Time, error) {
return parse(layout, value, UTC, Local)
}
Docs:
// 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
// would be interpreted if it were the value; it serves as an example of
// the input format. The same interpretation will then be made to the
// input string.
//
// Predefined layouts ANSIC, UnixDate, RFC3339 and others describe standard
// and convenient representations of the reference time. For more information
// about the formats and the definition of the reference time, see the
// documentation for ANSIC and the other constants defined by this package.
// Also, the executable example for time.Format demonstrates the working
// of the layout string in detail and is a good reference.
//
// Elements omitted from the value are assumed to be zero or, when
// zero is impossible, one, so parsing "3:04pm" returns the time
// corresponding to Jan 1, year 0, 15:04:00 UTC (note that because the year is
// 0, this time is before the zero Time).
// Years must be in the range 0000..9999. The day of the week is checked
// for syntax but it is otherwise ignored.
//
// In the absence of a time zone indicator, Parse returns a time in UTC.
//
// When parsing a time with a zone offset like -0700, if the offset corresponds
// to a time zone used by the current location (Local), then Parse uses that
// location and zone in the returned time. Otherwise it records the time as
// being in a fabricated location with time fixed at the given zone offset.
//
// No checking is done that the day of the month is within the month's
// valid dates; any one- or two-digit value is accepted. For example
// February 31 and even February 99 are valid dates, specifying dates
// in March and May. This behavior is consistent with time.Date.
//
// When parsing a time with a zone abbreviation like MST, if the zone abbreviation
// has a defined offset in the current location, then that offset is used.
// The zone abbreviation "UTC" is recognized as UTC regardless of location.
// If the zone abbreviation is unknown, Parse records the time as being
// in a fabricated location with the given zone abbreviation and a zero offset.
// This choice means that such a time can be parsed and reformatted with the
// same layout losslessly, but the exact instant used in the representation will
// differ by the actual zone offset. To avoid such problems, prefer time layouts
// that use a numeric zone offset, or use ParseInLocation.
You may use
t, err := time.Parse("2006-01-02 15:04:05", time.Now().String()[:19])
Try on The Go Playground:
package main
import (
"fmt"
"time"
)
func main() {
t, err := time.Parse("2006-01-02 15:04:05", time.Now().String()[:19])
if err != nil {
panic(err)
}
fmt.Println(t)
}
output:
2009-11-10 23:00:00 +0000 UTC
I had the same problem, so I came here to say golang will some times mean "month" they meant "DAY OF THE MONTH", the error message is wrong, here is an example:
package main
import (
"fmt"
"time"
)
func main() {
dateAsString:= "31/Oct/2019"
layout := "01/Jan/2006" // BAD BAD BAD SHOULD BE 02 INSTEAD OF 01
fmt.Println("INPUT:" + dateAsString)
t, err := time.Parse(layout, dateAsString)
if err != nil {
fmt.Println("DATE UNPARSEABLE:3", err)
}
fmt.Println(t)
}

Resources