Skip to content
Permalink
Browse files
fix: Always use . as decimal separator in PGInterval. (#1705)
  • Loading branch information
Toddy69 committed Feb 14, 2020
1 parent 202f88e commit 94641ef08922d77ac021f70af91cafe85866d854
Showing 2 changed files with 20 additions and 1 deletion.
@@ -8,6 +8,7 @@
import java.io.Serializable;
import java.sql.SQLException;
import java.text.DecimalFormat;
import java.text.NumberFormat;
import java.util.Calendar;
import java.util.Date;
import java.util.Locale;
@@ -243,6 +244,9 @@ public void setValue(int years, int months, int days, int hours, int minutes, do
* @return String represented interval
*/
public String getValue() {
DecimalFormat df = (DecimalFormat) NumberFormat.getInstance(Locale.US);
df.applyPattern("0.0#####");

return String.format(
Locale.ROOT,
"%d years %d mons %d days %d hours %d mins %s secs",
@@ -251,7 +255,7 @@ public String getValue() {
days,
hours,
minutes,
new DecimalFormat("0.0#####").format(getSeconds())
df.format(getSeconds())
);
}

@@ -25,6 +25,7 @@
import java.util.Calendar;
import java.util.Date;
import java.util.GregorianCalendar;
import java.util.Locale;

public class IntervalTest {
private Connection conn;
@@ -336,6 +337,20 @@ public void testGetValueForSmallValue() throws SQLException {
assertEquals(orig, copy);
}

@Test
public void testGetValueForSmallValueWithCommaAsDecimalSeparatorInDefaultLocale() throws SQLException {
Locale originalLocale = Locale.getDefault();
Locale.setDefault(Locale.GERMANY);
try {
PGInterval orig = new PGInterval("0.0001 seconds");
PGInterval copy = new PGInterval(orig.getValue());

assertEquals(orig, copy);
} finally {
Locale.setDefault(originalLocale);
}
}

@Test
public void testGetSecondsForSmallValue() throws SQLException {
PGInterval pgi = new PGInterval("0.000001 seconds");

0 comments on commit 94641ef

Please sign in to comment.