4 minutes
Formatting Time in Go
In Go, there’s no direct equivalent to the struct tm type in C, which is used to represent a calendar time broken down into its components. Instead, Go provides the time.Time type, which represents an instant in time, along with a variety of methods for working with dates and times.
struct tm {
int tm_sec; /* seconds, range 0 to 59 */
int tm_min; /* minutes, range 0 to 59 */
int tm_hour; /* hours, range 0 to 23 */
int tm_mday; /* day of the month, range 1 to 31 */
int tm_mon; /* month, range 0 to 11 */
int tm_year; /* The number of years since 1900 */
int tm_wday; /* day of the week, range 0 to 6, Sunday = 0 */
int tm_yday; /* day in the year, range 0 to 365, 1 Jan = 0 */
int tm_isdst; /* daylight saving time */
};
strftime pattern examples in sh
> date +"%A %B, %e, %Y"
Tuesday March, 7 2023
> date +%r
05:36:07 PM
Format function
In Go, the equivalent of the strftime function is the Format method of the time.Time type. It allows you to format a date and time as a string according to a specific format string.
func main() {
myTime := time.Date(2022, 8, 15, 12, 34, 56, 0, time.UTC)
fmt.Println(myTime.Format("01/02/06 03:04 PM"))
}
In this example,
In this example, "01/02/06 03:04 PM"
represents the desired format, where "01"
represents the month, "02"
represents the day, "06"
represents the year, "03"
represents the hour, "04"
represents the minute, and "PM"
represents the AM/PM indicator.
We can build an intuition for formatting time by writing a program that prompts the user to enter time format strings, and then displays the current time formatted according to that string:
func main() {
scanner := bufio.NewScanner(os.Stdin)
now := time.Now()
for {
fmt.Printf(" > ")
if !scanner.Scan() {
break
}
log.Println(now.Format(scanner.Text()))
}
}
When you run this program, you can type in time format strings (e.g., “2006-01-02 15:04:05”) and see the current time formatted according to that string.
In Go, "15"
is used as a constant to represent the hour value in 24-hour format, "04"
is used as a constant to represent the minute value in two-digit zero-padded format, and "05"
is used as a constant to represent the second value in two-digit zero-padded format.
We can see what constants refer to time format values in Go.
When we can see by entering go doc time.Layout | more
in a terminal.
package time // import "time"
const (
Layout = "01/02 03:04:05PM '06 -0700" // The reference time, in numerical order.
ANSIC = "Mon Jan _2 15:04:05 2006"
UnixDate = "Mon Jan _2 15:04:05 MST 2006"
RubyDate = "Mon Jan 02 15:04:05 -0700 2006"
RFC822 = "02 Jan 06 15:04 MST"
RFC822Z = "02 Jan 06 15:04 -0700" // RFC822 with numeric zone
RFC850 = "Monday, 02-Jan-06 15:04:05 MST"
RFC1123 = "Mon, 02 Jan 2006 15:04:05 MST"
RFC1123Z = "Mon, 02 Jan 2006 15:04:05 -0700" // RFC1123 with numeric zone
RFC3339 = "2006-01-02T15:04:05Z07:00"
RFC3339Nano = "2006-01-02T15:04:05.999999999Z07:00"
Kitchen = "3:04PM"
// Handy time stamps.
Stamp = "Jan _2 15:04:05"
StampMilli = "Jan _2 15:04:05.000"
StampMicro = "Jan _2 15:04:05.000000"
StampNano = "Jan _2 15:04:05.000000000"
)
other examples when the running intuition for formatting time.
> 01/02 03:04:05 2006 0700
2023/03/07 17:06:23 03/07 05:04:37 2023 0700
> 700
2023/03/07 17:06:33 700
> -0700
2023/03/07 17:06:36 -0500
> 01
2023/03/07 17:06:36 03
> January
2023/03/07 17:06:39 March
> Jan
2023/03/07 17:06:40 Mar
> 02
2023/03/07 17:06:40 07
> Monday
2023/03/07 17:06:58 Tuesday
> Mon
2023/03/07 17:07:01 Tue
> foo bar 2006
2023/03/07 17:07:19 foo bar 2023
> 2006
2023/03/07 17:08:01 2023
> 06
2023/03/07 17:08:04 23
How to format a time like “Sunday March 5, 2023”?
answer: Monday January 2, 2006
Parse function example
func main()
// march 17 1988
t, err := time.Parse("Jan 2 2006", "Mar 17 1988")
if err != nil {
log.Fatal(err)
}
log.Print(t)
2023-03-07 17:56:16 1988-03-17 00:00:00 +0000 UTC
Conclusion
Go’s approach to working with time is somewhat different from C and other programming languages, but it is quite flexible once you get used to it.