运维八一 运维八一
首页
运维杂记
编程浅尝
周积跬步
专栏
生活
关于
收藏
  • 分类
  • 标签
  • 归档
Source (opens new window)

运维八一

运维,运维!
首页
运维杂记
编程浅尝
周积跬步
专栏
生活
关于
收藏
  • 分类
  • 标签
  • 归档
Source (opens new window)
  • Go

    • 前言

    • Go基础知识

    • Go基本语法

    • 实战项目:简单web服务

    • 基本数据类型

    • 内置运算符

    • 分支和循环

    • 函数 function

    • 结构体 struct

    • 方法 method

    • 实战项目:跟踪函数调用链

    • 接口 interface

    • 并发 concurrency

    • 指针

    • 实战项目:实现轻量级线程池

    • 实战项目:实现TCP服务器

    • go常用包

      • fmt包
      • time包
        • 2. time
          • 2.1 时间转换
          • 2.2 时间类型
          • 2.3 时间戳
          • 2.4 时间间隔
          • 2.5 时间格式化
          • 2.6 时间操作函数
          • 2.7 定时器
      • os包
      • flag包
      • net-http包
    • Gin框架

    • go随记

  • Python

  • Shell

  • Java

  • Vue

  • 前端

  • 编程浅尝
  • Go
  • go常用包
lyndon
2022-06-23
目录

time包

# 2. time

  • 时间对象: golang中定义的一个对象
  • 时间戳: 秒的整数形式
  • 格式化时间: 给人看的时间(2022-03-27 09:15:29)

# 2.1 时间转换

package main

import (
	"fmt"
	"time"
)

func main() {
	// 1、时间对象
	now := time.Now()
	// 2、格式化时间
	strTime := now.Format("2006-01-02 15:04:05")
	// 3、时间搓(从1970年1月1日 开始到现在多少s的时间)
	fmt.Println(now.Unix())
	// 4、格式化时间转时间对象
	loc, _ := time.LoadLocation("Asia/Shanghai")
	timeObj, _ := time.ParseInLocation("2006-01-02 15:04:05", strTime, loc)
	fmt.Println(timeObj.Unix()) // 1647858133
}
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19

# 2.2 时间类型

可以通过 time.Now()函数获取当前的时间对象,然后获取时间对象的年月日时分秒等信息。

注意:%02d 中的 2 表示宽度,如果整数不够 2 列就补上 0

package main

import (
	"fmt"
	"time"
)

func main() {
	now := time.Now() //获取当前时间
	fmt.Printf("current time:%v\n", now)
	year := now.Year()     //年
	month := now.Month()   //月
	day := now.Day()       //日
	hour := now.Hour()     //小时
	minute := now.Minute() //分钟
	second := now.Second() //秒
	// 打印结果为:2021-05-19 09:20:06
	fmt.Printf("%d-%02d-%02d %02d:%02d:%02d\n", year, month, day, hour, minute,
		second)
}
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20

# 2.3 时间戳

package main

import (
	"fmt"
	"time"
)

func main() {
	now := time.Now()   //获取当前时间
	timestamp1 := now.Unix()   //时间戳
	timestamp2 := now.UnixNano()  //纳秒时间戳
	fmt.Printf("current timestamp1:%v\n", timestamp1) // current timestamp1:1623560753
	fmt.Printf("current timestamp2:%v\n", timestamp2) // current timestamp2:1623560753965606600
}
1
2
3
4
5
6
7
8
9
10
11
12
13
14

使用 time.Unix() 函数可以将时间戳转为时间格式

func timestampDemo2(timestamp int64) {
	timeObj := time.Unix(timestamp, 0) //将时间戳转为时间格式
	fmt.Println(timeObj)
	year := timeObj.Year() //年
	month := timeObj.Month() //月
	day := timeObj.Day() //日
	hour := timeObj.Hour() //小时
	minute := timeObj.Minute() //分钟
	second := timeObj.Second() //秒
	fmt.Printf("%d-%02d-%02d %02d:%02d:%02d\n", year, month, day, hour, minute,
	second)
}
1
2
3
4
5
6
7
8
9
10
11
12

# 2.4 时间间隔

time.Duration 是 time 包定义的一个类型,它代表两个时间点之间经过的时间,以纳秒为单位。

time.Duration 表示一段时间间隔,可表示的最长时间段大约290年。

