What is the significance of Go's time.Format(layout string) reference time, ie:
Mon Jan 2 15:04:05 -0700 MST 2006
This specific time couldn't have been chosen completely randomly, right?
Source: http://golang.org/pkg/time/#Time.Format
CODE EXAMPLE To format or parse a time in Go, you format a special layout parameter (Mon Jan 2 15:04:05 MST 2006) the same way as the time should be formatted. About Home Algorithms Go Format a time or date [complete guide]
The layouts use the reference time on Mon Jan 2 15:04:05 MST 2006 to indicate the pattern under which to format the time object. Let us look at a few examples of formatting time using various custom layouts.
CODE EXAMPLE To format or parse a time in Go, you format a special layout parameter (Mon Jan 2 15:04:05 MST 2006) the same way as the time should be formatted. About Home Algorithms Go Format a time or date [complete guide] yourbasic.org/golang Basic example Standard time and date formats Layout options Corner cases Basic example
The model is to demonstrate what the reference time looks like so that the Format and Parse methods can apply the same transformation to a general time value. In short: you write the reference time Mon Jan 2 15:04:05 MST 2006 in the format you want and pass that string to Time.Format ()
Each part of the date is used as an index:
Jan -> 1 -> Month
2 -> 2 -> Day-of-Month
15 = 3PM -> 15/3 -> hour
04 -> 4 -> minute
05 -> 5 -> second
2006 -> 6 -> year
-0700 -> 7 -> time-zone
So according to the doc:
Since MST is GMT-0700, the reference time can be thought of as 01/02 03:04:05PM '06 -0700
This makes it easy for the time.Format
method to parse human-readable date format specifications that are visually identical to the desired result.
Compare this to for example the strftime
C function that uses hard-to-remember format strings such as "%a, %d %b %y %T %z"
which represents a RFC 822-compliant date format.
The Go equivalent is: "Mon, 02 Jan 06 15:04 MST"
.
The time.Format
will tokenize this string and analyze each word.
':'
character is left untouchedSee https://github.com/golang/go/blob/go1.15/src/time/format.go#L151 for the exact algorithm.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With