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
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
use crate::kapi;
use std::convert::TryFrom;
use std::fmt;
use std::time::{Duration, SystemTime};

pub(crate) const K_NANO_OFFSET: i64 = 946_684_800_000_000_000;
pub(crate) const K_SEC_OFFSET: i64 = K_NANO_OFFSET / 1_000_000_000;
pub(crate) const K_DAY_OFFSET: i32 = (K_SEC_OFFSET / 86_400) as i32;

/// Represents the number of seconds since midnight (00:00)
#[repr(transparent)]
#[derive(Clone, Copy, PartialEq, Eq)]
pub struct Second(i32);

impl Second {
    /// Creates a new Second from the specified number.
    pub fn new(seconds_since_midnight: i32) -> Self {
        Second(seconds_since_midnight)
    }
}

impl From<i32> for Second {
    fn from(val: i32) -> Second {
        Second(val)
    }
}

impl From<Second> for i32 {
    fn from(val: Second) -> i32 {
        val.0
    }
}

impl fmt::Display for Second {
    fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
        write!(f, "{} seconds", self.0)
    }
}

impl fmt::Debug for Second {
    fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
        fmt::Display::fmt(self, f)
    }
}

/// Represents the number of minutes since midnight (00:00).
#[repr(transparent)]
#[derive(Clone, Copy, PartialEq, Eq)]
pub struct Minute(i32);

impl Minute {
    /// Creates a new Minute from the specified number.
    pub fn new(minutes_since_midnight: i32) -> Self {
        Minute(minutes_since_midnight)
    }
}

impl From<i32> for Minute {
    fn from(val: i32) -> Minute {
        Minute(val)
    }
}

impl From<Minute> for i32 {
    fn from(val: Minute) -> i32 {
        val.0
    }
}

impl fmt::Display for Minute {
    fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
        write!(f, "{} minutes", self.0)
    }
}

impl fmt::Debug for Minute {
    fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
        fmt::Display::fmt(self, f)
    }
}

/// Represents the number of days since 1 Jan 2000.
#[repr(transparent)]
#[derive(Clone, Copy, Debug, PartialEq, Eq)]
pub struct Date(i32);

impl Date {
    /// Creates a new date.
    pub fn new(year: i32, month: i32, day: i32) -> Self {
        Date(unsafe { kapi::ymd(year, month, day) })
    }

    /// Returns the date as the number of days since 1 Jan 2000.
    pub fn as_raw(&self) -> i32 {
        self.0
    }
}

impl From<i32> for Date {
    fn from(val: i32) -> Date {
        Date(val)
    }
}

impl From<Date> for i32 {
    fn from(val: Date) -> i32 {
        val.0
    }
}

impl From<SystemTime> for Date {
    fn from(st: SystemTime) -> Date {
        let dur = st.duration_since(SystemTime::UNIX_EPOCH).unwrap();
        let dur_secs = (dur.as_secs() / 86400) as i64;
        Date(dur_secs as i32 - K_DAY_OFFSET)
    }
}

impl From<Date> for SystemTime {
    fn from(date: Date) -> SystemTime {
        let secs = date.0 as i64 * 86400 + K_SEC_OFFSET;
        SystemTime::UNIX_EPOCH + Duration::from_secs(secs as u64)
    }
}

/// The number of months since January 2000.
#[repr(transparent)]
#[derive(Clone, Copy, PartialEq, Eq)]
pub struct Month(i32);

impl Month {
    /// Creates a new month from the specified number of months.
    pub fn new(months_since_millenium: i32) -> Self {
        Month(months_since_millenium)
    }
}

impl From<i32> for Month {
    fn from(val: i32) -> Month {
        Month(val)
    }
}

impl From<Month> for i32 {
    fn from(val: Month) -> i32 {
        val.0
    }
}

impl fmt::Display for Month {
    fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
        write!(f, "{} months", self.0)
    }
}

impl fmt::Debug for Month {
    fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
        fmt::Display::fmt(self, f)
    }
}

/// The number of milliseconds since midnight.
#[repr(transparent)]
#[derive(Clone, Copy, Debug, PartialEq, Eq)]
pub struct Time(i32);

impl Time {
    /// Creates a Time from the specified number of milliseconds.
    pub fn new(millis_since_midnight: i32) -> Self {
        Time(millis_since_midnight)
    }
}

