For the complete documentation index, see llms.txt. This page is also available as Markdown.

Time

This Go code demonstrates working with the time package to handle dates and times. Let's go through the code with inline comments and explanations:

// Importing necessary packages.
import (
	"fmt"
	"time"
)

// The main function, where the execution of the program begins.
func main() {
	// Creating an alias for fmt.Println for brevity.
	p := fmt.Println

	// Getting the current time.
	now := time.Now()
	p(now)

	// Creating a specific date and time.
	then := time.Date(
		2009, 11, 17, 20, 34, 58, 651387237, time.UTC)
	p(then)

	// Extracting various components of the time.
	p(then.Year())
	p(then.Month())
	p(then.Day())
	p(then.Hour())
	p(then.Minute())
	p(then.Second())
	p(then.Nanosecond())
	p(then.Location())

	// Determining the weekday.
	p(then.Weekday())

	// Comparing two times.
	p(then.Before(now))
	p(then.After(now))
	p(then.Equal(now))

	// Calculating the time difference.
	diff := now.Sub(then)
	p(diff)

	// Extracting components of the time difference.
	p(diff.Hours())
	p(diff.Minutes())
	p(diff.Seconds())
	p(diff.Nanoseconds())

	// Adding and subtracting time durations.
	p(then.Add(diff))
	p(then.Add(-diff))
}

Output

Explanation:

  1. Getting Current Time:

    • time.Now() returns the current time.

  2. Creating Specific Time:

    • time.Date() is used to create a specific date and time.

  3. Extracting Components:

    • Various methods (Year(), Month(), Day(), etc.) are used to extract components of the time.

  4. Weekday:

    • Weekday() returns the day of the week.

  5. Comparing Times:

    • Before(), After(), and Equal() methods are used to compare times.

  6. Time Difference:

    • Sub() calculates the time difference between two times.

  7. Extracting Components of Time Difference:

    • Methods like Hours(), Minutes(), Seconds(), and Nanoseconds() are used to extract components of the time difference.

  8. Adding and Subtracting Time Durations:

    • Add() is used to add or subtract a time duration.

This code provides a basic overview of working with dates and times in Go using the time package. It covers creating, manipulating, comparing times, and calculating time differences.

Last updated