Skip to content

Commit

Permalink
🐛 extend usage of current_weather.time for more accurate local time…
Browse files Browse the repository at this point in the history
… of requested location and better testability

related to: f5f71bc
  • Loading branch information
ttytm committed Oct 14, 2024
1 parent d28fdc8 commit fd2e3c6
Show file tree
Hide file tree
Showing 3 changed files with 9 additions and 11 deletions.
7 changes: 4 additions & 3 deletions src/modules/display/day.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
use anyhow::Result;
use chrono::{Duration, Local};
use chrono::{Duration, NaiveDate};
use dialoguer::console::style;
use unicode_width::UnicodeWidthStr;

Expand Down Expand Up @@ -50,14 +50,15 @@ pub fn prep(product: &Product, params: &Params, day_index: usize) -> Result<Vec<
);
let precipitation_probability_max = format!("❲{}󰖎❳", weather.daily.precipitation_probability_max[day_index]);

let dt = Local::now() + Duration::days(day_index.try_into()?);
let dt = NaiveDate::parse_from_str(&product.weather.current_weather.time, "%Y-%m-%dT%H:%M")?
+ Duration::days(day_index.try_into()?);
let lang = &params.config.language;
let date = format!(
" {}",
if lang == "en_US" || lang == "en" {
dt.format("%a, %e %b").to_string()
} else {
Locales::localize_date(dt.date_naive(), lang)?
Locales::localize_date(dt, lang)?
}
);
let sunrise = format!(" {sunrise}");
Expand Down
7 changes: 4 additions & 3 deletions src/modules/display/product.rs
Original file line number Diff line number Diff line change
@@ -1,10 +1,10 @@
use anyhow::Result;
use chrono::NaiveDate;
use chrono::{Datelike, NaiveDate};
use scopeguard::defer;
use std::collections::HashMap;

use crate::modules::{
forecast::get_forecast_indices,
forecast,
params::Params,
weather::{OptionalWeather, Weather},
};
Expand Down Expand Up @@ -44,7 +44,8 @@ impl Product<'_> {
return Ok(());
}

let forecast_indices = get_forecast_indices(&params.config.forecast);
let current_date = NaiveDate::parse_from_str(&self.weather.current_weather.time, "%Y-%m-%dT%H:%M")?;
let forecast_indices = forecast::get_indices(&params.config.forecast, current_date.weekday());

if forecast_indices.contains(&0) && forecast_indices.contains(&7) {
// Current day with hours & weekly overview
Expand Down
6 changes: 1 addition & 5 deletions src/modules/forecast.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,11 +4,7 @@ use std::collections::HashSet;

use super::args::Forecast;

pub fn get_forecast_indices(forecast: &HashSet<Forecast>) -> Vec<usize> {
get_indices(forecast, Local::now().weekday())
}

fn get_indices(forecast: &HashSet<Forecast>, curr_day: Weekday) -> Vec<usize> {
pub fn get_indices(forecast: &HashSet<Forecast>, curr_day: Weekday) -> Vec<usize> {
let dist_from_ref_day = curr_day.number_from_monday();

// Indices for forecasts that should be rendered. 7 will be used as a special value
Expand Down

0 comments on commit fd2e3c6

Please sign in to comment.