当前位置:网站首页>[go ~ 0 to 1] obtain timestamp, time comparison, time format conversion, sleep and timer on the seventh day
[go ~ 0 to 1] obtain timestamp, time comparison, time format conversion, sleep and timer on the seventh day
2022-07-07 08:15:00 【Autumn sunset】
1. Time stamp
1.1 Get the current timestamp
Time stamp means 1970 year 1 month 1 Japan 0 when 0 branch To the present Millisecond value
// Get the timestamp
func getTimeStamp() {
now := time.Now()
unix := now.Unix()
fmt.Println(unix)
}
1.2 Timestamp conversion
now := time.Now()
// now Is the return value
unix := now.Unix()
fmt.Println(unix)
// Convert timestamps to time
// time It's a bag Unix It's the method name
t := time.Unix(unix, 0)
fmt.Println(t)
2. Time plus minus , Compare
2.1 Get the current time year month Japan
// Get the current time
now := time.Now()
fmt.Println(now)
// Get current year
year := now.Year()
// Get the current month
month := now.Month()
// Get current day
day := now.Day()
// Get the current hour
hour := now.Hour()
// Get the current minute
minute := now.Minute()
// Get the current second
second := now.Second()
fmt.Printf(" The current time is %d year %d month %d Japan %d when %d branch %d second ", year, month, day, hour, minute, second)
2.2 Add the time
// Get the current time
now := time.Now()
// The current time plus 1.5 Hours = 1 Hours 30 minute
duration := time.Duration(time.Hour + time.Duration(30)*time.Minute)
add := now.Add(duration)
fmt.Println(add)
2.3 Calculate the time interval
// Calculation 2018 year 8 month 8 Japan To 1999 year 8 month 8 The time interval between days
// East eight
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)
2.4 Judge whether the time is before Before
// Get the current time
now := time.Now()
// Subtract... From the current time 30 minute Note that Minus sign
duration := time.Minute * 30
old := now.Add(-duration)
// Judge old Whether in new Before
flag := old.Before(now)
fmt.Println(flag) // Output results true
2.5 Judge whether the current time is later After
// Get the current time
now := time.Now()
// current time Extra day
add := now.Add(time.Duration(time.Hour) * 24)
// Judge add Whether in now after
println(add.After(now)) // true
3. Sleep And Timer
3.1 Sleep
fmt.Println(" Sleep 5 second ", time.Now().Second())
// The program sleeps here 5 second
time.Sleep(time.Second * 5)
fmt.Println(" End of sleep ", time.Now().Second())
3.2 Timer
num := 0
// Define a timer that executes once per second
for t := range time.Tick(time.Second) {
// perform 5 Seconds later
if num++; num >= 5 {
return
}
fmt.Println(t.Second())
}
4. Time formatting and conversion
4.1 Time format output
Fixed number | Corresponding format |
---|---|
2006 | yyyy |
01 | MM |
02 | dd |
15 | HH |
04 | mm |
05 | SS |
// Get the current time
now := time.Now()
// Format the current time
format := now.Format("2006/01/02 15/04/05")
fmt.Println(format)
4.2 Convert string to Time
str := "2022/07/04 22/40/50"
t, _ := time.Parse("2006/01/02 15/04/05", str)
fmt.Println(t) // Default time zone 2022-07-04 22:40:50 +0000 UTC
// Resolve in the specified time zone
location, _ := time.LoadLocation("Asia/Shanghai")
inLocation, _ := time.ParseInLocation("2006/01/02 15/04/05", str, location)
fmt.Println(inLocation) // East eight 2022-07-04 22:40:50 +0800 CST
end practice
1. Count the running time of the program , Accurate to nanoseconds
begin := time.Now().Nanosecond()
beginUnixMilli := time.Now().UnixMilli()
beginSecond := time.Now().Second()
strToTime()
fmt.Printf(" The program runs %d nanosecond \n", time.Now().Nanosecond()-begin)
fmt.Printf(" The program runs %d millisecond \n", time.Now().UnixMilli()-beginUnixMilli)
fmt.Printf(" The program runs %d second ", time.Now().Second()-beginSecond)
边栏推荐
- Fast parsing intranet penetration escorts the document encryption industry
- 解析机器人科技发展观对社会研究论
- 运放电路的反馈电阻上并联一个电容是什么作用
- Myabtis_ Plus
- Unityhub cracking & unity cracking
- 轻松上手Fluentd,结合 Rainbond 插件市场,日志收集更快捷
- Vulnerability recurrence easy_ tornado
- 【数字IC验证快速入门】14、SystemVerilog学习之基本语法1(数组、队列、结构体、枚举、字符串...内含实践练习)
- 机器人教育在动手实践中的真理
- JS复制图片到剪切板 读取剪切板
猜你喜欢
Interactive book delivery - signed version of Oracle DBA work notes
Network learning (III) -- highly concurrent socket programming (epoll)
王爽 《汇编语言》之寄存器
在Rainbond中实现数据库结构自动化升级
Qinglong panel - today's headlines
Bugku CTF daily one question chessboard with only black chess
Leetcode medium question my schedule I
opencv学习笔记五——梯度计算/边缘检测
提高企业产品交付效率系列(1)—— 企业应用一键安装和升级
在 Rainbond 中一键安装高可用 Nacos 集群
随机推荐
Zsh shell adds automatic completion and syntax highlighting
LeetCode简单题之判断一个数的数字计数是否等于数位的值
Recursive method constructs binary tree from middle order and post order traversal sequence
Use of out covariance and in inversion in kotlin
Lattice coloring - matrix fast power optimized shape pressure DP
Rainbond 5.7.1 支持对接多家公有云和集群异常报警
Avatary的LiveDriver试用体验
offer收割机:两个长字符串数字相加求和(经典面试算法题)
ROS Bridge 笔记(05)— carla_ackermann_control 功能包(将Ackermann messages 转化为 CarlaEgoVehicleControl 消息)
Bugku CTF daily one question chessboard with only black chess
太真实了,原来自己一直没有富裕起来是有原因的
互动送书-《Oracle DBA工作笔记》签名版
Interview questions (CAS)
Minimum absolute difference of binary search tree (use medium order traversal as an ordered array)
JS copy picture to clipboard read clipboard
Quick analysis of Intranet penetration helps the foreign trade management industry cope with a variety of challenges
LeetCode简单题之字符串中最大的 3 位相同数字
Network learning (II) -- Introduction to socket
Find the mode in the binary search tree (use medium order traversal as an ordered array)
Leetcode 187 Repeated DNA sequence (2022.07.06)