Coverage Report

Created: 2025-12-28 06:31

next uncovered line (L), next uncovered region (R), next uncovered branch (B)
/rust/registry/src/index.crates.io-1949cf8c6b5b557f/httpdate-1.0.3/src/date.rs
Line
Count
Source
1
use std::cmp;
2
use std::fmt::{self, Display, Formatter};
3
use std::str::FromStr;
4
use std::time::{Duration, SystemTime, UNIX_EPOCH};
5
6
use crate::Error;
7
8
/// HTTP timestamp type.
9
///
10
/// Parse using `FromStr` impl.
11
/// Format using the `Display` trait.
12
/// Convert timestamp into/from `SytemTime` to use.
13
/// Supports comparsion and sorting.
14
#[derive(Copy, Clone, Debug, Eq, PartialEq, Hash)]
15
pub struct HttpDate {
16
    /// 0...59
17
    sec: u8,
18
    /// 0...59
19
    min: u8,
20
    /// 0...23
21
    hour: u8,
22
    /// 1...31
23
    day: u8,
24
    /// 1...12
25
    mon: u8,
26
    /// 1970...9999
27
    year: u16,
28
    /// 1...7
29
    wday: u8,
30
}
31
32
impl HttpDate {
33
0
    fn is_valid(&self) -> bool {
34
0
        self.sec < 60
35
0
            && self.min < 60
36
0
            && self.hour < 24
37
0
            && self.day > 0
38
0
            && self.day < 32
39
0
            && self.mon > 0
40
0
            && self.mon <= 12
41
0
            && self.year >= 1970
42
0
            && self.year <= 9999
43
0
            && &HttpDate::from(SystemTime::from(*self)) == self
44
0
    }
45
}
46
47
impl From<SystemTime> for HttpDate {
48
0
    fn from(v: SystemTime) -> HttpDate {
49
0
        let dur = v
50
0
            .duration_since(UNIX_EPOCH)
51
0
            .expect("all times should be after the epoch");
52
0
        let secs_since_epoch = dur.as_secs();
53
54
0
        if secs_since_epoch >= 253402300800 {
55
            // year 9999
56
0
            panic!("date must be before year 9999");
57
0
        }
58
59
        /* 2000-03-01 (mod 400 year, immediately after feb29 */
60
        const LEAPOCH: i64 = 11017;
61
        const DAYS_PER_400Y: i64 = 365 * 400 + 97;
62
        const DAYS_PER_100Y: i64 = 365 * 100 + 24;
63
        const DAYS_PER_4Y: i64 = 365 * 4 + 1;
64
65
0
        let days = (secs_since_epoch / 86400) as i64 - LEAPOCH;
66
0
        let secs_of_day = secs_since_epoch % 86400;
67
68
0
        let mut qc_cycles = days / DAYS_PER_400Y;
69
0
        let mut remdays = days % DAYS_PER_400Y;
70
71
0
        if remdays < 0 {
72
0
            remdays += DAYS_PER_400Y;
73
0
            qc_cycles -= 1;
74
0
        }
75
76
0
        let mut c_cycles = remdays / DAYS_PER_100Y;
77
0
        if c_cycles == 4 {
78
0
            c_cycles -= 1;
79
0
        }
80
0
        remdays -= c_cycles * DAYS_PER_100Y;
81
82
0
        let mut q_cycles = remdays / DAYS_PER_4Y;
83
0
        if q_cycles == 25 {
84
0
            q_cycles -= 1;
85
0
        }
86
0
        remdays -= q_cycles * DAYS_PER_4Y;
87
88
0
        let mut remyears = remdays / 365;
89
0
        if remyears == 4 {
90
0
            remyears -= 1;
91
0
        }
92
0
        remdays -= remyears * 365;
93
94
0
        let mut year = 2000 + remyears + 4 * q_cycles + 100 * c_cycles + 400 * qc_cycles;
95
96
0
        let months = [31, 30, 31, 30, 31, 31, 30, 31, 30, 31, 31, 29];
97
0
        let mut mon = 0;
98
0
        for mon_len in months.iter() {
99
0
            mon += 1;
100
0
            if remdays < *mon_len {
101
0
                break;
102
0
            }
103
0
            remdays -= *mon_len;
104
        }
105
0
        let mday = remdays + 1;
106
0
        let mon = if mon + 2 > 12 {
107
0
            year += 1;
108
0
            mon - 10
109
        } else {
110
0
            mon + 2
111
        };
112
113
0
        let mut wday = (3 + days) % 7;
114
0
        if wday <= 0 {
115
0
            wday += 7
116
0
        };
117
118
0
        HttpDate {
119
0
            sec: (secs_of_day % 60) as u8,
120
0
            min: ((secs_of_day % 3600) / 60) as u8,
121
0
            hour: (secs_of_day / 3600) as u8,
122
0
            day: mday as u8,
123
0
            mon: mon as u8,
124
0
            year: year as u16,
125
0
            wday: wday as u8,
126
0
        }
127
0
    }
128
}
129
130
impl From<HttpDate> for SystemTime {
131
0
    fn from(v: HttpDate) -> SystemTime {
132
0
        let leap_years =
133
0
            ((v.year - 1) - 1968) / 4 - ((v.year - 1) - 1900) / 100 + ((v.year - 1) - 1600) / 400;
134
0
        let mut ydays = match v.mon {
135
0
            1 => 0,
136
0
            2 => 31,
137
0
            3 => 59,
138
0
            4 => 90,
139
0
            5 => 120,
140
0
            6 => 151,
141
0
            7 => 181,
142
0
            8 => 212,
143
0
            9 => 243,
144
0
            10 => 273,
145
0
            11 => 304,
146
0
            12 => 334,
147
0
            _ => unreachable!(),
148
0
        } + v.day as u64
149
            - 1;
150
0
        if is_leap_year(v.year) && v.mon > 2 {
151
0
            ydays += 1;
152
0
        }
153
0
        let days = (v.year as u64 - 1970) * 365 + leap_years as u64 + ydays;
154
0
        UNIX_EPOCH
155
0
            + Duration::from_secs(
156
0
                v.sec as u64 + v.min as u64 * 60 + v.hour as u64 * 3600 + days * 86400,
157
0
            )
158
0
    }
159
}
160
161
impl FromStr for HttpDate {
162
    type Err = Error;
163
164
0
    fn from_str(s: &str) -> Result<HttpDate, Error> {
165
0
        if !s.is_ascii() {
166
0
            return Err(Error(()));
167
0
        }
168
0
        let x = s.trim().as_bytes();
169
0
        let date = parse_imf_fixdate(x)
170
0
            .or_else(|_| parse_rfc850_date(x))
171
0
            .or_else(|_| parse_asctime(x))?;
172
0
        if !date.is_valid() {
173
0
            return Err(Error(()));
174
0
        }
175
0
        Ok(date)
176
0
    }
177
}
178
179
impl Display for HttpDate {
180
0
    fn fmt(&self, f: &mut Formatter) -> fmt::Result {
181
0
        let wday = match self.wday {
182
0
            1 => b"Mon",
183
0
            2 => b"Tue",
184
0
            3 => b"Wed",
185
0
            4 => b"Thu",
186
0
            5 => b"Fri",
187
0
            6 => b"Sat",
188
0
            7 => b"Sun",
189
0
            _ => unreachable!(),
190
        };
191
192
0
        let mon = match self.mon {
193
0
            1 => b"Jan",
194
0
            2 => b"Feb",
195
0
            3 => b"Mar",
196
0
            4 => b"Apr",
197
0
            5 => b"May",
198
0
            6 => b"Jun",
199
0
            7 => b"Jul",
200
0
            8 => b"Aug",
201
0
            9 => b"Sep",
202
0
            10 => b"Oct",
203
0
            11 => b"Nov",
204
0
            12 => b"Dec",
205
0
            _ => unreachable!(),
206
        };
207
208
0
        let mut buf: [u8; 29] = *b"   , 00     0000 00:00:00 GMT";
209
0
        buf[0] = wday[0];
210
0
        buf[1] = wday[1];
211
0
        buf[2] = wday[2];
212
0
        buf[5] = b'0' + (self.day / 10);
213
0
        buf[6] = b'0' + (self.day % 10);
214
0
        buf[8] = mon[0];
215
0
        buf[9] = mon[1];
216
0
        buf[10] = mon[2];
217
0
        buf[12] = b'0' + (self.year / 1000) as u8;
218
0
        buf[13] = b'0' + (self.year / 100 % 10) as u8;
219
0
        buf[14] = b'0' + (self.year / 10 % 10) as u8;
220
0
        buf[15] = b'0' + (self.year % 10) as u8;
221
0
        buf[17] = b'0' + (self.hour / 10);
222
0
        buf[18] = b'0' + (self.hour % 10);
223
0
        buf[20] = b'0' + (self.min / 10);
224
0
        buf[21] = b'0' + (self.min % 10);
225
0
        buf[23] = b'0' + (self.sec / 10);
226
0
        buf[24] = b'0' + (self.sec % 10);
227
0
        f.write_str(std::str::from_utf8(&buf[..]).unwrap())
228
0
    }
229
}
230
231
impl Ord for HttpDate {
232
0
    fn cmp(&self, other: &HttpDate) -> cmp::Ordering {
233
0
        SystemTime::from(*self).cmp(&SystemTime::from(*other))
234
0
    }
235
}
236
237
impl PartialOrd for HttpDate {
238
0
    fn partial_cmp(&self, other: &HttpDate) -> Option<cmp::Ordering> {
239
0
        Some(self.cmp(other))
240
0
    }
241
}
242
243
0
fn toint_1(x: u8) -> Result<u8, Error> {
244
0
    let result = x.wrapping_sub(b'0');
245
0
    if result < 10 {
246
0
        Ok(result)
247
    } else {
248
0
        Err(Error(()))
249
    }
250
0
}
251
252
0
fn toint_2(s: &[u8]) -> Result<u8, Error> {
253
0
    let high = s[0].wrapping_sub(b'0');
254
0
    let low = s[1].wrapping_sub(b'0');
255
256
0
    if high < 10 && low < 10 {
257
0
        Ok(high * 10 + low)
258
    } else {
259
0
        Err(Error(()))
260
    }
261
0
}
262
263
#[allow(clippy::many_single_char_names)]
264
0
fn toint_4(s: &[u8]) -> Result<u16, Error> {
265
0
    let a = u16::from(s[0].wrapping_sub(b'0'));
266
0
    let b = u16::from(s[1].wrapping_sub(b'0'));
267
0
    let c = u16::from(s[2].wrapping_sub(b'0'));
268
0
    let d = u16::from(s[3].wrapping_sub(b'0'));
269
270
0
    if a < 10 && b < 10 && c < 10 && d < 10 {
271
0
        Ok(a * 1000 + b * 100 + c * 10 + d)
272
    } else {
273
0
        Err(Error(()))
274
    }
275
0
}
276
277
0
fn parse_imf_fixdate(s: &[u8]) -> Result<HttpDate, Error> {
278
    // Example: `Sun, 06 Nov 1994 08:49:37 GMT`
279
0
    if s.len() != 29 || &s[25..] != b" GMT" || s[16] != b' ' || s[19] != b':' || s[22] != b':' {
280
0
        return Err(Error(()));
281
0
    }
282
    Ok(HttpDate {
283
0
        sec: toint_2(&s[23..25])?,
284
0
        min: toint_2(&s[20..22])?,
285
0
        hour: toint_2(&s[17..19])?,
286
0
        day: toint_2(&s[5..7])?,
287
0
        mon: match &s[7..12] {
288
0
            b" Jan " => 1,
289
0
            b" Feb " => 2,
290
0
            b" Mar " => 3,
291
0
            b" Apr " => 4,
292
0
            b" May " => 5,
293
0
            b" Jun " => 6,
294
0
            b" Jul " => 7,
295
0
            b" Aug " => 8,
296
0
            b" Sep " => 9,
297
0
            b" Oct " => 10,
298
0
            b" Nov " => 11,
299
0
            b" Dec " => 12,
300
0
            _ => return Err(Error(())),
301
        },
302
0
        year: toint_4(&s[12..16])?,
303
0
        wday: match &s[..5] {
304
0
            b"Mon, " => 1,
305
0
            b"Tue, " => 2,
306
0
            b"Wed, " => 3,
307
0
            b"Thu, " => 4,
308
0
            b"Fri, " => 5,
309
0
            b"Sat, " => 6,
310
0
            b"Sun, " => 7,
311
0
            _ => return Err(Error(())),
312
        },
313
    })
314
0
}
315
316
0
fn parse_rfc850_date(s: &[u8]) -> Result<HttpDate, Error> {
317
    // Example: `Sunday, 06-Nov-94 08:49:37 GMT`
318
0
    if s.len() < 23 {
319
0
        return Err(Error(()));
320
0
    }
321
322
0
    fn wday<'a>(s: &'a [u8], wday: u8, name: &'static [u8]) -> Option<(u8, &'a [u8])> {
323
0
        if &s[0..name.len()] == name {
324
0
            return Some((wday, &s[name.len()..]));
325
0
        }
326
0
        None
327
0
    }
