-
Notifications
You must be signed in to change notification settings - Fork 6
/
time.go
74 lines (64 loc) · 2.27 KB
/
time.go
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
71
72
73
74
// Copyright (c) 2020 HigKer
// Open Source: MIT License
// Author: SDing <deen.job@qq.com>
// Date: 2020/4/17 - 12:26 下午
package logker
import (
"strconv"
"time"
)
type logTimeZone string
const (
//https://blog.imdst.com/guo-bf/
//美东 EST /usr/share/zoneinfo/EST5EDT
//美西 PST /usr/share/zoneinfo/PST8PDT
//澳洲 CST /usr/share/zoneinfo/CST6CDT
//欧洲 GMT /usr/share/zoneinfo/Europe/London
//泰国 Thailand /usr/share/zoneinfo/Asia/Bangkok
//越南 Vietnam /usr/share/zoneinfo/Asia/Ho_Chi_Minh
//新加坡 Singapore /usr/share/zoneinfo/Asia/Singapore
//台湾 Taiwan /usr/share/zoneinfo/Asia/Taipei
//香港 HongKong /usr/share/zoneinfo/Asia/Hong_Kong
//日本 Japan /usr/share/zoneinfo/Asia/Tokyo
//韩国 Korea /usr/share/zoneinfo/Asia/Pyongyang
EST logTimeZone = "EST5EDT"
PST logTimeZone = "PST8PDT"
CST logTimeZone = "CST6CDT"
GMT logTimeZone = "Europe/London"
Thailand logTimeZone = "Asia/Bangkok"
Vietnam logTimeZone = "Asia/Ho_Chi_Minh"
Singapore logTimeZone = "Asia/Singapore"
Taiwan logTimeZone = "Asia/Taipei"
HongKong logTimeZone = "Asia/Hong_Kong"
Japan logTimeZone = "Asia/Tokyo"
Korea logTimeZone = "Asia/Pyongyang"
Shanghai logTimeZone = "Asia/Shanghai" // Shanghai
//timeFormat = "2006-01-02 15:04:05.0000"
timeFormat = "2006-01-02 15:04:05.0000"
logTimeFormat = "2006_01_02_15_04_05.0000"
)
// 自定义时间类型 :customize time zone type struct
type timeZone struct {
TimeZoneStr logTimeZone // your custom time zone,recommend use this.
}
// get time str fmt: 2006-01-02 15:04:05.0000
func (tz *timeZone) NowTimeStr() (ts string) {
return tz.localTime().Format(timeFormat)
}
// Log file name format : log_2006_01_02_15_04_05_0000.log
func (tz *timeZone) NowTimeStrLogName() (ts string) {
return tz.localTime().Format(logTimeFormat)
}
// ret: nanosecond string
func (tz *timeZone) NanoSecondStr() string {
return strconv.FormatInt(tz.localTime().UnixNano(), 10)
}
//set customize time zone
func (tz *timeZone) localTime() time.Time {
// set time zone
ltz, err := time.LoadLocation(string(tz.TimeZoneStr))
if err != nil {
panic("core init set time zone fail.")
}
return time.Now().In(ltz)
}