diff --git a/src/free_api_v25/current/mod.rs b/src/free_api_v25/current/mod.rs index d37d286..8b80684 100644 --- a/src/free_api_v25/current/mod.rs +++ b/src/free_api_v25/current/mod.rs @@ -21,5 +21,6 @@ pub mod clouds; pub mod coord; pub mod main; pub mod precipitation; +pub mod sys; pub mod weather; pub mod wind; diff --git a/src/free_api_v25/current/sys.rs b/src/free_api_v25/current/sys.rs new file mode 100644 index 0000000..ddf8bb2 --- /dev/null +++ b/src/free_api_v25/current/sys.rs @@ -0,0 +1,54 @@ +use chrono::{DateTime, Utc}; +use serde::{Deserialize, Serialize}; + +/// System and location information +/// +/// Contains country, sunrise/sunset times, and internal API parameters. +#[derive(Debug, Clone, Deserialize, Serialize, Default, PartialEq)] +pub struct Sys { + /// Internal parameter + #[serde(default)] + pub r#type: Option, + + /// Internal parameter + #[serde(default)] + pub id: Option, + + /// Country code (ISO 3166-1 alpha-2, e.g. "GB", "JP") + #[serde(default)] + pub country: Option, + + /// Sunrise time (UTC Unix timestamp) + #[serde(with = "chrono::serde::ts_seconds_option")] + pub sunrise: Option>, + + /// Sunset time (UTC Unix timestamp) + #[serde(with = "chrono::serde::ts_seconds_option")] + pub sunset: Option>, + + /// Internal parameter (often used for error messages) + #[serde(default)] + pub message: Option, +} + +impl Sys { + /// Returns true if it's currently daytime at the location + /// + /// Compares current UTC time with sunrise and sunset times. + #[must_use] + pub fn is_daytime(&self) -> Option { + let now = Utc::now(); + match (self.sunrise, self.sunset) { + (Some(sunrise), Some(sunset)) => Some(now >= sunrise && now <= sunset), + _ => None, + } + } + + /// Returns the duration of daylight (sunset - sunrise) + /// + /// Returns `None` if either sunrise or sunset data is missing. + #[must_use] + pub fn daylight_duration(&self) -> Option { + Some(self.sunset? - self.sunrise?) + } +}