ParseDate parses a given date string using a large list of commonly found feed date formats.
(ds string)
| 189 | // ParseDate parses a given date string using a large |
| 190 | // list of commonly found feed date formats. |
| 191 | func ParseDate(ds string) (t time.Time, err error) { |
| 192 | d := strings.TrimSpace(ds) |
| 193 | if d == "" { |
| 194 | return t, fmt.Errorf("Date string is empty") |
| 195 | } |
| 196 | for _, f := range dateFormats { |
| 197 | if t, err = time.Parse(f, d); err == nil { |
| 198 | return |
| 199 | } |
| 200 | } |
| 201 | for _, f := range dateFormatsWithNamedZone { |
| 202 | t, err = time.Parse(f, d) |
| 203 | if err != nil { |
| 204 | continue |
| 205 | } |
| 206 | |
| 207 | // This is a format match! Now try to load the timezone name |
| 208 | loc, err := time.LoadLocation(t.Location().String()) |
| 209 | if err != nil { |
| 210 | // We couldn't load the TZ name. Just use UTC instead... |
| 211 | return t, nil |
| 212 | } |
| 213 | |
| 214 | if t, err = time.ParseInLocation(f, ds, loc); err == nil { |
| 215 | return t, nil |
| 216 | } |
| 217 | // This should not be reachable |
| 218 | } |
| 219 | |
| 220 | err = fmt.Errorf("Failed to parse date: %s", ds) |
| 221 | return |
| 222 | } |
no test coverage detected
searching dependent graphs…