Skip to content

Commit

Permalink
fix: Always use . as decimal separator in PGInterval. (#1705)
Browse files Browse the repository at this point in the history
  • Loading branch information
Toddy69 committed Feb 14, 2020
1 parent 202f88e commit 94641ef
Show file tree
Hide file tree
Showing 2 changed files with 20 additions and 1 deletion.
6 changes: 5 additions & 1 deletion pgjdbc/src/main/java/org/postgresql/util/PGInterval.java
Expand Up @@ -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;
Expand Down Expand Up @@ -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",
Expand All @@ -251,7 +255,7 @@ public String getValue() {
days,
hours,
minutes,
new DecimalFormat("0.0#####").format(getSeconds())
df.format(getSeconds())
);
}

Expand Down
15 changes: 15 additions & 0 deletions pgjdbc/src/test/java/org/postgresql/test/jdbc2/IntervalTest.java
Expand Up @@ -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;
Expand Down Expand Up @@ -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");
Expand Down

0 comments on commit 94641ef

Please sign in to comment.