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
Reasonable support for java.time.Duration
-> Interval conversion
#1281
Labels
Milestone
Comments
Here - this would make a fine addition to Interval, and should not have accuracy problems: static Interval fromDuration(java.time.Duration duration) {
long seconds = duration.toSeconds();
long minutes = seconds / 60;
seconds -= minutes * 60;
long hours = minutes / 60;
minutes -= hours * 60;
long days = hours / 24;
hours -= days * 24;
double micro = duration.getNano() / 1000D;
return new Interval(0, 0, (int) days, (int) hours, (int) minutes, (int) seconds, (int) round(micro));
} And optionally, in the other direction, though months and years are necessarily fuzzy quantities (could return an object that indicates if the conversion is inexact, or an public Duration toDuration(Interval ival) {
return java.time.Duration.ofDays(365 * getYears())
.plus(Duration.ofSeconds((long) (getMonths() * 2.628e+6)))
.plus(Duration.ofDays(getDays()))
.plus(Duration.ofHours(getHours()))
.plus(Duration.ofMinutes(getMinutes()))
.plus(Duration.ofSeconds(getSeconds()))
.plus(Duration.ofNanos(1000L * getMicroseconds()));
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Describe the feature
Working with the standard
java.time.Duration
class in postgres is unnecessarily painful.Use cases
I ran into this issue with a simple query that invokes a stored procedure:
where the code supplying the
interval
is handed ajava.time.Duration
. Having done some testing inpsql
, Postgres supports ISO-8601 duration strings perfectly well in a query - i.e.P1Y::interval
gets you an interval of a year. Great! So, I try to passTuple.of(duration.toString(), ...)
.vertx-pg-client helpfully (and silently!) replaces the duration with
NULL
. Very surprising when my queries start returning tens of thousands of result.Tried a few incantations of
::varchar
and similar trying to head-fake vertx-pg-client into doing the right thing. No luck.Solutions:
Tuple
should really just accept ajava.time.Duration
and convert it to anInterval
(or whatever) under the hoodDuration
toInterval
in a constructor, so if none of the above is acceptable, there is at least a low-complexity way of doing that (could be as simple as creating an interval with the number of seconds and microseconds in the duration - though it would be more polite to anyone reading queries from the database log to extract those elements that don't have varying lengths - i.e. days, hours, minutes, seconds, microseconds)Contribution
I might consider it.
Quick'n'dirty, unpretty code to do the job with some necessary inaccuracy around years and months (but good enough for my current purposes):
The text was updated successfully, but these errors were encountered: