• 【Go ~ 0到1 】 第七天 获取时间戳,时间比较,时间格式转换,Sleep与定时器


    1.时间戳

    1.1 获取当前时间戳

    时间戳是指 1970年1月1日 0时0分 都现在的 毫秒值

    //获取时间戳
    func getTimeStamp() {
    	now := time.Now()
    	unix := now.Unix()
    	fmt.Println(unix)
    }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6

    1.2 时间戳转换

    	now := time.Now()
    	// now是返回值
    	unix := now.Unix()
    	fmt.Println(unix)
    
    	// 将时间戳转换为时间
    	// time是包 Unix 是方法名
    
    	t := time.Unix(unix, 0)
    
    	fmt.Println(t)
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11

    2.时间加减,比较

    2.1 获取当前时间 年 月 日

    //获取当前时间
    now := time.Now()
    fmt.Println(now)
    
    //获取当前年份
    year := now.Year()
    
    //获取当前月
    month := now.Month()
    
    //获取当前日
    day := now.Day()
    
    //获取当前小时
    hour := now.Hour()
    
    //获取当前分钟
    minute := now.Minute()
    
    //获取当前秒
    second := now.Second()
    
    	fmt.Printf("当前时间为 %d 年 %d 月 %d 日 %d 时 %d 分 %d 秒", 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
    • 21
    • 22
    • 23

    2.2 添加时间

    	// 获取当前时间
    	now := time.Now()
    
    	// 当前时间加上 1.5小时  = 1小时 30 分钟
    	duration := time.Duration(time.Hour + time.Duration(30)*time.Minute)
    
    	add := now.Add(duration)
    
    	fmt.Println(add)
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9

    2.3 计算时间间隔

    	// 计算 2018年8月8日  到 1999年8月8日的时间间隔
    	// 东八区
    	location, _ := time.LoadLocation("Asia/Shanghai")
    	new := time.Date(2018, 8, 8, 0, 0, 0, 0, location)
    	old := time.Date(1999, 8, 8, 0, 0, 0, 0, location)
    
    	sub := new.Sub(old)
    
    	fmt.Println(sub)
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9

    2.4 判断时间是否在前 Before

    	//获取当前时间
    	now := time.Now()
    
    	//当前时间减去30分钟  注意是 负号
    	duration := time.Minute * 30
    
    	old := now.Add(-duration)
    
    	// 判断 old 是否在 new 之前
    	flag := old.Before(now)
    
    	fmt.Println(flag)  // 输出结果 true
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12

    2.5 判断当前时间是否在后 After

    	//获取当前时间
    	now := time.Now()
    
    	//当前时间 加了一天
    	add := now.Add(time.Duration(time.Hour) * 24)
    
    	// 判断  add 是否在  now 之后
    	println(add.After(now)) // true
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8

    3. Sleep 与 定时器

    3.1 Sleep

    	fmt.Println("休眠5秒 ", time.Now().Second())
    	//程序运行到此处休眠5秒
    	time.Sleep(time.Second * 5)
    	fmt.Println("休眠结束 ", time.Now().Second())
    
    • 1
    • 2
    • 3
    • 4

    3.2 定时器

    	num := 0
    	// 定义一个每秒执行一次的定时器
    	for t := range time.Tick(time.Second) {
    		//执行5秒后结束
    		if num++; num >= 5 {
    			return
    		}
    		fmt.Println(t.Second())
    
    	}
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10

    4. 时间格式化与转换

    4.1 时间格式化输出

    固定数字对应格式
    2006yyyy
    01MM
    02dd
    15HH
    04mm
    05SS
    	// 获取当前时间
    	now := time.Now()
    
    	// 将当前时间格式化
    	format := now.Format("2006/01/02 15/04/05")
    
    	fmt.Println(format)
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7

    4.2 将字符串转换为 时间

    	str := "2022/07/04 22/40/50"
    
    	t, _ := time.Parse("2006/01/02 15/04/05", str)
    
    	fmt.Println(t) //默认时区 2022-07-04 22:40:50 +0000 UTC
    
    	// 以指定的时区解析
    	location, _ := time.LoadLocation("Asia/Shanghai")
    
    	inLocation, _ := time.ParseInLocation("2006/01/02 15/04/05", str, location)
    
    	fmt.Println(inLocation) //东八区 2022-07-04 22:40:50 +0800 CST
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12

    end 练习

    1.统计程序运行时间,精确到纳秒

    	begin := time.Now().Nanosecond()
    	beginUnixMilli := time.Now().UnixMilli()
    	beginSecond := time.Now().Second()
    	strToTime()
    	fmt.Printf("程序运行 %d 纳秒 \n", time.Now().Nanosecond()-begin)
    	fmt.Printf("程序运行 %d 毫秒 \n", time.Now().UnixMilli()-beginUnixMilli)
    	fmt.Printf("程序运行 %d 秒", time.Now().Second()-beginSecond)
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
  • 相关阅读:
    【Linux】进程替换
    计算机辅助药物设计- - 从蛋白质结构到药物候选物的全方位实战
    node的express模块
    JVisualVM连接远程阿里云Linux
    锐捷EG易网关 phpinfo.view.php 信息泄露
    一文看懂Oracle 19c OCM认证考试(需要Oracle OCP证书)
    docker中odoo项目路径
    js常见面试题
    正则 删除特定字符后面所有的元素(不区分大小写)
    《动态规划 ---- 线性规划一》----- 动态规划的基本概念,线性动态规划-->背包问题
  • 原文地址:https://blog.csdn.net/JAVAlife2021/article/details/125610368