技术标签: GO语言
每种语言都需要对时间进行处理,golang当然也不例外,go语言为我们提供了time package用于各种时间的转换,处理。
Package time provides functionality for measuring and displaying time.
获取当前时间
func Now
func Now() Time
1
Now returns the current local time.
func (Time) UTC
func (t Time) UTC() Time
1
UTC returns t with the location set to UTC.
func (Time) Unix
func (t Time) Unix() int64
1
Unix returns t as a Unix time, the number of seconds elapsed since January 1, 1970 UTC.
func (Time) UnixNano
func (t Time) UnixNano() int64
1
UnixNano returns t as a Unix time, the number of nanoseconds elapsed since January 1, 1970 UTC.
看到Unix和UnixNano的区别了吧,就是精度不同而已:
package main
import (
"fmt"
"strconv"
"time"
)
func main() {
t := time.Now()
fmt.Println(t)
fmt.Println(t.UTC().Format(time.UnixDate))
fmt.Println(t.Unix())
timestamp := strconv.FormatInt(t.UTC().UnixNano(), 10)
fmt.Println(timestamp)
timestamp = timestamp[:10]
fmt.Println(timestamp)
}
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
输出:
2017-06-21 11:52:29.0826692 +0800 CST
Wed Jun 21 03:52:29 UTC 2017
1498017149
1498017149082669200
1498017149
时间格式化字符串转换
func Parse
package main
import (
"fmt"
"strconv"
"time"
)
func main() {
const longForm = "Jan 2, 2006 at 3:04pm (MST)"
t, _ := time.Parse(longForm, "Jun 21, 2017 at 0:00am (PST)")
fmt.Println(t)
const shortForm = "2006-Jan-02"
t, _ = time.Parse(shortForm, "2017-Jun-21")
fmt.Println(t)
t, _ = time.Parse("01/02/2006", "06/21/2017")
fmt.Println(t)
fmt.Println(t.Unix())
i, err := strconv.ParseInt("1498003200", 10, 64)
if err != nil {
panic(err)
}
tm := time.Unix(i, 0)
fmt.Println(tm)
var timestamp int64 = 1498003200
tm2 := time.Unix(timestamp, 0)
fmt.Println(tm2.Format("2006-01-02 03:04:05 PM"))
fmt.Println(tm2.Format("02/01/2006 15:04:05 PM"))
}
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
输出:
2017-06-21 00:00:00 +0000 PST
2017-06-21 00:00:00 +0000 UTC
2017-06-21 00:00:00 +0000 UTC
1498003200
2017-06-21 08:00:00 +0800 CST
2017-06-21 08:00:00 AM
21/06/2017 08:00:00 AM
再看一个例子:
package main
import (
"fmt"
"strings"
"time"
)
func main() {
var dates [4]time.Time
dates[0], _ = time.Parse("2006-01-02 15:04:05.000000000 MST -07:00", "1609-09-12 19:02:35.123456789 PDT +03:00")
dates[1], _ = time.Parse("2006-01-02 03:04:05 PM -0700", "1995-11-07 04:29:43 AM -0209")
dates[2], _ = time.Parse("PM -0700 01/02/2006 03:04:05", "AM -0209 11/07/1995 04:29:43")
dates[3], _ = time.Parse("Time:Z07:00T15:04:05 Date:2006-01-02 ", "Time:-03:30T19:18:35 Date:2119-10-29")
defaultFormat := "2006-01-02 15:04:05 PM -07:00 Jan Mon MST"
formats := []map[string]string{
{"format": "2006", "description": "Year"},
{"format": "06", "description": "Year"},
{"format": "01", "description": "Month"},
{"format": "1", "description": "Month"},
{"format": "Jan", "description": "Month"},
{"format": "January", "description": "Month"},
{"format": "02", "description": "Day"},
{"format": "2", "description": "Day"},
{"format": "Mon", "description": "Week day"},
{"format": "Monday", "description": "Week day"},
{"format": "03", "description": "Hours"},
{"format": "3", "description": "Hours"},
{"format": "15", "description": "Hours"},
{"format": "04", "description": "Minutes"},
{"format": "4", "description": "Minutes"},
{"format": "05", "description": "Seconds"},
{"format": "5", "description": "Seconds"},
{"format": "PM", "description": "AM or PM"},
{"format": ".000", "description": "Miliseconds"},
{"format": ".000000", "description": "Microseconds"},
{"format": ".000000000", "description": "Nanoseconds"},
{"format": "-0700", "description": "Timezone offset"},
{"format": "-07:00", "description": "Timezone offset"},
{"format": "Z0700", "description": "Timezone offset"},
{"format": "Z07:00", "description": "Timezone offset"},
{"format": "MST", "description": "Timezone"}}
for _, date := range dates {
fmt.Printf("\n\n %s \n", date.Format(defaultFormat))
fmt.Printf("%-15s + %-12s + %12s \n", strings.Repeat("-", 15), strings.Repeat("-", 12), strings.Repeat("-", 12))
fmt.Printf("%-15s | %-12s | %12s \n", "Type", "Placeholder", "Value")
fmt.Printf("%-15s + %-12s + %12s \n", strings.Repeat("-", 15), strings.Repeat("-", 12), strings.Repeat("-", 12))
for _, f := range formats {
fmt.Printf("%-15s | %-12s | %-12s \n", f["description"], f["format"], date.Format(f["format"]))
}
fmt.Printf("%-15s + %-12s + %12s \n", strings.Repeat("-", 15), strings.Repeat("-", 12), strings.Repeat("-", 12))
}
}
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
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
time包的一些其他用法
time包很强大,这里不能整个篇幅的进行介绍,可以自己去看官方的文档。
func Date
func Date(year int, month Month, day, hour, min, sec, nsec int, loc *Location) Time
1
time常用方法
After(u Time) bool
时间类型比较,是否在Time之后
Before(u Time) bool
时间类型比较,是否在Time之前
Equal(u Time) bool
比较两个时间是否相等
IsZero() bool
判断时间是否为零值,如果sec和nsec两个属性都是0的话,则该时间类型为0
Date() (year int, month Month, day int)
返回年月日,三个参数
Year() int
返回年份
Month() Month
返回月份.是Month类型
Day() int
返回多少号
Weekday() Weekday
返回星期几,是Weekday类型
ISOWeek() (year, week int)
返回年份,和该填是在这年的第几周.
Clock() (hour, min, sec int)
返回小时,分钟,秒
Hour() int
返回小时
Minute() int
返回分钟
Second() int
返回秒数
Nanosecond() int
返回纳秒
应用:
package main
import "fmt"
import "time"
func main() {
p := fmt.Println
now := time.Now()
p(now)
then := time.Date(
2017, 06, 21, 20, 34, 58, 0, time.UTC)
p(then)
p(then.Year())
p(then.Month())
p(then.Day())
p(then.Hour())
p(then.Minute())
p(then.Second())
p(then.Nanosecond())
p(then.Location())
p(then.Weekday())
p(then.Before(now))
p(then.After(now))
p(then.Equal(now))
diff := now.Sub(then)
p(diff)
p(diff.Hours())
p(diff.Minutes())
p(diff.Seconds())
p(diff.Nanoseconds())
p(then.Add(diff))
p(then.Add(-diff))
}
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
输出:
2017-06-21 13:22:36.5138633 +0800 CST
2017-06-21 20:34:58 +0000 UTC
2017
June
21
20
34
58
0
UTC
Wednesday
false
true
false
-15h12m21.4861367s
-15.205968371305556
-912.3581022783334
-54741.4861367
-54741486136700
2017-06-21 05:22:36.5138633 +0000 UTC
2017-06-22 11:47:19.4861367 +0000 UTC
---------------------
原文:https://blog.csdn.net/wangshubo1989/article/details/73543377
文章浏览阅读6.6k次,点赞3次,收藏70次。文章目录前言原题目运行效果项目环境项目阐述参考前言之前报名了学校Python比赛,题目很高大上,不过放心,我做的完全达不到这个深度。这个项目其实需要改进的地方还很多,不过作为简单的选课系统来说,基本功能均已实现。后续会继续完善上传到GitHub上面,有需要源码的可以先行联系我。到时候传到GitHub后也会贴出地址,供大家交流。原题目【题目名称】 区块链学分管理系统【背景说明】在当..._系统名称学生选课管理系统 开发环境python 3.10 + django + mysql 运行环境dja
文章浏览阅读4.5k次。我们呼叫一个目标分机,并向给本次呼叫指定一个自定义的uuid,应该是这样: originate {origination_uuid=xxxxxx-xxxxxx-xxxxxxx-xxxxx}user/1001 &park 在freeswitch的wiki和freeswitch权威指南中,都可以找到相关说明 但是两个分机相互拨打,那么每个分机的uuid都是系统生成的,默_origination_uuid effective_number
文章浏览阅读9.4k次,点赞6次,收藏23次。这是Schur引理的引理Schur引理的复矩阵版本和实矩阵版本摘自《矩阵论教程》第2版,张绍飞,p49_schur引理
文章浏览阅读531次。题目来源: HihoCoder1305题目要求: 给定两个区间集合A和B,其中集合A包含N个区间[A1,A2],[A3,A4],..., [A2n-1,A2n],集合B包含M个区间[B1,B2], [B3,B4], ..., [B2M-1, B2M]。求的A-B长度。 解答: A-B = A - A∩B。题目要求从A组中的所有区间中删去和B组_给定两个区间集合 a 和 b,其中集合 a 包含 n 个区
文章浏览阅读1.7k次。哈工大计算机网络课程,传输层详解之:流水线机制与滑动窗口协议_滑动窗口 gbn
文章浏览阅读180次。阿里在GitHub上的开源工具https://github.com/oldmanpushcart/greys-anatomy摘要:线上系统为何经常出错?数据库为何屡遭黑手?业务调用为何频频失败?连环异常堆栈案,究竟是那次调用所为? 数百台服务器意外雪崩背后又隐藏着什么?是软件的扭曲还是硬件的沦丧? 走进科学带你了解Greys, Java线上问题诊断工具。..._greys 查看class文件
文章浏览阅读2.5k次。ISecurityInformation::GetSecurity 该函数通过句柄找到某一对象,并获取该对象的安全描述符。DWORD GetSecurityInfo( HANDLE handle, SE_OBJECT_TYPE ObjectType, SECURITY_INFORMATION SecurityInfo, PSID* ppsidOwner,_getsecurityinfo
文章浏览阅读6.7k次,点赞6次,收藏24次。uni-app基础教程_uniapp教程
文章浏览阅读80次。题目描述java实现class Solution { public List<Integer> grayCode(int n) { List<Integer> result = new LinkedList<>(); //1<<2 = 100(4) for (int i = 0; i &l...
文章浏览阅读106次。题目:http://poj.org/problem?id=2531题目大意:给你N个点,然后以矩阵形式给你了 Cij 的权值, 然后让你把这N个点,分成两部分,使 值最大。思路:显然对于深搜极差的我体验感极差,首先分为 0,1 集合,在1里面就减,在0里面就加,结合代码理解一下dfs(1,0),然后在for里面搜索情况,可以自己模拟一下样例就很清楚了。如果把一个数放入1中,权值变小...
文章浏览阅读593次。作者的观点是尽量减少无用功来达到最大化公司的灵活程度和高效率。作者的模式确实是在当时的创新者,并且做到了极大的成功。下了很多的断言,但是不一定适用于所有场景。开头作者给出了人们由于过多的谨慎导致不能接受创新的观点,不知道作者是如何知道的这个观点。Don’t get fooled by the stats,失败不是成功的先决条件。一项哈佛商学院的研究发现,已成功的企业家更容易再次成功。一个很新颖的观_tp5.0结合微信小程序构建商城全栈应用 protoss tool
文章浏览阅读913次,点赞22次,收藏19次。这项为期2年的纵向研究包括患有糖尿病或艾滋病的家庭,所选病症代表了慢性病经历的差异例如:传染性,治疗类型,污名化程度。数据收集时间为2010年冬至2013年冬。密歇根大学机构审查委员会批准了该项研究。