commit 410312e46a93b280f76c4c41287f4e94b4f7ca46
parent 90d4608cf2f4a73cf13cb252e8892a27fa090e62
Author: Antoine A <>
Date: Tue, 10 Dec 2024 16:38:52 +0100
utils: improve timestamp logic
Diffstat:
1 file changed, 14 insertions(+), 1 deletion(-)
diff --git a/taler-common/src/api_common.rs b/taler-common/src/api_common.rs
@@ -54,10 +54,22 @@ struct TimestampImpl {
}
impl Timestamp {
+ /** Timestamp corresponding to "now" */
pub fn now() -> Self {
Self::Time(SystemTime::now())
}
+ /** Timestamp corresponding to now as it would be stored in db */
+ pub fn now_stable() -> Self {
+ Self::from_sql_micros(Self::now().as_sql_micros())
+ .expect("timestamp sql roundtrip must always succedd")
+ }
+ /** Timestamp corresponding to "never" */
+ pub const fn never() -> Self {
+ Self::Never
+ }
+
+ /** I64 equivalent of this timestamp for db storage */
pub fn as_sql_micros(&self) -> i64 {
match self {
Timestamp::Never => i64::MAX,
@@ -66,10 +78,11 @@ impl Timestamp {
.unwrap()
.as_micros()
.try_into()
- .expect("expect timestamp to be a valid i64 number"),
+ .expect("timestamp must be a valid i64"),
}
}
+ /** Timestamp equivalent of as i64 as stored in db */
pub fn from_sql_micros(micros: i64) -> Result<Self, String> {
if micros == i64::MAX {
Ok(Self::Never)