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

lockfile: Port away from deprecated chrono method #4596

Merged
merged 1 commit into from
Sep 14, 2023
Merged
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
22 changes: 17 additions & 5 deletions rust/src/lockfile.rs
Original file line number Diff line number Diff line change
Expand Up @@ -300,16 +300,20 @@ pub(crate) fn lockfile_read(filenames: &Vec<String>) -> CxxResult<Box<LockfileCo
Ok(Box::new(lockfile_parse_multiple(filenames)?))
}

/// Get current time (in UTC), but scrub nanoseconds; it's overkill to serialize that
fn coarse_utc_timestamp_now() -> chrono::DateTime<chrono::Utc> {
DateTime::from_naive_utc_and_offset(
Utc::now().date_naive().and_time(Default::default()),
chrono::Utc,
)
}

pub(crate) fn lockfile_write(
filename: &str,
mut packages: Pin<&mut crate::ffi::CxxGObjectArray>,
mut rpmmd_repos: Pin<&mut crate::ffi::CxxGObjectArray>,
) -> CxxResult<()> {
// get current time, but scrub nanoseconds; it's overkill to serialize that
let now = DateTime::from_utc(
Utc::now().date_naive().and_time(Default::default()),
chrono::Utc,
);
let now = coarse_utc_timestamp_now();

let mut lockfile = LockfileConfig {
packages: Some(BTreeMap::new()),
Expand Down Expand Up @@ -380,3 +384,11 @@ pub(crate) fn lockfile_write(
})?;
Ok(())
}

#[test]
fn test_coarse_now() {
let now = Utc::now();
let now_coarse = coarse_utc_timestamp_now();
assert_eq!(now_coarse.format("%H:%M:%S").to_string(), "00:00:00");
assert_eq!(now.date_naive(), now_coarse.date_naive());
}