refactor: Swap logic order for UTC time convertion

Signed-off-by: Tim Hårek Andreassen <tim@harek.no>
This commit is contained in:
Tim Hårek Andreassen 2024-10-04 23:38:23 +02:00
parent 687d3e8be7
commit bfbd031e41
No known key found for this signature in database
GPG key ID: E59C7734F0E10EB5
5 changed files with 31 additions and 52 deletions

View file

@ -42,12 +42,11 @@ func forecast(cmd *cobra.Command, args []string) {
t := table.New() t := table.New()
for _, item := range f.Forecast { for _, item := range f.Forecast {
itemTime := item.Time.Local() if !helper.isUTC {
if helper.isUTC { item.Time = item.Time.Local()
itemTime = item.Time
} }
t.Row( 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 °C", item.Temperature),
fmt.Sprintf("%.1f mm", item.Percipitation), fmt.Sprintf("%.1f mm", item.Percipitation),
fmt.Sprintf("%.1f m/s %s", item.Wind.Speed, item.Wind.DirectionToString()), fmt.Sprintf("%.1f m/s %s", item.Wind.Speed, item.Wind.DirectionToString()),

View file

@ -50,6 +50,10 @@ func forecastHelper(cmd *cobra.Command, args []string) *forecastH {
} }
if isDebug { if isDebug {
if !isUTC {
f.Expires = f.Expires.Local()
f.LastModified = f.LastModified.Local()
}
fmt.Printf("Expires: %v\n", f.Expires) fmt.Printf("Expires: %v\n", f.Expires)
fmt.Printf("LastModified: %v\n", f.LastModified) fmt.Printf("LastModified: %v\n", f.LastModified)
fmt.Printf("Default cache-dir: %v\n", os.TempDir()) fmt.Printf("Default cache-dir: %v\n", os.TempDir())

View file

@ -38,15 +38,14 @@ func now(cmd *cobra.Command, args []string) {
outputJson(n) outputJson(n)
} }
itemTime := n.Time.Local() if !helper.isUTC {
if helper.isUTC { n.Time = n.Time.Local()
itemTime = n.Time
} }
if isOneLine { if isOneLine {
fmt.Printf("%s %s: %.1f °C, %.1f mm, %.1f m/s from %s\n", fmt.Printf("%s %s: %.1f °C, %.1f mm, %.1f m/s from %s\n",
n.Location, n.Location,
itemTime.Format("Monday 2 Jan 15:04"), n.Time.Format("Monday 2 Jan 15:04"),
n.Temperature, n.Temperature,
n.Percipitation, n.Percipitation,
n.Wind.Speed, n.Wind.Speed,
@ -62,7 +61,7 @@ func now(cmd *cobra.Command, args []string) {
Wind: %.1f m/s from %s Wind: %.1f m/s from %s
`, `,
n.Location, n.Location,
itemTime.Format("Monday 2 Jan 15:04"), n.Time.Format("Monday 2 Jan 15:04"),
n.Temperature, n.Temperature,
n.Percipitation, n.Percipitation,
n.Wind.Speed, n.Wind.Speed,

View file

@ -38,12 +38,11 @@ func today(cmd *cobra.Command, args []string) {
if item.Time.Format(time.DateOnly) != today.Format(time.DateOnly) { if item.Time.Format(time.DateOnly) != today.Format(time.DateOnly) {
continue continue
} }
itemTime := item.Time.Local() if !helper.isUTC {
if helper.isUTC { item.Time = item.Time.Local()
itemTime = item.Time
} }
t.Row( t.Row(
itemTime.Format("15:04"), item.Time.Format("15:04"),
fmt.Sprintf("%.1f °C", item.Temperature), fmt.Sprintf("%.1f °C", item.Temperature),
fmt.Sprintf("%.1f mm", item.Percipitation), fmt.Sprintf("%.1f mm", item.Percipitation),
fmt.Sprintf("%.1f m/s %s", item.Wind.Speed, item.Wind.DirectionToString()), fmt.Sprintf("%.1f m/s %s", item.Wind.Speed, item.Wind.DirectionToString()),

View file

@ -6,9 +6,7 @@ import (
"os" "os"
"time" "time"
"git.sr.ht/~timharek/yr/cmd/flags"
"git.sr.ht/~timharek/yr/cmd/internal/ui/table" "git.sr.ht/~timharek/yr/cmd/internal/ui/table"
"git.sr.ht/~timharek/yr/pkg/nominatim"
"git.sr.ht/~timharek/yr/yr" "git.sr.ht/~timharek/yr/yr"
"github.com/pkg/browser" "github.com/pkg/browser"
"github.com/spf13/cobra" "github.com/spf13/cobra"
@ -27,33 +25,10 @@ func init() {
} }
func tomorrow(cmd *cobra.Command, args []string) { func tomorrow(cmd *cobra.Command, args []string) {
isJson, err := cmd.Flags().GetBool(flags.JSON) helper := forecastHelper(cmd, args)
cobra.CheckErr(err) f := helper.f
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)
c, err := yr.New() if helper.isWeb {
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 {
url := fmt.Sprintf("https://www.yr.no/en/forecast/hourly-table/%.4f,%.4f?i=1", f.Coordinates.Latitude, f.Coordinates.Longitude) 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) err := browser.OpenURL(url)
if err != nil { if err != nil {
@ -62,30 +37,33 @@ func tomorrow(cmd *cobra.Command, args []string) {
os.Exit(0) os.Exit(0)
} }
if isJson {
j, err := json.MarshalIndent(f, "", " ")
cobra.CheckErr(err)
fmt.Printf("%s", j)
return
}
t := table.New() t := table.New()
tomorrows := []yr.Forecast{}
tomorrow := time.Now().AddDate(0, 0, 1) tomorrow := time.Now().AddDate(0, 0, 1)
for _, item := range f.Forecast { for _, item := range f.Forecast {
if item.Time.Format(time.DateOnly) != tomorrow.Format(time.DateOnly) { if item.Time.Format(time.DateOnly) != tomorrow.Format(time.DateOnly) {
continue continue
} }
itemTime := item.Time.Local() if !helper.isUTC {
if isUTC { item.Time = item.Time.Local()
itemTime = item.Time
} }
tomorrows = append(tomorrows, item)
t.Row( 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 °C", item.Temperature),
fmt.Sprintf("%.1f mm", item.Percipitation), fmt.Sprintf("%.1f mm", item.Percipitation),
fmt.Sprintf("%.1f m/s %s", item.Wind.Speed, item.Wind.DirectionToString()), 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) fmt.Println(t)
} }