@@ -45,50 +45,52 @@ type fastime struct {
45
45
location atomic.Pointer [time.Location ]
46
46
}
47
47
48
- const bufSize = 64
48
+ const (
49
+ bufSize = 64
50
+ bufMargin = 10
51
+ )
49
52
50
53
// New returns Fastime
51
- func New () Fastime {
54
+ func New () ( f Fastime ) {
52
55
return newFastime ()
53
56
}
54
57
55
- func newFastime () * fastime {
56
- f : = & fastime {
57
- ut : math .MaxInt64 ,
58
- unt : math .MaxInt64 ,
59
- uut : math .MaxUint32 ,
60
- uunt : math .MaxUint32 ,
58
+ func newFastime () ( f * fastime ) {
59
+ f = & fastime {
60
+ ut : math .MaxInt64 ,
61
+ unt : math .MaxInt64 ,
62
+ uut : math .MaxUint32 ,
63
+ uunt : math .MaxUint32 ,
61
64
correctionDur : time .Millisecond * 100 ,
62
65
}
63
66
64
67
form := time .RFC3339
65
68
f .format .Store (& form )
66
69
loc := func () (loc * time.Location ) {
67
- tz , ok := syscall .Getenv ("TZ" )
68
- if ok && tz != "" {
69
- var err error
70
- loc , err = time .LoadLocation (tz )
71
- if err == nil {
72
- return loc
73
- }
70
+ tz , ok := syscall .Getenv ("TZ" )
71
+ if ok && tz != "" {
72
+ var err error
73
+ loc , err = time .LoadLocation (tz )
74
+ if err == nil {
75
+ return loc
74
76
}
75
- return new (time.Location )
76
- }()
77
+ }
78
+ return new (time.Location )
79
+ }()
77
80
78
81
f .location .Store (loc )
79
82
80
-
81
- buf := f .newBuffer (len (f .GetFormat ()) + 10 )
83
+ buf := f .newBuffer (len (form ) + bufMargin )
82
84
f .ft .Store (& buf )
83
85
84
86
return f .refresh ()
85
87
}
86
88
87
- func (f * fastime ) update () * fastime {
89
+ func (f * fastime ) update () ( ft * fastime ) {
88
90
return f .store (f .Now ().Add (time .Duration (atomic .LoadInt64 (& f .dur ))))
89
91
}
90
92
91
- func (f * fastime ) refresh () * fastime {
93
+ func (f * fastime ) refresh () ( ft * fastime ) {
92
94
return f .store (f .now ())
93
95
}
94
96
@@ -102,7 +104,7 @@ func (f *fastime) newBuffer(max int) (b []byte) {
102
104
return b
103
105
}
104
106
105
- func (f * fastime ) store (t time.Time ) * fastime {
107
+ func (f * fastime ) store (t time.Time ) ( ft * fastime ) {
106
108
f .t .Store (& t )
107
109
f .formatValid .Store (false )
108
110
ut := t .Unix ()
@@ -114,42 +116,42 @@ func (f *fastime) store(t time.Time) *fastime {
114
116
return f
115
117
}
116
118
117
- func (f * fastime ) IsDaemonRunning () bool {
119
+ func (f * fastime ) IsDaemonRunning () ( running bool ) {
118
120
return f .running .Load ()
119
121
}
120
122
121
- func (f * fastime ) GetLocation () * time.Location {
122
- loc : = f .location .Load ()
123
+ func (f * fastime ) GetLocation () ( loc * time.Location ) {
124
+ loc = f .location .Load ()
123
125
if loc == nil {
124
126
return nil
125
127
}
126
128
return loc
127
129
}
128
130
129
- func (f * fastime ) GetFormat () string {
131
+ func (f * fastime ) GetFormat () ( form string ) {
130
132
return * f .format .Load ()
131
133
}
132
134
133
135
// SetLocation replaces time location
134
- func (f * fastime ) SetLocation (location * time.Location ) Fastime {
135
- if location == nil {
136
+ func (f * fastime ) SetLocation (loc * time.Location ) ( ft Fastime ) {
137
+ if loc == nil {
136
138
return f
137
139
}
138
- f .location .Store (location )
140
+ f .location .Store (loc )
139
141
f .refresh ()
140
142
return f
141
143
}
142
144
143
145
// SetFormat replaces time format
144
- func (f * fastime ) SetFormat (format string ) Fastime {
146
+ func (f * fastime ) SetFormat (format string ) ( ft Fastime ) {
145
147
f .format .Store (& format )
146
148
f .formatValid .Store (false )
147
149
f .refresh ()
148
150
return f
149
151
}
150
152
151
153
// Now returns current time
152
- func (f * fastime ) Now () time.Time {
154
+ func (f * fastime ) Now () ( t time.Time ) {
153
155
return * f .t .Load ()
154
156
}
155
157
@@ -167,43 +169,43 @@ func (f *fastime) stop() {
167
169
f .wg .Wait ()
168
170
}
169
171
170
- func (f * fastime ) Since (t time.Time ) time.Duration {
172
+ func (f * fastime ) Since (t time.Time ) ( dur time.Duration ) {
171
173
return f .Now ().Sub (t )
172
174
}
173
175
174
176
// UnixNow returns current unix time
175
- func (f * fastime ) UnixNow () int64 {
177
+ func (f * fastime ) UnixNow () ( now int64 ) {
176
178
return atomic .LoadInt64 (& f .ut )
177
179
}
178
180
179
181
// UnixNow returns current unix time
180
- func (f * fastime ) UnixUNow () uint32 {
182
+ func (f * fastime ) UnixUNow () ( now uint32 ) {
181
183
return atomic .LoadUint32 (& f .uut )
182
184
}
183
185
184
186
// UnixNanoNow returns current unix nano time
185
- func (f * fastime ) UnixNanoNow () int64 {
187
+ func (f * fastime ) UnixNanoNow () ( now int64 ) {
186
188
return atomic .LoadInt64 (& f .unt )
187
189
}
188
190
189
191
// UnixNanoNow returns current unix nano time
190
- func (f * fastime ) UnixUNanoNow () uint32 {
192
+ func (f * fastime ) UnixUNanoNow () ( now uint32 ) {
191
193
return atomic .LoadUint32 (& f .uunt )
192
194
}
193
195
194
196
// FormattedNow returns formatted byte time
195
- func (f * fastime ) FormattedNow () []byte {
197
+ func (f * fastime ) FormattedNow () ( now []byte ) {
196
198
// only update formatted value on swap
197
199
if f .formatValid .CompareAndSwap (false , true ) {
198
200
form := f .GetFormat ()
199
- buf := f .Now ().AppendFormat (f .newBuffer (len (form )+ 10 ), form )
201
+ buf := f .Now ().AppendFormat (f .newBuffer (len (form )+ bufMargin ), form )
200
202
f .ft .Store (& buf )
201
203
}
202
204
return * f .ft .Load ()
203
205
}
204
206
205
207
// StartTimerD provides time refresh daemon
206
- func (f * fastime ) StartTimerD (ctx context.Context , dur time.Duration ) Fastime {
208
+ func (f * fastime ) StartTimerD (ctx context.Context , dur time.Duration ) ( ft Fastime ) {
207
209
f .mu .Lock ()
208
210
defer f .mu .Unlock ()
209
211
// if the daemon was already running, restart
0 commit comments