Skip to content

Commit

Permalink
Merge pull request #942 from dimagi/ctsims/time_question_tz_suppress
Browse files Browse the repository at this point in the history
Suppress TimeZone info on TimeData questions
  • Loading branch information
ctsims authored Oct 28, 2020
2 parents e090150 + 5abf660 commit a036bf5
Show file tree
Hide file tree
Showing 4 changed files with 25 additions and 4 deletions.
4 changes: 2 additions & 2 deletions src/main/java/org/javarosa/core/model/data/TimeData.java
Original file line number Diff line number Diff line change
Expand Up @@ -77,12 +77,12 @@ public void writeExternal(DataOutputStream out) throws IOException {

@Override
public UncastData uncast() {
return new UncastData(DateUtils.formatTime(d, DateUtils.FORMAT_ISO8601));
return new UncastData(DateUtils.formatTime(d, DateUtils.FORMAT_ISO8601_WALL_TIME));
}

@Override
public TimeData cast(UncastData data) throws IllegalArgumentException {
Date ret = DateUtils.parseTime(data.value);
Date ret = DateUtils.parseTime(data.value, true);
if (ret != null) {
return new TimeData(ret);
}
Expand Down
16 changes: 15 additions & 1 deletion src/main/java/org/javarosa/core/model/utils/DateUtils.java
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ public class DateUtils {
private static final int MONTH_OFFSET = (1 - Calendar.JANUARY);

public static final int FORMAT_ISO8601 = 1;
public static final int FORMAT_ISO8601_WALL_TIME = 10;
public static final int FORMAT_HUMAN_READABLE_SHORT = 2;
public static final int FORMAT_HUMAN_READABLE_DAYS_FROM_TODAY = 5;
public static final int FORMAT_TIMESTAMP_SUFFIX = 7;
Expand Down Expand Up @@ -296,6 +297,8 @@ private static String formatTime(DateFields f, int format) {
switch (format) {
case FORMAT_ISO8601:
return formatTimeISO8601(f);
case FORMAT_ISO8601_WALL_TIME:
return formatTimeISO8601(f, true);
case FORMAT_HUMAN_READABLE_SHORT:
return formatTimeColloquial(f);
case FORMAT_TIMESTAMP_SUFFIX:
Expand Down Expand Up @@ -342,7 +345,14 @@ private static String formatDateSuffix(DateFields f) {
}

private static String formatTimeISO8601(DateFields f) {
return formatTimeISO8601(f, false);
}

private static String formatTimeISO8601(DateFields f, boolean suppressTimezone) {
String time = intPad(f.hour, 2) + ":" + intPad(f.minute, 2) + ":" + intPad(f.second, 2) + "." + intPad(f.secTicks, 3);
if (suppressTimezone) {
return time;
}

int offset;
if (timezoneOffset() != -1) {
Expand Down Expand Up @@ -499,7 +509,11 @@ private static boolean parseDateAndStore(String dateStr, DateFields df) {
}

public static Date parseTime(String str) {
if (timezoneOffset() != -1 && !str.contains("+") && !str.contains("-") && !str.contains("Z")) {
return parseTime(str, false);
}

public static Date parseTime(String str, boolean ignoreTimezone) {
if (!ignoreTimezone && (timezoneOffset() != -1 && !str.contains("+") && !str.contains("-") && !str.contains("Z"))) {
str = str + getOffsetInStandardFormat(timezoneOffset());
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -105,7 +105,7 @@ public Object serializeAnswerData(DateTimeData data) {
* formatting
*/
public Object serializeAnswerData(TimeData data) {
return DateUtils.formatTime((Date)data.getValue(), DateUtils.FORMAT_ISO8601);
return DateUtils.formatTime((Date)data.getValue(), DateUtils.FORMAT_ISO8601_WALL_TIME);
}

/**
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package org.javarosa.core.model.data.test;

import org.javarosa.core.model.data.TimeData;
import org.javarosa.core.model.data.UncastData;
import org.junit.BeforeClass;
import org.junit.Test;

Expand Down Expand Up @@ -66,4 +67,10 @@ public void testNullData() {
assertTrue("TimeData failed to throw an exception when setting null data", exceptionThrown);
assertTrue("TimeData overwrote existing value on incorrect input", data.getValue().equals(now));
}

@Test
public void testIdentity() {
TimeData data = new TimeData().cast(new UncastData("10:00"));
assertEquals("10:00", data.getDisplayText());
}
}

0 comments on commit a036bf5

Please sign in to comment.