Skip to content

Commit

Permalink
Added support for TIME datatype to display up to 6 digits of fraction…
Browse files Browse the repository at this point in the history
…al second data
  • Loading branch information
bhvkshah committed Feb 12, 2024
1 parent bc73261 commit e03b483
Show file tree
Hide file tree
Showing 2 changed files with 43 additions and 2 deletions.
6 changes: 4 additions & 2 deletions src/main/java/com/amazon/redshift/jdbc/TimestampUtils.java
Original file line number Diff line number Diff line change
Expand Up @@ -620,11 +620,13 @@ public synchronized Time toTime(Calendar cal, String s) throws SQLException {
long timeMillis = useCal.getTimeInMillis() + ts.nanos / 1000000;
if (ts.tz != null || (ts.year == 1970 && ts.era == GregorianCalendar.AD)) {
// time with time zone has proper time zone, so the value can be returned as is
return new Time(timeMillis);
Time timeObj = new Time(timeMillis);
return (ts.nanos > 0) ? new RedshiftTime(timeObj, ts.nanos) : timeObj;
}

// 2) Truncate date part so in given time zone the date would be formatted as 01/01/1970
return convertToTime(timeMillis, useCal.getTimeZone());
Time timeObj = convertToTime(timeMillis, useCal.getTimeZone());
return (ts.nanos > 0) ? new RedshiftTime(timeObj, ts.nanos) : timeObj;
}

public synchronized Date toDate(Calendar cal, String s) throws SQLException {
Expand Down
39 changes: 39 additions & 0 deletions src/main/java/com/amazon/redshift/util/RedshiftTime.java
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
import java.sql.PreparedStatement;
import java.sql.Time;
import java.util.Calendar;
import java.text.SimpleDateFormat;

/**
* This class augments the Java built-in Time to allow for explicit setting of the time zone.
Expand Down Expand Up @@ -139,4 +140,42 @@ public Object clone() {
}
return clone;
}

/**
* Threadsafe access to ready to use time formatter
*/
public static ThreadLocal<SimpleDateFormat> TIME_FORMAT =
new ThreadLocal<SimpleDateFormat>()
{
@Override
protected SimpleDateFormat initialValue()
{
return new SimpleDateFormat("HH:mm:ss");
}
};


/**
* Override default to string method to handle nanoSeconds
*/
@Override
public String toString()
{
StringBuilder baseResult = new StringBuilder();
baseResult.append(TIME_FORMAT.get().format(this.getTime()));

// TIME columns store values with up to a maximum of 6 digits of precision for fractional seconds.
// If TIME has more than 6 digits, trim down to 6 digits from the end of TIME.
if (0 < nanos)
{
while (999999 < nanos)
{
nanos /= 10;
}
baseResult.append(".");
baseResult.append(String.valueOf(nanos));
}

return baseResult.toString();
}
}

0 comments on commit e03b483

Please sign in to comment.