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

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

Open
ysndr opened this issue Jan 13, 2025 · 1 comment

Comments

@ysndr
Copy link

ysndr commented Jan 13, 2025

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 #488 which should have been resolved a while ago.

@jhpratt
Copy link
Member

jhpratt commented Jan 19, 2025

This was almost certainly introduced in #679. To be honest, it's a no-win situation. Floating point values are inherently imprecise. If this can be made to work, I'm all ears, but I suspect that may not be possible (at least easily).

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants