Documentation
¶
Overview ¶
Package dateparse parses date-strings without knowing the format in advance, using a fast lex based approach to eliminate shotgun attempts. It leans towards US style dates when there is a conflict.
Index ¶
- Variables
- func MustParse(datestr string, opts ...ParserOption) time.Time
- func ParseAny(datestr string, opts ...ParserOption) (time.Time, error)
- func ParseFormat(datestr string, opts ...ParserOption) (string, error)
- func ParseIn(datestr string, loc *time.Location, opts ...ParserOption) (time.Time, error)
- func ParseLocal(datestr string, opts ...ParserOption) (time.Time, error)
- func ParseStrict(datestr string, opts ...ParserOption) (time.Time, error)
- type ParserOption
Constants ¶
This section is empty.
Variables ¶
var ErrAmbiguousMMDD = fmt.Errorf("This date has ambiguous mm/dd vs dd/mm type format")
ErrAmbiguousMMDD for date formats such as 04/02/2014 the mm/dd vs dd/mm are ambiguous, so it is an error for strict parse rules.
Functions ¶
func MustParse ¶
func MustParse(datestr string, opts ...ParserOption) time.Time
MustParse parse a date, and panic if it can't be parsed. Used for testing. Not recommended for most use-cases.
func ParseAny ¶
func ParseAny(datestr string, opts ...ParserOption) (time.Time, error)
ParseAny parse an unknown date format, detect the layout. Normal parse. Equivalent Timezone rules as time.Parse(). NOTE: please see readme on mmdd vs ddmm ambiguous dates.
func ParseFormat ¶
func ParseFormat(datestr string, opts ...ParserOption) (string, error)
ParseFormat parse's an unknown date-time string and returns a layout string that can parse this (and exact same format) other date-time strings.
layout, err := dateparse.ParseFormat("2013-02-01 00:00:00") // layout = "2006-01-02 15:04:05"
func ParseIn ¶
ParseIn with Location, equivalent to time.ParseInLocation() timezone/offset rules. Using location arg, if timezone/offset info exists in the datestring, it uses the given location rules for any zone interpretation. That is, MST means one thing when using America/Denver and something else in other locations.
func ParseLocal ¶
func ParseLocal(datestr string, opts ...ParserOption) (time.Time, error)
ParseLocal Given an unknown date format, detect the layout, using time.Local, parse.
Set Location to time.Local. Same as ParseIn Location but lazily uses the global time.Local variable for Location argument.
denverLoc, _ := time.LoadLocation("America/Denver") time.Local = denverLoc t, err := dateparse.ParseLocal("3/1/2014")
Equivalent to:
t, err := dateparse.ParseIn("3/1/2014", denverLoc)
func ParseStrict ¶
func ParseStrict(datestr string, opts ...ParserOption) (time.Time, error)
ParseStrict parse an unknown date format. IF the date is ambigous mm/dd vs dd/mm then return an error. These return errors: 3.3.2014 , 8/8/71 etc
Types ¶
type ParserOption ¶
type ParserOption func(*parser) error
ParserOption defines a function signature implemented by options Options defined like this accept the parser and operate on the data within
func PreferMonthFirst ¶
func PreferMonthFirst(preferMonthFirst bool) ParserOption
PreferMonthFirst is an option that allows preferMonthFirst to be changed from its default
func RetryAmbiguousDateWithSwap ¶
func RetryAmbiguousDateWithSwap(retryAmbiguousDateWithSwap bool) ParserOption
RetryAmbiguousDateWithSwap is an option that allows retryAmbiguousDateWithSwap to be changed from its default