1// SPDX-FileCopyrightText: Amolith <amolith@secluded.site>
2//
3// SPDX-License-Identifier: AGPL-3.0-or-later
4
5// Package dateutil provides natural language date parsing for lune commands.
6package dateutil
7
8import (
9 "fmt"
10
11 "git.secluded.site/go-lunatask"
12 dps "github.com/markusmobius/go-dateparser"
13)
14
15// Parse parses a natural language date string into a lunatask.Date.
16// Supports formats like "yesterday", "2 days ago", "March 5", "2024-01-15", etc.
17// Returns today's date if the input is empty.
18func Parse(input string) (lunatask.Date, error) {
19 if input == "" {
20 return lunatask.Today(), nil
21 }
22
23 parsed, err := dps.Parse(nil, input)
24 if err != nil {
25 return lunatask.Date{}, fmt.Errorf("parsing date %q: %w", input, err)
26 }
27
28 return lunatask.NewDate(parsed.Time), nil
29}