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

2024-01-24 13:15:46.5888334 +0530 IST m=+0.001584301
2009-11-17 20:34:58.651387237 +0000 UTC
2009
November
17
20
34
58
651387237
UTC
Tuesday
true
false
false
124331h10m47.937446163s
124331.17998262393
7.459870798957436e+06
4.475922479374462e+08
447592247937446163
2024-01-24 07:45:46.5888334 +0000 UTC
1995-09-12 09:24:10.713941074 +0000 UTC

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