go-datetime_timer

日期格式化

1
2
3
4
strDate := time.Now().Format("2006-01-02 15:04:05")

unixTime := int64(binary.LittleEndian.Uint32(b.value[fpos:]))
block.header.Time = time.Unix(unixTime, 0).Format("2006-01-02 15:04:05")

获取当前时间戳

1
2
timeUnix:=time.Now().Unix()            //单位s,打印结果:1491888244
timeUnixNano:=time.Now().UnixNano() //单位纳秒,打印结果:1491888244752784461

unix timestamp

从字符串转

1
2
3
4
5
6
7
8
9
10
11
12
13
14
package main
import (
"fmt"
"time"
"strconv"
)
func main() {
i, err := strconv.ParseInt("1518328047", 10, 64)
if err != nil {
panic(err)
}
tm := time.Unix(i, 0)
fmt.Println(tm)
}

从 int 转

1
2
3
4
5
6
7
8
9
package main
import (
"fmt"
"time"
)
func main() {
tm := time.Unix(1518328047, 0)
fmt.Println(tm)
}

从 timestamp 到 UTC 时间

1
2
3
4
5
6
7
loc, _ := time.LoadLocation("UTC")
now := time.Unix(int64(reversArr), 0).In(loc)
//fmt.Println(" Time : ", now) // UTC
buf["timestamp"] = now.Format("2006-01-02 15:04:05 -07:00 MST")

输出:
Time : 2013-07-05 22:47:09 +0000 UTC

convert timestamp to RFC3339

1
2
3
4
5
6
7
8
9
10
11
12
13
14
package main
import (
"fmt"
"time"
)
func main() {

unixTimeUTC:=time.Unix(1405544146, 0) //gives unix time stamp in utc

unitTimeInRFC3339 :=unixTimeUTC.Format(time.RFC3339) // converts utc time to RFC3339 format

fmt.Println("unix time stamp in UTC :--->",unixTimeUTC)
fmt.Println("unix time stamp in unitTimeInRFC3339 format :---->",unitTimeInRFC3339)
}

举例: place holder的用法
1609-09-12 19:02:35 PM +03:00 Sep Sat PDT

Type Placeholder Value
Year 2006 1609
Year 06 09
Month 01 09
Month 1 9
Month Jan Sep
Month January September
Day 02 12
Day 2 12
Week day Mon Sat
Week day Monday Saturday
Hours 03 07
Hours 3 7
Hours 15 19
Minutes 04 02
Minutes 4 2
Seconds 05 35
Seconds 5 35
AM or PM PM PM
Milliseconds .000 .123
Microseconds .000000 .123456
Nanoseconds .000000000 .123456789
Timezone offset -0700 +0300
Timezone offset -07:00 +03:00
Timezone offset Z0700 +0300
Timezone offset Z07:00 +03:00
Timezone MST PDT
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
package main

import (
"fmt"
"time"
"strings"
)


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))
}
}

sleep

1
time.Sleep(5 * time.Second)

定时

启动的时候执行一次,以后每天晚上12点执行

1
2
3
4
5
6
7
8
9
10
11
12
13
func startTimer(f func()) {
go func() {
for {
f()
now := time.Now()
// 计算下一个零点
next := now.Add(time.Hour * 24)
next = time.Date(next.Year(), next.Month(), next.Day(), 0,0,0,0,next.Location())
t := time.NewTimer(next.Sub(now))
<-t.C
}
}()
}
坚持原创技术分享,您的支持将鼓励我继续创作!