Detailed explanation of time package in Golang (1): time.Time

In the daily development process, you will frequently encounter scenarios that operate on time. Using the time package in Golang can easily implement time-related operations. The next few articles will explain the time package in detail. This article first explains the structure time.Time in the time package.

time.Time

The time.Time type is used to represent a specific time point, which can be accurate to nanoseconds. The structure definition and corresponding methods are as follows:

type Time struct {
	wall uint64
	ext  int64
	loc *Location
}

Methods for obtaining various time point attributes

  • func (t Time) Date() (year int, month Month, day int), get date (year, month, day) information.
  • func (t Time) Year() int, get year information.
  • func (t Time) YearDay() int, get the day of the year (1~365).
  • func (t Time) Month() Month, get month information, return a Month type;
  • func (t Time) ISOWeek() (year, week int), returns the year and week (1-53) in ISO 8601 format.
  • func (t Time) Weekday() Weekday, returns a Weekday type.
  • func (t Time) Day() int, get the number of the month (1~31).
  • func (t Time) Clock() (hour, min, sec int), get time (hour, minute, second) information.
  • func (t Time) Hour() int, get hour information (0~23).
  • func (t Time) Minute() int, get minute information (0~59).
  • func (t Time) Second() int, get the second information (0~59).
  • func (t Time) Nanosecond() int, get nanosecond information (0~999999999).
  • func (t Time) Unix() int64, get the timestamp in seconds.
  • func (t Time) UnixMilli() int64, get timestamp in milliseconds.
  • func (t Time) UnixMicro() int64, get the timestamp in microseconds.
  • func (t Time) UnixNano() int64, get nanosecond timestamp.
  • func (t Time) String() string, returns a time format of type "2006-01-02 15:04:05.999999999 -0700 MST".
  • func (t Time) Location() *Location, get time zone information.

Look at a simple example:

package main

import (
	"fmt"
	"time"
)

func main() {
	t := time.Now()
	fmt.Println(t.Date())
	fmt.Println(t.Year())
	fmt.Println(t.YearDay())
	fmt.Println(t.Month())
	fmt.Println(t.ISOWeek())
	fmt.Println(t.Weekday())
	fmt.Println(t.Day())
	fmt.Println(t.Clock())
	fmt.Println(t.Hour())
	fmt.Println(t.Minute())
	fmt.Println(t.Second())
	fmt.Println(t.Nanosecond())
	fmt.Println(t.Unix())
	fmt.Println(t.UnixMilli())
	fmt.Println(t.UnixMicro())
	fmt.Println(t.UnixNano())
  fmt.Println(t.String())
	fmt.Println(t.Location())
}

Time processing methods (comparison, judgment, analysis)

  • func (t Time) Format(layout string) string, format the time into the specified format.
  • func (t Time) Add(d Duration) Time, plus the specified time.
  • func (t Time) AddDate(years int, months int, days int) Time, returns the point in time when the given years, months and days are added to t.
  • func (t Time) Sub(u Time) Duration, returns the time difference between two time points.
  • func (t Time) Truncate(d Duration) Time, truncate the specified time.
  • func (t Time) Round(d Duration) Time, which rounds the time to the specified time.
  • func (t Time) Equal(u Time) bool, judge whether two time points are equal.
  • func (t Time) After(u Time) bool, judge whether the time point t is after the time point u.
  • func (t Time) Before(u Time) bool, judge whether the time point t is before the time point u.

Other methods will not be explained one by one, you can refer to the official documentation for details.

Guess you like

Origin blog.csdn.net/luduoyuan/article/details/131949652