-
Notifications
You must be signed in to change notification settings - Fork 532
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
Conversion between Timestamp and chrono::DateTime #414
Conversation
331dbe7
to
4cbfce9
Compare
Can you not use the existing |
To add a bit more color, in the medium term I'd like to add options to |
@danburkert To clarify, are you saying prost would use syntax = "proto3";
import "google/protobuf/timestamp.proto";
message Record {
google.protobuf.Timestamp created_at = 1
} #[derive(::prost::Message)]
pub struct Record {
pub created_at: std::time::SystemTime
} FWIW I use |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
@danburkert To clarify, are you saying prost would use
std::time::SystemTime
as the Rust representation ofgoogle.protobuf.Timestamp
(as shown below)?
Yes. Half of this is already possible today through a prost_build::Config::extern_paths(".google.protobuf.Timestamp", "std::time::SystemTime")
. The only piece that's missing is adding an impl Message for std::time::SystemTime
.
( I think, that's not tested) |
Any news here? I think it would be cool to have ability for converting between protobuf Timestamp and Chrono DateTime |
The resolution and representable range of |
I haven't looked in |
Right, but like I said, it varies by platform. macOS and 64-bit Linux use the same time representation as
|
I am going to close this PR since it seems like we have consensus that we don't want to include chrono as a dep for prost. I believe the systemtime conversion should work. |
My question might be naive, but why not just gate chrono under a feature flag? |
Totally valid question and I am not 100% sure if we don't want to include it. There are two reasons 1) the fields of timestamp are public so its quite easy to write your own custom conversion, 2) its more timestamp code to maintain. That said, I would be open to accepting this now I had a change of heart :) |
I would like to consider a bit what @danburkert said above as well before we introduce chrono. I am not convinced either way and need to spend sometime reading up on timestamps (super fun stuff). |
Great @LucioFranco, IMHO it's part of the additions that would make prost more convenient to work with. |
4cbfce9
to
ea4eb89
Compare
(Note that I've fixed the conflicts in case you'd want to merge this MR) |
ea4eb89
to
79ed1f7
Compare
I've thought about this a bit more and now believe this feature should be added to chrono behind a |
@LucioFranco I respectfully disagree with your judgement on this. I feel like best practice in the ecosystem is that higher-level crates (more complex API/functionality, more semver-breaking releases) should hold the integration with lower-level crates (simpler API/functionality, fewer breaking releases). In this case, it seems clear to me that prost is higher-level than chrono, so if the integration will live in either one of our crates it should be in the prost one IMO. |
As a user of both |
These changes enable idiomatic conversions between
chrono::DateTime
and the protobufTimestamp
.Such conversions were already provided with objects from
std::time
, but not with ones from thechrono
crate.Since it adds a dependency, this code is conditionally used, only if feature
chrono-conversions
is set.