gmsol_store/states/market/
clock.rs1use anchor_lang::{
2 error::Error,
3 solana_program::{clock::Clock, sysvar::Sysvar},
4};
5
6pub struct AsClockMut<'a> {
8 last: &'a mut i64,
9}
10
11impl AsClockMut<'_> {
12 pub fn just_passed_in_seconds(&mut self) -> gmsol_model::Result<u64> {
14 let current = Clock::get().map_err(Error::from)?.unix_timestamp;
15 let duration = current.saturating_sub(*self.last);
16 if duration > 0 {
17 *self.last = current;
18 Ok(duration as u64)
19 } else {
20 Ok(0)
21 }
22 }
23}
24
25impl<'a> From<&'a mut i64> for AsClockMut<'a> {
26 fn from(last: &'a mut i64) -> Self {
27 Self { last }
28 }
29}
30
31pub struct AsClock<'a> {
33 last: &'a i64,
34}
35
36impl AsClock<'_> {
37 pub fn passed_in_seconds(&mut self) -> gmsol_model::Result<u64> {
39 let current = Clock::get().map_err(Error::from)?.unix_timestamp;
40 let duration = current.saturating_sub(*self.last);
41 if duration > 0 {
42 Ok(duration as u64)
43 } else {
44 Ok(0)
45 }
46 }
47}
48
49impl<'a> From<&'a i64> for AsClock<'a> {
50 fn from(last: &'a i64) -> Self {
51 Self { last }
52 }
53}