From 770216f7ffdf6f9e49797905abef00df9d55512f Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Lubo=C5=A1=20Lu=C5=88=C3=A1k?= Date: Mon, 18 Sep 2017 22:33:00 +0200 Subject: [PATCH 1/3] force US locale for tests --- .../isoron/uhabits/core/utils/DateUtils.java | 21 +++++++++++++++---- .../uhabits/core/utils/DateUtilsTest.java | 8 +++++++ 2 files changed, 25 insertions(+), 4 deletions(-) diff --git a/uhabits-core/src/main/java/org/isoron/uhabits/core/utils/DateUtils.java b/uhabits-core/src/main/java/org/isoron/uhabits/core/utils/DateUtils.java index 9d2a3f476..59ae5c6df 100644 --- a/uhabits-core/src/main/java/org/isoron/uhabits/core/utils/DateUtils.java +++ b/uhabits-core/src/main/java/org/isoron/uhabits/core/utils/DateUtils.java @@ -34,6 +34,8 @@ public abstract class DateUtils private static TimeZone fixedTimeZone = null; + private static Locale fixedLocale = null; + /** * Time of the day when the new day starts. */ @@ -58,7 +60,7 @@ public abstract class DateUtils public static String formatHeaderDate(GregorianCalendar day) { - Locale locale = Locale.getDefault(); + Locale locale = getLocale(); String dayOfMonth = Integer.toString(day.get(DAY_OF_MONTH)); String dayOfWeek = day.getDisplayName(DAY_OF_WEEK, SHORT, locale); return dayOfWeek + "\n" + dayOfMonth; @@ -82,7 +84,7 @@ public abstract class DateUtils for (int i = 0; i < wdays.length; i++) { wdays[i] = - day.getDisplayName(DAY_OF_WEEK, format, Locale.getDefault()); + day.getDisplayName(DAY_OF_WEEK, format, getLocale()); day.add(DAY_OF_MONTH, 1); } @@ -100,7 +102,7 @@ public abstract class DateUtils /** * @return array with weekday names starting according to locale settings, - * e.g. [Mo,Di,Mi,Do,Fr,Sa,So] in Europe + * e.g. [Mo,Di,Mi,Do,Fr,Sa,So] in Germany */ public static String[] getLocaleDayNames(int format) { @@ -111,7 +113,7 @@ public abstract class DateUtils for (int i = 0; i < days.length; i++) { days[i] = calendar.getDisplayName(DAY_OF_WEEK, format, - Locale.getDefault()); + getLocale()); calendar.add(DAY_OF_MONTH, 1); } @@ -195,6 +197,17 @@ public abstract class DateUtils fixedLocalTime = timestamp; } + public static void setFixedLocale(Locale locale) + { + fixedLocale = locale; + } + + private static Locale getLocale() + { + if(fixedLocale != null) return fixedLocale; + return Locale.getDefault(); + } + public static Long truncate(TruncateField field, long timestamp) { GregorianCalendar cal = DateUtils.getCalendar(timestamp); diff --git a/uhabits-core/src/test/java/org/isoron/uhabits/core/utils/DateUtilsTest.java b/uhabits-core/src/test/java/org/isoron/uhabits/core/utils/DateUtilsTest.java index 57358649a..ca6a3e73a 100644 --- a/uhabits-core/src/test/java/org/isoron/uhabits/core/utils/DateUtilsTest.java +++ b/uhabits-core/src/test/java/org/isoron/uhabits/core/utils/DateUtilsTest.java @@ -31,6 +31,14 @@ import static org.hamcrest.MatcherAssert.*; public class DateUtilsTest extends BaseUnitTest { + @Before + @Override + public void setUp() throws Exception + { + super.setUp(); + DateUtils.setFixedLocale(Locale.US); + } + @Test public void testFormatHeaderDate() { From 3fa9be2ffb6492b051a9a6f7386a7a858ef7b031 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Lubo=C5=A1=20Lu=C5=88=C3=A1k?= Date: Mon, 18 Sep 2017 22:44:58 +0200 Subject: [PATCH 2/3] fix csv export with locales that do not use dot as decimal separator --- .../src/main/java/org/isoron/uhabits/core/models/ScoreList.java | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/uhabits-core/src/main/java/org/isoron/uhabits/core/models/ScoreList.java b/uhabits-core/src/main/java/org/isoron/uhabits/core/models/ScoreList.java index 43959c11c..b3ba68370 100644 --- a/uhabits-core/src/main/java/org/isoron/uhabits/core/models/ScoreList.java +++ b/uhabits-core/src/main/java/org/isoron/uhabits/core/models/ScoreList.java @@ -172,7 +172,7 @@ public abstract class ScoreList implements Iterable for (Score s : this) { String timestamp = dateFormat.format(s.getTimestamp().getUnixTime()); - String score = String.format("%.4f", s.getValue()); + String score = String.format((Locale)null, "%.4f", s.getValue()); out.write(String.format("%s,%s\n", timestamp, score)); } } From 5d7ee8264b2ce107714a2794df3a2811625e6318 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Lubo=C5=A1=20Lu=C5=88=C3=A1k?= Date: Mon, 18 Sep 2017 23:13:53 +0200 Subject: [PATCH 3/3] fix testTruncate_dayOfWeek failing with some locales --- .../src/main/java/org/isoron/uhabits/core/utils/DateUtils.java | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/uhabits-core/src/main/java/org/isoron/uhabits/core/utils/DateUtils.java b/uhabits-core/src/main/java/org/isoron/uhabits/core/utils/DateUtils.java index 59ae5c6df..2771f3500 100644 --- a/uhabits-core/src/main/java/org/isoron/uhabits/core/utils/DateUtils.java +++ b/uhabits-core/src/main/java/org/isoron/uhabits/core/utils/DateUtils.java @@ -69,7 +69,7 @@ public abstract class DateUtils private static GregorianCalendar getCalendar(long timestamp) { GregorianCalendar day = - new GregorianCalendar(TimeZone.getTimeZone("GMT")); + new GregorianCalendar(TimeZone.getTimeZone("GMT"), getLocale()); day.setTimeInMillis(timestamp); return day; }