go语言的魔幻旅程25-time包

海上生明月,天涯共此时

时间如流水,逝者如斯夫 不舍昼夜,作为格子间的一员,傍晚降临的时候没有黎明的黄昏,只有黑夜的陪伴,欣赏夕阳是一种奢侈,仰望星空成为一种憧憬。

go语言的time包

time包作为go语言的一个内置库,提供的关于时间操作的功能非常的多,内容也非常的夯实,下面这对time 的函数和方法展开讲解.

package main

/*********************************************************************/
/**************** golang中time包相关API讲解 ************************/
/********************************************************************/

/*
Constants
func After(d Duration) <-chan Time
func Sleep(d Duration)
func Tick(d Duration) <-chan Time
type Duration
	func ParseDuration(s string) (Duration, error)
	func Since(t Time) Duration
	func Until(t Time) Duration
	func (d Duration) Hours() float64
	func (d Duration) Microseconds() int64
	func (d Duration) Milliseconds() int64
	func (d Duration) Minutes() float64
	func (d Duration) Nanoseconds() int64
	func (d Duration) Round(m Duration) Duration
	func (d Duration) Seconds() float64
	func (d Duration) String() string
	func (d Duration) Truncate(m Duration) Duration
type Location
	func FixedZone(name string, offset int) *Location
	func LoadLocation(name string) (*Location, error)
	func LoadLocationFromTZData(name string, data []byte) (*Location, error)
	func (l *Location) String() string
type Month
	func (m Month) String() string
type ParseError
	func (e *ParseError) Error() string
type Ticker
	func NewTicker(d Duration) *Ticker
	func (t *Ticker) Reset(d Duration)
	func (t *Ticker) Stop()
type Time
	func Date(year int, month Month, day, hour, min, sec, nsec int, loc *Location) Time
	func Now() Time
	func Parse(layout, value string) (Time, error)
	func ParseInLocation(layout, value string, loc *Location) (Time, error)
	func Unix(sec int64, nsec int64) Time
	func (t Time) Add(d Duration) Time
	func (t Time) AddDate(years int, months int, days int) Time
	func (t Time) After(u Time) bool
	func (t Time) AppendFormat(b []byte, layout string) []byte
	func (t Time) Before(u Time) bool
	func (t Time) Clock() (hour, min, sec int)
	func (t Time) Date() (year int, month Month, day int)
	func (t Time) Day() int
	func (t Time) Equal(u Time) bool
	func (t Time) Format(layout string) string
	func (t *Time) GobDecode(data []byte) error
	func (t Time) GobEncode() ([]byte, error)
	func (t Time) Hour() int
	func (t Time) ISOWeek() (year, week int)
	func (t Time) In(loc *Location) Time
	func (t Time) IsZero() bool
	func (t Time) Local() Time
	func (t Time) Location() *Location
	func (t Time) MarshalBinary() ([]byte, error)
	func (t Time) MarshalJSON() ([]byte, error)
	func (t Time) MarshalText() ([]byte, error)
	func (t Time) Minute() int
	func (t Time) Month() Month
	func (t Time) Nanosecond() int
	func (t Time) Round(d Duration) Time
	func (t Time) Second() int
	func (t Time) String() string
	func (t Time) Sub(u Time) Duration
	func (t Time) Truncate(d Duration) Time
	func (t Time) UTC() Time
	func (t Time) Unix() int64
	func (t Time) UnixNano() int64
	func (t *Time) UnmarshalBinary(data []byte) error
	func (t *Time) UnmarshalJSON(data []byte) error
	func (t *Time) UnmarshalText(data []byte) error
	func (t Time) Weekday() Weekday
	func (t Time) Year() int
	func (t Time) YearDay() int
	func (t Time) Zone() (name string, offset int)
type Timer
	func AfterFunc(d Duration, f func()) *Timer
	func NewTimer(d Duration) *Timer
	func (t *Timer) Reset(d Duration) bool
	func (t *Timer) Stop() bool
type Weekday
	func (d Weekday) String() string
*/


