djs-model 0.1.0-alpha.1

Discord API v10 types — users, guilds, channels, messages, interactions and gateway events.
Documentation
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
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
//! ISO 8601 timestamps as used by the Discord API.

use core::{fmt, str::FromStr};

use serde::{
    Deserialize, Deserializer, Serialize, Serializer,
    de::{Error as DeError, Visitor},
};

/// A point in time, as sent by Discord in ISO 8601 format.
///
/// Discord timestamps look like `2026-11-10T19:42:07.123000+00:00`. They are stored here as a
/// single `i64` of **microseconds since the Unix epoch** (8 bytes, `Copy`), which is the
/// precision Discord uses. Parsing and formatting are implemented without any date/time
/// dependency.
///
/// Timestamps are ordered chronologically.
///
/// discord.js counterpart: none (discord.js turns timestamps into `Date`s).
///
/// # Examples
///
/// ```
/// use djs_model::util::Timestamp;
///
/// let ts: Timestamp = "2021-01-01T12:30:00.5+00:00".parse().unwrap();
/// assert_eq!(ts.as_unix_millis(), 1_609_504_200_500);
/// assert_eq!(ts.to_string(), "2021-01-01T12:30:00.500000+00:00");
///
/// // Offsets are normalised to UTC.
/// let same: Timestamp = "2021-01-01T14:30:00.500+02:00".parse().unwrap();
/// assert_eq!(ts, same);
/// ```
#[derive(Clone, Copy, Debug, PartialEq, Eq, PartialOrd, Ord, Hash)]
pub struct Timestamp {
    unix_micros: i64,
}

const MICROS_PER_SECOND: i64 = 1_000_000;
const SECONDS_PER_DAY: i64 = 86_400;

impl Timestamp {
    /// Creates a timestamp from microseconds since the Unix epoch.
    ///
    /// discord.js counterpart: `new Date(ms)`.
    #[must_use]
    pub const fn from_unix_micros(unix_micros: i64) -> Self {
        Self { unix_micros }
    }

    /// Creates a timestamp from milliseconds since the Unix epoch.
    ///
    /// Returns `None` if the value overflows the microsecond representation.
    ///
    /// discord.js counterpart: `new Date(ms)`.
    #[must_use]
    pub const fn from_unix_millis(unix_millis: i64) -> Option<Self> {
        match unix_millis.checked_mul(1_000) {
            Some(unix_micros) => Some(Self { unix_micros }),
            None => None,
        }
    }

    /// Creates a timestamp from seconds since the Unix epoch.
    ///
    /// Returns `None` if the value overflows the microsecond representation.
    ///
    /// discord.js counterpart: `new Date(seconds * 1000)`.
    #[must_use]
    pub const fn from_unix_secs(unix_secs: i64) -> Option<Self> {
        match unix_secs.checked_mul(MICROS_PER_SECOND) {
            Some(unix_micros) => Some(Self { unix_micros }),
            None => None,
        }
    }

    /// Returns the current system time.
    ///
    /// discord.js counterpart: `Date.now()`.
    ///
    /// # Panics
    ///
    /// Panics if the system clock is set before 1970 or after the year 294 247.
    #[must_use]
    pub fn now() -> Self {
        let since_epoch = std::time::SystemTime::now()
            .duration_since(std::time::UNIX_EPOCH)
            .expect("system clock is set before the Unix epoch");
        Self {
            unix_micros: i64::try_from(since_epoch.as_micros())
                .expect("system clock is too far in the future"),
        }
    }

    /// Returns the number of microseconds since the Unix epoch.
    ///
    /// discord.js counterpart: `Date#getTime`.
    #[must_use]
    pub const fn as_unix_micros(self) -> i64 {
        self.unix_micros
    }

    /// Returns the number of whole milliseconds since the Unix epoch (rounded towards negative
    /// infinity).
    ///
    /// discord.js counterpart: `Date#getTime`.
    #[must_use]
    pub const fn as_unix_millis(self) -> i64 {
        self.unix_micros.div_euclid(1_000)
    }

