-
-
Notifications
You must be signed in to change notification settings - Fork 9
Expand file tree
/
Copy pathdate.go
More file actions
41 lines (33 loc) · 865 Bytes
/
date.go
File metadata and controls
41 lines (33 loc) · 865 Bytes
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
package httpsfv
import (
"errors"
"io"
"time"
)
var ErrInvalidDateFormat = errors.New("invalid date format")
// marshalDate serializes as defined in
// https://httpwg.org/specs/rfc9651.html#ser-date.
func marshalDate(b io.StringWriter, i time.Time) error {
_, err := b.WriteString("@")
if err != nil {
return err
}
return marshalInteger(b, i.Unix())
}
// parseDate parses as defined in
// https://httpwg.org/specs/rfc9651.html#parse-date.
func parseDate(s *scanner) (time.Time, error) {
if s.eof() || s.data[s.off] != '@' {
return time.Time{}, &UnmarshalError{s.off, ErrInvalidDateFormat}
}
s.off++
n, err := parseNumber(s)
if err != nil {
return time.Time{}, &UnmarshalError{s.off, ErrInvalidDateFormat}
}
i, ok := n.(int64)
if !ok {
return time.Time{}, &UnmarshalError{s.off, ErrInvalidDateFormat}
}
return time.Unix(i, 0), nil
}