Documentation
¶
Overview ¶
Package nowandlater parses natural-language date and time strings into concrete time.Time values.
The package is self-contained with no external dependencies.
Quick start ¶
p := nowandlater.Parser{}
t, err := p.Parse("next Monday at 9:30 AM")
start, end, err := p.ParseInterval("this week")
Parser is the primary entry point. Its zero value is valid and uses English, time.Local, and time.Now as defaults.
Supported input formats ¶
Absolute dates:
2026-12-04 2026/12/04 2026.12.04 December 4, 2026 Dec 4 2026 04-Dec-2026 12/04/2026 2026-dec-04
Relative expressions:
tomorrow yesterday now next Monday last Friday this week in 3 days 2 hours ago a week from now 3 days before tomorrow
Times:
9:30 AM 14:30:00 noon midnight next Monday at 9:30 AM 2026-12-04T09:30:00Z
Machine formats (RFC 3339, RFC 2822, ANSI C, Unix, Ruby, timestamp):
2026-03-22T10:00:00-07:00 Mon, 02 Jan 2006 15:04:05 -0700 Mon Jan 2 15:04:05 2006 1774711545 (Unix timestamp, seconds since epoch; ≥ 5 digits)
Multi-language support ¶
Built-in languages: [LangEn], [LangEs], [LangFr], [LangDe], [LangIt], [LangPt], [LangRu], [LangJa], [LangZh].
Custom languages can be added by constructing a Lang with a [Lang.Words] map, [Lang.OrdinalSuffixes], and optional [Lang.DateOrder] and [Lang.Handlers] overrides. See docs/contributing.md for the full guide.
Interval support ¶
Parser.ParseInterval resolves an expression to a half-open calendar interval [start, end). [ResolveInterval] and [EndOf] are available for lower-level use.
Error handling ¶
An unrecognised input returns [ErrUnknownSignature]. An input that is recognisably date-like but genuinely ambiguous returns [ErrAmbiguous] — for example, "mar 5" in Spanish where "mar" abbreviates both martes (Tuesday) and marzo (March). All other errors indicate invalid timezone tokens.
Index ¶
Constants ¶
This section is empty.
Variables ¶
This section is empty.
Functions ¶
func LookupLang ¶
LookupLang returns the built-in Lang for the given ISO 639-1 code, or nil if the code is not recognised. The lookup is case-insensitive and region suffixes are ignored ("en_US" and "en-GB" both return &LangEn).
Types ¶
type Parser ¶
type Parser struct {
// Lang is the language configuration to use for parsing.
// If nil, LangEn is used.
Lang *Lang
// Location is the default timezone for the returned time when the
// input string contains no explicit timezone. If nil, time.Local is used.
// An explicit timezone in the input (e.g. "3pm EST") always takes priority.
Location *time.Location
// Now is the time source used as the reference point for relative
// expressions ("tomorrow", "next week", etc.). If nil, time.Now is used.
// Set this to a fixed function for deterministic tests.
Now func() time.Time
}
Parser wraps a Lang with runtime defaults, providing a single-call Parse method that combines tokenization and resolution.
The zero value is valid: it uses LangEn, time.Local, and time.Now.
func (Parser) Parse ¶
Parse converts a natural-language date/time string into a time.Time. It calls Lang.Parse followed by Resolve, using the configured defaults.
func (Parser) ParseInterval ¶
ParseInterval converts a natural-language date/time string into a half-open calendar interval [start, end). It calls Lang.Parse followed by ResolveInterval, using the configured defaults.
See ResolveInterval for semantics, including the calendar-alignment behaviour for delta-based expressions.