-
Notifications
You must be signed in to change notification settings - Fork 3
Expand file tree
/
Copy pathtime.go
More file actions
61 lines (49 loc) · 990 Bytes
/
time.go
File metadata and controls
61 lines (49 loc) · 990 Bytes
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
package concurrent
import (
"encoding/json"
"sync"
"time"
)
// NewTime creates a new concurrent time
func NewTime() *Time {
return &Time{}
}
// Time is a concurrent time object
type Time struct {
time time.Time
mutex sync.RWMutex
}
// Now sets time to current time
func (s *Time) Now() *Time {
s.Set(time.Now())
return s
}
// Set sets the time
func (s *Time) Set(t time.Time) *Time {
s.mutex.Lock()
s.time = t
s.mutex.Unlock()
return s
}
// Get returns the time
func (s *Time) Get() time.Time {
s.mutex.RLock()
defer s.mutex.RUnlock()
return s.time
}
// Since implements time.Since
func (s *Time) Since() time.Duration {
s.mutex.RLock()
defer s.mutex.RUnlock()
return time.Since(s.time)
}
// String returns the time formatted as string
func (s *Time) String() string {
return s.Get().String()
}
// MarshalJSON implements save marshalling
func (s *Time) MarshalJSON() ([]byte, error) {
s.mutex.RLock()
defer s.mutex.RUnlock()
return json.Marshal(s.time)
}