Documentation
¶
Overview ¶
Package dateparse parses date-strings without knowing the format in advance, using a fast lex based approach to eliminate shotgun attempts. Validates comprehensively to avoid false positives.
By default it leans towards US style dates when there is a conflict. This can be adjusted using the `PreferMonthFirst` parser option. Some ambiguous formats can fail (e.g., trying to parse `31/03/2023“ as the default month-first format `MM/DD/YYYY`), but can be automatically retried with `RetryAmbiguousDateWithSwap`.
Consider turning on the the `SimpleErrorMessages` option if you will be attempting to parse many strings that do not match any known format and you need to maximize performance.
See README.md for key points on how timezone/location parsing works in go, as this can be counterintuitive initially.
Index ¶
- Constants
- Variables
- func BeginningOfMonth(date time.Time) time.Time
- func Calculate(startTime, endTime time.Time) int
- func CalculateToNow(givenTime time.Time) int
- func EndOfMonth(date time.Time) time.Time
- func EnglishToNepali(year int, month int, day int) (*[3]int, error)
- func GetCurrentEnglishTime() time.Time
- func GetNepaliLocation() *time.Location
- func IsLeapYear(givenYear int) bool
- func MustParse(datestr string, opts ...ParserOption) time.Time
- func NepaliToEnglish(year int, month int, day int) (*[3]int, error)
- func NextLeapYear(givenYear int) (foundYear int, found bool)
- func Parse(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)
- func Pretty(pretty bool) func(*gparser) error
- func PrevLeapYear(givenYear int) (foundYear int, found bool)
- func Size(size int) func(*gparser) error
- func TimeElapsed(now time.Time, then time.Time, full bool) string
- type Direction
- type ExprType
- type NepaliFormatter
- type NepaliMonthData
- type NepaliTime
- func (obj *NepaliTime) Clock() (hour, min, sec int)
- func (obj *NepaliTime) Date() (year, month, day int)
- func (obj *NepaliTime) Day() int
- func (obj *NepaliTime) Format(format string) (string, error)
- func (obj *NepaliTime) GetEnglishTime() time.Time
- func (obj *NepaliTime) Hour() int
- func (obj *NepaliTime) Minute() int
- func (obj *NepaliTime) Month() int
- func (obj *NepaliTime) Nanosecond() int
- func (obj *NepaliTime) Second() int
- func (obj *NepaliTime) String() string
- func (obj *NepaliTime) Weekday() time.Weekday
- func (obj *NepaliTime) Year() int
- type Option
- type ParserOption
Constants ¶
const ( ExprTypeInvalid = 0 ExprTypeDate = ExprType(1 << iota) ExprTypeTime ExprTypeNow ExprTypeRelativeMinutes ExprTypeRelativeHours ExprTypeRelativeDays ExprTypeRelativeWeeks ExprTypeRelativeWeekdays ExprTypeRelativeMonth ExprTypeRelativeYear ExprTypeClock12Hour ExprTypeClock24Hour )
const (
Timezone string = "Asia/Kathmandu"
)
Variables ¶
var ( // 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. ErrAmbiguousMMDD = fmt.Errorf("this date has ambiguous mm/dd vs dd/mm type format") ErrCouldntFindFormat = fmt.Errorf("could not find format for") ErrUnexpectedTail = fmt.Errorf("unexpected content after date/time: ") ErrUnknownTZOffset = fmt.Errorf("TZ offset not recognized") ErrUnknownTimeZone = fmt.Errorf("timezone not recognized") ErrFracSecTooLong = fmt.Errorf("fractional seconds too long") )
Functions ¶
func Calculate ¶ added in v0.0.4
Calculate returns an integer-value age based on the duration between the two times that are given as arguments.
func CalculateToNow ¶ added in v0.0.4
CalculateToNow returns an integer-value age based on the duration between the given time and the present time.
func EnglishToNepali ¶ added in v0.0.4
Converts english date to nepali. Accepts the input parameters year, month, day. Returns dates in array and error.
func GetCurrentEnglishTime ¶ added in v0.0.4
Gets current English date along with time level precision. Current Time of Asia/Kathmandu
func GetNepaliLocation ¶ added in v0.0.4
Returns location for Asia/Kathmandu (constants.Timezone)
func IsLeapYear ¶ added in v0.0.4
IsLeapYear returns true only if the given year contains a leap day, meaning that the year is a leap year.
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 NepaliToEnglish ¶ added in v0.0.4
Converts nepali date to english. Accepts the input parameters year, month, day. Returns dates in array and error.
func NextLeapYear ¶ added in v0.0.4
NextLeapYear returns the next leap year after a given year. It also returns a boolean value that is set to false if no such year can be found.
func Parse ¶
func Parse(datestr string, opts ...ParserOption) (time.Time, error)
Parse 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 parses an unknown date-time string and returns a layout string that can parse this (and exact same format) other date-time strings.
In certain edge cases, this may produce a format string of a different length than the input string. If this happens, it's an edge case that requires individually parsing each time.
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. See README for a more detailed explanation.
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
func PrevLeapYear ¶ added in v0.0.4
PrevLeapYear returns the previous leap year before a given year. It also returns a boolean value that is set to false if no such year can be found.
Types ¶
type Direction ¶ added in v0.0.4
type Direction int
Direction is the direction used for ambiguous expressions.
type ExprType ¶ added in v0.0.4
type ExprType int
func ParseNaturalDate ¶ added in v0.0.4
ParseNaturalDate query string.
func (ExprType) IsDateOnly ¶ added in v0.0.4
func (ExprType) IsTimeOnly ¶ added in v0.0.4
type NepaliFormatter ¶ added in v0.0.4
type NepaliFormatter struct {
// contains filtered or unexported fields
}
func NewFormatter ¶ added in v0.0.4
func NewFormatter(nepaliTime *NepaliTime) *NepaliFormatter
type NepaliMonthData ¶ added in v0.0.4
type NepaliMonthData struct {
// contains filtered or unexported fields
}
type NepaliTime ¶ added in v0.0.4
type NepaliTime struct {
// contains filtered or unexported fields
}
func Date ¶ added in v0.0.4
func Date(year, month, day, hour, min, sec, nsec int) (*NepaliTime, error)
Date returns the Time corresponding to
yyyy-mm-dd hh:mm:ss + nsec nanoseconds
func FromEnglishTime ¶ added in v0.0.4
func FromEnglishTime(englishTime time.Time) (*NepaliTime, error)
Converts Time object to NepaliTime
func Now ¶ added in v0.0.4
func Now() *NepaliTime
Now returns the current nepali time. this function should always work and should not return nil in normal circumstances
func ParseNP ¶ added in v0.0.4
func ParseNP(datetimeStr string, format string) (*NepaliTime, error)
ParseNP equivalent to time.Parse()
func (*NepaliTime) Clock ¶ added in v0.0.4
func (obj *NepaliTime) Clock() (hour, min, sec int)
Clock returns the hour, minute, and second of the day.
func (*NepaliTime) Date ¶ added in v0.0.4
func (obj *NepaliTime) Date() (year, month, day int)
Date returns the year, month, and day
func (*NepaliTime) Day ¶ added in v0.0.4
func (obj *NepaliTime) Day() int
Day returns the day of the month.
func (*NepaliTime) Format ¶ added in v0.0.4
func (obj *NepaliTime) Format(format string) (string, error)
formats the nepalitime object into the passed format
func (*NepaliTime) GetEnglishTime ¶ added in v0.0.4
func (obj *NepaliTime) GetEnglishTime() time.Time
Get's the corresponding english time
func (*NepaliTime) Hour ¶ added in v0.0.4
func (obj *NepaliTime) Hour() int
Hour returns the hour within the day, in the range [0, 23].
func (*NepaliTime) Minute ¶ added in v0.0.4
func (obj *NepaliTime) Minute() int
Minute returns the minute offset within the hour, in the range [0, 59].
func (*NepaliTime) Month ¶ added in v0.0.4
func (obj *NepaliTime) Month() int
Month returns the month of the year.
func (*NepaliTime) Nanosecond ¶ added in v0.0.4
func (obj *NepaliTime) Nanosecond() int
Nanosecond returns the nanosecond offset within the second, in the range [0, 999999999].
func (*NepaliTime) Second ¶ added in v0.0.4
func (obj *NepaliTime) Second() int
Second returns the second offset within the minute, in the range [0, 59].
func (*NepaliTime) String ¶ added in v0.0.4
func (obj *NepaliTime) String() string
String returns a string representing the duration in the form "2079-10-06 01:00:05"
func (*NepaliTime) Weekday ¶ added in v0.0.4
func (obj *NepaliTime) Weekday() time.Weekday
Weekday returns the day of the week. Sunday = 0, Monday = 1, Saturday = 6
func (*NepaliTime) Year ¶ added in v0.0.4
func (obj *NepaliTime) Year() int
Year returns the year in which nepalitime occurs.
type Option ¶ added in v0.0.4
type Option func(*gparser)
Option function.
func WithDirection ¶ added in v0.0.4
WithDirection sets the direction used for ambiguous expressions. By default the Past direction is used, so "sunday" will be the previous Sunday, rather than the next Sunday.
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 AllowPartialStringMatch ¶
func AllowPartialStringMatch(allowPartialStringMatch bool) ParserOption
AllowPartialStringMatch is an option that allows allowPartialStringMatch to be changed from its default. If true, then strings can be attempted to be parsed / matched even if the end of the string might contain more than a date/time. This defaults to false.
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
func SimpleErrorMessages ¶
func SimpleErrorMessages(simpleErrorMessages bool) ParserOption
SimpleErrorMessages is an option that will cause returned error messages to contain less detail, but it will avoid allocating any memory for the custom error message. If you expect to attempt to parse a lot of text that is not valid, this could help reduce GC pressure.