Skip to content

Commit

Permalink
fix: removed chrono, added time
Browse files Browse the repository at this point in the history
  • Loading branch information
veeso committed Dec 18, 2023
1 parent df27ed6 commit edbc820
Show file tree
Hide file tree
Showing 5 changed files with 45 additions and 152 deletions.
171 changes: 34 additions & 137 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,6 @@ version = "0.1.0"
async-trait = "0.1"
bytes = "1.5"
candid = "0.9"
chrono = "0.4"
ic-cdk = "0.11"
ic-cdk-macros = "0.8"
ic-cdk-timers = "0.5"
Expand All @@ -33,6 +32,7 @@ num-bigint = "0.4"
num-traits = "0.2"
serde = { version = "1", features = ["derive"] }
thiserror = "1.0"
time = "0.3.28"

[profile.dev]
debug = false
Expand Down
2 changes: 1 addition & 1 deletion src/fly/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,6 @@ did = []
[dependencies]
bytes = { workspace = true }
candid = { workspace = true }
chrono = { workspace = true }
did = { path = "../did" }
ic-cdk = { workspace = true }
ic-cdk-macros = { workspace = true }
Expand All @@ -31,6 +30,7 @@ num-bigint = { workspace = true }
num-traits = { workspace = true }
serde = { workspace = true }
thiserror = { workspace = true }
time = { workspace = true }
xrc = { path = "../xrc" }

[dev-dependencies]
Expand Down
11 changes: 5 additions & 6 deletions src/fly/src/app/reward.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@
use std::cell::RefCell;

use chrono::Datelike;
use did::fly::{FlyError, FlyResult, PicoFly, PoolError};
use did::ID;
use ic_stable_structures::memory_manager::VirtualMemory;
Expand Down Expand Up @@ -54,9 +53,9 @@ thread_local! {
).unwrap()
);

static LAST_MONTH: RefCell<StableCell<u32, VirtualMemory<DefaultMemoryImpl>>> =
static LAST_MONTH: RefCell<StableCell<u8, VirtualMemory<DefaultMemoryImpl>>> =
RefCell::new(StableCell::new(MEMORY_MANAGER.with(|mm| mm.get(LAST_MONTH_MEMORY_ID)),
crate::utils::date().month()
crate::utils::date().month() as u8
).unwrap()
);

Expand Down Expand Up @@ -152,7 +151,7 @@ impl Reward {
/// Check if current month is different from the last month.
fn should_adjust_avidity() -> bool {
let last_month = LAST_MONTH.with_borrow(|last_month| *last_month.get());
let current_month = crate::utils::date().month();
let current_month = crate::utils::date().month() as u8;
// check time
current_month != last_month
}
Expand Down Expand Up @@ -187,7 +186,7 @@ impl Reward {
});
// update last month
LAST_MONTH.with_borrow_mut(|last_month| {
last_month.set(crate::utils::date().month()).unwrap();
last_month.set(crate::utils::date().month() as u8).unwrap();
});
}
}
Expand Down Expand Up @@ -267,7 +266,7 @@ mod test {

#[test]
fn test_should_tell_whether_to_adjust_avidity() {
let month = crate::utils::date().month();
let month = crate::utils::date().month() as u8;
LAST_MONTH.with_borrow_mut(|last_month| {
last_month.set(month - 1).unwrap();
});
Expand Down
11 changes: 4 additions & 7 deletions src/fly/src/utils.rs
Original file line number Diff line number Diff line change
@@ -1,8 +1,6 @@
use std::time::{Duration, UNIX_EPOCH};

use candid::{Nat, Principal};
use chrono::{DateTime, NaiveDate, Utc};
use icrc::icrc1::account::Subaccount;
use time::{Date, OffsetDateTime};

/// Returns current time in nanoseconds
pub fn time() -> u64 {
Expand All @@ -20,12 +18,11 @@ pub fn time() -> u64 {
}

/// Returns current date
pub fn date() -> NaiveDate {
pub fn date() -> Date {
let time = time();

let duration = Duration::from_nanos(time);
let datetime = UNIX_EPOCH + duration;
DateTime::<Utc>::from(datetime).date_naive()
let date = OffsetDateTime::from_unix_timestamp_nanos(time as i128).unwrap();
date.date()
}

/// Returns canister id
Expand Down

0 comments on commit edbc820

Please sign in to comment.