Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Make local timezone support optional #460

Merged
merged 1 commit into from
Jun 11, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 3 additions & 2 deletions numbat/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ num-format = "0.4.4"
walkdir = "2"
chrono = "0.4.31"
chrono-tz = "0.8.5"
iana-time-zone = "0.1"
iana-time-zone = { version = "0.1", optional = true }
termcolor = { version = "1.4.1", optional = true }
html-escape = { version = "0.2.13", optional = true }
rand = "0.8.5"
Expand All @@ -40,9 +40,10 @@ indexmap = "2.2.6"
mendeleev = "0.8.0"

[features]
default = ["fetch-exchangerates"]
default = ["fetch-exchangerates", "local-timezone"]
fetch-exchangerates = ["numbat-exchange-rates/fetch-exchangerates"]
html-formatter = ["termcolor", "html-escape"]
local-timezone = ["iana-time-zone"]

[dev-dependencies]
approx = "0.5"
Expand Down
7 changes: 7 additions & 0 deletions numbat/src/datetime.rs
Original file line number Diff line number Diff line change
@@ -1,15 +1,22 @@
use chrono::{DateTime, Datelike, FixedOffset, LocalResult};
use chrono_tz::Tz;

#[cfg(feature = "local-timezone")]
pub fn get_local_timezone() -> Option<Tz> {
let tz_str = iana_time_zone::get_timezone().ok()?;
tz_str.parse().ok()
}

#[cfg(feature = "local-timezone")]
pub fn get_local_timezone_or_utc() -> Tz {
get_local_timezone().unwrap_or(chrono_tz::UTC)
}

#[cfg(not(feature = "local-timezone"))]
pub fn get_local_timezone_or_utc() -> Tz {
chrono_tz::UTC
}

pub fn parse_datetime(input: &str) -> Option<DateTime<FixedOffset>> {
if let Ok(dt) = chrono::DateTime::parse_from_rfc3339(input) {
Some(dt)
Expand Down
Loading