marshalTimeJson does what time.MarshalJson does along with supporting RFC3339 non compliant time zones in a timestamp. While go 1.20 changes the behaviour of time.MarshalJSON, we do not want to throw error suddenly because we can't marshal the stored data correctly any more.
(t time.Time)
| 699 | // time zones in a timestamp. While go 1.20 changes the behaviour of time.MarshalJSON, we do |
| 700 | // not want to throw error suddenly because we can't marshal the stored data correctly any more. |
| 701 | func marshalTimeJson(t time.Time) ([]byte, error) { |
| 702 | _, offset := t.Zone() |
| 703 | // normal case |
| 704 | if types.GoodTimeZone(offset) { |
| 705 | return t.MarshalJSON() |
| 706 | } |
| 707 | |
| 708 | // If zone >23 or <-23, we need to handle this case ourselves. |
| 709 | // This is because, in go1.20, MarshalJSON fails for invalid zones. |
| 710 | // We, for now, call MarshalJSON for timestamp without the zone (or making it UTC zone). |
| 711 | b, err := t.Add(time.Duration(offset) * time.Second).UTC().MarshalJSON() |
| 712 | if err != nil { |
| 713 | return nil, err |
| 714 | } |
| 715 | |
| 716 | // we will get a byte slice that has Z appended at the end along with a quote (") |
| 717 | // e.g.: []byte("2018-05-28T14:41:57Z"). We replace Z with -/+. |
| 718 | zone := offset / 60 |
| 719 | if zone < 0 { |
| 720 | b[len(b)-2] = '-' |
| 721 | zone = -zone |
| 722 | } else { |
| 723 | b[len(b)-2] = '+' |
| 724 | } |
| 725 | return append(b[:len(b)-1], []byte(fmt.Sprintf("%02d:%02d\"", zone/60, zone%60))...), nil |
| 726 | } |
| 727 | |
| 728 | func (enc *encoder) writeKey(fj fastJsonNode) error { |
| 729 | if _, err := enc.buf.WriteRune('"'); err != nil { |
searching dependent graphs…