328
0
    let (wday, s) = wday(s, 1, b"Monday, ")
329
0
        .or_else(|| wday(s, 2, b"Tuesday, "))
330
0
        .or_else(|| wday(s, 3, b"Wednesday, "))
331
0
        .or_else(|| wday(s, 4, b"Thursday, "))
332
0
        .or_else(|| wday(s, 5, b"Friday, "))
333
0
        .or_else(|| wday(s, 6, b"Saturday, "))
334
0
        .or_else(|| wday(s, 7, b"Sunday, "))
335
0
        .ok_or(Error(()))?;
336
0
    if s.len() != 22 || s[12] != b':' || s[15] != b':' || &s[18..22] != b" GMT" {
337
0
        return Err(Error(()));
338
0
    }
339
0
    let mut year = u16::from(toint_2(&s[7..9])?);
340
0
    if year < 70 {
341
0
        year += 2000;
342
0
    } else {
343
0
        year += 1900;
344
0
    }
345
    Ok(HttpDate {
346
0
        sec: toint_2(&s[16..18])?,
347
0
        min: toint_2(&s[13..15])?,
348
0
        hour: toint_2(&s[10..12])?,
349
0
        day: toint_2(&s[0..2])?,
350
0
        mon: match &s[2..7] {
351
0
            b"-Jan-" => 1,
352
0
            b"-Feb-" => 2,
353
0
            b"-Mar-" => 3,
354
0
            b"-Apr-" => 4,
355
0
            b"-May-" => 5,
356
0
            b"-Jun-" => 6,
357
0
            b"-Jul-" => 7,
358
0
            b"-Aug-" => 8,
359
0
            b"-Sep-" => 9,
360
0
            b"-Oct-" => 10,
361
0
            b"-Nov-" => 11,
362
0
            b"-Dec-" => 12,
363
0
            _ => return Err(Error(())),
364
        },
365
0
        year,
366
0
        wday,
367
    })
