fix: correct timezone offset for parsed dates, add timezone_local option

This commit is contained in:
ThetaDev 2025-01-25 01:13:38 +01:00
parent 3a2370b97c
commit a5a7be5b4e
No known key found for this signature in database
GPG key ID: E319D3C5148D65B6
15 changed files with 202 additions and 98 deletions

View file

@ -897,8 +897,6 @@ impl RustyPipeBuilder {
/// Set the timezone and its associated UTC offset in minutes used
/// when accessing the YouTube API.
///
/// This will also change the UTC offset of the returned dates.
///
/// **Default value**: `0` (UTC)
///
/// **Info**: you can set this option for individual queries, too
@ -909,6 +907,16 @@ impl RustyPipeBuilder {
self
}
/// Access the YouTube API using the local system timezone
///
/// If the local timezone could not be determined, an error is logged and RustyPipe falls
/// back to UTC.
#[must_use]
pub fn timezone_local(self) -> Self {
let (timezone, utc_offset_minutes) = local_tz_offset();
self.timezone(timezone, utc_offset_minutes)
}
/// Generate a report on every operation.
///
/// This should only be used for debugging.
@ -1689,8 +1697,6 @@ impl RustyPipeQuery {
/// Set the timezone and its associated UTC offset in minutes used
/// when accessing the YouTube API.
///
/// This will also change the UTC offset of the returned dates.
#[must_use]
pub fn timezone<S: Into<String>>(mut self, timezone: S, utc_offset_minutes: i16) -> Self {
self.opts.timezone = Some(timezone.into());
@ -1698,6 +1704,13 @@ impl RustyPipeQuery {
self
}
/// Access the YouTube API using the local system timezone
#[must_use]
pub fn timezone_local(self) -> Self {
let (timezone, utc_offset_minutes) = local_tz_offset();
self.timezone(timezone, utc_offset_minutes)
}
/// Generate a report on every operation.
///
/// This should only be used for debugging.
@ -2611,6 +2624,19 @@ fn validate_country(country: Country) -> Country {
}
}
fn local_tz_offset() -> (String, i16) {
match (
util::local_timezone_name(),
UtcOffset::current_local_offset().map_err(|_| Error::Other("indeterminate offset".into())),
) {
(Ok(timezone), Ok(offset)) => (timezone, offset.whole_minutes()),
(Err(e), _) | (_, Err(e)) => {
tracing::error!("{e}");
("UTC".to_owned(), 0)
}
}
}
#[cfg(test)]
mod tests {
use super::*;