impl From<i32> for Time {
    fn from(val: i32) -> Time {
        Time(val)
    }
}

impl From<Time> for i32 {
    fn from(val: Time) -> i32 {
        val.0
    }
}

/// Represents a date and time in KDB. Conversions between the
/// Unix Epoch and the KDB Epoch are done automatically.
///
/// Note that `Timestamp` is the preferred datatype for storing
/// high precision temporal data.
#[repr(transparent)]
#[derive(Clone, Copy, Debug, PartialEq)]
pub struct DateTime(f64);

impl DateTime {
    /// Creates a DateTime with the supplied value.
    pub fn new(dt: f64) -> Self {
        DateTime(dt)
    }
}

impl From<f64> for DateTime {
    fn from(val: f64) -> DateTime {
        DateTime(val)
    }
}

impl From<DateTime> for f64 {
    fn from(val: DateTime) -> f64 {
        val.0
    }
}

/// Represents a timestamp in KDB. Conversions between the
/// Unix Epoch and the KDB Epoch are done automatically.
#[repr(transparent)]
#[derive(Clone, Copy, Debug, PartialEq, Eq)]
pub struct Timestamp(i64);

impl Timestamp {
    /// Creates a timestamp from a count of nanoseconds in the unix epoch.
    pub fn from_nanos_unix(n: u64) -> Timestamp {
        Timestamp(n as i64 - K_NANO_OFFSET)
    }

    /// Converts the timestamp to the number of nanoseconds from the unix epoch and returns it.
    pub fn as_nanos_unix(&self) -> u64 {
        (self.0 + K_NANO_OFFSET) as u64
    }

    /// Returns the raw timestamp stored as KDB values.
    pub fn as_raw(&self) -> i64 {
        self.0
    }

    /// Creates a timestamp based on a count of nanoseconds since 1 Jan 2000.
    pub fn from_raw(nanos_since_millenium: i64) -> Timestamp {
        Timestamp(nanos_since_millenium)
    }
}

impl From<Timestamp> for SystemTime {
    fn from(date: Timestamp) -> SystemTime {
        SystemTime::UNIX_EPOCH + Duration::from_nanos(date.as_nanos_unix())
    }
}

impl From<SystemTime> for Timestamp {
    fn from(st: SystemTime) -> Timestamp {
        let dur = st.duration_since(SystemTime::UNIX_EPOCH).unwrap();
        Timestamp::from_nanos_unix(dur.as_nanos() as u64)
    }
}

impl From<i64> for Timestamp {
    fn from(val: i64) -> Timestamp {
        Timestamp(val)
    }
}

impl From<Timestamp> for i64 {
    fn from(val: Timestamp) -> i64 {
        val.0
    }
}

/// Represents an elapsed span of time
#[repr(transparent)]
#[derive(Clone, Copy, Debug, PartialEq, Eq)]
pub struct Timespan(i64);

impl Timespan {
    /// Creates a Timespan from the specified number of nanoseconds.
    #[inline]
    pub fn from_nanos(nanos: i64) -> Self {
        Timespan(nanos)
    }

    /// Creates a Timespan from the specified number of microseconds.
    #[inline]
    pub fn from_micros(micros: i64) -> Self {
        Timespan(1_000 * micros)
    }

    /// Creates a Timespan from the specified number of milliseconds.
    #[inline]
    pub fn from_millis(millis: i64) -> Self {
        Timespan(1_000_000 * millis)
    }

    /// Creates a Timespan from the specified number of seconds.
    #[inline]
    pub fn from_secs(millis: i64) -> Self {
        Timespan(1_000_000_000 * millis)
    }
}

impl From<i64> for Timespan {
    fn from(val: i64) -> Timespan {
        Timespan(val)
    }
}

impl From<Timespan> for i64 {
    fn from(val: Timespan) -> i64 {
        val.0
    }
}

impl From<Timespan> for Duration {
    fn from(span: Timespan) -> Duration {
        Duration::from_nanos(span.0 as u64)
    }
}

impl TryFrom<Duration> for Timespan {
    type Error = crate::error::ConversionError;

    fn try_from(d: Duration) -> Result<Timespan, Self::Error> {
        let d = d.as_nanos();
        if d > std::i64::MAX as u128 {
            Err(crate::error::ConversionError::DurationTooLong)
        } else {
            Ok(Timespan(d as i64))
        }
    }
}