Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

refactor: Replace deprecated chrono methods #345

Merged
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 3 additions & 2 deletions lib/src/crypto/x509.rs
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ use std::{
result::Result,
};

use chrono::{DateTime, TimeZone, Utc};
use chrono::{DateTime, NaiveDateTime, Utc};
use openssl::{
asn1::*,
hash,
Expand Down Expand Up @@ -593,7 +593,8 @@ impl X509 {
} else {
date
};
Utc.datetime_from_str(date, "%b %d %H:%M:%S %Y")
NaiveDateTime::parse_from_str(date, "%b %d %H:%M:%S %Y")
.map(|naive| naive.and_utc())
.map_err(|e| {
error!("Cannot parse ASN1 date, err = {:?}", e);
X509Error
Expand Down
28 changes: 17 additions & 11 deletions lib/src/types/date_time.rs
Original file line number Diff line number Diff line change
Expand Up @@ -140,12 +140,18 @@ impl From<(u16, u16, u16, u16, u16, u16, u32)> for DateTime {
if nanos as i64 >= NANOS_PER_SECOND {
panic!("Invalid nanosecond");
}
let dt = Utc.ymd(year as i32, month as u32, day as u32).and_hms_nano(
hour as u32,
minute as u32,
second as u32,
nanos,
);
let dt = Utc
.with_ymd_and_hms(
year as i32,
month as u32,
day as u32,
hour as u32,
minute as u32,
second as u32,
)
.unwrap()
.with_nanosecond(nanos)
.unwrap();
DateTime::from(dt)
}
}
Expand Down Expand Up @@ -211,12 +217,10 @@ impl DateTime {
/// in and out of rfc3999 without any loss of precision to make it easier to do comparison tests.
#[cfg(test)]
pub fn rfc3339_now() -> DateTime {
use chrono::NaiveDateTime;
use std::time::{SystemTime, UNIX_EPOCH};

let duration = SystemTime::now().duration_since(UNIX_EPOCH).unwrap();
let naive = NaiveDateTime::from_timestamp(duration.as_secs() as i64, 0);
let now = DateTimeUtc::from_utc(naive, Utc);
let now = DateTimeUtc::from_timestamp(duration.as_secs() as i64, 0).unwrap();
DateTime::from(now)
}

Expand Down Expand Up @@ -327,13 +331,15 @@ impl DateTime {

/// The OPC UA epoch - Jan 1 1601 00:00:00
fn epoch_chrono() -> DateTimeUtc {
Utc.ymd(MIN_YEAR as i32, 1, 1).and_hms(0, 0, 0)
Utc.with_ymd_and_hms(MIN_YEAR as i32, 1, 1, 0, 0, 0)
.unwrap()
}

/// The OPC UA endtimes - Dec 31 9999 23:59:59 i.e. the date after which dates are returned as MAX_INT64 ticks
/// Spec doesn't say what happens in the last second before midnight...
fn endtimes_chrono() -> DateTimeUtc {
Utc.ymd(MAX_YEAR as i32, 12, 31).and_hms(23, 59, 59)
Utc.with_ymd_and_hms(MAX_YEAR as i32, 12, 31, 23, 59, 59)
.unwrap()
}

/// Turns a duration to ticks
Expand Down
Loading