    /// Returns the number of whole seconds since the Unix epoch (rounded towards negative
    /// infinity).
    ///
    /// This is the value expected by Discord's `<t:…>` timestamp markdown.
    ///
    /// discord.js counterpart: `Date#getTime`.
    #[must_use]
    pub const fn as_unix_secs(self) -> i64 {
        self.unix_micros.div_euclid(MICROS_PER_SECOND)
    }

    /// Parses an ISO 8601 / RFC 3339 timestamp such as `2026-11-10T19:42:07.123000+00:00`.
    ///
    /// Accepted format: `YYYY-MM-DDTHH:MM:SS`, an optional fraction of 1–9 digits (digits
    /// beyond microseconds are truncated), and a `Z` or `±HH:MM` offset. A space is accepted in
    /// place of `T`.
    ///
    /// discord.js counterpart: `new Date(string)`, as discord.js parses timestamps.
    ///
    /// # Errors
    ///
    /// Returns [`ParseTimestampError`] if the input is not in that format or contains an
    /// out-of-range component (for example month `13` or `February 30`).
    pub fn parse(input: &str) -> Result<Self, ParseTimestampError> {
        parse(input.as_bytes(), false).ok_or(ParseTimestampError(()))
    }

    /// Like [`parse`](Self::parse), but a missing offset means UTC (webhook event bodies send
    /// times without one).
    pub(crate) fn parse_assume_utc(input: &str) -> Result<Self, ParseTimestampError> {
        parse(input.as_bytes(), true).ok_or(ParseTimestampError(()))
    }
}

impl FromStr for Timestamp {
    type Err = ParseTimestampError;

    fn from_str(s: &str) -> Result<Self, Self::Err> {
        Self::parse(s)
    }
}

/// Formats as `YYYY-MM-DDTHH:MM:SS.ffffff+00:00`, the format Discord itself uses.
impl fmt::Display for Timestamp {
    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
        let days = self
            .unix_micros
            .div_euclid(SECONDS_PER_DAY * MICROS_PER_SECOND);
        let micros_of_day = self
            .unix_micros
            .rem_euclid(SECONDS_PER_DAY * MICROS_PER_SECOND);
        let (year, month, day) = civil_from_days(days);
        let seconds_of_day = micros_of_day / MICROS_PER_SECOND;
        let micros = micros_of_day % MICROS_PER_SECOND;
        write!(
            f,
            "{year:04}-{month:02}-{day:02}T{:02}:{:02}:{:02}.{micros:06}+00:00",
            seconds_of_day / 3_600,
            seconds_of_day / 60 % 60,
            seconds_of_day % 60,
        )
    }
}

/// The error returned when parsing a [`Timestamp`] fails.
///
/// discord.js counterpart: none (discord.js turns timestamps into `Date`s).
#[derive(Clone, Debug, PartialEq, Eq)]
pub struct ParseTimestampError(());

impl fmt::Display for ParseTimestampError {
    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
        f.write_str("invalid ISO 8601 timestamp")
    }
}

impl core::error::Error for ParseTimestampError {}

impl Serialize for Timestamp {
    fn serialize<S: Serializer>(&self, serializer: S) -> Result<S::Ok, S::Error> {
        serializer.collect_str(self)
    }
}

impl<'de> Deserialize<'de> for Timestamp {
    fn deserialize<D: Deserializer<'de>>(deserializer: D) -> Result<Self, D::Error> {
        struct TimestampVisitor;

        impl Visitor<'_> for TimestampVisitor {
            type Value = Timestamp;

            fn expecting(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
                f.write_str("an ISO 8601 timestamp string")
            }

            fn visit_str<E: DeError>(self, value: &str) -> Result<Self::Value, E> {
                Timestamp::parse(value).map_err(E::custom)
            }
        }

        deserializer.deserialize_str(TimestampVisitor)
    }
}

