/
gotimestamp.go
55 lines (43 loc) · 1.2 KB
/
gotimestamp.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
package gotimestamp
import (
"time"
)
var Location *time.Location
func SetLocation(loc *time.Location) {
Location = loc
}
var Format string
func SetFormat(f string) {
Format = f
}
type TimeStamp struct {
CreatedAt time.Time `db:"created_at" json:"-"`
UpdatedAt time.Time `db:"updated_at" json:"-"`
CreatedAtInLocal string `db:"-" json:"created_at"`
UpdatedAtInLocal string `db:"-" json:"updated_at"`
}
func (t *TimeStamp) CreateTimeStamp() {
t.CreatedAt = time.Now()
t.UpdatedAt = time.Now()
}
func (t *TimeStamp) UpdateTimeStamp() {
t.UpdatedAt = time.Now()
}
func (t *TimeStamp) ComputeLocalTime() {
t.CreatedAtInLocal = t.CreatedAt.In(Location).Format(Format)
t.UpdatedAtInLocal = t.UpdatedAt.In(Location).Format(Format)
}
func (t *TimeStamp) ComputeLocalTimeWithLocation(l *time.Location) {
t.CreatedAtInLocal = t.CreatedAt.In(l).Format(Format)
t.UpdatedAtInLocal = t.UpdatedAt.In(l).Format(Format)
}
func (t *TimeStamp) ParseInLocation(createdat, updatedat string) error {
var err error
if createdat != "" {
t.CreatedAt, err = time.ParseInLocation(Format, createdat, Location)
}
if updatedat != "" {
t.UpdatedAt, err = time.ParseInLocation(Format, updatedat, Location)
}
return err
}