hdbconnect_impl/types_impl/
longdate.rs

1use crate::{HdbResult, HdbValue, impl_err};
2use byteorder::{LittleEndian, ReadBytesExt};
3
4const NULL_REPRESENTATION: i64 = 3_155_380_704_000_000_001;
5const SECOND_FACTOR: i64 = 10_000_000;
6const MINUTE_FACTOR: i64 = 600_000_000; // 10_000_000 * 60;
7const HOUR_FACTOR: i64 = 36_000_000_000; // 10_000_000 * 60 * 60;
8const DAY_FACTOR: i64 = 864_000_000_000; // 10_000_000 * 60 * 60 * 24;
9
10const ZEITENWENDE: i64 = 1_721_424;
11const JGREG: i64 = 2_299_161;
12// const IGREG: i64 = 18_994;             // Julian day of 01.01.0001 n. Chr.
13
14/// Implementation of HANA's `LongDate`.
15///
16/// The type is used internally to implement deserialization from the wire.
17/// It is agnostic of timezones.
18#[derive(Clone, Debug, Serialize)]
19pub struct LongDate(i64);
20
21impl std::fmt::Display for LongDate {
22    // The format chosen supports the conversion to chrono types.
23    fn fmt(&self, fmt: &mut std::fmt::Formatter) -> std::fmt::Result {
24        let (year, month, day, hour, minute, second, fraction) = self.as_ymd_hms_f();
25        write!(
26            fmt,
27            "{year:04}-{month:02}-{day:02}T{hour:02}:{minute:02}:{second:02}.{fraction:07}",
28        )
29    }
30}
31
32impl std::cmp::PartialEq<LongDate> for LongDate {
33    fn eq(&self, other: &Self) -> bool {
34        self.0 == other.0
35    }
36}
37
38impl LongDate {
39    pub(crate) fn new(raw: i64) -> Self {
40        Self(raw)
41    }
42    pub(crate) fn ref_raw(&self) -> &i64 {
43        &self.0
44    }
45
46    // Convert into tuple of "elements".
47    #[allow(clippy::cast_possible_truncation)]
48    #[allow(clippy::cast_precision_loss)]
49    #[allow(clippy::cast_sign_loss)]
50    pub(crate) fn as_ymd_hms_f(&self) -> (i32, u8, u8, u8, u8, u8, u32) {
51        let value = match self.0 {
52            0 => 0, // maps the special value '' == 0 to '0001-01-01 00:00:00.000000000' = 1
53            v => v - 1,
54        };
55
56        let datevalue = value / DAY_FACTOR;
57        let mut timevalue = value - (datevalue * DAY_FACTOR);
58        let hour: u8 = (timevalue / HOUR_FACTOR) as u8;
59        timevalue -= HOUR_FACTOR * (i64::from(hour));
60        let minute: u8 = (timevalue / MINUTE_FACTOR) as u8;
61        timevalue -= MINUTE_FACTOR * (i64::from(minute));
62        let second: u8 = (timevalue / SECOND_FACTOR) as u8;
63        timevalue -= SECOND_FACTOR * (i64::from(second));
64        let fraction: u32 = timevalue as u32; // 10**-7
65
66        let julian: i64 = datevalue + ZEITENWENDE;
67        let ja: i64 = if julian >= JGREG {
68            let jalpha: i64 = (((julian - 1_867_216) as f64 - 0.25_f64) / 36_524.25_f64) as i64;
69            julian + 1 + jalpha - ((0.25_f64 * jalpha as f64) as i64)
70        } else {
71            julian
72        };
73
74        let jb: i64 = ja + 1524;
75        let jc: i64 = (6680_f64 + ((jb - 2_439_870) as f64 - 122.1_f64) / 365.25_f64) as i64;
76        let jd: i64 = ((365 * jc) as f64 + (0.25_f64 * jc as f64)) as i64;
77        let je: i64 = ((jb - jd) as f64 / 30.6001) as i64;
78
79        let day: u8 = (jb - jd - ((30.6001 * je as f64) as i64)) as u8;
80        let mut month: u8 = je as u8 - 1;
81        let mut year: i32 = jc as i32 - 4715;
82
83        if month > 12 {
84            month -= 12;
85        }
86        if month > 2 {
87            year -= 1;
88        }
89        if year <= 0 {
90            year -= 1;
91        }
92        (year, month, day, hour, minute, second, fraction)
93    }
94}
95
96pub(crate) fn parse_longdate(
97    nullable: bool,
98    rdr: &mut dyn std::io::Read,
99) -> HdbResult<HdbValue<'static>> {
100    let i = rdr.read_i64::<LittleEndian>()?;
101    if i == NULL_REPRESENTATION {
102        if nullable {
103            Ok(HdbValue::NULL)
104        } else {
105            Err(impl_err!("found NULL value for NOT NULL LONGDATE column",))
106        }
107    } else {
108        Ok(HdbValue::LONGDATE(LongDate::new(i)))
109    }
110}