368
0
}
369
370
0
fn parse_asctime(s: &[u8]) -> Result<HttpDate, Error> {
371
    // Example: `Sun Nov  6 08:49:37 1994`
372
0
    if s.len() != 24 || s[10] != b' ' || s[13] != b':' || s[16] != b':' || s[19] != b' ' {
373
0
        return Err(Error(()));
374
0
    }
375
    Ok(HttpDate {
376
0
        sec: toint_2(&s[17..19])?,
377
0
        min: toint_2(&s[14..16])?,
378
0
        hour: toint_2(&s[11..13])?,
379
        day: {
380
0
            let x = &s[8..10];
381
            {
382
0
                if x[0] == b' ' {
383
0
                    toint_1(x[1])
384
                } else {
385
0
                    toint_2(x)
386
                }
387
0
            }?
388
        },
389
0
        mon: match &s[4..8] {
390
0
            b"Jan " => 1,
391
0
            b"Feb " => 2,
392
0
            b"Mar " => 3,
393
0
            b"Apr " => 4,
394
0
            b"May " => 5,
395
0
            b"Jun " => 6,
396
0
            b"Jul " => 7,
397
0
            b"Aug " => 8,
398
0
            b"Sep " => 9,
399
0
            b"Oct " => 10,
400
0
            b"Nov " => 11,
401
0
            b"Dec " => 12,
402
0
            _ => return Err(Error(())),
403
        },
404
0
        year: toint_4(&s[20..24])?,
405
0
        wday: match &s[0..4] {
406
0
            b"Mon " => 1,
407
0
            b"Tue " => 2,
408
0
            b"Wed " => 3,
409
0
            b"Thu " => 4,
410
0
            b"Fri " => 5,
411
0
            b"Sat " => 6,
412
0
            b"Sun " => 7,
413
0
            _ => return Err(Error(())),
414
        },
415
    })
416
0
}
417
418
0
fn is_leap_year(y: u16) -> bool {
419
0
    y % 4 == 0 && (y % 100 != 0 || y % 400 == 0)
420
0
}