From 391452e3d95ada3dc38d1de3be033790cbc6b234 Mon Sep 17 00:00:00 2001 From: Dan Jones Date: Sun, 25 Feb 2024 12:36:43 -0600 Subject: [PATCH] =?UTF-8?q?=E2=9C=A8=20ParseDate=20should=20handle=20DateF?= =?UTF-8?q?ormat?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .gitignore | 2 + cmd/test.go | 73 ---- cover.html | 863 --------------------------------------- models/entry.go | 4 +- tools/parse_date.go | 10 +- tools/parse_date_test.go | 5 +- 6 files changed, 17 insertions(+), 940 deletions(-) delete mode 100644 cmd/test.go delete mode 100644 cover.html diff --git a/.gitignore b/.gitignore index 461da1e..3a550dc 100644 --- a/.gitignore +++ b/.gitignore @@ -123,3 +123,5 @@ Temporary Items # End of https://www.toptal.com/developers/gitignore/api/go,linux,emacs,macos my-log +cover.html +cmd/test.go diff --git a/cmd/test.go b/cmd/test.go deleted file mode 100644 index 05c426c..0000000 --- a/cmd/test.go +++ /dev/null @@ -1,73 +0,0 @@ -/* -Copyright © 2024 Dan Jones - -This program is free software: you can redistribute it and/or modify -it under the terms of the GNU Affero General Public License as published by -the Free Software Foundation, either version 3 of the License, or -(at your option) any later version. - -This program is distributed in the hope that it will be useful, -but WITHOUT ANY WARRANTY; without even the implied warranty of -MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the -GNU Affero General Public License for more details. - -You should have received a copy of the GNU Affero General Public License -along with this program. If not, see . -*/ -package cmd - -import ( - "fmt" - - "codeberg.org/danjones000/my-log/config" - "github.com/spf13/cobra" - // "github.com/BurntSushi/toml" - dp "github.com/markusmobius/go-dateparser" -) - -// testCmd represents the test command -var testCmd = &cobra.Command{ - Use: "test", - Short: "A brief description of your command", - //Long: ``, - Run: func(cmd *cobra.Command, args []string) { - c, err := config.Load() - fmt.Println("error", err) - fmt.Printf("%+v\n", c) - //ne := "output.stdout.config.json = true" - //toml.Decode(ne, &c) - st, _ := c.Outputs.Stdout() - fmt.Printf("%+v\n", st) - //nc := "\n[input]\nrecurse = false\n[output.stdout.config]\njson = true" - //toml.Decode(nc, &c) - //fmt.Printf("%+v\n", c) - d, err := dp.Parse(nil, "now") - fmt.Println(d.Time, d.Period) - d, err = dp.Parse(nil, "today") - fmt.Println(d.Time, d.Period) - d, err = dp.Parse(nil, "2 days, 3 minutes ago") - fmt.Println(d.Time, d.Period) - d, err = dp.Parse(nil, "in 2 decades, 5 days, 3 minutes") - fmt.Println(d.Time, d.Period) - d, err = dp.Parse(nil, "3 years ago") - fmt.Println(d.Time, d.Period) - d, err = dp.Parse(nil, "1707711800") - fmt.Println(d.Time, d.Period) - d, err = dp.Parse(nil, "@1707711800") - fmt.Println(d.Time, d.Period) - }, -} - -func init() { - rootCmd.AddCommand(testCmd) - - // Here you will define your flags and configuration settings. - - // Cobra supports Persistent Flags which will work for this command - // and all subcommands, e.g.: - // testCmd.PersistentFlags().String("foo", "", "A help for foo") - - // Cobra supports local flags which will only run when this command - // is called directly, e.g.: - // testCmd.Flags().BoolP("toggle", "t", false, "Help message for toggle") -} diff --git a/cover.html b/cover.html deleted file mode 100644 index 239f0e5..0000000 --- a/cover.html +++ /dev/null @@ -1,863 +0,0 @@ - - - - - - config: Go Coverage Report - - - -
- -
- not tracked - - no coverage - low coverage - * - * - * - * - * - * - * - * - high coverage - -
-
-
- - - - - - - - - - - - - - - - - - - -
- - - diff --git a/models/entry.go b/models/entry.go index 24054b2..8b8f3bc 100644 --- a/models/entry.go +++ b/models/entry.go @@ -9,9 +9,11 @@ import ( "strings" "sync" "time" + + "codeberg.org/danjones000/my-log/tools" ) -const DateFormat = "January 02, 2006 at 03:04:05PM -0700" +const DateFormat = tools.DateFormat type Entry struct { Title string diff --git a/tools/parse_date.go b/tools/parse_date.go index 0fd0cb2..e110f0f 100644 --- a/tools/parse_date.go +++ b/tools/parse_date.go @@ -7,6 +7,8 @@ import ( "github.com/markusmobius/go-dateparser/date" ) +const DateFormat = "January 02, 2006 at 03:04:05PM -0700" + // These are somewhat arbitrary, but reasonably useful min and max times var ( MinTime = time.Unix(-2208988800, 0) // Jan 1, 1900 @@ -21,12 +23,16 @@ func ParseDate(in string) (t time.Time, err error) { return MaxTime, nil } - d, err := dp.Parse(&dp.Configuration{ + conf := dp.Configuration{ CurrentTime: time.Now().Local(), ReturnTimeAsPeriod: true, - }, in) + } + + d, err := dp.Parse(&conf, in) t = d.Time if err != nil { + d, err = dp.Parse(&conf, in, DateFormat) + t = d.Time return } diff --git a/tools/parse_date_test.go b/tools/parse_date_test.go index 5ba355d..cfaadd1 100644 --- a/tools/parse_date_test.go +++ b/tools/parse_date_test.go @@ -23,9 +23,11 @@ func TestParseDate(t *testing.T) { firstMonth := time.Date(y, mon, 1, 0, 0, 0, 0, loc) firstYear := time.Date(y, 1, 1, 0, 0, 0, 0, loc) exact := "2075-02-12T12:13:54.536-02:00" - exactd, _ := time.ParseInLocation(time.RFC3339, exact, time.FixedZone("UTC-02:00", -2*60*60)) + exactd, _ := time.ParseInLocation(time.RFC3339, exact, time.FixedZone("UTC-02:00", -7200)) var ts int64 = 1708876012 tsd := time.Unix(ts, 0) + ent := "February 25, 2024 at 04:00:13AM +0230" + entd, _ := time.Parse(DateFormat, ent) tests := []struct { name string @@ -44,6 +46,7 @@ func TestParseDate(t *testing.T) { {"max", MaxTime, ""}, {exact, exactd, ""}, {fmt.Sprint(ts), tsd, ""}, + {ent, entd, ""}, {"not a date", now, fmt.Sprintf(`failed to parse "%s": unknown format`, "not a date")}, } for _, tt := range tests {