refactor: Swap logic order for UTC time convertion
Signed-off-by: Tim Hårek Andreassen <tim@harek.no>
This commit is contained in:
parent
687d3e8be7
commit
bfbd031e41
5 changed files with 31 additions and 52 deletions
|
@ -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()),
|
||||
|
|
|
@ -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())
|
||||
|
|
|
@ -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,
|
||||
|
|
|
@ -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()),
|
||||
|
|
|
@ -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)
|
||||
}
|
||||
|
|
Loading…
Reference in a new issue