2022-4-24 实习Day21

1、跨支付前置系统报文查看,GPI问题测试 –3小时 100%
2、报文查看,GPI问题测试文档编写 –3小时 100%
3、跨支付前置系统监控模块问题测试 –1小时 30%

Golang编程学习(part 17)

1、时间和日期相关函数

① 时间和日期相关函数,需要导入time包import "time"
② time.Time 类型,用于表示时间
1
2
3
4
5
now := time.Now()
fmt.Printf("now=%v\ntype=%T", now, now)

now=2022-05-02 13:11:59.4656385 +0800 CST m=+0.003187201
type=time.Time
③ 如何获取到其他的日期信息
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
package main
import (
"fmt"
"time"
)
func main() {
// 获取当前时间
now := time.Now()
a, b, c := now.Date()
fmt.Println(a)
fmt.Println(b)
fmt.Println(c)
fmt.Println()
fmt.Printf("年=%v\n", now.Year())
fmt.Printf("月=%v\n", now.Month())
fmt.Printf("月=%v\n", int(now.Month()))
fmt.Printf("日=%v\n", now.Day())
fmt.Printf("时=%v\n", now.Hour())
fmt.Printf("分=%v\n", now.Minute())
fmt.Printf("秒=%v\n", now.Second())
}

2022
May
2

年=2022
月=May
月=5
日=2
时=13
分=22
秒=12
④ 格式化日期时间
方式1:就是使用Printf或者Sprintf
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
package main
import (
"fmt"
"time"
)
func main() {
now := time.Now()
fmt.Printf("当前年月日 %d-%d-%d %d:%d:%d \n",
now.Year(), now.Month(), now.Day(),
now.Hour(), now.Minute(), now.Second())

dateStr := fmt.Sprintf("当前年月日 %d-%d-%d %d:%d:%d \n",
now.Year(), now.Month(), now.Day(),
now.Hour(), now.Minute(), now.Second())
fmt.Printf("dateStr=%v\n", dateStr)
}
方式二:使用time.Format()方法完成
1
2
3
4
5
6
7
8
9
10
11
12
13
14
package main
import (
"fmt"
"time"
)
func main() {
now := time.Now()
fmt.Printf(now.Format("2006-01-02 15:04:05"))
fmt.Println()
fmt.Printf(now.Format("2006-01-02"))
fmt.Println()
fmt.Printf(now.Format("15:04:05"))
fmt.Println()
}

"2006-01-02 15:04:05"这个字符串的各个数字是固定的,必须这样写。

"2006-01-02 15:04:05"这个字符串的各个数字可以自由的组合,这样可以按程序需求来返回时间和日期。

⑤ 时间常量
const (

    Nanosecond  Duration = 1        //纳秒                    

    Microsecond = 1000 * Nanosecond //微秒

    Millisecond = 1000 * Microsecond//毫秒

    Second = 1000 * Millisecond     //秒

    Minute = 60 * Second            //分钟                      

    Hour = 60 * Minute              //小时                         

)
常量的作用:在程序中可用于获取指定时间单位的时间,比如想得到 100 毫秒100*time.Millisecond
⑥ 结合 Sleep 来使用一下时间常量
1
2
3
4
5
// 需求,每隔0.1秒打印一个数字,打印到100就退出
for i := 1 ; i < 101 ; i++{
fmt.Println(i)
time.Sleep(time.Millisecond*100)
}
⑦ time的Unix 和 UnixNano的方法【可以用来计算程序执行时间!!!】
1
2
3
// Unix将t表示为Unix时间,即从时间点January 1,1970 UTC
// 到时间t所经过的时间(单位秒)
func (t Time) Unix() int64
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
// UnixNano将t表示为Unix时间,即从时间的January 1,1970 UTC
// 到时间t所经过的时间(单位纳秒)。如果纳秒为单位的unix时间超出了int64
// 能表示的范围,结果是未定义的。注意这就意味着Time零值调用UnixNano方法的话,结果是未定义的
package main

import (
"fmt"
"time"
)

func main() {

now := time.Now()
fmt.Printf("unix时间戳=%v unixnano时间戳=%v\n",now.Unix(), now.UnixNano())
}

unix时间戳=1651471639
unixnano时间戳=1651471639764079600