sea_orm/value/
with_time.rs

1use super::impl_timestamp;
2use crate as sea_orm;
3use crate::{DbErr, TryGetError, prelude::TimeDateTimeWithTimeZone};
4use std::ops::{Deref, DerefMut};
5
6/// A OffsetDateTime mapped to i64 in database
7#[derive(derive_more::Debug, Copy, Clone, PartialEq, Eq, Hash)]
8#[debug("{_0:?}")]
9pub struct TimeUnixTimestamp(pub TimeDateTimeWithTimeZone);
10
11/// A OffsetDateTime mapped to i64 in database, but in milliseconds
12#[derive(derive_more::Debug, Copy, Clone, PartialEq, Eq, Hash)]
13#[debug("{_0:?}")]
14pub struct TimeUnixTimestampMillis(pub TimeDateTimeWithTimeZone);
15
16impl_timestamp!(
17    TimeUnixTimestamp,
18    TimeDateTimeWithTimeZone,
19    from_timestamp,
20    to_timestamp
21);
22
23impl_timestamp!(
24    TimeUnixTimestampMillis,
25    TimeDateTimeWithTimeZone,
26    from_timestamp_millis,
27    to_timestamp_millis
28);
29
30fn from_timestamp(ts: i64) -> Option<TimeUnixTimestamp> {
31    TimeDateTimeWithTimeZone::from_unix_timestamp(ts)
32        .ok()
33        .map(TimeUnixTimestamp)
34}
35
36fn to_timestamp(ts: TimeUnixTimestamp) -> i64 {
37    ts.0.unix_timestamp()
38}
39
40fn from_timestamp_millis(ts: i64) -> Option<TimeUnixTimestampMillis> {
41    TimeDateTimeWithTimeZone::from_unix_timestamp_nanos(ts as i128 * 1_000_000)
42        .ok()
43        .map(TimeUnixTimestampMillis)
44}
45
46fn to_timestamp_millis(ts: TimeUnixTimestampMillis) -> i64 {
47    (ts.0.unix_timestamp_nanos() / 1_000_000) as i64
48}