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
Related
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
This question already has answers here:
Parsing RFC-3339 / ISO-8601 date-time string in Go
(8 answers)
Closed 2 years ago.
I have a string that has a timestamp in the format
"2021-02-04 23:45:00" but when I try and parse this with time.parse it seemingly cuts off the year part.
The code is
case "period_end":
fmt.Println(record[i])
ts, err := time.Parse("2021-02-04 23:45:00", record[i])
if err != nil {
log.Printf("Time conversion failed: %v", err)
return
}
reading.Interval = t
where record[i] at this point is a string with
2021-02-04 00:15:00
and reading.Interval is time.Time
The error returned in the Printf is
Time conversion failed: parsing time "2021-02-04 00:15:00" as "2021-02-04 23:45:00": cannot parse "-02-04 00:15:00" as "1"
which I can't find in any search I've done. What am I missing here?
Replace the first parameter in time.Parse:
from
"2021-02-04 23:45:00"
to
"2006-01-02 15:04:00"
Golang uses a specific date for formatting, no idea why https://golang.org/src/time/format.go
Go uses this default time for setting up the layout:
"2006-01-02T15:04:05.000Z"
More info for this layout:
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
So to solve your problem:
package main
import (
"fmt"
"time"
)
func main() {
recordTime := "2021-02-04 23:45:00"
ts, err := time.Parse("2006-01-02 15:04:05", recordTime)
if err != nil {
fmt.Println("error: ", err)
return
}
fmt.Println(ts)
}
This code can be found here.
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.
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)
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