-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathCustomTime.go
More file actions
69 lines (58 loc) · 1.69 KB
/
CustomTime.go
File metadata and controls
69 lines (58 loc) · 1.69 KB
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
package hyperstack
import (
"fmt"
"strings"
"time"
)
type CustomTime struct {
time.Time
}
const ctLayout = "2006-01-02T15:04:05"
// UnmarshalJSON converts JSON to CustomTime
// Supports multiple formats:
// - "2006-01-02T15:04:05" (original format)
// - "2006-01-02T15:04:05Z" (RFC3339 with Z)
// - "2006-01-02T15:04:05+00:00" (RFC3339 with offset)
// - time.RFC3339 (full RFC3339 format)
func (ct *CustomTime) UnmarshalJSON(b []byte) error {
s := strings.Trim(string(b), "\"")
if s == "null" {
ct.Time = time.Time{}
return nil
}
// Try multiple formats
formats := []string{
time.RFC3339, // "2006-01-02T15:04:05Z07:00"
"2006-01-02T15:04:05Z", // "2006-01-02T15:04:05Z"
"2006-01-02T15:04:05+00:00", // "2006-01-02T15:04:05+00:00"
ctLayout, // "2006-01-02T15:04:05" (original)
}
var err error
for _, layout := range formats {
parsedTime, parseErr := time.Parse(layout, s)
if parseErr == nil {
ct.Time = parsedTime
return nil
}
err = parseErr
}
// If all formats failed, return the last error
return fmt.Errorf("failed to parse time %q: %v", s, err)
}
// MarshalJSON converts CustomTime to JSON
func (ct CustomTime) MarshalJSON() ([]byte, error) {
return []byte(fmt.Sprintf("\"%s\"", ct.Time.Format(ctLayout))), nil
}
// NewCustomTime creates a CustomTime from time.Time
func NewCustomTime(t time.Time) CustomTime {
return CustomTime{Time: t}
}
// Now returns the current time as CustomTime
func Now() CustomTime {
return CustomTime{Time: time.Now()}
}
// ParseCustomTime parses a string into CustomTime
func ParseCustomTime(layout, value string) (CustomTime, error) {
t, err := time.Parse(layout, value)
return CustomTime{Time: t}, err
}