-
Notifications
You must be signed in to change notification settings - Fork 52
/
Copy pathclock.rs
27 lines (22 loc) · 915 Bytes
/
clock.rs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
//! This module provides a function to get the current Unix timestamp.
//! During testing, the timestamp can be overridden using the [`set_test_timestamp`] function.
//! When not testing, the timestamp is retrieved from the Solana runtime.
//! This makes it easy to unit test functions that depend on the current time
//! without having to instantiate a Solana runtime.
#[cfg(not(test))]
use anchor_lang::prelude::*;
use anchor_lang::solana_program::clock::UnixTimestamp;
#[cfg(test)]
static TEST_TIMESTAMP: std::sync::Mutex<i64> = std::sync::Mutex::new(0);
pub fn current_timestamp() -> UnixTimestamp {
#[cfg(not(test))]
return anchor_lang::solana_program::clock::Clock::get()
.unwrap()
.unix_timestamp;
#[cfg(test)]
return *TEST_TIMESTAMP.lock().unwrap();
}
#[cfg(test)]
pub fn set_test_timestamp(timestamp: UnixTimestamp) {
*TEST_TIMESTAMP.lock().unwrap() = timestamp;
}