-
Notifications
You must be signed in to change notification settings - Fork 18
/
Copy pathstime.go
134 lines (92 loc) · 2.67 KB
/
stime.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
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
// Copyright 2014 The sutil Author. All rights reserved.
// Use of this source code is governed by a BSD-style
// license that can be found in the LICENSE file.
package stime
import (
"time"
//"fmt"
)
func DayBeginStamp(now int64) int64 {
_, offset := time.Now().Zone()
//fmt.Println(zone, offset)
return now - (now+int64(offset)) % int64(3600 * 24)
//return (now + int64(offset))/int64(3600 * 24) * int64(3600 * 24) - int64(offset)
}
func HourBeginStamp(now int64) int64 {
_, offset := time.Now().Zone()
//fmt.Println(zone, offset)
return now - (now+int64(offset)) % int64(3600)
//return (now + int64(offset))/int64(3600 * 24) * int64(3600 * 24) - int64(offset)
}
// 获取指定天的时间范围
// 天格式 2006-01-02
// 为空时候返回当天的
func DayBeginStampFromStr(day string) (int64, error) {
nowt := time.Now()
now := nowt.Unix()
var begin int64
if len(day) > 0 {
tm, err := time.ParseInLocation("2006-01-02", day, nowt.Location())
if err != nil {
return 0, err
}
begin = tm.Unix()
} else {
begin = DayBeginStamp(now)
}
return begin, nil
}
func WeekScope(stamp int64) (int64, int64) {
now := time.Unix(stamp, 0)
weekday := time.Duration(now.Weekday())
if weekday == 0 {
weekday = 7
}
year, month, day := now.Date()
currentZeroDay:= time.Date(year, month, day, 0, 0, 0, 0, time.Local)
begin := currentZeroDay.Add(-1 * (weekday - 1) * 24 * time.Hour).Unix()
return begin, begin+24*3600*7-1
}
func MonthScope(stamp int64) (int64, int64) {
now := time.Unix(stamp, 0)
currentYear, currentMonth, _ := now.Date()
currentLocation := now.Location()
firstOfMonth := time.Date(currentYear, currentMonth, 1, 0, 0, 0, 0, currentLocation)
lastOfMonth := firstOfMonth.AddDate(0, 1, -1)
return firstOfMonth.Unix(), lastOfMonth.Unix()+3600*24-1
}
var (
Since2014 int64 = time.Date(2014, 1, 1, 0, 0, 0, 0, time.UTC).UnixNano() / 1000
)
func Timestamp2014() uint64 {
return uint64(time.Now().UnixNano()/1000 - Since2014)
}
type runTimeStat struct {
//logkey string
since time.Time
}
//func (m *runTimeStat) StatLog() string {
// return fmt.Sprintf("%s RUNTIME:%d", m.logkey, m.Duration())
//}
func (m *runTimeStat) Millisecond() int64 {
return m.Microsecond() / 1000
}
func (m *runTimeStat) Microsecond() int64 {
return m.Duration().Nanoseconds() / 1000
}
func (m *runTimeStat) Nanosecond() int64 {
return m.Duration().Nanoseconds()
}
func (m *runTimeStat) Duration() time.Duration {
return time.Since(m.since)
}
func (m *runTimeStat) Reset() {
m.since = time.Now()
}
//func NewTimeStat(key string) *runTimeStat {
func NewTimeStat() *runTimeStat {
return &runTimeStat {
//logkey: key,
since: time.Now(),
}
}