I've got the following string:
Sun, 03 Jan 2016 10:00:07 CET
Id like to parse it into time, but cannot seem to figure out how to write the format.
This is what I've got so far:
layout := "Mon, 01 Jan 03:04:05"
t, _ := time.Parse(layout, "Sun, 03 Jan 2016 10:00:07 CET")
fmt.Println(t)
Output I get is:
0001-01-01 00:00:00 +0000 UTC
First of all: You're silently ignoring the error this is returned as second return value of time.Parse
. I'd suggest handling the error appropriately, instead.
Secondly, let's have a look at the documentation of time.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 beMon 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.
The time.Parse
function expects its layout
parameter to represent a fixed example date. So, in order to parse the date Sun, 03 Jan 2016 10:00:07 CET
, the appropriate example layout string should be Mon, 02 Jan 2006 15:04:05 MST
:
layout := "Mon, 02 Jan 2006 15:04:05 MST"
t, err := time.Parse(layout, "Sun, 03 Jan 2016 10:00:07 CET")
if err != nil {
// handle the error somehow!
}
fmt.Println(t)