forked from openfoodfacts/openfoodfacts-go
-
Notifications
You must be signed in to change notification settings - Fork 0
/
epoch_time.go
47 lines (38 loc) · 1022 Bytes
/
epoch_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
// Copyright © 2019 OpenFoodFacts. All rights reserved.
// Use of this source code is governed by the MIT license which can be found in the LICENSE.txt file.
package openfoodfacts
import (
"fmt"
"strconv"
"strings"
"time"
)
// EpochTime allows the proper un/marshaling of a seconds-since-epoch value from JSON.
type EpochTime struct {
time.Time
}
func (t *EpochTime) UnmarshalJSON(b []byte) (err error) {
s := strings.Trim(string(b), "\"")
if s == "null" {
t.Time = time.Time{}
return nil
}
secs, err := strconv.ParseInt(string(b), 0, 64)
if err != nil {
t.Time = time.Time{}
return err
}
t.Time = time.Unix(secs, 0)
return nil
}
func (t *EpochTime) MarshalJSON() ([]byte, error) {
if !t.isSet() {
return []byte("null"), nil
}
return []byte(fmt.Sprintf("%d", t.Time.UnixNano()/1000000)), nil
}
var nilTime = (time.Time{}).UnixNano()
// isSet allows testing if the value was set. If it is empty, it will return false.
func (t *EpochTime) isSet() bool {
return t.UnixNano() != nilTime
}