Suggestion: time::Time
precision and explicit Timestamp
type
#729
Labels
A-core
Area: anything not otherwise covered
C-blocked
Category: blocked by another (possibly upstream) issue
C-feature-request
Category: a new feature (not already implemented)
Hi, I'm a beginner in Rust and I don't know the whole story of this crate, it's my first issue in this repository.
My suggestion consists of two parts:
Time
precisionTimestamp
I think that it is important to talk about both parts in one issue, because of the interaction between them.
about the
time::Time
precisionIt is often needs to relax time precision requirements. Suppose we need to design the train tickets selling system. So we need the ticket representation in our program. Every ticket has a departure time and an arriving time and we want show it in our representation. Usually, trains schedule builds with minute precision (without seconds or nanoseconds), so we want to store the minutes and hours of departure and arriving. And, as trains may travel long distances, we want to store the date and time with utc offset. Summarizing,
time::OffsetDateTime
is most suitable type for us. And here we got into trouble. If we want to be able to compare departure or arriving times, we must keep invariant that nanoseconds (and seconds) will always be zero. And the library will not help us. It is all on us. Also we have to store at least 5 extra unused bytes for each time instance in our program.I suggest to provide different
Time
types for each one precision quality and add suitable cross-operations between them. I would like to discuss the exact design, but for further reading suppose we have typeTime
with generic parameter precision.about the
Timestamp
Current unix timestamp support is quite not ergonomic. All we have are just couple of methods on
time::OffesetDateTime
to convert from/into unix timestamp that actuallyi64
, but there is no way to keep the timestamp invariant. I suggest to add explicitTimestamp
type that is transparentlyi64
for backwards compatibility.Here are my arguments, explaining why this type seems reasonable:
time::OffesetDateTime
andTimestamp
do not require any explicit checks for users, because the type system guarantees that if timestamp value exists, it is correct. And any valid timestamp may be safely converted intotime::OffsetDateTime
. This may seem like just transferring responsibility to another type, but it is not. Once created and checked, the 'Timestamp' value can be used without further checks. (unless we need to change it)Timestamp
is defined as numbers of seconds, and if we want to keep nanoseconds, we must store it explicitly. That's why I want to suggest to define another typeTimestampNanos
to handle this case. So, the relations between the timestamp values and the related nanoseconds will not get messed up. But theTimestamp
type is still important, because we cannot safely create theTime<Precision::Seconds>
(or whatever) fromTimestampNanos
.However, there is an easier way. We may just add the
unsafe from_unix_timestamp_unchecked
method totime::OffesetDateTime
. And users will have the ability to write their own wrappers.Also I picked simplified
Timestamp
version, but it is also possible to designTimestamp
with other kinds of precision, not just nanoseconds. Maybe we can reuse sub second precision kinds fromTime
in this case?Bringing all together
Time
precision design. My thoughts are that we should define the new time type to keeptime::Time
for compatibility. I would like to see something likePrecision
enum, but there are a lot of nuances.Timestamp
andTimestampNanos
types.Time
with precision seconds andTimestamp
,Time
with precision nanoseconds andTimestampNanos
.The text was updated successfully, but these errors were encountered: