Documentation
¶
Index ¶
- Variables
- func IsKnownLocale(code string) bool
- func Parse(cfg *Configuration, str string, formats ...string) (date.Date, error)
- func ParseHijri(cfg *Configuration, str string) (date.Date, error)
- func ParseJalali(cfg *Configuration, str string) (date.Date, error)
- type Configuration
- type DateOrder
- type Parser
- type ParserType
- type PreferredDateSource
- type PreferredDayOfMonth
- type SearchResult
Constants ¶
This section is empty.
Variables ¶
var ( YMD = func(_ string) string { return "YMD" } YDM = func(_ string) string { return "YDM" } MYD = func(_ string) string { return "MYD" } MDY = func(_ string) string { return "MDY" } DYM = func(_ string) string { return "DYM" } DMY = func(_ string) string { return "DMY" } DefaultDateOrder = func(locale string) string { if ld, exist := data.GetLocaleData(locale); exist { return ld.DateOrder } else { return "YMD" } } )
Functions ¶
func IsKnownLocale ¶
IsKnownLocale is helper function to check if the specified locale or language can be parsed by the Parser. The code must be in ISO639 format.
func Parse ¶
Parse parses string representing date and/or time in recognizable localized formats, using the default Parser. Useful for quick use.
func ParseHijri ¶
func ParseHijri(cfg *Configuration, str string) (date.Date, error)
ParseHijri parses a Hijri date string using Umm al-Qura Calendar which commonly used in Islamic country.
func ParseJalali ¶
func ParseJalali(cfg *Configuration, str string) (date.Date, error)
ParseJalali parses a Jalali date (which also called Persian or Solar Hijri date) which commonly used in Iran and Afghanistan.
Types ¶
type Configuration ¶
type Configuration struct {
// Locales is a list of locale codes, e.g. ['fr-PF', 'qu-EC', 'af-NA'].
// The parser uses only these locales to translate date string.
Locales []string
// Languages is a list of language codes, e.g. ['en', 'es', 'zh-Hant']. If
// locales are not given, languages and region are used to construct locales
// for translation.
Languages []string
// Region is a region code, e.g. 'IN', '001', 'NE'. If locales are not given,
// languages and region are used to construct locales for translation.
Region string
// If true, locales previously used to translate date are tried first.
TryPreviousLocales bool
// If true, locales are tried for translation of date string in the order in
// which they are given.
UseGivenOrder bool
// Default languages is a list of language codes in ISO 639 (e.g. "en", "fr") that will be
// used as default languages for parsing when language detection fails. When using this
// setting, these languages will be tried after trying with the detected languages with no
// success. It is especially useful when using the `DetectLanguagesFunction`.
DefaultLanguages []string
// DateOrder is function that specifies the order in which date components year, month
// and day are expected while parsing ambiguous dates. If empty, parser will use each
// language specific date order.
DateOrder DateOrder
// CurrentTime is the base datetime to use for interpreting partial or relative date
// strings. Defaults to the current date and time in UTC.
CurrentTime time.Time
// PreferredDayOfMonth specify the day for date with missing day. Defaults to `Current`.
PreferredDayOfMonth PreferredDayOfMonth
// PreferredDateSource specify the date source to fill incomplete date values. Defaults
// to `CurrentPeriod`.
PreferredDateSource PreferredDateSource
// StrictParsing when set to true will make the parser returns a date only if the date
// is complete, i.e. has day, month and year value. Defaults to false.
StrictParsing bool
// RequiredParts is list of date components that required by the parser. Defaults to
// `nil` and can accept "day", "month" and "year".
RequiredParts []string
// SkipTokens is a list of tokens to discard while detecting language. Defaults to
// []string{"t"} which skips T in iso format datetime string e.g. 2015-05-02T10:20:19+0000.
SkipTokens []string
// ReturnTimeAsPeriod returns `Time` as period in date object, if time component is present
// in date string. Defaults to false.
ReturnTimeAsPeriod bool
}
Configuration is object to control and configure parsing behavior of date parser.
func (Configuration) Clone ¶
func (c Configuration) Clone() *Configuration
Clone clones the config to a new, separate one.
type DateOrder ¶
DateOrder is function that returns date order string for specified language and/or locale. The returned date order MUST only uses characters M, D or Y which represents month, day and year.
type Parser ¶
type Parser struct {
sync.Mutex
// DetectLanguagesFunction is a function for language detection that takes
// as input a `text` and returns a list of detected language codes. Note:
// this function is only used if `languages` and `locales` are not provided.
DetectLanguagesFunction func(string) []string
// ParserTypes is a list of types of parsers to try, allowing to customize which parsers are tried
// against the input date string, and in which order they are tried. By default it will use
// all parser in following order: `Timestamp`, `RelativeTime`, `CustomFormat`, `AbsoluteTime`,
// and finally `NoSpacesTime`.
ParserTypes []ParserType
// contains filtered or unexported fields
}
Parser is object that handles language detection, translation and subsequent generic parsing of string representing date and/or time.
func (*Parser) Parse ¶
Parse parses string representing date and/or time in recognizable localized formats. Supports parsing multiple languages.
func (*Parser) Search ¶
func (p *Parser) Search(cfg *Configuration, text string) (string, []SearchResult, error)
Search detect the suitable language of the text, then find all substrings of the given string which represent date and/or time and parse them using the detected language.
func (*Parser) SearchWithLanguage ¶
func (p *Parser) SearchWithLanguage(cfg *Configuration, lang string, text string) ([]SearchResult, error)
SearchWithLanguage find all substrings of the given string which represent date and/or time and parse them using the specified language.
type ParserType ¶
type ParserType uint8
ParserType is the variable to specify which type of parser that will be used.
const ( // Timestamp is parser to parse Unix timestamp. Timestamp ParserType = iota // NegativeTimestamp is parser to parse Unix timestamp in negative value. NegativeTimestamp // RelativeTime is parser to parse date string with relative value like // "1 year, 2 months ago" and "3 hours, 50 minutes ago". RelativeTime // CustomFormat is parser to parse a date string with custom formats. CustomFormat // AbsoluteTime is parser to parse date string with absolute value like // "12 August 2021" and "23 January, 15:10:01". AbsoluteTime // NoSpacesTime is parser to parse date string that written without spaces, // for example 2021-10-11 that written as 20211011. NoSpacesTime )
type PreferredDateSource ¶
type PreferredDateSource uint8
PreferredDateSource is the variable to set date source to fill incomplete dates value.
const ( // CurrentPeriod means parser will use current period to fill the incomplete dates value. // So, for current year 2021 and date string "10 December", parser will return 2021-12-10. CurrentPeriod PreferredDateSource = iota // Past means parser will use past period to fill the incomplete dates value. So, for current // year 2021 and date string "10 December", parser will return 2020-12-10. Past // Future means parser will use future period to fill the incomplete dates value. So, for // current year 2021 and date string "10 December", parser will return 2022-12-10. Future )
type PreferredDayOfMonth ¶
type PreferredDayOfMonth uint8
PreferredDayOfMonth is the variable to set day value for date that has month and year, but missing the day value. For example, date like "2021-12" or "February 2000".
const ( // Current means parser will use current day to fill the day value. So, if today is // 2022-01-20 and date string is "February 2020", parser will return 2020-02-20. Current PreferredDayOfMonth = iota // First means parser will use first day of the month to fill the day value. So, if // date string is "February 2020", parser will return 2020-02-01. First // Last means parser will use last day of the month to fill the day value. So, if // date string is "February 2020", parser will return 2020-02-29. Last )
type SearchResult ¶
func Search ¶
func Search(cfg *Configuration, text string) (string, []SearchResult, error)
Search detect the suitable language of the text, then find all substrings of the given string which represent date and/or time and parse them using the default parser. Useful for quick use.