时间元素编程
打印当前时间,程序清单如下:
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
以字符串格式表示时间
类型 | 字符串 |
ANSIC | Mon Jan _2 15:04:05 2006 |
UnixDate | Mon Jan _2 15:04:05 MST 2006 |
RubyDate | Mon Jan _2 15:04:05 -0700 2006 |
RFC822 | 02 Jan 06 15:04 MST |
RFC822Z | 02 Jan 06 15:04 -0700 |
RFC850 | Monday, 02-Jan-06 15:04:05 MST |
RFC1123 | Mon, 02 Jan 2006 15:04:05 MST |
RFC1123Z | Mon, 02 Jan 2006 15:04:05 -0700 |
RFC3339 | 2006-01-02T15:04:05Z07:00 |
RFC3339Nano | 2006-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