This is a creation in Article, where the information may have evolved or changed.
A section of code
There is a code play to explain layout. As you can see, it is entirely possible for go to automatically recognize and convert time strings.
The time package for Go provides the. The Format function, which is used for formatting the output of time.
Similarly, there is time. Parse is used to parse the time of the string type to Time.time. This is a function of two reciprocal inverses.
The problem is that the format layout used by Go is different from any experience we've used in the past. So that the first contact is always a fog
In fact, go provides this layout of the algorithm is very scientific and efficient implementation, and very regular. Below we break down in detail.
Direct Last Correspondence table
The preceding is meaning, followed by Go's representation value, multiple representations, comma "," split
- Month 1,01,jan,january
- Day 2,02,_2
- When 3,03,15,pm,pm,am,am
- Sub 4,04
- SEC 5,05
- Year 06,2006
- Weeks Mon,monday
- Time zone difference means -07,-0700,z0700,z07:00,-07:00,mst
- Time zone letter abbreviation MST
Do you see the pattern? Oh yes, you found out that none of this is duplicated, and all values represent a single time part. And it covers a number of formatting combinations.
such as the expression of the hour (originally defined as 3 o'clock in the afternoon, that is, 15 o'clock)
- 3 in 12-hour notation, minus the leading 0
- 03 in 12-hour notation, reserved for leading 0
- 15 in 24-hour notation, reserved for leading 0
- 03PM 24-hour AM/PM indicates that the leading 0 is retained in the afternoon.
- 3pm 24-hour AM/PM means that the leading 0 is removed in the afternoon.
2014-02-26 in the analytic layout to use PM or PM, respectively, corresponding to the data am/pm, AM/PM, case sensitive. If layout has a leading 0, it cannot be omitted from the data.
Another example is the month
- 1 numbers indicate the month, minus the leading 0
- 01 numbers represent the month, leaving the leading 0
- Jan abbreviation word denotes month
- January the whole word to indicate the month
Real time: My UTC time is December 5, 2013, my local time zone is Asia/shanghai
Character representation: 5 CST
Go layout:2006 2 MST
Real time: My UTC time is December 2013 22 o'clock, my local time zone is Asia/shanghai
Character representation: CST
Go layout:2006 MST
and the order of all these numbers is exactly 1,2,4,5,6,7 and a time zone.
2014-01-17 Day found the above time example is inaccurate, should add time and seconds to clarify the time zone problem
The following example time zone problem
Real time: My local time is 2014-01-17 01:19:15, my local timezone is Asia/shanghai
RFC3339 format: 2006-01-02t15:04:05z07:00
RFC3339 Output: 2014-01-17t01:19:15+08:00
Custom format: 2006-01-02 15:04:05-07:00
Custom output: 2014-01-17 01:19:15 +08:00
Custom format: 2006-01-02 15:04:05-07:00 MST
Custom output: 2014-01-17 01:19:15 +08:00 CST
UCT () Output: 2014-01-16 17:19:15.9092754 +0000 UTC
Note: The difference between UTC time and local time and time zone difference
Time output that is not processed by the UTC () function represents a local time zone (if formatted with a time zone)
If you want to calculate UTC time, be sure to remember to use the UTC () function before you do anything else.
MST is the English abbreviation for the Mountain Time zone in North America, and the Asia/shanghai corresponding time zone abbreviation is CST. This CST is obtained from the operating system, Windows system and other different, but also made a special processing, more complex. Interested please analyze Zoneinfo_abbrs _windows.go.
Note that abbreviations, while representing a time zone, cannot be computed because they are duplicated, see time zone abbreviations
It's unreliable to judge the time difference by abbreviations.
In addition to the time zone abbreviation name above indicates the time zone, the time zone can also be used
The prefix "Z" and "-" are two styles that represent time zones with jet lag.
There's actually a second repeated. Digits for fractional seconds notation
It's 0 and 9, it's rarely used, it's written in the source code.
StdFracSecond0 //". 0", ". xx", ..., trailing zeros includedstdFracSecond9 //". 9", ".", ..., trailing zeros OMI tted
Time. Parse Non-jetlag time zone layout
See Code and output
Time. Parse ("2006-01-02 15:04:05", "2014-01-17 03:06:54")//2014-01-17 03:06:54 +0000 UTC <nil>
If layout does not define a slack time zone, the time zone is calculated in UTC. The abbreviated time zone is unreliable.
Apart from those values are demarcation symbols, natural matching, direct example of the bar
Character representation: 2013-12 CST
Go layout:2006-01 MST
Character representation: December 2013 21 o'clock time zone CST
Go layout:2006 January 15 o'clock time zone MST
Well, do you feel that this means compatibility is better, more adaptable, more easy to remember it.
The question of time BC
The time period in BC is negative, and when the package output is not a problem, but parser is not a negative value of the year, I do not know if this is a bug. Crossing please pay attention to yourself.
A issues was submitted. Officials say the problem is not serious, which is a special scenario that developers can deal with in particular. You do not need to change the time package for this.
UTC time January 2 3:4 P.M. 5 Seconds 2006 years, local time zone-0700, and a fraction of 999999999 nanoseconds