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
|
package internal
import (
"fmt"
"time"
)
//go:generate stringer -type=TimeFormatType
type TimeFormatType int
const (
TimeFormatNoTimezone TimeFormatType = iota
TimeFormatNamedTimezone
TimeFormatNumericTimezone
TimeFormatNumericAndNamedTimezone
TimeFormatTimeOnly
)
type TimeFormat struct {
Format string
Typ TimeFormatType
}
func (f TimeFormat) HasTimezone() bool {
// We don't include the formats with only named timezones, see
// https://github.com/golang/go/issues/19694#issuecomment-289103522
return f.Typ >= TimeFormatNumericTimezone && f.Typ <= TimeFormatNumericAndNamedTimezone
}
var TimeFormats = []TimeFormat{
// Keep common formats at the top.
{"2006-01-02", TimeFormatNoTimezone},
{time.RFC3339, TimeFormatNumericTimezone},
{"2006-01-02T15:04:05", TimeFormatNoTimezone}, // iso8601 without timezone
{time.RFC1123Z, TimeFormatNumericTimezone},
{time.RFC1123, TimeFormatNamedTimezone},
{time.RFC822Z, TimeFormatNumericTimezone},
{time.RFC822, TimeFormatNamedTimezone},
{time.RFC850, TimeFormatNamedTimezone},
{"2006-01-02 15:04:05.999999999 -0700 MST", TimeFormatNumericAndNamedTimezone}, // Time.String()
{"2006-01-02T15:04:05-0700", TimeFormatNumericTimezone}, // RFC3339 without timezone hh:mm colon
{"2006-01-02 15:04:05Z0700", TimeFormatNumericTimezone}, // RFC3339 without T or timezone hh:mm colon
{"2006-01-02 15:04:05", TimeFormatNoTimezone},
{time.ANSIC, TimeFormatNoTimezone},
{time.UnixDate, TimeFormatNamedTimezone},
{time.RubyDate, TimeFormatNumericTimezone},
{"2006-01-02 15:04:05Z07:00", TimeFormatNumericTimezone},
{"02 Jan 2006", TimeFormatNoTimezone},
{"2006-01-02 15:04:05 -07:00", TimeFormatNumericTimezone},
{"2006-01-02 15:04:05 -0700", TimeFormatNumericTimezone},
{time.Kitchen, TimeFormatTimeOnly},
{time.Stamp, TimeFormatTimeOnly},
{time.StampMilli, TimeFormatTimeOnly},
{time.StampMicro, TimeFormatTimeOnly},
{time.StampNano, TimeFormatTimeOnly},
}
func ParseDateWith(s string, location *time.Location, formats []TimeFormat) (d time.Time, e error) {
for _, format := range formats {
if d, e = time.Parse(format.Format, s); e == nil {
// Some time formats have a zone name, but no offset, so it gets
// put in that zone name (not the default one passed in to us), but
// without that zone's offset. So set the location manually.
if format.Typ <= TimeFormatNamedTimezone {
if location == nil {
location = time.Local
}
year, month, day := d.Date()
hour, min, sec := d.Clock()
d = time.Date(year, month, day, hour, min, sec, d.Nanosecond(), location)
}
return
}
}
return d, fmt.Errorf("unable to parse date: %s", s)
}
|