Documentation
¶
Overview ¶
Package durationparser contains a duration parser created to have a wider range of units than the stdlib time.ParseDuration and to attempt to take into account the variability in calender units such as months or years
To do this it uses a time.Time reference and custom transform functions for certain units instead of a duration sum with fixed units, as such all durations are parsed relative to a point in time, using `ParseAt` you can specify what time to use, or you can use `Parse` to parse relative to time.Now or `ParseStatic` to parse relative to a blank time.Time{}
It also includes some extra customisability like the ability to create custom units or set a max or minimum duration
Index ¶
Constants ¶
const ( ErrorUnknownError dpErrorType = iota ErrorUndefinedUnit ErrorTooHigh ErrorTooLow )
Some various error internal error types. These can be used with the errors.Is function provided by the errors package to help narrow down the error cause and provide a better formatted error message to a user. Note that these only define the errors specific to the parser, errors from other packages (such as a malformed float or int) are not defined or handled by this package, those errors are simply returned as is by the parse function. (see strconv.ErrRange and strconv.ErrSyntax for checking those)
Variables ¶
var ( EnglishIdentifiersMiliseconds = []string{"milliseconds", "millisecond", "miliseconds", "milisecond", "ms"} EnglishIdentifiersSeconds = []string{"seconds", "second", "secs", "sec", "s"} EnglishIdentifiersMinutes = []string{"minutes", "minute", "mins", "min", "m"} EnglishIdentifiersHours = []string{"hours", "hour", "h"} EnglishIdentifiersDays = []string{"days", "day", "d"} EnglishIdentifiersWeeks = []string{"weeks", "week", "wks", "wk", "w"} EnglishIdentifiersMonths = []string{"months", "month", "mo"} EnglishIdentifiersQuarters = []string{"quarters", "quarter", "q"} EnglishIdentifiersYears = []string{"years", "year", "y"} EnglishIdentifiersDecades = []string{"decades", "decade", "dec"} )
Some default English terms for time
var ( Millisecond = Unit{ Unit: StaticAdder(time.Millisecond), Identifiers: EnglishIdentifiersMiliseconds, } Second = Unit{ Unit: StaticAdder(time.Second), Identifiers: EnglishIdentifiersSeconds, } Minute = Unit{ Unit: StaticAdder(time.Minute), Identifiers: EnglishIdentifiersMinutes, } Hour = Unit{ Unit: StaticAdder(time.Hour), Identifiers: EnglishIdentifiersHours, } Day = Unit{ Unit: DayAdder(1), Identifiers: EnglishIdentifiersDays, } Week = Unit{ Unit: DayAdder(7), Identifiers: EnglishIdentifiersWeeks, } Month = Unit{ Unit: MonthAdder(1), Identifiers: EnglishIdentifiersMonths, } Year = Unit{ Unit: YearAdder(1), Identifiers: EnglishIdentifiersYears, } Decade = Unit{ Unit: YearAdder(10), Identifiers: EnglishIdentifiersDecades, } )
Preset english units
var Default = &Parser{ Units: []Unit{ Millisecond, Second, Minute, Hour, Day, Week, Month, Year, Decade, }, }
Default is a presetup default parser
Functions ¶
func Parse ¶
Parse is an alias to Parse in the default parser, see the documentation on Parser and its methods for more information
Types ¶
type DayAdder ¶
type DayAdder int
DayAdder is a UnitAdder that adds a number of days taking account of the calander (via time.Time.AddDate)
type MonthAdder ¶
type MonthAdder int
MonthAdder is a UnitAdder that adds a number of months taking account of the calander (via time.Time.AddDate)
type Parser ¶
type Parser struct { // The units names and their assosiated functions to use in parsing a duration Units []Unit // The Maximum duration, if Max != 0 it will check if the parsed duration exceeds max and will return the value of max and ErrorTooHigh // ErrorTooHigh can be safely ignored as the returned duration will be the value of max (check for ErrorTooHigh using errors.Unwrap) Maximum time.Duration // The minimum duration, functions the same as maximum except flipped to check if the duration is less than minimum and returns ErrorTooLow instead Minimum time.Duration // the unit to use if no unit is specified DefaultUnit string // This sets whether or not spaces should be completely ignored (false) or whether they should be kept in unit identifiers // when disabled (default) `1 0 h o u r s` gets parsed the same as 10 `hours` // when enabled it gets parsed as 10 `h o u r s` KeepSpacesInIdentifier bool }
Parser is a duration parsing tool that uses the calander via time.Time as a reference point instead of a static length sum. the goal being that by using time.Time as a base instead of a duration it should be able to properly take into account the variable length of calender units, so that if its may 4th a duration string such as `2 months` returns the duration of time between may 4th and july 4th instead of just assuming all months are the same and returning 60 days. Parser is safe for concurrent use as long as the units or settings are not modified while in use and no custom unit methods are created with concurrency unsafe code.
type StaticAdder ¶
StaticAdder is a UnitAdder that adds a fixed unit of duration to t
type UnitAdder ¶
type UnitAdder interface { // t is the time to be added to // integer is the number given with the unit (rounded down/tunctuated if any decimal point was given) // float is the number given with the unit as a floating point number, including the decimal point if given Add(t time.Time, integer int, float float64) time.Time }
UnitAdder is an interface to allow custom means of adding time to things