postgresql-jdbc.git / summary / log / commit / refs

commit    94641ef08922d77ac021f70af91cafe85866d854
Author:   Toddy69 <3174167+Toddy69@users.noreply.github.com>
Commit:   GitHub <noreply@github.com>
Date:     Fri Feb 14 00:54:25 2020 +0000

    fix: Always use . as decimal separator in PGInterval. (#1705)


pgjdbc/src/main/java/org/postgresql/util/PGInterval.java | 6 +++++- .../test/java/org/postgresql/test/jdbc2/IntervalTest.java | 15 +++++++++++++++ 2 files changed, 20 insertions(+), 1 deletion(-) diff --git a/pgjdbc/src/main/java/org/postgresql/util/PGInterval.java b/pgjdbc/src/main/java/org/postgresql/util/PGInterval.java index c438dd2c..7df831f3 100644 --- a/pgjdbc/src/main/java/org/postgresql/util/PGInterval.java +++ b/pgjdbc/src/main/java/org/postgresql/util/PGInterval.java @@ -8,6 +8,7 @@ package org.postgresql.util; 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 class PGInterval extends PGobject implements Serializable, Cloneable { * @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 class PGInterval extends PGobject implements Serializable, Cloneable { days, hours, minutes, - new DecimalFormat("0.0#####").format(getSeconds()) + df.format(getSeconds()) ); } diff --git a/pgjdbc/src/test/java/org/postgresql/test/jdbc2/IntervalTest.java b/pgjdbc/src/test/java/org/postgresql/test/jdbc2/IntervalTest.java index 13e3a794..78c27946 100644 --- a/pgjdbc/src/test/java/org/postgresql/test/jdbc2/IntervalTest.java +++ b/pgjdbc/src/test/java/org/postgresql/test/jdbc2/IntervalTest.java @@ -25,6 +25,7 @@ import java.sql.Statement; 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 class IntervalTest { 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"); [parent: 202f88e13c11]