github.com/mailgun/holster/v4@v4.20.0/clock/rfc822.go (about) 1 package clock 2 3 import ( 4 "strconv" 5 "time" 6 ) 7 8 var datetimeLayouts = [48]string{ 9 // Day first month 2nd abbreviated. 10 "Mon, 2 Jan 2006 15:04:05 MST", 11 "Mon, 2 Jan 2006 15:04:05 -0700", 12 "Mon, 2 Jan 2006 15:04:05 -0700 (MST)", 13 "2 Jan 2006 15:04:05 MST", 14 "2 Jan 2006 15:04:05 -0700", 15 "2 Jan 2006 15:04:05 -0700 (MST)", 16 "Mon, 2 Jan 2006 15:04 MST", 17 "Mon, 2 Jan 2006 15:04 -0700", 18 "Mon, 2 Jan 2006 15:04 -0700 (MST)", 19 "2 Jan 2006 15:04 MST", 20 "2 Jan 2006 15:04 -0700", 21 "2 Jan 2006 15:04 -0700 (MST)", 22 23 // Month first day 2nd abbreviated. 24 "Mon, Jan 2 2006 15:04:05 MST", 25 "Mon, Jan 2 2006 15:04:05 -0700", 26 "Mon, Jan 2 2006 15:04:05 -0700 (MST)", 27 "Jan 2 2006 15:04:05 MST", 28 "Jan 2 2006 15:04:05 -0700", 29 "Jan 2 2006 15:04:05 -0700 (MST)", 30 "Mon, Jan 2 2006 15:04 MST", 31 "Mon, Jan 2 2006 15:04 -0700", 32 "Mon, Jan 2 2006 15:04 -0700 (MST)", 33 "Jan 2 2006 15:04 MST", 34 "Jan 2 2006 15:04 -0700", 35 "Jan 2 2006 15:04 -0700 (MST)", 36 37 // Day first month 2nd not abbreviated. 38 "Mon, 2 January 2006 15:04:05 MST", 39 "Mon, 2 January 2006 15:04:05 -0700", 40 "Mon, 2 January 2006 15:04:05 -0700 (MST)", 41 "2 January 2006 15:04:05 MST", 42 "2 January 2006 15:04:05 -0700", 43 "2 January 2006 15:04:05 -0700 (MST)", 44 "Mon, 2 January 2006 15:04 MST", 45 "Mon, 2 January 2006 15:04 -0700", 46 "Mon, 2 January 2006 15:04 -0700 (MST)", 47 "2 January 2006 15:04 MST", 48 "2 January 2006 15:04 -0700", 49 "2 January 2006 15:04 -0700 (MST)", 50 51 // Month first day 2nd not abbreviated. 52 "Mon, January 2 2006 15:04:05 MST", 53 "Mon, January 2 2006 15:04:05 -0700", 54 "Mon, January 2 2006 15:04:05 -0700 (MST)", 55 "January 2 2006 15:04:05 MST", 56 "January 2 2006 15:04:05 -0700", 57 "January 2 2006 15:04:05 -0700 (MST)", 58 "Mon, January 2 2006 15:04 MST", 59 "Mon, January 2 2006 15:04 -0700", 60 "Mon, January 2 2006 15:04 -0700 (MST)", 61 "January 2 2006 15:04 MST", 62 "January 2 2006 15:04 -0700", 63 "January 2 2006 15:04 -0700 (MST)", 64 } 65 66 // Allows seamless JSON encoding/decoding of rfc822 formatted timestamps. 67 // https://www.ietf.org/rfc/rfc822.txt section 5. 68 type RFC822Time struct { 69 Time 70 } 71 72 // NewRFC822Time creates RFC822Time from a standard Time. The created value is 73 // truncated down to second precision because RFC822 does not allow for better. 74 func NewRFC822Time(t Time) RFC822Time { 75 return RFC822Time{Time: t.Truncate(Second)} 76 } 77 78 // ParseRFC822Time parses an RFC822 time string. 79 func ParseRFC822Time(s string) (Time, error) { 80 var t time.Time 81 var err error 82 for _, layout := range &datetimeLayouts { 83 t, err = Parse(layout, s) 84 if err == nil { 85 return t, nil 86 } 87 } 88 return t, err 89 } 90 91 // NewRFC822Time creates RFC822Time from a Unix timestamp (seconds from Epoch). 92 func NewRFC822TimeFromUnix(timestamp int64) RFC822Time { 93 return RFC822Time{Time: Unix(timestamp, 0).UTC()} 94 } 95 96 func (t RFC822Time) MarshalJSON() ([]byte, error) { 97 return []byte(strconv.Quote(t.Format(RFC1123))), nil 98 } 99 100 func (t RFC822Time) MarshalText() ([]byte, error) { 101 return []byte(t.String()), nil 102 } 103 104 func (t *RFC822Time) UnmarshalText(s []byte) error { 105 parsed, err := ParseRFC822Time(string(s)) 106 if err != nil { 107 return err 108 } 109 t.Time = parsed 110 return nil 111 } 112 113 func (t *RFC822Time) UnmarshalJSON(s []byte) error { 114 q, err := strconv.Unquote(string(s)) 115 if err != nil { 116 return err 117 } 118 parsed, err := ParseRFC822Time(q) 119 if err != nil { 120 return err 121 } 122 t.Time = parsed 123 return nil 124 } 125 126 func (t RFC822Time) String() string { 127 return t.Format(RFC1123) 128 } 129 130 func (t RFC822Time) StringWithOffset() string { 131 return t.Format(RFC1123Z) 132 }