Skip to content

Commit cce7616

Browse files
committed
Fallible timestamp cast i64 -> u32.
1 parent 206f392 commit cce7616

File tree

1 file changed

+24
-11
lines changed

1 file changed

+24
-11
lines changed

git-date/src/parse.rs

Lines changed: 24 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,8 @@
11
use crate::time::format::{DEFAULT, ISO8601, ISO8601_STRICT, RFC2822, SHORT};
22
use crate::time::Sign;
33
use crate::Time;
4-
use std::num::ParseIntError;
4+
use std::convert::TryInto;
5+
use std::num::TryFromIntError;
56
use std::str::FromStr;
67
use time::{Date, OffsetDateTime};
78

@@ -10,13 +11,10 @@ use time::{Date, OffsetDateTime};
1011
pub enum Error {
1112
#[error("Date string can not be parsed")]
1213
InvalidDateString,
13-
14-
#[error("Timezone offset can not be parsed")]
15-
InvalidTzOffset,
1614
#[error("Relative period can not be parsed")]
1715
InvalidPeriod,
18-
#[error("Integer string can not be parsed")]
19-
InvalidInteger(#[from] ParseIntError),
16+
#[error("Dates past 2038 can not be represented.")]
17+
InvalidDate(#[from] TryFromIntError),
2018
}
2119

2220
#[allow(missing_docs)]
@@ -27,15 +25,30 @@ pub fn parse(input: &str) -> Result<Time, Error> {
2725
} else {
2826
return if let Ok(val) = Date::parse(input, SHORT) {
2927
let val = val.with_hms(0, 0, 0).expect("date is in range").assume_utc();
30-
Ok(Time::new(val.unix_timestamp() as u32, val.offset().whole_seconds()))
28+
Ok(Time::new(
29+
val.unix_timestamp().try_into()?,
30+
val.offset().whole_seconds(),
31+
))
3132
} else if let Ok(val) = OffsetDateTime::parse(input, RFC2822) {
32-
Ok(Time::new(val.unix_timestamp() as u32, val.offset().whole_seconds()))
33+
Ok(Time::new(
34+
val.unix_timestamp().try_into()?,
35+
val.offset().whole_seconds(),
36+
))
3337
} else if let Ok(val) = OffsetDateTime::parse(input, ISO8601) {
34-
Ok(Time::new(val.unix_timestamp() as u32, val.offset().whole_seconds()))
38+
Ok(Time::new(
39+
val.unix_timestamp().try_into()?,
40+
val.offset().whole_seconds(),
41+
))
3542
} else if let Ok(val) = OffsetDateTime::parse(input, ISO8601_STRICT) {
36-
Ok(Time::new(val.unix_timestamp() as u32, val.offset().whole_seconds()))
43+
Ok(Time::new(
44+
val.unix_timestamp().try_into()?,
45+
val.offset().whole_seconds(),
46+
))
3747
} else if let Ok(val) = OffsetDateTime::parse(input, DEFAULT) {
38-
Ok(Time::new(val.unix_timestamp() as u32, val.offset().whole_seconds()))
48+
Ok(Time::new(
49+
val.unix_timestamp().try_into()?,
50+
val.offset().whole_seconds(),
51+
))
3952
} else if let Ok(val) = u32::from_str(input) {
4053
// Format::Unix
4154
Ok(Time::new(val, 0))

0 commit comments

Comments
 (0)