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

fix: resolved #175 to use hk as default timezone #176

Merged
merged 1 commit into from
Aug 17, 2023
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
2 changes: 1 addition & 1 deletion cli/Cargo.toml
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
[package]
name = "zhang"
version = "0.1.0-alpha.4"
version = "0.1.0-alpha.5"
authors = ["Kilerd <[email protected]>"]
description = "a plain text double-accounting tool which is compatible with beancount but more powerful"
edition = "2018"
Expand Down
1 change: 1 addition & 0 deletions core/src/constants.rs
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ pub const DEFAULT_COMMODITY_PRECISION: i32 = 2;
pub const DEFAULT_OPERATING_CURRENCY: &str = "CNY";
pub const DEFAULT_ROUNDING: Rounding = Rounding::RoundDown;
pub const DEFAULT_BALANCE_TOLERANCE_PRECISION: i32 = 2;
pub const DEFAULT_TIMEZONE: &str = "Asia/Hong_Kong";

pub const DEFAULT_ROUNDING_PLAIN: &str = "RoundDown";
pub const DEFAULT_COMMODITY_PRECISION_PLAIN: &str = "2";
Expand Down
19 changes: 14 additions & 5 deletions core/src/options.rs
Original file line number Diff line number Diff line change
@@ -1,12 +1,14 @@
use itertools::Itertools;
use log::{error, info};
use log::{error, info, warn};
use sqlx::SqliteConnection;
use std::str::FromStr;
use std::string::ToString;
use strum::{AsRefStr, EnumIter, EnumString, IntoEnumIterator};
use zhang_ast::{Directive, Options, Rounding, SpanInfo, Spanned, ZhangString};

use crate::constants::{DEFAULT_BALANCE_TOLERANCE_PRECISION_PLAIN, DEFAULT_COMMODITY_PRECISION_PLAIN, DEFAULT_OPERATING_CURRENCY, DEFAULT_ROUNDING_PLAIN};
use crate::constants::{
DEFAULT_BALANCE_TOLERANCE_PRECISION_PLAIN, DEFAULT_COMMODITY_PRECISION_PLAIN, DEFAULT_OPERATING_CURRENCY, DEFAULT_ROUNDING_PLAIN, DEFAULT_TIMEZONE,
};
use crate::ZhangResult;
use chrono_tz::Tz;

Expand Down Expand Up @@ -36,9 +38,16 @@ impl BuiltinOption {
BuiltinOption::DefaultBalanceTolerancePrecision => DEFAULT_BALANCE_TOLERANCE_PRECISION_PLAIN.to_owned(),
BuiltinOption::DefaultCommodityPrecision => DEFAULT_COMMODITY_PRECISION_PLAIN.to_owned(),
BuiltinOption::Timezone => {
let system_timezone = iana_time_zone::get_timezone().expect("cannot get the system timezone");
info!("detect system timezone is {}", system_timezone);
system_timezone
match iana_time_zone::get_timezone() {
Ok(timezone) => {
info!("detect system timezone is {}", timezone);
timezone
}
Err(e) => {
warn!("cannot get timezone, fall back to use GMT+8 as default timezone: {}", e);
DEFAULT_TIMEZONE.to_owned()
}
}
}
}
}
Expand Down
14 changes: 14 additions & 0 deletions core/src/utils/logging.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
use log::warn;

pub trait LoggingExit {
fn warn_if_none(self, msg: impl AsRef<str>) -> Self;
}

impl<T> LoggingExit for Option<T> {
fn warn_if_none(self, msg: impl AsRef<str>) -> Self {
if self.is_none() {
warn!("{}", msg.as_ref())
}
self
}
}
1 change: 1 addition & 0 deletions core/src/utils/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ pub mod hashmap;
pub mod id;
pub mod price_grip;
pub mod string_;
pub mod logging;

pub fn has_path_visited<'a>(visited: impl IntoIterator<Item = &'a Pattern>, path: &Path) -> bool {
visited.into_iter().any(|pattern| pattern.matches_path(path))
Expand Down
Loading