Core

/app/util/time.go (934 B)

 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
package util

import (
"time"
)

type Timer struct {
Started int64 `json:"started"`
Completed int64 `json:"complete"`
}

func TimerStart() *Timer {
return &Timer{Started: TimeCurrentNanos()}
}

func (t *Timer) End() int {
t.Completed = TimeCurrentNanos()
return t.Elapsed()
}

func (t *Timer) EndString() string {
t.End()
return t.String()
}

func (t *Timer) Elapsed() int {
if t.Completed == 0 {
return int((TimeCurrentNanos() - 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 := TimeCurrentP()
return &EpochTimestamp{Epoch: t.UnixMilli(), ISO8601: t.Format(time.RFC3339), Time: t}
}