Origin of "Mon Jan 2 15:04:05 MST 2006" in golang
Categories:
Unraveling Go's Magic Date: 'Mon Jan 2 15:04:05 MST 2006'

Explore the curious origin and practical implications of Go's unique reference time format for parsing and formatting dates.
If you've ever worked with time and date formatting in Go, you've undoubtedly encountered the peculiar string "Mon Jan 2 15:04:05 MST 2006"
. This isn't a placeholder or a random example; it's Go's specific reference time layout. Unlike other languages that use symbolic representations (like YYYY-MM-DD
or HH:mm:ss
), Go uses a fixed date and time to define the format. This article delves into why this specific date was chosen, how it works, and best practices for using it effectively.
The Go Time Format Philosophy
Go's approach to time formatting is designed to be unambiguous and intuitive once understood. Instead of a complex set of format codes that can vary between platforms or libraries, Go provides a concrete example of the desired layout. When you want to parse a string into a time.Time
object or format a time.Time
object into a string, you provide a layout string that looks exactly like Mon Jan 2 15:04:05 MST 2006
, but with the components rearranged to match your target format.
flowchart TD A[User wants to format/parse time] --> B{Go's Reference Time: "Mon Jan 2 15:04:05 MST 2006"} B --> C{Each component maps to a specific value} C --> D["Month: Jan (1)"] C --> E["Day: 2"] C --> F["Hour: 15 (3 PM)"] C --> G["Minute: 04"] C --> H["Second: 05"] C --> I["Year: 2006"] C --> J["Weekday: Mon"] C --> K["Timezone: MST"] D & E & F & G & H & I & J & K --> L{User constructs layout string using these values} L --> M[Go's `time.Parse` or `time.Format` uses layout]
How Go's reference time maps to formatting components.
Why This Specific Date?
The choice of Mon Jan 2 15:04:05 MST 2006
is not arbitrary. It's a clever mnemonic designed to make each component unique and easily identifiable. Each number corresponds to its position in a sequence, making it easier to remember and construct custom layouts:
- 1 (January) - The first month
- 2 (Day of month) - The second day
- 3 (15:00, 3 PM) - The third hour in a 12-hour clock (or 15th in 24-hour)
- 4 (04 minutes) - The fourth minute
- 5 (05 seconds) - The fifth second
- 6 (2006) - The sixth year
Additionally, Mon
for Monday and MST
for Mountain Standard Time (which is UTC-7, making it distinct) complete the pattern. This design ensures that every part of the date and time has a unique, non-overlapping numerical or alphabetical representation within the reference string, preventing ambiguity when defining custom layouts.
package main
import (
"fmt"
"time"
)
func main() {
// The reference time layout
const goReferenceTime = "Mon Jan 2 15:04:05 MST 2006"
// Example 1: Formatting a time.Time object
now := time.Now()
fmt.Println("Current time (default):", now)
// Format using a custom layout based on the reference time
customLayout := "2006-01-02 15:04:05"
formattedTime := now.Format(customLayout)
fmt.Println("Formatted time (custom):", formattedTime)
// Example 2: Parsing a string into a time.Time object
dateString := "2023-10-27 10:30:00"
parseLayout := "2006-01-02 15:04:05"
parsedTime, err := time.Parse(parseLayout, dateString)
if err != nil {
fmt.Println("Error parsing time:", err)
return
}
fmt.Println("Parsed time:", parsedTime)
// Example 3: Using predefined layouts
fmt.Println("RFC3339Nano:", now.Format(time.RFC3339Nano))
fmt.Println("ANSIC:", now.Format(time.ANSIC))
}
Demonstrating time.Format
and time.Parse
with Go's reference time.
1
, 2
, 3
, 4
, 5
, 6
in the layout string correspond to January, day 2, 3 PM, 4 minutes, 5 seconds, and year 2006, respectively. You don't use MM
for month or DD
for day; you use 01
for month, 02
for day, etc., to represent the desired format.Common Layout Components and Their Meanings
Understanding the core components of the reference time is key to mastering Go's date formatting. Here's a quick reference for common elements you might use in your layouts:
Mon
: Day of the week, abbreviated (e.g., Mon, Tue)Monday
: Full day of the week (e.g., Monday, Tuesday)Jan
: Month, abbreviated (e.g., Jan, Feb)January
: Full month name (e.g., January, February)01
: Month as a two-digit number (e.g., 01, 12)1
: Month as a one or two-digit number (e.g., 1, 12)02
: Day of the month as a two-digit number (e.g., 02, 31)2
: Day of the month as a one or two-digit number (e.g., 2, 31)15
: Hour in 24-hour format (e.g., 00-23)03
: Hour in 12-hour format (e.g., 01-12)4
: Minute as a one or two-digit number (e.g., 0, 59)04
: Minute as a two-digit number (e.g., 00, 59)5
: Second as a one or two-digit number (e.g., 0, 59)05
: Second as a two-digit number (e.g., 00, 59)PM
: AM/PM designation (e.g., AM, PM)MST
: Timezone abbreviation (e.g., MST, PST)-0700
: Numeric timezone offset (e.g., -0700, +0100)2006
: Year with century (e.g., 2006, 2023)06
: Year without century (e.g., 06, 23)
time
package, such as time.RFC3339
, time.ANSIC
, time.UnixDate
, and time.Kitchen
. These are often sufficient for common use cases and help ensure consistency.