go-dateparser
go-dateparser copied to clipboard
Preference like `DefaultToFuture` and `DefaultToPast` of ijt/go-anytime?
RT
You can specify the preferred date source by setting PreferredDateSource in the configuration. Here is the complete example:
package main
import (
"fmt"
"strings"
"time"
dps "github.com/markusmobius/go-dateparser"
)
func main() {
// Prepare sample texts
maxTextLen := 17
texts := []string{
"10 December",
"March",
"Friday",
"Monday",
"Sunday",
"10:00PM",
"16:10",
"14:05",
"15 february 15:00",
"3/3/50",
"3/3/94",
}
// Prepare config
now := time.Date(2023, 3, 15, 12, 25, 0, 0, time.UTC)
cfg := dps.Configuration{CurrentTime: now}
// Print current time
fmt.Println("CURRENT TIME")
fmt.Println("============")
fmt.Println(strTime(now))
// Parse each sample text
dateSources := []dps.PreferredDateSource{
dps.CurrentPeriod,
dps.Past,
dps.Future,
}
for _, dateSource := range dateSources {
// Prepare title
title := "PREFER CURRENT PERIOD"
if dateSource == dps.Past {
title = "PREFER PAST PERIOD"
} else if dateSource == dps.Future {
title = "PREFER FUTURE PERIOD"
}
fmt.Println()
fmt.Println(title)
fmt.Println(strings.Repeat("=", len(title)))
// Parse each text
cfg.PreferredDateSource = dateSource
for _, text := range texts {
dt, _ := dps.Parse(&cfg, text)
nSpacePad := maxTextLen - len(text)
fmt.Printf("%q%s => %q\n", text,
strings.Repeat(" ", nSpacePad),
strTime(dt.Time))
}
}
}
func strTime(t time.Time) string {
str := t.Format("Monday, 2006-01-02 15:04:05")
str = strings.ReplaceAll(str, " 00:00:00", "")
return str
}
Which will give us the following result:
CURRENT TIME
============
Wednesday, 2023-03-15 12:25:00
PREFER CURRENT PERIOD
=====================
"10 December" => "Sunday, 2023-12-10"
"March" => "Wednesday, 2023-03-15"
"Friday" => "Friday, 2023-03-10"
"Monday" => "Monday, 2023-03-13"
"Sunday" => "Sunday, 2023-03-12"
"10:00PM" => "Wednesday, 2023-03-15 22:00:00"
"16:10" => "Wednesday, 2023-03-15 16:10:00"
"14:05" => "Wednesday, 2023-03-15 14:05:00"
"15 february 15:00" => "Wednesday, 2023-02-15 15:00:00"
"3/3/50" => "Thursday, 2050-03-03"
"3/3/94" => "Thursday, 1994-03-03"
PREFER PAST PERIOD
==================
"10 December" => "Saturday, 2022-12-10"
"March" => "Wednesday, 2023-03-15"
"Friday" => "Friday, 2023-03-10"
"Monday" => "Monday, 2023-03-13"
"Sunday" => "Sunday, 2023-03-12"
"10:00PM" => "Tuesday, 2023-03-14 22:00:00"
"16:10" => "Tuesday, 2023-03-14 16:10:00"
"14:05" => "Tuesday, 2023-03-14 14:05:00"
"15 february 15:00" => "Wednesday, 2023-02-15 15:00:00"
"3/3/50" => "Friday, 1950-03-03"
"3/3/94" => "Thursday, 1994-03-03"
PREFER FUTURE PERIOD
====================
"10 December" => "Sunday, 2023-12-10"
"March" => "Friday, 2024-03-15"
"Friday" => "Friday, 2023-03-17"
"Monday" => "Monday, 2023-03-20"
"Sunday" => "Sunday, 2023-03-19"
"10:00PM" => "Wednesday, 2023-03-15 22:00:00"
"16:10" => "Wednesday, 2023-03-15 16:10:00"
"14:05" => "Wednesday, 2023-03-15 14:05:00"
"15 february 15:00" => "Thursday, 2024-02-15 15:00:00"
"3/3/50" => "Thursday, 2050-03-03"
"3/3/94" => "Wednesday, 2094-03-03"
Since it's already supported via configuration, I'll close this issue for now.