time包中定义的时间间隔类型的常量如下:

const (
	Nanosecond Duration = 1
	Microsecond = 1000 * Nanosecond
	Millisecond = 1000 * Microsecond
	Second = 1000 * Millisecond
	Minute = 60 * Second
	Hour = 60 * Minute
)
1
2
3
4
5
6
7
8

# 2.5 时间格式化

时间类型有一个自带的方法 Format 进行格式化,需要注意的是Go语言中格式化时间模板不是常见的 Y-m-d H:M:S,而是使用Go的诞生时间2006年1月2号15点04分(记忆口诀为2006 1 2 3 4)。

补充:如果想格式化为12小时方式,需指定 PM 。

package main

import (
	"fmt"
	"time"
)

func main() {
	now := time.Now()
	// 格式化的模板为Go的出生时间2006年1月2号15点04分 Mon Jan
	fmt.Println(now.Format("2006-01-02 15:04:05.000 Mon Jan")) // 2021-06-13 13:10:18.143 Sun Jun
}
1
2
3
4
5
6
7
8
9
10
11
12

字符串转时间类型

package main

import (
	"fmt"
	"time"
)

func main() {
	loc, _ := time.LoadLocation("Asia/Shanghai")
	// 按照指定时区和指定格式解析字符串时间
	timeObj, err := time.ParseInLocation("2006-01-02 15:04:05", "2019-08-04 14:15:20",
		loc)
	if err != nil {
		fmt.Println(err)
		return
	}
	fmt.Println(timeObj) // 2019-08-04 14:15:20 +0800 CST
}

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19

# 2.6 时间操作函数

  • Add
    • 在日常的编码过程中可能会遇到要求时间+时间间隔的需求
    • Go 语言的时间对象有提供Add 方法如下
  • Sub
    • 求两个时间之间的差值
package main
import (
	"fmt"
	"time"
)

func main() {
	now := time.Now()   // 获取当前时间
	// 10分钟前
	m, _ := time.ParseDuration("-1m")
	m1 := now.Add(m)
	fmt.Println(m1)
	// 8个小时前
	h, _ := time.ParseDuration("-1h")
	h1 := now.Add(8 * h)
	fmt.Println(h1)
	// 一天前
	d, _ := time.ParseDuration("-24h")
	d1 := now.Add(d)
	fmt.Println(d1)

	// 10分钟后
	mm, _ := time.ParseDuration("1m")
	mm1 := now.Add(mm)
	fmt.Println(mm1)
	// 8小时后
	hh, _ := time.ParseDuration("1h")
	hh1 := now.Add(hh)
	fmt.Println(hh1)
	// 一天后
	dd, _ := time.ParseDuration("24h")
	dd1 := now.Add(dd)
	fmt.Println(dd1)

	// Sub 计算两个时间差
	subM := now.Sub(m1)
	fmt.Println(subM.Minutes(), "分钟")   // 1 分钟
	sumH := now.Sub(h1)
	fmt.Println(sumH.Hours(), "小时")    // 8 小时
	sumD := now.Sub(d1)
	fmt.Printf("%v 天\n", sumD.Hours()/24)   // 1 天
}
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
34
35
36
37
38
39
40
41
42
  • Equal
    • 判断两个时间是否相同,会考虑时区的影响,因此不同时区标准的时间也可以正确比较。
    • 本方法和用t==u不同,这种方法还会比较地点和时区信息。
  • Before
    • 如果t代表的时间点在u之前,返回真;否则返回假。
  • After
    • 如果t代表的时间点在u之后,返回真;否则返回假。

# 2.7 定时器

使用time.Tick(时间间隔)来设置定时器,定时器的本质上是一个通道(channel)。

func tickDemo() {
	ticker := time.Tick(time.Second) //定义一个1秒间隔的定时器
	for i := range ticker {
		fmt.Println(i)//每秒都会执行的任务
	}
}
1
2
3
4
5
6
上次更新: 2022/06/23, 23:41:50
fmt包
os包

← fmt包 os包→

最近更新
01
ctr和crictl显示镜像不一致
03-13
02
alpine镜像集成常用数据库客户端
03-13
03
create-cluster
02-26
更多文章>
Theme by Vdoing | Copyright © 2015-2024 op81.com
苏ICP备18041258号-2
  • 跟随系统
  • 浅色模式
  • 深色模式
  • 阅读模式