This repository was archived by the owner on Jul 30, 2026. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathoffsetdatetime_text.go
More file actions
113 lines (99 loc) · 2.42 KB
/
Copy pathoffsetdatetime_text.go
File metadata and controls
113 lines (99 loc) · 2.42 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
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
package goda
import (
"database/sql/driver"
"errors"
"time"
)
// String returns the ISO 8601 string representation (yyyy-MM-ddTHH:mm:ss[.nnnnnnnnn]±HH:mm).
func (odt OffsetDateTime) String() string {
return stringImpl(odt)
}
// AppendText implements encoding.TextAppender.
func (odt OffsetDateTime) AppendText(b []byte) ([]byte, error) {
if odt.IsZero() {
return b, nil
}
b, _ = odt.datetime.AppendText(b)
b, _ = odt.offset.AppendText(b)
return b, nil
}
// MarshalText implements encoding.TextMarshaler.
func (odt OffsetDateTime) MarshalText() ([]byte, error) {
return marshalTextImpl(odt)
}
// UnmarshalText implements encoding.TextUnmarshaler.
// Accepts ISO 8601 format: yyyy-MM-ddTHH:mm:ss[.nnnnnnnnn]±HH:mm[:ss] or Z for UTC.
func (odt *OffsetDateTime) UnmarshalText(text []byte) (e error) {
defer deferOpInParse(text, &e)
if len(text) == 0 {
*odt = OffsetDateTime{}
return nil
}
// Find the offset part (starts with +, -, or Z)
offsetIdx := -1
for i := len(text) - 1; i >= 0; i-- {
ch := text[i]
if ch == '+' || ch == '-' {
offsetIdx = i
break
}
if ch == 'Z' || ch == 'z' {
offsetIdx = i
break
}
}
if offsetIdx < 0 {
return errors.New("invalid offset date-time format: missing offset")
}
// Parse date-time part
var dt LocalDateTime
if err := dt.UnmarshalText(text[:offsetIdx]); err != nil {
return err
}
// Parse offset part
var offset ZoneOffset
if err := offset.UnmarshalText(text[offsetIdx:]); err != nil {
return err
}
*odt = OffsetDateTime{
datetime: dt,
offset: offset,
}
return nil
}
// MarshalJSON implements json.Marshaler.
func (odt OffsetDateTime) MarshalJSON() ([]byte, error) {
return marshalJsonImpl(odt)
}
// UnmarshalJSON implements json.Unmarshaler.
func (odt *OffsetDateTime) UnmarshalJSON(data []byte) error {
if len(data) == 4 && string(data) == "null" {
*odt = OffsetDateTime{}
return nil
}
return unmarshalJsonImpl(odt, data)
}
// Scan implements sql.Scanner.
func (odt *OffsetDateTime) Scan(src any) error {
switch v := src.(type) {
case nil:
*odt = OffsetDateTime{}
return nil
case string:
return odt.UnmarshalText([]byte(v))
case []byte:
return odt.UnmarshalText(v)
case time.Time:
*odt = OffsetDateTimeOfGoTime(v)
return nil
default:
return sqlScannerDefaultBranch(v)
}
}
// Value implements driver.Valuer.
func (odt OffsetDateTime) Value() (driver.Value, error) {
if odt.IsZero() {
return nil, nil
}
return odt.String(), nil
}