Go语言时间编程

时间元素编程

打印当前时间,程序清单如下:

package main

import (
	"fmt"
	"time"
)
func main() {
	fmt.Println(time.Now())
}

运行结果如下:

2022-01-27 16:45:55.719641 +0800 CST m=+0.000094166

通过终端的命令行将时间设置为2050年元旦,如下:

sudo date +%Y%m%d -s "20500101

之后,通过上面代码试一下运行结果以后,出现2050-01-01 。

网络时间协议(Network Time Protocol,NTP)是一种在整个网络中同步时间的网络协议。

让程序休眠

休眠3s,程序清单如下:

package main

import (
	"fmt"
	"time"
)
func main() {
	fmt.Println("Start")
	time.Sleep(3*time.Second)
	fmt.Println("I'm awake")
}

运行结果:中间有休眠3s。

Start
I'm awake

设置超过时间

使用函数After触发超时,程序清单如下:

package main

import (
	"fmt"
	"time"
)
func main() {
	fmt.Println("You have two seconds to calculate 19 * 4")
	for {
		select{
		case <-time.After(2*time.Second):
			fmt.Println("Time's up! The answer is 74. Did you get it?")
			return
		}
	}
}

运行结果如下:中间有2s间隔时间。

You have two seconds to calculate 19 * 4
Time's up! The answer is 74. Did you get it?

使用ticker

使用ticker可让代码每隔特定的时间就重复执行一次。需要在很长的时间内定期执行任务时,使用ticker很有用。

使用ticker显示时间,程序清单如下:

package main

import (
	"fmt"
	"time"
)
func main() {
	c:=time.Tick(5*time.Second)
	for t:=range c{
		fmt.Printf("The time is now %v\n",t)
	}
}

运行结果如下:每隔5s重新显示当前时间,死循环。

The time is now 2022-01-27 17:02:32.433434 +0800 CST m=+5.001339425
The time is now 2022-01-27 17:02:37.433578 +0800 CST m=+10.001333286
The time is now 2022-01-27 17:02:42.433728 +0800 CST m=+15.001333825
The time is now 2022-01-27 17:02:47.433834 +0800 CST m=+20.001290053
The time is now 2022-01-27 17:02:52.434041 +0800 CST m=+25.001346373
The time is now 2022-01-27 17:02:57.434178 +0800 CST m=+30.001332645
The time is now 2022-01-27 17:03:02.43433 +0800 CST m=+35.001333344
The time is now 2022-01-27 17:03:07.434484 +0800 CST m=+40.001335474

以字符串格式表示时间

时间的字符串表示
类型字符串
ANSICMon Jan _2 15:04:05 2006
UnixDateMon Jan _2 15:04:05 MST 2006
RubyDateMon Jan _2 15:04:05 -0700 2006
RFC82202 Jan 06 15:04 MST
RFC822Z02 Jan 06 15:04 -0700
RFC850Monday, 02-Jan-06 15:04:05 MST
RFC1123Mon, 02 Jan 2006 15:04:05 MST
RFC1123ZMon, 02 Jan 2006 15:04:05 -0700
RFC33392006-01-02T15:04:05Z07:00
RFC3339Nano2006-01-02T15:04:05.999999999Z07:00

分析表示时间的字符串,程序清单如下:

package main

import (
	"fmt"
	"log"
	"time"
)
func main() {
	s:="2006-01-02T15:04:05+07:00"
	t,err:=time.Parse(time.RFC3339,s)
	if err!=nil{
		log.Fatal(err)
	}
	fmt.Println(t)
}

运行结果如下:

2006-01-02 15:04:05 +0700 +0700

使用结构体Time

结构体Time包含的方法,程序清单如下:

package main

import (
	"fmt"
	"log"
	"time"
)
func main() {
	s:="2006-01-02T15:04:05+07:00"
	t,err:=time.Parse(time.RFC3339,s)
	if err!=nil{
		log.Fatal(err)
	}
	fmt.Printf("The hour is %v\n",t.Hour())
	fmt.Printf("The minute is %v\n",t.Minute())
	fmt.Printf("The second is %v\n",t.Second())
	fmt.Printf("The day is %v\n",t.Day())
	fmt.Printf("The month is %v\n",t.Month())
	fmt.Printf("UNIX time is %v\n",t.Unix())
	fmt.Printf("The day of the week is %v\n",t.Weekday())
}

运行结果如下:

The hour is 15
The minute is 4
The second is 5
The day is 2
The month is January
UNIX time is 1136189045
The day of the week is Monday

时间加减

方法Add在即有时间的基础上加上一定的时间,程序清单如下:

package main

import (
	"fmt"
	"log"
	"time"
)
func main() {
	s:="2006-01-02T15:04:05+07:00"
	t,err:=time.Parse(time.RFC3339,s)
	if err!=nil{
		log.Fatal(err)
	}
	nt:=t.Add(2*time.Second)
	fmt.Printf("The hour is %v\n",t.Hour())
	fmt.Printf("The minute is %v\n",t.Minute())
	fmt.Printf("The second is %v\n",t.Second())
	fmt.Printf("The day is %v\n",t.Day())
	fmt.Printf("The month is %v\n",t.Month())
	fmt.Printf("UNIX time is %v\n",t.Unix())
	fmt.Printf("The day of the week is %v\n",t.Weekday())
	fmt.Printf("The hour is %v\n",nt.Hour())
	fmt.Printf("The minute is %v\n",nt.Minute())
	fmt.Printf("The second is %v\n",nt.Second())
	fmt.Printf("The day is %v\n",nt.Day())
	fmt.Printf("The month is %v\n",nt.Month())
	fmt.Printf("UNIX time is %v\n",nt.Unix())
	fmt.Printf("The day of the week is %v\n",nt.Weekday())
}

运行结果如下:

The hour is 15
The minute is 4
The second is 5
The day is 2
The month is January
UNIX time is 1136189045
The day of the week is Monday
The hour is 15
The minute is 4
The second is 7
The day is 2
The month is January
UNIX time is 1136189047
The day of the week is Monday

方法Sub从即有时间中送去指定的时间,程序清单如下:

nt:=t.Sub(2*time.Second)

运行结果如下:失败,不知道怎么解决。

比较两个不同的Time结构体

比较结构体Time,程序清单如下:

package main

import (
	"fmt"
	"log"
	"time"
)
func main() {
	s1:="2017-09-03T18:00:00+00:00"
	s2:="2017-09-04T18:00:00+00:00"
	today,err:=time.Parse(time.RFC3339,s1)
	if err!=nil{
		log.Fatal(err)
	}
	tomorrow,err:=time.Parse(time.RFC3339,s2)
	if err!=nil{
		log.Fatal(err)
	}
	fmt.Println(today.After(tomorrow))
	fmt.Println(today.Before(tomorrow))
	fmt.Println(today.Equal(tomorrow))
}

运行结果如下:

false
true
false

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值