-
Notifications
You must be signed in to change notification settings - Fork 103
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
Using Instant with limited database precision #468
Comments
Hi!
Could you please explain where this requirement comes from? To me, this looks like a (tiny) waste of computations: if Google Cloud Datastore is already truncating the value for you, why do it in your code as well? Could you describe the business logic that mandates this? If the only reason you need this is to simplify testing, this can be done differently: val originalInstant = Clock.System.now()
val instantAfterRoundTrip: Instant = roundTripToCloudDatastore(originalInstant)
// assertEquals(originalInstant, instantAfterRoundTrip) // won't work due to precision loss
val difference = (instantAfterRoundTrip - originalInstant).absoluteValue
assertTrue(difference < 1.microseconds)
Not at the moment. It's not clear yet why this would be needed. As a workaround, you could do this: import kotlin.time.Duration.Companion.nanoseconds
fun Instant.truncatedToMicroseconds(): Instant =
this - (nanosecondsOfSecond % 1000).nanoseconds |
If you have the requirement to synchronize data, you need time values to be the same or to treat tiny differences as the same value. As you described, this requires manual adjustments each time you compare time values, which could be error prone. While the datastore values are truncated, incoming request from clients using Kotlin.Instant are not truncated by default. |
Could you provide some (pseudo)code that explains the problem? I've never worked on projects like the one you're describing, so it's difficult for me to imagine why your code needs to compare |
Hi,
I'm trying to use the Instant type with the widely used Google Cloud Datastore. Its corresponding type is Timestamp, which offers the same precision. Storing and retrieving Timestamps though limits precision to microseconds instead of nanoseconds. While I don't need nanoseconds precision I require the values stored and retrieved to be the same.
I wonder if this library provides a solution to this or if it is up to the user.
The best way is probably to limit the precision when receiving the Instant value. This would mean handling Clock precision and modifying the precision of all received values via APIs. However you probably would also have to limit the precision in all your client apps. This adds a lot of complexity.
Alternatively you should store Instant values as Strings if the native time value is of less precision.
What do you think?
The text was updated successfully, but these errors were encountered: