-
Notifications
You must be signed in to change notification settings - Fork 4
/
time.go
47 lines (38 loc) · 1012 Bytes
/
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
// Content managed by Project Forge, see [projectforge.md] for details.
package util
import (
"time"
)
type Timer struct {
Started int64 `json:"started"`
Completed int64 `json:"complete"`
}
func TimerStart() *Timer {
return &Timer{Started: time.Now().UnixNano()}
}
func (t *Timer) End() int {
t.Completed = time.Now().UnixNano()
return t.Elapsed()
}
func (t *Timer) EndString() string {
t.End()
return t.String()
}
func (t *Timer) Elapsed() int {
if t.Completed == 0 {
return int((time.Now().UnixNano() - t.Started) / int64(time.Microsecond))
}
return int((t.Completed - t.Started) / int64(time.Microsecond))
}
func (t *Timer) String() string {
return MicrosToMillis(t.Elapsed())
}
type EpochTimestamp struct {
Epoch int64 `json:"epoch,omitempty"`
ISO8601 string `json:"iso8601,omitempty"`
Time *time.Time `json:"-"`
}
func NewEpochTimestamp() *EpochTimestamp {
t := time.Now()
return &EpochTimestamp{Epoch: t.UnixMilli(), ISO8601: t.Format(time.RFC3339), Time: &t}
}