Golang time error: month out of range - go

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)
}

Related

Subtracting time to get age

My aim is to calculate the age of the pod by doing the subtraction of "current_time - pod_creation_time" so that I will get the age, I am getting creation time from metadata but it's in the format "2021-07-13 16:34:22 +0530 IST", so when I trying to subtract it from time.Now(), I am getting parsing error like below:
invalid operation: "t2 : " + t2 (mismatched types string and time.Time)
Anyone could please help how to have creation time "2021-07-13 16:34:22 +0530 IST" from metadata in the proper format so that I can do "time.Now - (creation time)"
I tried some workaround like below:
creatTime, err := time.Parse("2006-01-02 15:04:05 -0700 MST",
pod.ObjectMeta.CreationTimestamp.String())
and then subtracted creationTime from Current Time. It works, but I think this is not the right way.
There's a type mismatch as time.Now() return the current time stored in the type time.Time whereas 2021-07-13 16:34:22 +0530 IST is a string. You can perform the required subtraction operation on mismatched types i.e., time.Time and string.
You have to parse the string by specifying the layout. I'd recommend reading the time package's doc.
I've explained every operation in the sample code below; I hope it helps. If you understand this, you can also then look at helper functions like time.Since that can help you write the same program in fewer lines.
package main
import (
"fmt"
"time"
)
func main() {
// K8s timestamp
t := "2021-07-13 16:34:22 +0530 IST"
// Format of K8s timestamp
format := "2006-01-02 15:04:05 -0700 MST" // Mon Jan 2 15:04:05 -0700 MST 2006
// Parse the timestamp so that it's stored in time.Time
cur, err := time.Parse(format, t)
if err != nil {
panic(err)
}
// Current time
now := time.Now()
// As both are of type time.Time, it's subtractable
dur := now.Sub(cur)
// Print duration
fmt.Println(dur)
// Print duration (in seconds)
fmt.Println(dur.Seconds())
}
Also, I'd like you to learn how to write questions on StackOverflow. The formatting of your question is pretty bad. When seeking good solutions; it is the OP's duty to post the question correctly first so that everybody could understand it and then expect answers.
Read: https://stackoverflow.com/help/how-to-ask

Is the UTC() call redundant in rand.Seed(time.Now().UTC().UnixNano())?

Many examples on the Internet use rand.Seed(time.Now().UTC().UnixNano()) to initialise the pseudo random number generator seed.
I see that if I omit the UTC() call, it still works fine.
Unix (or UnixNano) time is anyway the number of seconds (or milliseconds) since the epoch, that is, 1970-01-01 00:00:00.000000000 UTC. Unix or UnixNano time is anyway timezone-agnostic.
As an example, take the following code:
package main
import (
"fmt"
"time"
)
func main() {
t := time.Now()
fmt.Println(t.UnixNano())
fmt.Println(t.UTC().UnixNano())
}
So my question is: Is there any purpose to UTC() call or is it safe to omit the UTC() call and just call rand.Seed(time.Now().UnixNano()) instead?
It is safe to say you can omit the UTC() when using UnixNano()
First see the code of UTC() in time.go:1107:
// UTC returns t with the location set to UTC.
func (t Time) UTC() Time {
t.setLoc(&utcLoc)
return t
}
It only sets the Location of the current Time.
Now, according to the comment on the In() methode in the time.go file the Location info are only for "display purposes". See time.go:1119:
// In returns a copy of t representing the same time instant, but
// with the copy's location information set to loc for display
// purposes.
//
// In panics if loc is nil.
func (t Time) In(loc *Location) Time {
if loc == nil {
panic("time: missing Location in call to Time.In")
}
t.setLoc(loc)
return t
}
The Location is only used if the Time has to be displayed:
// abs returns the time t as an absolute time, adjusted by the zone offset.
// It is called when computing a presentation property like Month or Hour.
func (t Time) abs() uint64 {
l := t.loc
// Avoid function calls when possible.
if l == nil || l == &localLoc {
l = l.get()
}
sec := t.unixSec()
if l != &utcLoc {
if l.cacheZone != nil && l.cacheStart <= sec && sec < l.cacheEnd {
sec += int64(l.cacheZone.offset)
} else {
_, offset, _, _ := l.lookup(sec)
sec += int64(offset)
}
}
return uint64(sec + (unixToInternal + internalToAbsolute))
}
Run the following code to see the difference. Both are based on the same UnixNano, only the hour changes, since the location is only applied before printing:
var now = time.Now()
var utc = now.UTC()
fmt.Printf("now UnixNano: %d, Hour: %d, Minute: %d, Second: %d\n", now.UnixNano(), now.Hour(), now.Minute(), now.Second())
fmt.Printf("utc UnixNano: %d, Hour: %d, Minute: %d, Second: %d\n", utc.UnixNano(), utc.Hour(), utc.Minute(), utc.Second())
now UnixNano: 1595836999431598000, Hour: 10, Minute: 3, Second: 19
utc UnixNano: 1595836999431598000, Hour: 8, Minute: 3, Second: 19
You set the pseudo-random number generator seed, to make generated numbers difficult to guess.
When you look at UTC() method documentation, you'll see that only thing it does is sets location (timezone). It is irrelevant which timezone is used for random seed generation.
What is important, is that UnixNano() is used, and that platform would actually return time with such precision. Otherwise, the random seed might be guessed, which may allow for: random number generator attack
Please consider a safer way to initialize random seed generator in answer: https://stackoverflow.com/a/54491783/5279383
Time.UnixNano() returns the Unix time of the source time, the number of nanoseconds elapsed since January 1, 1970 UTC. It is always interpreted in UTC zone, it doesn't matter what location the source time has. The unix time is zone-independent. Its documentation clearly states this:
The result does not depend on the location associated with t.
So you do not need to call Time.UTC(), you will get the same result.
See this example:
t1, err := time.Parse("2006-01-02 15:04:05 -0700", "2020-07-27 13:50:00 +0200")
if err != nil {
panic(err)
}
fmt.Printf("%v\n\t%v\n\t%v\n", t1, t1.UnixNano(), t1.UTC().UnixNano())
t2, err := time.Parse("2006-01-02 15:04:05 -0700", "2020-07-27 13:50:00 +0000")
if err != nil {
panic(err)
}
fmt.Printf("%v\n\t%v\n\t%v\n", t2, t2.UnixNano(), t2.UTC().UnixNano())
We parse 2 input times, once in a non-UTC zone, and another in UTC zone. We print UnixNano() for both, with and without calling UTC(). The results are identical.
Output (try it on the Go Playground):
2020-07-27 13:50:00 +0200 +0200
1595850600000000000
1595850600000000000
2020-07-27 13:50:00 +0000 UTC
1595857800000000000
1595857800000000000
Is there any purpose to UTC() call - YES
is it safe to omit the UTC() - YES

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.

