time icon indicating copy to clipboard operation
time copied to clipboard

`serde(with = "time::serde::iso8601")` may produce values with off by one nanoseconds

Open ysndr opened this issue 10 months ago • 1 comments

There seems to be another bug when serializing iso8601 timestamps where the nanoseconds are off by -1:

#[test]
fn test_info_round_trip() {
	let last_checked = datetime!(2025-01-10 23:01:16.000081999 +00:00:00);

	// serialized into componenetsm, this works
	let serialized = serde_json::to_string(&last_checked).unwrap();
	
	println!("{}", serialized); // [2025,10,23,1,16,81999,0,0,0]
	
	let deserialized: OffsetDateTime = serde_json::from_str(&serialized).unwrap();
	assert_eq!(last_checked, deserialized);
	
	
	// serialized with `time::serde::iso8601`, this does not:
	#[derive(Debug, Clone, PartialEq, Deserialize, Serialize)]
	struct UpgradeInformation {
		#[serde(with = "time::serde::iso8601")]
		pub last_checked: OffsetDateTime,
	}
	let info = UpgradeInformation { last_checked };
	let serialized = serde_json::to_string(&info).unwrap();
	
	println!("{}", serialized); // {"last_checked":"+002025-01-10T23:01:16.000081998Z"}
	
	let deserialized: UpgradeInformation = serde_json::from_str(&serialized).unwrap();
	assert_eq!(info, deserialized);
}

Appears sounds similar to https://github.com/time-rs/time/issues/488 which should have been resolved a while ago.

ysndr avatar Jan 13 '25 14:01 ysndr