Repository navigation
Why isn't Timestamp::MAX equivalent to 9999-12-31T23:59:59.999999999Z?
#302
use jiff::Timestamp;
fn main() {
// Shouldn't this display as `-9999-01-01T00:00:00Z`?
assert_eq!("-009999-01-02T01:59:59Z", Timestamp::MIN.to_string());
// Shouldn't this display as `9999-12-31T23:59:59.999999999Z`?
assert_eq!("9999-12-30T22:00:00.999999999Z", Timestamp::MAX.to_string());
} |
Replies: 2 comments 3 replies
|
These are correct. The leading As for the specific values, they are correct. To understand why, we have to look at the minimum and maximum values for the following three things in concert:
Specifically, the docs for
So what is the maximum possible offset? It is documented to be So in order for any possible use jiff::{tz::Offset, Timestamp};
fn main() -> anyhow::Result<()> {
let dt = Offset::MAX.to_datetime(Timestamp::MAX);
println!("{dt}");
Ok(())
}Running this program, we get: And that's the maximum civil datetime. If we allowed any timestamps bigger than what it is without adjusting the offset maximum, it would permit getting a civil datetime greater than use jiff::{tz::Offset, Timestamp};
fn main() -> anyhow::Result<()> {
let dt = Offset::UTC.to_datetime(Timestamp::MAX);
println!("{dt}");
Ok(())
}Which gives us: The idea here is that by choosing the boundaries carefully, we can make one of timestamp->datetime or datetime->timestamp infallible, but not both. I chose the former because the latter is already somewhat fallible, given that there may be ambiguity in which timestamp a datetime corresponds to. But if you have a timestamp, there's always exactly one datetime that corresponds to it for any particular time zone. The trade off is that the minimum and maximum timestamp values in UTC are somewhat "odd." But the minimum and maximum civil datetimes are sensible. (The same argument applies to the minimum values.) |
|
I like to keep Discussions open, otherwise they don't show up on the Discussions page by default. It's super weird that we have both "answered or not" and "closed or not" states for Discussions. |
These are correct. The leading
00for the negative year is part of ISO 8601 IIRC. It's definitely at least specified by the Temporal ISO 8601 extension.As for the specific values, they are correct. To understand why, we have to look at the minimum and maximum values for the following three things in concert:
jiff::tz::Offsetjiff::Timestampjiff::civil::DateTimeSpecifically, the docs for
jiff::civil::DateTime::MAXsay (emphasis mine):So what is the maximum possible offset? It is documented to be
25:59:59(that is, 25 hours, 59 minutes, 59 seconds).So in …