/// Parses `len` ASCII digits starting at `at`.
fn digits(input: &[u8], at: usize, len: usize) -> Option<i64> {
    let slice = input.get(at..at + len)?;
    let mut value = 0_i64;
    for &byte in slice {
        if !byte.is_ascii_digit() {
            return None;
        }
        value = value * 10 + i64::from(byte - b'0');
    }
    Some(value)
}

fn expect(input: &[u8], at: usize, byte: u8) -> Option<()> {
    (input.get(at) == Some(&byte)).then_some(())
}

fn parse(input: &[u8], assume_utc: bool) -> Option<Timestamp> {
    // YYYY-MM-DDTHH:MM:SS
    let year = digits(input, 0, 4)?;
    expect(input, 4, b'-')?;
    let month = digits(input, 5, 2)?;
    expect(input, 7, b'-')?;
    let day = digits(input, 8, 2)?;
    if !matches!(input.get(10), Some(b'T' | b't' | b' ')) {
        return None;
    }
    let hour = digits(input, 11, 2)?;
    expect(input, 13, b':')?;
    let minute = digits(input, 14, 2)?;
    expect(input, 16, b':')?;
    let second = digits(input, 17, 2)?;

    if !(1..=12).contains(&month)
        || day < 1
        || day > days_in_month(year, month)
        || hour > 23
        || minute > 59
        || second > 59
    {
        return None;
    }

    // Optional fraction: 1–9 digits, truncated to microseconds.
    let mut at = 19;
    let mut micros = 0_i64;
    if input.get(at) == Some(&b'.') {
        at += 1;
        let start = at;
        while let Some(byte) = input.get(at).filter(|byte| byte.is_ascii_digit()) {
            if at - start < 6 {
                micros = micros * 10 + i64::from(byte - b'0');
            }
            at += 1;
        }
        let len = at - start;
        if len == 0 || len > 9 {
            return None;
        }
        for _ in len..6 {
            micros *= 10;
        }
    }

    // Offset: `Z` or `±HH:MM` (or nothing, read as UTC, when `assume_utc`).
    let offset_seconds = match input.get(at) {
        None if assume_utc => 0,
        Some(b'Z' | b'z') => {
            at += 1;
            0
        }
        Some(sign @ (b'+' | b'-')) => {
            let offset_hour = digits(input, at + 1, 2)?;
            expect(input, at + 3, b':')?;
            let offset_minute = digits(input, at + 4, 2)?;
            if offset_hour > 23 || offset_minute > 59 {
                return None;
            }
            at += 6;
            let offset = offset_hour * 3_600 + offset_minute * 60;
            if *sign == b'+' { offset } else { -offset }
        }
        _ => return None,
    };
    if at != input.len() {
        return None;
    }

    let days = days_from_civil(year, month, day);
    let seconds = days * SECONDS_PER_DAY + hour * 3_600 + minute * 60 + second - offset_seconds;
    Some(Timestamp::from_unix_micros(
        seconds * MICROS_PER_SECOND + micros,
    ))
}

const fn is_leap_year(year: i64) -> bool {
    year % 4 == 0 && (year % 100 != 0 || year % 400 == 0)
}

const fn days_in_month(year: i64, month: i64) -> i64 {
    match month {
        2 if is_leap_year(year) => 29,
        2 => 28,
        4 | 6 | 9 | 11 => 30,
        _ => 31,
    }
}

/// Days since 1970-01-01 for a proleptic Gregorian date.
///
/// Algorithm by Howard Hinnant, "chrono-Compatible Low-Level Date Algorithms".
const fn days_from_civil(year: i64, month: i64, day: i64) -> i64 {
    let year = if month <= 2 { year - 1 } else { year };
    let era = year.div_euclid(400);
    let year_of_era = year - era * 400;
    let month_index = (month + 9) % 12; // March = 0
    let day_of_year = (153 * month_index + 2) / 5 + day - 1;
    let day_of_era = year_of_era * 365 + year_of_era / 4 - year_of_era / 100 + day_of_year;
    era * 146_097 + day_of_era - 719_468
}

/// Inverse of [`days_from_civil`].
const fn civil_from_days(days: i64) -> (i64, i64, i64) {
    let days = days + 719_468;
    let era = days.div_euclid(146_097);
    let day_of_era = days - era * 146_097;
    let year_of_era =
        (day_of_era - day_of_era / 1_460 + day_of_era / 36_524 - day_of_era / 146_096) / 365;
    let day_of_year = day_of_era - (365 * year_of_era + year_of_era / 4 - year_of_era / 100);
    let month_index = (5 * day_of_year + 2) / 153;
    let day = day_of_year - (153 * month_index + 2) / 5 + 1;
    let month = if month_index < 10 {
        month_index + 3
    } else {
        month_index - 9
    };
    let year = year_of_era + era * 400 + if month <= 2 { 1 } else { 0 };
    (year, month, day)
}

#[cfg(test)]
mod tests {
    use super::*;

    #[test]
    fn parse_discord_formats() {
        let with_fraction: Timestamp = "2017-07-11T17:27:07.299000+00:00".parse().unwrap();
        assert_eq!(with_fraction.as_unix_millis(), 1_499_794_027_299);

        let without_fraction: Timestamp = "2017-07-11T17:27:07+00:00".parse().unwrap();
        assert_eq!(without_fraction.as_unix_secs(), 1_499_794_027);

        let zulu: Timestamp = "2017-07-11T17:27:07.299Z".parse().unwrap();
        assert_eq!(zulu, with_fraction);
    }

    #[test]
    fn nanosecond_fraction_is_truncated() {
        let ts: Timestamp = "1970-01-01T00:00:00.123456789Z".parse().unwrap();
        assert_eq!(ts.as_unix_micros(), 123_456);
    }

    #[test]
    fn display_round_trip() {
        for input in [
            "1970-01-01T00:00:00.000000+00:00",
            "2000-02-29T23:59:59.999999+00:00",
            "2026-11-10T19:42:07.123000+00:00",
            "1969-12-31T23:59:59.500000+00:00",
        ] {
            let ts: Timestamp = input.parse().unwrap();
            assert_eq!(ts.to_string(), input);
        }
    }

    #[test]
    fn negative_offset() {
        let ts: Timestamp = "2021-01-01T00:00:00-05:30".parse().unwrap();
        assert_eq!(ts.to_string(), "2021-01-01T05:30:00.000000+00:00");
    }

    #[test]
    fn missing_offset_only_with_assume_utc() {
        let input = "2024-10-18T14:42:53.064834";
        assert!(input.parse::<Timestamp>().is_err());
        let ts = Timestamp::parse_assume_utc(input).unwrap();
        assert_eq!(ts, "2024-10-18T14:42:53.064834Z".parse().unwrap());
        // An explicit offset still applies, and junk is still rejected.
        assert_eq!(
            Timestamp::parse_assume_utc("2024-10-18T16:42:53.064834+02:00").unwrap(),
            ts
        );
        assert!(Timestamp::parse_assume_utc("2024-10-18T14:42:53.").is_err());
        assert!(Timestamp::parse_assume_utc("2024-10-18T14:42:53x").is_err());
    }

    #[test]
    fn rejects_invalid() {
        for input in [
            "",
            "2021-13-01T00:00:00Z",
            "2021-02-29T00:00:00Z",
            "2021-01-01T24:00:00Z",
            "2021-01-01T00:00:00",
            "2021-01-01T00:00:00.Z",
            "2021-01-01T00:00:00.1234567890Z",
            "2021-01-01T00:00:00+0000",
            "2021-01-01T00:00:00Zjunk",
            "2021-1-01T00:00:00Z",
        ] {
            assert!(
                input.parse::<Timestamp>().is_err(),
                "{input:?} should be rejected"
            );
        }
    }

    #[test]
    fn civil_round_trip() {
        for days in (-800_000..800_000).step_by(997) {
            let (year, month, day) = civil_from_days(days);
            assert_eq!(days_from_civil(year, month, day), days);
        }
    }
}