Get the current time

import time

func getCurTime(a) {
	// Local time (if you are in China, the time is e8)
	curLocalTime := time.Now()
	/ / UTC time
	curUTCTime := time.Now().UTC()
	fmt.Println(curLocalTime, curUTCTime)
}
Copy the code

Set the time zone

func setTimeZone(a) {
	curLocalTime := time.Now()
	curUtcTime := curLocalTime.In(time.UTC)
	fmt.Println(curUtcTime)
}
Copy the code

Time type to string: Format function

func time2TimeStr(a) {
	localTimeStr := time.Now().Format("The 2006-01-02 15:04:05")
	/ / UTC time
	utcTimeStr := time.Now().UTC().Format("The 2006-01-02 15:04:05")
	fmt.Println(localTimeStr, utcTimeStr)
}
Copy the code

Time type transfer timestamp: Unix function

func getCurTimeStamp(a) {
	// Time stamp, accurate to second
	timestamp := time.Now().Unix()
	// Time stamp, accurate to nanoseconds
	timestampNano := time.Now().UnixNano()
	fmt.Println(timestamp, timestampNano)
}

Copy the code

Timestamp transfer time type: Unix function

func timestamp2Time(a) {
	timestamp := time.Now().Unix()
	localTimeObj := time.Unix(timestamp, 0)
	fmt.Println(localTimeObj)
}
Copy the code

Time string transfer time type: parse function

func timeStr2Time(a) {
	timeStr := "The 2020-01-13 22:32:17"
	// Return 2020-01-13 22:32:17 +0000 UTC
	utcTimeObj, err := time.Parse("The 2006-01-02 15:04:05", timeStr)
	if err == nil {
		fmt.Println(utcTimeObj, utcTimeObj.Unix())
	}

	// The local time is 2020-01-13 22:32:17 +0800 CST
	localTimeObj, err := time.ParseInLocation("The 2006-01-02 15:04:05", timeStr, time.Local)
	if err == nil {
		fmt.Println(localTimeObj)
	}
}
Copy the code

Time string to timestamp:

Parse functions + Unix functions

Timestamp transfer string:

Unix function + Format function

Time to add and subtract

Note :(it is easier to add or subtract when converting to Time)

func addTime(a) {
	curTime := time.Now()
	/ / add 1 seconds
	addSecondTime := curTime.Add(time.Second * 1)
	// Add 1 minute
	addMinuteTime := curTime.Add(time.Minute * 1)
	addMinuteTime2 := curTime.Add(time.Second * time.Duration(60*1))
	fmt.Println(addSecondTime, addMinuteTime, addMinuteTime2)
}
Copy the code

Note: this is a part of the extract from wants to enter feifei “Golang cycle tasks, timing, task” : www.jianshu.com/p/4955e5d65…

Periodic task: Every 5 seconds

func intervalTask(a) {
	ticker := time.NewTicker(time.Second * 5)
	go func(a) {
		for _ = range ticker.C {
			fmt.Println("do the thing")
		}
	}()

	time.Sleep(time.Minute)
}

Copy the code

Countdown task: Main thread blocked

func countDownSyncTask(a) {
	timer1 := time.NewTimer(time.Second * 5)
	<-timer1.C
	fmt.Println("do the thing")}Copy the code

Countdown task: Main thread is not blocked

func countDownAsyncTask(a) {
	timer1 := time.NewTimer(time.Second * 3)

	go func(a) {
		// Wait for the trigger signal
		<-timer1.C
		fmt.Println("do the thing")
	}()
	// Since the above wait signal is in a new thread, the code continues to execute, stopping the timer
	time.Sleep(time.Second * 10)}Copy the code

A scheduled task is triggered at 10:00:00 every day

func scheduleTask(a) {
	go func(a) {
		for {
			fmt.Println("Triggered event")
			now := time.Now()
			// Calculate the next zero
			next := now.Add(time.Hour * 24)
			next = time.Date(next.Year(), next.Month(), next.Day(), 10.0.0.0, next.Location())
			fmt.Println(next.Sub(now))
			t := time.NewTimer(next.Sub(now))
			<-t.C
		}
	}()
	time.Sleep(time.Hour * 24)}Copy the code