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
 28
 29
 30
 31
 32
 33
 34
 35
 36
 37
 38
 39
 40
 41
 42
 43
 44
 45
 46
 47
 48
 49
 50
 51
 52
 53
 54
 55
 56
 57
 58
 59
 60
 61
 62
 63
 64
 65
 66
 67
 68
 69
 70
 71
 72
 73
 74
 75
 76
 77
 78
 79
 80
 81
 82
 83
 84
 85
 86
 87
 88
 89
 90
 91
 92
 93
 94
 95
 96
 97
 98
 99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
use std::cmp::Ordering;
use std::convert::TryInto;
use std::fmt::{Display, Formatter};
use std::time::{Duration, SystemTime, UNIX_EPOCH};

use crate::compressor::Compressor;
use crate::decompressor::Decompressor;
use crate::errors::{QCompressError, QCompressResult};
use crate::types::NumberLike;

const BILLION_U32: u32 = 1_000_000_000;

// an instant - does not store time zone
// always relative to Unix Epoch
#[derive(Copy, Clone, Debug, Default, Eq, PartialEq)]
pub struct TimestampNs(i128);

#[derive(Copy, Clone, Debug, Default, Eq, PartialEq)]
pub struct TimestampMicros(i128);

macro_rules! impl_timestamp {
  ($t: ty, $parts_per_sec: expr, $header_byte: expr) => {
    impl $t {
      const MAX: i128 = $parts_per_sec as i128 * (i64::MAX as i128 + 1) - 1;
      const MIN: i128 = $parts_per_sec as i128 * (i64::MIN as i128);
      const NS_PER_PART: u32 = 1_000_000_000 / $parts_per_sec;

      pub fn new(parts: i128) -> QCompressResult<Self> {
        if parts > Self::MAX || parts < Self::MIN {
          Err(QCompressError::invalid_argument(format!(
            "invalid timestamp with {}/{} of a second",
            parts,
            $parts_per_sec,
          )))
        } else {
          Ok(Self(parts))
        }
      }

      pub fn from_secs_and_nanos(seconds: i64, subsec_nanos: u32) -> Self {
        Self(seconds as i128 * $parts_per_sec as i128 + (subsec_nanos / Self::NS_PER_PART) as i128)
      }

      pub fn to_secs_and_nanos(self) -> (i64, u32) {
        let parts = self.0;
        let seconds = parts.div_euclid($parts_per_sec as i128) as i64;
        let subsec_nanos = parts.rem_euclid($parts_per_sec as i128) as u32 * Self::NS_PER_PART;
        (seconds, subsec_nanos)
      }

      pub fn to_total_parts(self) -> i128 {
        self.0
      }

      pub fn from_bytes_safe(bytes: &[u8]) -> QCompressResult<$t> {
        let mut full_bytes = vec![0; 4];
        full_bytes.extend(bytes);
        let parts = (u128::from_be_bytes(full_bytes.try_into().unwrap()) as i128) + Self::MIN;
        Self::new(parts)
      }
    }

    impl From<SystemTime> for $t {
      fn from(system_time: SystemTime) -> Self {
        let (seconds, subsec_nanos) = if system_time.lt(&UNIX_EPOCH) {
          let dur = UNIX_EPOCH.duration_since(system_time)
            .expect("time difference error (pre-epoch)");
          (dur.as_secs() as i64, dur.subsec_nanos())
        } else {
          let dur = system_time.duration_since(UNIX_EPOCH)
            .expect("time difference error");
          let complement_nanos = dur.subsec_nanos();
          let ceil_secs = -(dur.as_secs() as i64);
          if complement_nanos == 0 {
            (ceil_secs, 0)
          } else {
            (ceil_secs - 1, BILLION_U32 - complement_nanos)
          }
        };

        Self::from_secs_and_nanos(seconds, subsec_nanos)
      }
    }

    impl From<$t> for SystemTime {
      fn from(value: $t) -> SystemTime {
        let (seconds, subsec_nanos) = value.to_secs_and_nanos();
        if seconds >= 0 {
          let dur = Duration::new(seconds as u64, subsec_nanos);
          UNIX_EPOCH + dur
        } else {
          let dur = if subsec_nanos == 0 {
            Duration::new((-seconds) as u64, 0)
          } else {
            Duration::new((-seconds - 1) as u64, BILLION_U32 - subsec_nanos)
          };
          UNIX_EPOCH - dur
        }
      }
    }

    impl Display for $t {
      fn fmt(&self, f: &mut Formatter<'_>) -> std::fmt::Result {
        write!(
          f,
          "Timestamp({}/{})",
          self.0,
          $parts_per_sec,
        )
      }
    }

    impl NumberLike for $t {
      const HEADER_BYTE: u8 = $header_byte;
      const PHYSICAL_BITS: usize = 96;

      type Unsigned = u128;

      fn to_unsigned(self) -> u128 {
        self.0.wrapping_sub(i128::MIN) as u128
      }

      fn from_unsigned(off: u128) -> Self {
        Self(i128::MIN.wrapping_add(off as i128))
      }

      fn num_eq(&self, other: &Self) -> bool {
        self.0.eq(&other.0)
      }

      fn num_cmp(&self, other: &Self) -> Ordering {
        self.0.cmp(&other.0)
      }

      fn bytes_from(value: Self) -> Vec<u8> {
        ((value.0 - Self::MIN) as u128).to_be_bytes()[4..].to_vec()
      }

      fn from_bytes(bytes: Vec<u8>) -> Self {
        Self::from_bytes_safe(&bytes).expect("corrupt timestamp bytes")
      }
    }
  }
}

impl_timestamp!(TimestampNs, 1_000_000_000_u32, 8);
impl_timestamp!(TimestampMicros, 1_000_000_u32, 9);

pub type TimestampNsCompressor = Compressor<TimestampNs>;
pub type TimestampNsDecompressor = Decompressor<TimestampNs>;
pub type TimestampMicrosCompressor = Compressor<TimestampMicros>;
pub type TimestampMicrosDecompressor = Decompressor<TimestampMicros>;