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?) } }