package internal
import (
"fmt"
"time"
)
type TimeFormatType int
const (
TimeFormatNoTimezone TimeFormatType = iota
TimeFormatNamedTimezone
TimeFormatNumericTimezone
TimeFormatNumericAndNamedTimezone
TimeFormatTimeOnly
)
type TimeFormat struct {
Format string
Typ TimeFormatType
}
func (f TimeFormat ) HasTimezone () bool {
return f .Typ >= TimeFormatNumericTimezone && f .Typ <= TimeFormatNumericAndNamedTimezone
}
var TimeFormats = []TimeFormat {
{"2006-01-02" , TimeFormatNoTimezone },
{time .RFC3339 , TimeFormatNumericTimezone },
{"2006-01-02T15:04:05" , TimeFormatNoTimezone },
{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 },
{"2006-01-02T15:04:05-0700" , TimeFormatNumericTimezone },
{"2006-01-02 15:04:05Z0700" , TimeFormatNumericTimezone },
{"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 {
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 )
}
The pages are generated with Golds v0.8.7 . (GOOS=linux GOARCH=amd64)
Golds is a Go 101 project developed by Tapir Liu .
PR and bug reports are welcome and can be submitted to the issue list .
Please follow @zigo_101 (reachable from the left QR code) to get the latest news of Golds .