diff options
| author | JP Appel <jeanpierre.appel01@gmail.com> | 2025-06-26 23:31:09 -0400 |
|---|---|---|
| committer | JP Appel <jeanpierre.appel01@gmail.com> | 2025-06-26 23:37:30 -0400 |
| commit | 916430e5d3f33a24b13e188428d5335862472411 (patch) | |
| tree | 9618a1ee28eb6cb6985fddbc507efdef525c0a5d /pkg/util | |
| parent | dd431b36c21b4a909d3ae0a12a4e607d326ac3cc (diff) | |
Improve compilation of clauses to sqlite3 SQL
Diffstat (limited to 'pkg/util')
| -rw-r--r-- | pkg/util/util.go | 26 |
1 files changed, 26 insertions, 0 deletions
diff --git a/pkg/util/util.go b/pkg/util/util.go index 0ed64b8..577401e 100644 --- a/pkg/util/util.go +++ b/pkg/util/util.go @@ -37,6 +37,32 @@ func ParseDateTime(s string) (time.Time, error) { return time.Time{}, err } +// Estimate an interval around a time which is still "meaningful" +// +// Ex: 2025-06-14 -> [2025-06-10, 2025-06-18] +// Ex: 2025-06-14T12:00 -> [2025-06-14T8:00, 2025-06-14T16:00] +func FuzzDatetime(t time.Time) (start time.Time, stop time.Time) { + hour, minute, sec := t.Clock() + _, month, day := t.Date() + + var d time.Duration + if sec != 0 { + d = 5 * time.Minute + } else if minute != 0 { + d = 30 * time.Minute + } else if hour != 0 { + d = 4 * time.Hour + } else if day != 1 { + d = 84 * time.Hour // +- 3.5 days + } else if month != time.January { + d = 336 * time.Hour // +- .5 months + } else { + d = 4380 * time.Hour // search +- 6months + } + + return t.Add(-d), t.Add(d) +} + // Create a copy of a slice with all values that satisfy cond func Fitler[E any](s []E, cond func(e E) bool) []E { filtered := make([]E, 0, len(s)) |
