Skip to content

Commit

Permalink
Be stricter about the value to pass to java.sql.Date
Browse files Browse the repository at this point in the history
  • Loading branch information
beikov committed Apr 29, 2024
1 parent 9342b8b commit b833ea1
Showing 1 changed file with 11 additions and 7 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -145,7 +145,7 @@ public Object unwrap(Date value, Class type, WrapperOptions options) {
}

if ( java.sql.Timestamp.class.isAssignableFrom( type ) ) {
return new java.sql.Timestamp( value.getTime() );
return new java.sql.Timestamp( unwrapDateEpoch( value ) );
}

if ( java.sql.Time.class.isAssignableFrom( type ) ) {
Expand All @@ -158,18 +158,22 @@ public Object unwrap(Date value, Class type, WrapperOptions options) {
private LocalDate unwrapLocalDate(Date value) {
return value instanceof java.sql.Date
? ( (java.sql.Date) value ).toLocalDate()
: new java.sql.Date( value.getTime() ).toLocalDate();
: new java.sql.Date( unwrapDateEpoch( value ) ).toLocalDate();
}

private java.sql.Date unwrapSqlDate(Date value) {
return value instanceof java.sql.Date
? (java.sql.Date) value
: new java.sql.Date( value.getTime() );
: new java.sql.Date( unwrapDateEpoch( value ) );

}

private static long unwrapDateEpoch(Date value) {
return value.getTime();
return toDateEpoch( value.getTime() );
}

private static long toDateEpoch(long value) {
return value - ( value % 100000 );
}

@Override
Expand All @@ -183,15 +187,15 @@ public Date wrap(Object value, WrapperOptions options) {
}

if ( value instanceof Long ) {
return new java.sql.Date( (Long) value );
return new java.sql.Date( toDateEpoch( (Long) value ) );
}

if ( value instanceof Calendar ) {
return new java.sql.Date( ( (Calendar) value ).getTimeInMillis() );
return new java.sql.Date( toDateEpoch( ( (Calendar) value ).getTimeInMillis() ) );
}

if ( value instanceof Date ) {
return new java.sql.Date( ( (Date) value ).getTime() );
return unwrapSqlDate( (Date) value );
}

if ( value instanceof LocalDate ) {
Expand Down

0 comments on commit b833ea1

Please sign in to comment.