Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

What is the significance of Go's time.Format(layout string) reference time?

Tags:

go

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

like image 796
soroushjp Avatar asked Jan 22 '15 11:01

soroushjp


People also ask

How to format a time in Go programming language?

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]

Why do the layouts use the reference time on Mon Jan 2?

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.

How to format or parse a time in Golang?

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

How do you format a reference time?

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


1 Answers

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.

  • Mon is recognized litteraly as monday so this is the week day's name
  • the comma is left untouched
  • 02 is recognized as the integer value 2, representing a day-of-month in the index
  • Jan is the known english abbreviation for the january month, so this is used for the month part
  • 06 is 6 so this the year part
  • 15 is equivalent to 3 and represent the hour
  • the ':' character is left untouched
  • 04 is 4, therefore the minute
  • MST is interpreted litteraly

See https://github.com/golang/go/blob/go1.15/src/time/format.go#L151 for the exact algorithm.

like image 79
SirDarius Avatar answered Oct 18 '22 12:10

SirDarius