From bfbd031e418164ae0f2507d9178eef787b9cd0f7 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Tim=20H=C3=A5rek=20Andreassen?= Date: Fri, 4 Oct 2024 23:38:23 +0200 Subject: [PATCH] refactor: Swap logic order for UTC time convertion MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Signed-off-by: Tim Hårek Andreassen --- cmd/forecast.go | 7 +++--- cmd/forecast_helper.go | 4 +++ cmd/now.go | 9 +++---- cmd/today.go | 7 +++--- cmd/tomorrow.go | 56 +++++++++++++----------------------------- 5 files changed, 31 insertions(+), 52 deletions(-) diff --git a/cmd/forecast.go b/cmd/forecast.go index abfbba7..933addb 100644 --- a/cmd/forecast.go +++ b/cmd/forecast.go @@ -42,12 +42,11 @@ func forecast(cmd *cobra.Command, args []string) { t := table.New() for _, item := range f.Forecast { - itemTime := item.Time.Local() - if helper.isUTC { - itemTime = item.Time + if !helper.isUTC { + item.Time = item.Time.Local() } t.Row( - itemTime.Format("Mon, 2 Jan 15:04"), + item.Time.Format("Mon, 2 Jan 15:04"), fmt.Sprintf("%.1f °C", item.Temperature), fmt.Sprintf("%.1f mm", item.Percipitation), fmt.Sprintf("%.1f m/s %s", item.Wind.Speed, item.Wind.DirectionToString()), diff --git a/cmd/forecast_helper.go b/cmd/forecast_helper.go index ab7c592..8c25d45 100644 --- a/cmd/forecast_helper.go +++ b/cmd/forecast_helper.go @@ -50,6 +50,10 @@ func forecastHelper(cmd *cobra.Command, args []string) *forecastH { } if isDebug { + if !isUTC { + f.Expires = f.Expires.Local() + f.LastModified = f.LastModified.Local() + } fmt.Printf("Expires: %v\n", f.Expires) fmt.Printf("LastModified: %v\n", f.LastModified) fmt.Printf("Default cache-dir: %v\n", os.TempDir()) diff --git a/cmd/now.go b/cmd/now.go index 5e3696c..57cabe3 100644 --- a/cmd/now.go +++ b/cmd/now.go @@ -38,15 +38,14 @@ func now(cmd *cobra.Command, args []string) { outputJson(n) } - itemTime := n.Time.Local() - if helper.isUTC { - itemTime = n.Time + if !helper.isUTC { + n.Time = n.Time.Local() } if isOneLine { fmt.Printf("%s %s: %.1f °C, %.1f mm, %.1f m/s from %s\n", n.Location, - itemTime.Format("Monday 2 Jan 15:04"), + n.Time.Format("Monday 2 Jan 15:04"), n.Temperature, n.Percipitation, n.Wind.Speed, @@ -62,7 +61,7 @@ func now(cmd *cobra.Command, args []string) { Wind: %.1f m/s from %s `, n.Location, - itemTime.Format("Monday 2 Jan 15:04"), + n.Time.Format("Monday 2 Jan 15:04"), n.Temperature, n.Percipitation, n.Wind.Speed, diff --git a/cmd/today.go b/cmd/today.go index fe0fabd..1b5b2b8 100644 --- a/cmd/today.go +++ b/cmd/today.go @@ -38,12 +38,11 @@ func today(cmd *cobra.Command, args []string) { if item.Time.Format(time.DateOnly) != today.Format(time.DateOnly) { continue } - itemTime := item.Time.Local() - if helper.isUTC { - itemTime = item.Time + if !helper.isUTC { + item.Time = item.Time.Local() } t.Row( - itemTime.Format("15:04"), + item.Time.Format("15:04"), fmt.Sprintf("%.1f °C", item.Temperature), fmt.Sprintf("%.1f mm", item.Percipitation), fmt.Sprintf("%.1f m/s %s", item.Wind.Speed, item.Wind.DirectionToString()), diff --git a/cmd/tomorrow.go b/cmd/tomorrow.go index da94558..4db4289 100644 --- a/cmd/tomorrow.go +++ b/cmd/tomorrow.go @@ -6,9 +6,7 @@ import ( "os" "time" - "git.sr.ht/~timharek/yr/cmd/flags" "git.sr.ht/~timharek/yr/cmd/internal/ui/table" - "git.sr.ht/~timharek/yr/pkg/nominatim" "git.sr.ht/~timharek/yr/yr" "github.com/pkg/browser" "github.com/spf13/cobra" @@ -27,33 +25,10 @@ func init() { } func tomorrow(cmd *cobra.Command, args []string) { - isJson, err := cmd.Flags().GetBool(flags.JSON) - cobra.CheckErr(err) - isUTC, err := cmd.Flags().GetBool(flags.UTC) - cobra.CheckErr(err) - isWeb, err := cmd.Flags().GetBool(flags.WEB) - cobra.CheckErr(err) - lon, _ := cmd.Flags().GetFloat64(flags.LON) - lat, _ := cmd.Flags().GetFloat64(flags.LAT) + helper := forecastHelper(cmd, args) + f := helper.f - c, err := yr.New() - cobra.CheckErr(err) - - if len(args) == 0 && (lon == 0 || lat == 0) { - fmt.Fprintln(os.Stderr, "No location or coordinates provided.") - os.Exit(1) - } - f := &yr.ForecastResult{} - if len(args) == 0 { - f, err = c.ForecastCoords(&nominatim.Coordinates{Longitude: lon, Latitude: lat}, nil) - cobra.CheckErr(err) - } else { - location := args[0] - f, err = c.Forecast(location) - cobra.CheckErr(err) - } - - if isWeb { + if helper.isWeb { url := fmt.Sprintf("https://www.yr.no/en/forecast/hourly-table/%.4f,%.4f?i=1", f.Coordinates.Latitude, f.Coordinates.Longitude) err := browser.OpenURL(url) if err != nil { @@ -62,30 +37,33 @@ func tomorrow(cmd *cobra.Command, args []string) { os.Exit(0) } - if isJson { - j, err := json.MarshalIndent(f, "", " ") - cobra.CheckErr(err) - fmt.Printf("%s", j) - return - } - t := table.New() + tomorrows := []yr.Forecast{} + tomorrow := time.Now().AddDate(0, 0, 1) for _, item := range f.Forecast { if item.Time.Format(time.DateOnly) != tomorrow.Format(time.DateOnly) { continue } - itemTime := item.Time.Local() - if isUTC { - itemTime = item.Time + if !helper.isUTC { + item.Time = item.Time.Local() } + tomorrows = append(tomorrows, item) t.Row( - fmt.Sprintf("tomorrow %s", itemTime.Format("15:04")), + fmt.Sprintf("tomorrow %s", item.Time.Format("15:04")), fmt.Sprintf("%.1f °C", item.Temperature), fmt.Sprintf("%.1f mm", item.Percipitation), fmt.Sprintf("%.1f m/s %s", item.Wind.Speed, item.Wind.DirectionToString()), ) } + + if helper.isJson { + j, err := json.MarshalIndent(tomorrows, "", " ") + cobra.CheckErr(err) + fmt.Printf("%s", j) + return + } + fmt.Println(t) }