-
Notifications
You must be signed in to change notification settings - Fork 0
/
time.go
68 lines (59 loc) · 1.84 KB
/
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
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
/*
* Tencent is pleased to support the open source community by making 蓝鲸 available.
* Copyright (C) 2017-2018 THL A29 Limited, a Tencent company. All rights reserved.
* Licensed under the MIT License (the "License"); you may not use this file except
* in compliance with the License. You may obtain a copy of the License at
* http://opensource.org/licenses/MIT
* Unless required by applicable law or agreed to in writing, software distributed under
* the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND,
* either express or implied. See the License for the specific language governing permissions and
* limitations under the License.
*/
package metadata
import (
"database/sql/driver"
"fmt"
"strconv"
"time"
)
type Time struct {
time.Time
}
// Scan implement sql driver's Scan interface
func (t *Time) Scan(value interface{}) error {
t.Time = value.(time.Time)
return nil
}
// Value implement sql driver's Value interface
func (t Time) Value() (driver.Value, error) {
return t.Time, nil
}
func (t Time) MarshalJSON() ([]byte, error) {
return []byte(t.UTC().Format(`"2006-01-02T15:04:05Z"`)), nil
}
// UnmarshalJSON implements the json.Unmarshaler interface.
// The time is expected to be a quoted string in RFC 3339 format.
func (t *Time) UnmarshalJSON(data []byte) error {
// Ignore null, like in the main JSON package.
if string(data) == "null" {
return nil
}
parsed, err := time.Parse(`"`+time.RFC3339+`"`, string(data))
if err == nil {
*t = Time{parsed}
return nil
}
parsed, err = time.ParseInLocation(`"2006-01-02 15:04:05"`, string(data), time.UTC)
if err == nil {
*t = Time{parsed}
return nil
}
timestamp, err := strconv.ParseInt(fmt.Sprintf("%s", data), 10, 64)
if err == nil {
*t = Time{time.Unix(timestamp, 0)}
}
return err
}
func Now() Time {
return Time{time.Now()}
}