diff --git a/src/test/java/org/javarosa/xform/util/test/CalendarTests.java b/src/test/java/org/javarosa/xform/util/test/CalendarTests.java index 5f92ad89ca..48e5070b29 100644 --- a/src/test/java/org/javarosa/xform/util/test/CalendarTests.java +++ b/src/test/java/org/javarosa/xform/util/test/CalendarTests.java @@ -1,5 +1,9 @@ package org.javarosa.xform.util.test; +import org.javarosa.core.model.utils.DateUtils; +import org.javarosa.core.model.utils.TimezoneProvider; +import org.javarosa.core.services.locale.Localization; +import org.javarosa.core.services.locale.TableLocaleSource; import org.javarosa.xform.util.CalendarUtils; import org.javarosa.xform.util.UniversalDate; import org.joda.time.DateTimeZone; @@ -25,8 +29,10 @@ public void testTimesFallOnSameDate() { Calendar nepaliBeginningOfDayDate = Calendar.getInstance(nepaliTimeZone.toTimeZone()); nepaliBeginningOfDayDate.set(2007, Calendar.JULY, 7, 0, 0); - UniversalDate middleOfDay = CalendarUtils.fromMillis(nepaliMiddleOfDayDate.getTimeInMillis(), nepaliTimeZone); - UniversalDate beginningOfDay = CalendarUtils.fromMillis(nepaliBeginningOfDayDate.getTimeInMillis(), nepaliTimeZone); + UniversalDate middleOfDay = CalendarUtils.fromMillis(nepaliMiddleOfDayDate.getTimeInMillis(), + nepaliTimeZone); + UniversalDate beginningOfDay = CalendarUtils.fromMillis(nepaliBeginningOfDayDate.getTimeInMillis(), + nepaliTimeZone); assertSameDate(middleOfDay, beginningOfDay); Calendar nepaliEndOfDayDate = Calendar.getInstance(nepaliTimeZone.toTimeZone()); @@ -35,6 +41,45 @@ public void testTimesFallOnSameDate() { assertSameDate(endOfDay, beginningOfDay); } + // millis <=> date is different in every timezone + @Test + public void testConvertToNepaliString() { + configureLocaleForCalendar(); + MockTimeZoneProvider mockTimeZoneProvider = new MockTimeZoneProvider(TimeZone.getTimeZone("Europe/Madrid")); + DateUtils.setTimezoneProvider(mockTimeZoneProvider); + DateTimeZone timeZone = DateTimeZone.forOffsetMillis(mockTimeZoneProvider.getTimezone().getRawOffset()); + // this is what Nepali widget uses to calculate the millis from date fields + long millis = CalendarUtils.toMillisFromJavaEpoch(2081, 6, 16, timeZone); + String nepaliDateStr = CalendarUtils.convertToNepaliString(new Date(millis), null); + assertEquals( "16 Ashwin 2081", nepaliDateStr); + + + mockTimeZoneProvider.setTimezone(TimeZone.getTimeZone("Asia/Kathmandu")); + timeZone = DateTimeZone.forOffsetMillis(mockTimeZoneProvider.getTimezone().getRawOffset()); + millis = CalendarUtils.toMillisFromJavaEpoch(2081, 6, 16, timeZone); + nepaliDateStr = CalendarUtils.convertToNepaliString(new Date(millis), null); + assertEquals( "16 Ashwin 2081", nepaliDateStr); + + + mockTimeZoneProvider.setTimezone(TimeZone.getTimeZone("America/Chicago")); + timeZone = DateTimeZone.forOffsetMillis(mockTimeZoneProvider.getTimezone().getRawOffset()); + millis = CalendarUtils.toMillisFromJavaEpoch(2081, 6, 16, timeZone); + nepaliDateStr = CalendarUtils.convertToNepaliString(new Date(millis), null); + assertEquals( "16 Ashwin 2081", nepaliDateStr); + DateUtils.setTimezoneProvider(null); + } + + private void configureLocaleForCalendar() { + Localization.getGlobalLocalizerAdvanced().addAvailableLocale("default"); + Localization.setLocale("default"); + TableLocaleSource localeData = new TableLocaleSource(); + localeData.setLocaleMapping("ethiopian_months", + "Mäskäräm,T’ïk’ïmt,Hïdar,Tahsas,T’ïr,Yäkatit,Mägabit,Miyaziya,Gïnbot,Säne,Hämle,Nähäse,P’agume"); + localeData.setLocaleMapping("nepali_months", + "Baishakh,Jestha,Ashadh,Shrawan,Bhadra,Ashwin,Kartik,Mangsir,Poush,Magh,Falgun,Chaitra"); + Localization.getGlobalLocalizerAdvanced().registerLocaleResource("default", localeData); + } + private static void assertSameDate(UniversalDate a, UniversalDate b) { assertEquals(a.day, b.day); assertEquals(a.month, b.month); @@ -63,7 +108,8 @@ public void testUnpackingDateInDifferentTimezone() { mexicoCal.set(2007, Calendar.JULY, 7, 18, 46); UniversalDate mexicanDate = CalendarUtils.fromMillis(mexicoCal.getTimeInMillis(), mexicanTimeZone); - long time = CalendarUtils.toMillisFromJavaEpoch(mexicanDate.year, mexicanDate.month, mexicanDate.day, mexicanTimeZone); + long time = CalendarUtils.toMillisFromJavaEpoch(mexicanDate.year, mexicanDate.month, mexicanDate.day, + mexicanTimeZone); UniversalDate rebuiltDateInUsingDifferentTimezone = CalendarUtils.fromMillis(time, nepaliTimeZone); assertSameDate(rebuiltDateInUsingDifferentTimezone, mexicanDate); } @@ -103,4 +149,22 @@ public void serializeUniversalDateViaMillisTest() { deserializedNepaliDate = CalendarUtils.fromMillis(date.getTime(), bostonTimeZone); assertSameDate(nepaliDate, deserializedNepaliDate); } + + private class MockTimeZoneProvider extends TimezoneProvider { + + private TimeZone timeZone; + + public MockTimeZoneProvider(TimeZone timeZone) { + this.timeZone = timeZone; + } + + public void setTimezone(TimeZone timeZone){ + this.timeZone = timeZone; + } + + @Override + public TimeZone getTimezone() { + return timeZone; + } + } }