func main() {

	/**
	*在等待持续时间后,然后在返回的频道上发送当前时间。它相当于NewTimer(d).C。在定时器触发之前,
	*底层 Timer 不会被垃圾收集器恢复。如果需要提高效率,请改用 NewTimer ,如果不再需要定时器,
	*则调用 Timer.Stop。
	*func After(d Duration) <-chan Time
	*/

	/*
	select {
	case m := <-c:
		handle(m)
	case <-time.After(5 * time.Minute):
		fmt.Println("timed out")
	}
	*/

	/**
	*睡眠暂停当前的 goroutine 至少持续时间 d 。负值或零持续时间会导致 Sleep 立即返回
	*func Sleep(d Duration)
	*/

	/*
	time.Sleep(100 * time.Millisecond)
	*/

	/**
	*Tick 是 NewTicker 的便捷包装,仅提供对滴答声频道的访问。虽然 Tick 对于不需要关闭
	*Ticker 的客户端非常有用,但请注意,如果没有办法关闭它,底层 Ticker 不能被垃圾收集器
	*恢复; 它“泄漏”。与 NewTicker 不同,如果 d <= 0,Tick 将返回 nil。
	*func Tick(d Duration) <-chan Time
	*/


	/*
	c := time.Tick(5 * time.Second)
	for next := range c {
		fmt.Printf("%v %s\n", next, " ")
	}
	*/

	/*********************************************************************/
	/******************** Duration中相关方法讲解 ************************/
	/********************************************************************/

	/**
	*解析一个时间段字符串,一个时间段字符串是一个序列,每个片段包含可选的正负号、十进制数、
	×可选的小数部分和单位后缀,如"300ms"、"-1.5h"、"2h45m",合法的单位有"ns"、"us"
	×/"µs"、"ms"、"s"、"m"、"h"
	*func ParseDuration(s string) (Duration, error)
	*/

	/*
	d, _ := time.ParseDuration("1h15m30.918273645s")
	fmt.Println(d.Hours(), d.Minutes(), d.Nanoseconds())
	*/

	/**
	*返回从t到现在经过的时间,等价于time.Now().Sub(t)
	*func Since(t Time) Duration
	*/

	/*
	t := time.Date(2019, time.February, 7, 0, 0, 0, 0, time.UTC)
	time.Since(t)
	*/

	/**
	*返回从t到现在经过的时间,等价于time.Now().Sub(t)
	*func Until(t Time) Duration
	*/

	/*
	t := time.Date(2019, time.February, 7, 0, 0, 0, 0, time.UTC)
	time.Until(t)
	*/

	/**
	*将持续时间作为浮点小时数返回。
	*func (d Duration) Hours() float64
	*/

	/*
	h, _ := time.ParseDuration("4h30m")
	fmt.Printf("I've got %.1f hours of work left.", h.Hours())
	*/

	/**
	*将持续时间作为浮点微秒数返回。
	*func (d Duration) Microseconds() int64
	*/

	/*
	u, _ := time.ParseDuration("1s")
	fmt.Printf("One second is %d milliseconds.\n", u.Milliseconds())
	*/

	/**
	*将持续时间作为浮点豪秒数返回。
	*func (d Duration) Milliseconds() int64
	*/

	/*
	u, _ := time.ParseDuration("1s")
	fmt.Printf("One second is %d milliseconds.\n", u.Milliseconds())
	*/


	/**
	*将持续时间作为分钟数返回
	*func (d Duration) Minutes() float64
	*/

	/*
	m, _ := time.ParseDuration("1h30m")
	fmt.Printf("The movie is %.0f minutes long.", m.Minutes())
	*/

	/**
	*将持续时间作为纳秒数返回
	*func (d Duration) Nanoseconds() int64
	*/

	/*
	u, _ := time.ParseDuration("1µs")
	fmt.Printf("One microsecond is %d nanoseconds.\n", u.Nanoseconds())
	*/


	/**
	*Round将舍入d的结果返回到m的最接近的倍数。中间值的舍入行为是从零开始舍入。如果结果超过了持续
	*时间中可存储的最大(或最小)值,则返回最大(或最小)持续时间。如果m <= 0,则Round返回d不变。
	*func (d Duration) Round(m Duration) Duration
	*/

	/*
	d, err := time.ParseDuration("1h15m30.918273645s")
	if err != nil {
		panic(err)
	}

	round := []time.Duration{
		time.Nanosecond,
		time.Microsecond,
		time.Millisecond,
		time.Second,
		2 * time.Second,
		time.Minute,
		10 * time.Minute,
		time.Hour,
	}

	for _, r := range round {
		fmt.Printf("d.Round(%6s) = %s\n", r, d.Round(r).String())
	}
	*/

	/**
	*将持续时间作为秒数返回
	*func (d Duration) Seconds() float64
	*/

	/*
	m, _ := time.ParseDuration("1m30s")
	fmt.Printf("Take off in t-%.0f seconds.", m.Seconds())
	*/


	/**
	*字符串以“72h3m0.5s”的形式返回一个表示持续时间的字符串。
	*func (d Duration) String() string
	*/

	/*
	fmt.Println(1*time.Hour + 2*time.Minute + 300*time.Millisecond)
	fmt.Println(300 * time.Millisecond)
	*/

	/**
	*截断返回四舍入d的结果为m的倍数。如果m <= 0,则截断返回d不变。
	* func (d Duration) Truncate(m Duration) Duration
	*/

	/*
	d, err := time.ParseDuration("1h15m30.918273645s")
	if err != nil {
		panic(err)
	}

	trunc := []time.Duration{
		time.Nanosecond,
		time.Microsecond,
		time.Millisecond,
		time.Second,
		2 * time.Second,
		time.Minute,
		10 * time.Minute,
		time.Hour,
	}

	for _, t := range trunc {
		fmt.Printf("d.Truncate(%6s) = %s\n", t, d.Truncate(t).String())
	}
	*/


	/*********************************************************************/
	/******************** Location中相关方法讲解 ************************/
	/********************************************************************/

	/**
	* FixedZone返回一个始终使用给定区域名称和偏移量的位置(UTC以东的秒数)
	*func FixedZone(name string, offset int) *Location
	*/

	/*
	loc := time.FixedZone("UTC-8", -8*60*60)
	t := time.Date(2009, time.November, 10, 23, 0, 0, 0, loc)
	fmt.Println("The time is:", t.Format(time.RFC822))
	*/

	/**
	*返回使用给定的名字创建的*time.Location,如果name是""或"UTC",返回UTC;如果name是"Local",
	*返回Local;否则name应该是IANA时区数据库里有记录的地点名(该数据库记录了地点和对应的时区),
	*如"America/New_York",需要的时区数据库可能不是所有系统都提供,特别是非Unix系统。此时
	*LoadLocation会查找环境变量ZONEINFO指定目录或解压该变量指定的zip文件(如果有该环境变量);
	*然后查找Unix系统的惯例时区数据安装位置,最后查找$GOROOT/lib/time/zoneinfo.zip
	*func LoadLocation(name string) (*Location, error)
	*/

	/*
	location, err := time.LoadLocation("America/Los_Angeles")
	if err != nil {
		panic(err)
	}

	timeInUTC := time.Date(2018, 8, 30, 12, 0, 0, 0, time.UTC)
	fmt.Println(timeInUTC.In(location))
	*/

	/**
	*接收时区名称和时区数据文件,格式必须是:/etc/localtime, 返回一个 Location。
	*func LoadLocationFromTZData(name string, data []byte) (*Location, error)
	*/


	/**
	*字符串返回时区信息的描述性名称,对应于LoadLocation或FixedZone的名称参数。
	*func (l *Location) String() string
	*/

	/*
	loc := time.FixedZone("UTC-8", -8*60*60)
 	fmt.Println(loc.String())
	*/

	/*********************************************************************/
	/******************** Month中相关方法讲解 **************************/
	/********************************************************************/

	/**
	*返回字符串表示的月份
	*func (m Month) String() string
	*/

	/*
	_, month, _ := time.Now().Date()
	fmt.Println(month.String())
	*/


	/*********************************************************************/
	/******************** ParseError中相关方法讲解 **********************/
	/********************************************************************/

	/**
	*Error返回ParseError的字符串表示形式。
	*func (e *ParseError) Error() string
	*/

	/*
	var e *time.ParseError
	fmt.Println(e.Error())
	*/


	/*********************************************************************/
	/******************** Ticker中相关方法讲解 **************************/
	/*******************************************************************/

	/**
	*NewTicker返回一个新的Ticker,其中包含一个频道,该频道将在每个刻度之后发送时间。
	*变动周期由duration参数指定。此自动收录器将调整时间间隔或滴答滴答声,以弥补接收
	*器速度慢的问题。持续时间d必须大于零;如果没有,NewTicker将惊慌。停止行情自动收
	*录器以释放关联的资源。
	*func NewTicker(d Duration) *Ticker
	*/

	/*
	ticker := time.NewTicker(time.Second)
	defer ticker.Stop()
	done := make(chan bool)
	go func() {
		time.Sleep(10 * time.Second)
		done <- true
	}()
	for {
		select {
		case <-done:
			fmt.Println("Done!")
			return
		case t := <-ticker.C:
			fmt.Println("Current time: ", t)
		}
	}
	*/

	/**
	*重置将停止置顶器并将其周期重置为指定的持续时间。在新的时间段过去之后,下一个滴答声将到达。
	*func (t *Ticker) Reset(d Duration)
	*/

	/*
	ticker := time.NewTicker(time.Second)
	ticker.Reset(time.Second)
	*/

	/**
	*停止关闭代码。停止后,不会再发送任何蜱虫。停止不关闭通道,以防止从通道读取不正确的成功。
	*func (t *Ticker) Stop()
	*/

	/*
	ticker := time.NewTicker(time.Second)
	ticker.Stop()
	*/

	/*********************************************************************/
	/******************** Time中相关方法讲解 ***************************/
	/*******************************************************************/

	/**
	*日期返回对应的时间
	*func Date(year int, month Month, day, hour, min, sec, nsec int, loc *Location) Time
	*/

	/*
	t := time.Date(2009, time.November, 10, 23, 0, 0, 0, time.UTC)
	fmt.Printf("Go launched at %s\n", t.Local())
	*/

	/**
	*返回当前的时间
	*func Now() Time
	*/

	/*
	t := time.Now()
	fmt.Println(t)
	*/

	/**
	*解析解析一个格式化的字符串,并返回它表示的时间值
	*func Parse(layout, value string) (Time, error)
	*/

	/*
	t, _ := time.Parse(time.RFC3339, "2006-01-02T15:04:05Z")
	fmt.Println(t)
	t, _ = time.Parse(time.RFC3339, "2006-01-02T15:04:05+07:00")
	fmt.Println(t)
	_, err := time.Parse(time.RFC3339, time.RFC3339)
	fmt.Println("error", err)
	*/

	/**
	*ParseInLocation类似于Parse,但在两个重要方面有所不同。首先,在没有时区信息的情况下,Parse将时间解释为UTC。
	*ParseInLocation将时间解释为给定位置。其次,当给定区域偏移量或缩写时,Parse会尝试将其与本地位置进行匹配。
	*ParseInLocation使用给定的位置。
	*func ParseInLocation(layout, value string, loc *Location) (Time, error)
	*/

	/*
	loc, _ := time.LoadLocation("Europe/Berlin")
	const longForm = "Jan 2, 2006 at 3:04pm (MST)"
	t, _ := time.ParseInLocation(longForm, "Jul 9, 2012 at 5:02am (CEST)", loc)
	fmt.Println(t)

	const shortForm = "2006-Jan-02"
	t, _ = time.ParseInLocation(shortForm, "2012-Jul-09", loc)
	fmt.Println(t)
	*/

	/**
	*Unix返回t作为Unix时间,这是自1970年1月1日UTC以来经过的秒数.结果不取决于与t关联的位置。
	*Unix-like操作系统通常将时间记录为32位秒数,但是由于此处的方法返回的是64位值,因此它在
	*过去或将来的数十亿年内都有效。
	*func Unix(sec int64, nsec int64) Time
	*/

	/*
	fmt.Println(time.Unix(1e9, 0).UTC())
	fmt.Println(time.Unix(0, 1e18).UTC())
	fmt.Println(time.Unix(2e9, -1e18).UTC())

	t := time.Date(2001, time.September, 9, 1, 46, 40, 0, time.UTC)
	fmt.Println(t.Unix())
	fmt.Println(t.UnixNano())
	*/



	/**
	*加法返回时间t + d。
	*func (t Time) Add(d Duration) Time
   	*/

	/*
	start := time.Date(2009, 1, 1, 12, 0, 0, 0, time.UTC)
	afterTenSeconds := start.Add(time.Second * 10)
	afterTenMinutes := start.Add(time.Minute * 10)
	afterTenHours := start.Add(time.Hour * 10)
	afterTenDays := start.Add(time.Hour * 24 * 10)

	fmt.Printf("start = %v\n", start)
	fmt.Printf("start.Add(time.Second * 10) = %v\n", afterTenSeconds)
	fmt.Printf("start.Add(time.Minute * 10) = %v\n", afterTenMinutes)
	fmt.Printf("start.Add(time.Hour * 10) = %v\n", afterTenHours)
	fmt.Printf("start.Add(time.Hour * 24 * 10) = %v\n", afterTenDays)
	*/

	/**
	*AddDate返回对应于将给定的年,月和天数添加到t的时间。例如,应用于2011年1月1日的
	*AddDate(-1,2,3)返回2010年3月4日
	*func (t Time) AddDate(years int, months int, days int) Time
	*/

	/*
	start := time.Date(2009, 1, 1, 0, 0, 0, 0, time.UTC)
	oneDayLater := start.AddDate(0, 0, 1)
	oneMonthLater := start.AddDate(0, 1, 0)
	oneYearLater := start.AddDate(1, 0, 0)

	fmt.Printf("oneDayLater: start.AddDate(0, 0, 1) = %v\n", oneDayLater)
	fmt.Printf("oneMonthLater: start.AddDate(0, 1, 0) = %v\n", oneMonthLater)
	fmt.Printf("oneYearLater: start.AddDate(1, 0, 0) = %v\n", oneYearLater)
	*/


	/**
	*After报告时刻t是否在u之后。
	*func (t Time) After(u Time) bool
	*/

	/*
	year2000 := time.Date(2000, 1, 1, 0, 0, 0, 0, time.UTC)
	year3000 := time.Date(3000, 1, 1, 0, 0, 0, 0, time.UTC)

	isYear3000AfterYear2000 := year3000.After(year2000) // True
	isYear2000AfterYear3000 := year2000.After(year3000) // False

	fmt.Printf("year3000.After(year2000) = %v\n", isYear3000AfterYear2000)
	fmt.Printf("year2000.After(year3000) = %v\n", isYear2000AfterYear3000)
	*/

	/**
	*AppendFormat类似于Format,但将文本表示形式附加到b并返回扩展的缓冲区。
	*func (t Time) AppendFormat(b []byte, layout string) []byte
	*/

	/*
	t := time.Date(2017, time.November, 4, 11, 0, 0, 0, time.UTC)
	text := []byte("Time: ")

	text = t.AppendFormat(text, time.Kitchen)
	fmt.Println(string(text))
	*/

	/**
	*之前报告时刻t是否早于u。
	*func (t Time) Before(u Time) bool
	*/

	/*
	year2000 := time.Date(2000, 1, 1, 0, 0, 0, 0, time.UTC)
	year3000 := time.Date(3000, 1, 1, 0, 0, 0, 0, time.UTC)

	isYear2000BeforeYear3000 := year2000.Before(year3000) // True
	isYear3000BeforeYear2000 := year3000.Before(year2000) // False

	fmt.Printf("year2000.Before(year3000) = %v\n", isYear2000BeforeYear3000)
	fmt.Printf("year3000.Before(year2000) = %v\n", isYear3000BeforeYear2000)
	*/

	/**
	*时钟在 t 指定的日期内返回小时,分钟和秒。
	*func (t Time) Clock() (hour, min, sec int)
	*/

	/*
	year2000 := time.Date(2000, 1, 1, 0, 0, 0, 0, time.UTC)
	h, m, s := year2000.Clock()
	fmt.Println(h, m, s)
	*/

	/**
	*日期返回发生t的年,月和日。
	*func (t Time) Date() (year int, month Month, day int)
	*/

	/*
	d := time.Date(2000, 2, 1, 12, 30, 0, 0, time.UTC)
	year, month, day := d.Date()
	fmt.Printf("year = %v\n", year)
	fmt.Printf("month = %v\n", month)
	fmt.Printf("day = %v\n", day)
	*/

	/**
	*日期返回发生t的年,月和日。
	*func (t Time) Day() int
	*/

	/*
	d := time.Date(2000, 2, 1, 12, 30, 0, 0, time.UTC)
	day := d.Day()
	fmt.Printf("day = %v\n", day)
	*/

	/**
	*相等报告t和u是否代表同一时刻。即使它们位于不同的位置,两次也可以相等。例如,
	*6:00 +0200和4:00 UTC相等。有关将==与时间值一起使用的陷阱,请参见时间类
	*型的文档。大多数代码应改用Equal。
	*func (t Time) Equal(u Time) bool
	*/

	/*
	secondsEastOfUTC := int((8 * time.Hour).Seconds())
	beijing := time.FixedZone("Beijing Time", secondsEastOfUTC)

	d1 := time.Date(2000, 2, 1, 12, 30, 0, 0, time.UTC)
	d2 := time.Date(2000, 2, 1, 20, 30, 0, 0, beijing)

	datesEqualUsingEqualOperator := d1 == d2
	datesEqualUsingFunction := d1.Equal(d2)

	fmt.Printf("datesEqualUsingEqualOperator = %v\n", datesEqualUsingEqualOperator)
	fmt.Printf("datesEqualUsingFunction = %v\n", datesEqualUsingFunction)
	*/


	/**
	*Format返回根据布局格式化的时间值的文本表示,该时间值通过显示参考时间如何定义来定义格式。
	*func (t Time) Format(layout string) string
	*/

	/*
	t, err := time.Parse(time.UnixDate, "Wed Feb 25 11:06:39 PST 2015")
	if err != nil { // Always check errors even if they should not happen.
		panic(err)
	}

	fmt.Println("default format:", t)
	fmt.Println("Unix format:", t.Format(time.UnixDate))
	fmt.Println("Same, in UTC:", t.UTC().Format(time.UnixDate))
	*/


	/**
	*GobDecode实现gob.GobDecoder接口。
	*func (t *Time) GobDecode(data []byte) error
	*/

	/*
	t := time.Date(2019, time.February, 7, 0, 0, 0, 0, time.UTC)
	gobBytes, _ := t.GobEncode()
	_ = t.GobDecode(gobBytes)
	*/

	/**
	*GobEncode实现gob.GobEncoder接口。
	*func (t Time) GobEncode() ([]byte, error)
	*/

	/*
	t := time.Date(2019, time.February, 7, 0, 0, 0, 0, time.UTC)
	gobBytes, _ := t.GobEncode()
	fmt.Println(gobBytes)
	*/

	/**
	* 返回t对应的那一天的第几小时,范围[0, 23]
	*func (t Time) Hour() int)
	*/

	/*
	t := time.Date(2019, time.February, 7, 1, 0, 0, 0, time.UTC)
	h := t.Hour()
	fmt.Println(h)
	*/

	/**
	*返回时间点t对应的ISO 9601标准下的年份和星期编号, 星期编号范围[1,53],1月1号到
	*1月3号可能属于上一年的最后一周,12月29号到12月31号可能属于下一年的第一周
	*func (t Time) ISOWeek() (year, week int)
	*/

	/*
	t := time.Date(2019, time.February, 7, 1, 0, 0, 0, time.UTC)
	y, w := t.ISOWeek()
	fmt.Println(y, w)
	*/

	/**
	*在返回 t 时,位置信息设置为loc, 在中止情况下,如果 loc 为 nil 。
	*func (t Time) In(loc *Location) Time
	*/

	/*
	t := time.Date(2019, time.February, 7, 1, 0, 0, 0, time.UTC)
	loc, _ := time.LoadLocation("Europe/Berlin")
	time := t.In(loc)
	fmt.Println(time)
	*/

	/**
	*报告t是否代表Time零值的时间点,January 1, year 1, 00:00:00 UTC
	*func (t Time) IsZero() bool
	*/

	/*
	t := time.Date(2019, time.February, 7, 1, 0, 0, 0, time.UTC)
	fmt.Println(t.IsZero())
	*/

	/**
	* 返回采用本地和本地时区,但指向同一时间点的Time
	*func (t Time) Local() Time
	*/

	/*
	t := time.Date(2019, time.February, 7, 1, 0, 0, 0, time.UTC)
	t.Local()
	*/

	/**
	*返回t的地点和时区信息
	*func (t Time) Location() *Location
	*/

	/*
	t := time.Date(2019, time.February, 7, 0, 0, 0, 0, time.UTC)
	t.Location()
	*/

	/**
	*实现了encoding.BinaryMarshaler接口
	*func (t Time) MarshalBinary() ([]byte, error)
	*/

	/*
	t := time.Date(2019, time.February, 7, 0, 0, 0, 0, time.UTC)
	binaryBytes, _ := t.MarshalBinary()
	fmt.Println(binaryBytes)
	*/

	/**
	*MarshalJSON 实现了 json.Marshaler 接口。时间是以 RFC 3339 格式引用的字符串,
	*如果存在,则添加亚秒精度。
	*func (t Time) MarshalJSON() ([]byte, error)
	*/

	/*
	t := time.Date(2019, time.February, 7, 0, 0, 0, 0, time.UTC)
	jsonBytes, _ := t.MarshalJSON()
	fmt.Println(jsonBytes)
	*/

	/**
	*MarshalText 实现了 encoding.TextMarshaler 接口。时间格式为 RFC 3339 格式,如果存在,
	*则会添加亚秒精度。
	*func (t Time) MarshalText() ([]byte, error)
	*/

	/*
	t := time.Date(2019, time.February, 7, 0, 0, 0, 0, time.UTC)
	textBytes, _ := t.MarshalText()
	fmt.Println(textBytes)
	*/

	/**
	*分钟返回由 t 指定的小时内的分钟偏移量,范围为0,59(0到59)
	*func (t Time) Minute() int
	*/

	/*
	t := time.Date(2019, time.February, 7, 0, 3, 0, 0, time.UTC)
	fmt.Println(t.Minute())
	*/

	/**
	*月返回由 t 指定的年份的月份。
	*func (t Time) Month() Month
	*/

	/*
	t := time.Date(2019, time.February, 7, 0, 3, 0, 0, time.UTC)
	fmt.Println(t.Month())
	*/

	/**
	*毫微秒返回由秒指定的秒内的纳秒偏移量,范围为0,999999999(0到999999999
	*func (t Time) Nanosecond() int
	*/

	/*
	t := time.Date(2019, time.February, 7, 0, 3, 0, 0, time.UTC)
	fmt.Println(t.Nanosecond())
	*/

	/**
	*Round 将舍入 t 的结果返回到 d 的最近倍数(自零时间起)。中途值的舍入行为是四舍五入的。
	*如果 d <= 0,Round 将返回t中的任何单调时钟读数,但不改变。Round 在零时间之后作为绝
	*对持续时间运行; 它不适用于当时的演示文稿格式。因此, Round(Hour) 可能会返回非零分
	*钟的时间,具体取决于时间的位置。
	*func (t Time) Round(d Duration) Time
	*/

	/*
	t := time.Date(2019, time.February, 7, 0, 3, 0, 0, time.UTC)
	fmt.Println(t.Round(time.Hour))
	*/

	/**
	*Round 将舍入 t 的结果返回到 d 的最近倍数(自零时间起)。中途值的舍入行为是四舍五入的。
	*func (t Time) Second() int
	*/

	/*
	t := time.Date(2019, time.February, 7, 0, 3, 0, 0, time.UTC)
	fmt.Println(t.Second())
	*/

	/**
	*字符串返回使用格式字符串格式化的时间
	*func (t Time) String() string
	*/

	/*
	t := time.Date(2019, time.February, 7, 0, 3, 0, 0, time.UTC)
	fmt.Println(t.String())
	*/

	/**
	*字符串返回使用格式字符串格式化的时间
	*func (t Time) Sub(u Time) Duration
	*/

	/*
	start := time.Date(2000, 1, 1, 0, 0, 0, 0, time.UTC)
	end := time.Date(2000, 1, 1, 12, 0, 0, 0, time.UTC)

	difference := end.Sub(start)
	fmt.Printf("difference = %v\n", difference)
	*/

	/**
	*Truncate返回将t向下舍入为d的倍数的结果(因为零时间)。如果d <= 0,则Truncate返回t,
	*去除了任何单调时钟读数,但未更改。截断以从零时间开始的绝对持续时间为准。它不能按照当
	*时的演示形式进行操作。因此,根据时间的位置,Truncate(Hour)可能会返回一个非零分钟的时间。
	*func (t Time) Truncate(d Duration) Time
	*/

	/*
	t, _ := time.Parse("2006 Jan 02 15:04:05", "2012 Dec 07 12:15:30.918273645")
	trunc := []time.Duration{
		time.Nanosecond,
		time.Microsecond,
		time.Millisecond,
		time.Second,
		2 * time.Second,
		time.Minute,
		10 * time.Minute,
	}

	for _, d := range trunc {
		fmt.Printf("t.Truncate(%5s) = %s\n", d, t.Truncate(d).Format("15:04:05.999999999"))
	}
	midnight := time.Date(t.Year(), t.Month(), t.Day(), 0, 0, 0, 0, time.Local)
	_ = midnight
	*/

	/**
	*返回采用UTC和零时区,但指向同一时间点的Time
	*func (t Time) UTC() Time
	*/

	/*
	t := time.Date(2019, time.February, 7, 0, 0, 0, 0, time.UTC)
	t.UTC()
	*/


	/**
	*Unix 将 t 作为 Unix 时间返回,即 UTC 自1970年1月1日以来经过的秒数。
	*func (t Time) Unix() int64
	*/

	/*
	t := time.Date(2019, time.February, 7, 0, 0, 0, 0, time.UTC)
	fmt.Println(t.Unix())
	*/

	/**
	*UnixNano 返回 t 作为 Unix 时间,自 UTC  1970年1月1日起经过的纳秒数。
	*如果 Unix 时间(纳秒)不能用 int64(1678年之前或2262之后的日期)表示,
	*结果是不确定的。请注意,这意味着在零时间调用 UnixNano 的结果是未定义的。
	*func (t Time) UnixNano() int64
	*/

	/*
	t := time.Date(2019, time.February, 7, 0, 0, 0, 0, time.UTC)
	fmt.Println(t.UnixNano())
	*/


	/**
	*UnmarshalBinary 实现了 encoding.BinaryUnmarshaler 接口
	*func (t *Time) UnmarshalBinary(data []byte) error
	*/

	/*
	t := time.Date(2019, time.February, 7, 0, 0, 0, 0, time.UTC)
	binaryBytes, _ := t.MarshalBinary()
	_ = t.UnmarshalBinary(binaryBytes)
	*/


	/**
	*UnmarshalJSON 实现了 json.Unmarshaler 接口。预计该时间是RFC 3339格式中的带引号的字符串。
	*func (t *Time) UnmarshalJSON(data []byte) error
	*/

	/*
	t := time.Date(2019, time.February, 7, 0, 0, 0, 0, time.UTC)
	jsonBytes, _ := t.MarshalJSON()
	_ = t.UnmarshalJSON(jsonBytes)
	*/

	/**
	*实现了encoding.TextUnmarshaler接口
	*func (t *Time) UnmarshalText(data []byte) error
	*/

	/*
	t := time.Date(2019, time.February, 7, 0, 0, 0, 0, time.UTC)
	textBytes, _ := t.MarshalText()
	_ = t.UnmarshalText(textBytes)
	*/

	/**
	*平日返回由 t 指定的星期几。
	*func (t Time) Weekday() Weekday
	*/

	/*
	t := time.Date(2019, time.February, 7, 0, 0, 0, 0, time.UTC)
	w := t.Weekday()
	fmt.Println(w)
	*/

	/**
	*返回时间点t对应的年份
	*func (t Time) Year() int
	*/

	/*
	t := time.Date(2019, time.February, 7, 0, 0, 0, 0, time.UTC)
	y := t.Year()
	fmt.Println(y)
	*/

	/**
	* YearDay返回t指定的年份中的某一天,非闰年为 1,365,闰年为 1,366。
	*func (t Time) YearDay() int
	*/

	/*
	t := time.Date(2019, time.February, 7, 0, 0, 0, 0, time.UTC)
	y := t.YearDay()
	fmt.Println(y)
	*/

	/**
	*计算t所在的时区,返回该时区的规范名(如"CET")和该时区相对于UTC的时间偏移量(单位秒)
	*func (t Time) Zone() (name string, offset int)
	*/

	/*
	t := time.Date(2019, time.February, 7, 0, 0, 0, 0, time.UTC)
	t.Zone()
	*/

	/*********************************************************************/
	/******************** Timer中相关方法讲解 ***************************/
	/********************************************************************/

	/**
	*AfterFunc 等待持续时间,然后在自己的 goroutine 中调用 f 。它返回一个 Timer ,
	*它可以用来使用 Stop 方法取消调用。
	*func AfterFunc(d Duration, f func()) *Timer
	*/

	/*
	time.AfterFunc(time.Millisecond*1000, func() {
		fmt.Println("time after 1 second test")
	})
	*/

	/**
	*NewTimer 创建一个新的定时器,在至少持续时间 d 后,它将在其通道上发送当前时间。
	*func NewTimer(d Duration) *Timer
	*/

	/*
	time.NewTimer(time.Minute * 2)
	*/

	/**
	*重置更改定时器在持续时间 d 后过期。如果定时器处于活动状态,则返回 true ;
	*如果定时器已到期或已停止,则返回 false 。
	*func (t *Timer) Reset(d Duration) bool
	*/

	/*
	t := time.NewTimer(time.Minute * 2)
	t.Reset(time.Second)
	*/




	/**
	*停止防止定时器发射。如果调用停止定时器,则返回 true ;如果定时器已经过期或停止,
	*则返回 false 。停止不关闭通道,以防止从通道读取不正确的成功。为了防止在调用Stop
	*之后使用 NewTimer 创建的定时器触发,请检查返回值并排空通道。例如,假设程序尚未从
	*t.C 收到:
	*func (t *Timer) Stop() bool
	*/

	/*
	t := time.NewTimer(time.Minute * 2)
	t.Stop()
	*/

	/*********************************************************************/
	/******************** Weekday中相关方法讲解 ***********************/
	/*******************************************************************/


	/**
	*字符串返回当天的英文名称 ("Sunday", "Monday", ...) 。
	*func (d Weekday) String() string
	*/

	/*
	var a time.Weekday = 1
	fmt.Println(a.String())
	*/
}

小结

日常的开发工作中针对时间操作的场景还是非常多的,简单的如同时间的比较,复杂的如同获取当前时间之后的指定的时间、时间的相互转化,因此这个部分的内容是必须掌握的。

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值