Go: How to parse only date to time.Time?

I want to parse only date value to time.Time.
For example I have date in this format: 2016-03-31, and I want to parse it, like: time.Parse(FORMAT, "2016-03-31").
But it always fail.
What is the correct format string to use to parse only date with this format?
I have the code below as example, it is on playground also: https://play.golang.org/p/0MNLr9emZd
package main
import (
"fmt"
"time"
)
var dateToParse = "2016-03-31"
func main() {
format := "2006-12-01"
parseDate(format)
}
func parseDate(format string) {
t, err := time.Parse(format, dateToParse)
if err != nil {
fmt.Println("Format:", format)
fmt.Println(err)
fmt.Println("")
return
}
fmt.Println("Works Format:", format)
fmt.Println(t)
fmt.Println("")
}
The output is this:
Format: 2006-12-01
parsing time "2016-03-31" as "2006-12-01": cannot parse "-31" as "2"
Package time
These are predefined layouts for use in Time.Format and Time.Parse.
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
To define your own format, write down what the reference time would
look like formatted your way; see the values of constants like ANSIC,
StampMicro or Kitchen for examples.
Use format := "2006-01-02" for yyyy-mm-dd.
The new format DateOnly = "2006-01-02" of format.go will be added in the Go next release (1.20) per proposal time: add DateTime, DateOnly, TimeOnly format constants and commit
time.Parse(time.DateOnly, dateToParse)

parse time string type back to time type error

package main
import "fmt"
import "time"
func main() {
source := "2014-04-22 23:41:12.518845115 +0800 CST"
Form := "2014-04-22 23:41:12.518845115 +0800 CST"
t, err := time.Parse(Form, source)
if err == nil {
fmt.Println(t.String())
} else {
fmt.Println(err)
}
}
Error :parsing time "2014-04-22 23:41:12 +0800 CST": month out of range
I get source by time.Now().String(), but I could not convert it back. What's wrong with this piece of code?
From the documentation:
Parse parses a formatted string and returns the time value it
represents. The layout defines the format by showing how the reference
time,
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.
(Bolding mine).
So what you want is
Form := "2006-01-02 15:04:05.000000000 -0700 MST"
Which is the date listed in that quote in the format of your input string. One thing to note while I was writing this on the playground to confirm is that it looks like on the part 05.000000000 (the seconds and fractions of seconds) you need the format string to contain exactly as many decimal points as the string you want to parse.
Here's a playground version showing it works: http://play.golang.org/p/dRniJbqgl7

Resources