Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Nepali date fix #1440

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
package org.javarosa.core.model.utils;

import java.util.Calendar;
import java.util.TimeZone;

public class DefaultTimeZoneProvider extends TimezoneProvider {
@Override
public int getTimezoneOffsetMillis() {
return Calendar.getInstance().getTimeZone().getRawOffset();
}

@Override
public TimeZone getTimezone() {
return Calendar.getInstance().getTimeZone();
}
}
70 changes: 67 additions & 3 deletions src/test/java/org/javarosa/xform/util/test/CalendarTests.java
Original file line number Diff line number Diff line change
@@ -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;
Expand All @@ -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());
Expand All @@ -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);
Expand Down Expand Up @@ -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);
}
Expand Down Expand Up @@ -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;
}
}
}
Loading