This repository has been archived by the owner on Mar 22, 2023. It is now read-only.
generated from things-labs/cicd-go-template
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathdate_month_dot.go
76 lines (63 loc) · 2.17 KB
/
date_month_dot.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
69
70
71
72
73
74
75
76
package extime
import (
"errors"
"time"
)
// DateMonthSlash 格式: 2006.01
type DateMonthDot time.Time
// ToDateMonthDot time.Time to DateMonthDot
func ToDateMonthDot(t time.Time) DateMonthDot { return DateMonthDot(t) }
// MarshalJSON implemented interface Marshaler
func (t DateMonthDot) MarshalJSON() ([]byte, error) {
tt := time.Time(t)
if y := tt.Year(); y < 0 || y >= 10000 {
// RFC 3339 is clear that years are 4 digits exactly.
// See golang.org/issue/4556#c15 for more discussion.
return nil, errors.New("DateMonthDot.MarshalJSON: year outside of range [0,9999]")
}
b := make([]byte, 0, len(DateMonthDotLayout)+2)
b = append(b, '"')
b = tt.AppendFormat(b, DateMonthDotLayout)
b = append(b, '"')
return b, nil
}
// UnmarshalJSON implemented interface Unmarshaler
func (t *DateMonthDot) UnmarshalJSON(data []byte) error {
// Ignore null, like in the main JSON package.
if string(data) == "null" {
return nil
}
// Fractional seconds are handled implicitly by Parse.
tt, err := time.ParseInLocation(`"`+DateMonthDotLayout+`"`, string(data), time.Local)
*t = DateMonthDot(tt)
return err
}
// MarshalText implemented interface TextMarshaler
func (t DateMonthDot) MarshalText() ([]byte, error) {
tt := time.Time(t)
if y := tt.Year(); y < 0 || y >= 10000 {
// RFC 3339 is clear that years are 4 digits exactly.
// See golang.org/issue/4556#c15 for more discussion.
return nil, errors.New("DateMonthDot.MarshalJSON: year outside of range [0,9999]")
}
b := make([]byte, 0, len(DateMonthDotLayout))
b = tt.AppendFormat(b, DateMonthDotLayout)
return b, nil
}
// UnmarshalText implemented interface TextUnmarshaler
func (t *DateMonthDot) UnmarshalText(text []byte) error {
// Ignore null, like in the main JSON package.
if string(text) == "null" {
return nil
}
// Fractional seconds are handled implicitly by Parse.
tt, err := time.ParseInLocation(DateMonthDotLayout, string(text), time.Local)
*t = DateMonthDot(tt)
return err
}
// StdTime convert to standard time
func (t DateMonthDot) StdTime() time.Time { return time.Time(t) }
// String implemented interface Stringer
func (t DateMonthDot) String() string {
return time.Time(t).Format(DateMonthDotLayout)
}