Skip to content

Commit

Permalink
Allow specifying timezone for date/time string conversions (#174);
Browse files Browse the repository at this point in the history
document pre-existing behavior in Javadoc for Defaults.dateFormat
  • Loading branch information
Maia Everett committed Nov 5, 2014
1 parent 648f1e7 commit fa60743
Show file tree
Hide file tree
Showing 2 changed files with 81 additions and 4 deletions.
Expand Up @@ -324,31 +324,50 @@ public Date decode(JSONValue value) throws DecodingException {
if (value == null || value.isNull() != null) {
return null;
}

String format = Defaults.getDateFormat();

if (format == null) {
JSONNumber num = value.isNumber();
if (num == null) {
throw new DecodingException("Expected a json number, but was given: " + value);
}
return new Date((long) num.doubleValue());
}

JSONString str = value.isString();
if (str == null) {
throw new DecodingException("Expected a json string, but was given: " + value);
}
return DateTimeFormat.getFormat(format).parse(str.stringValue());

if (Defaults.getTimeZone() == null || Defaults.dateFormatHasTimeZone()) {
return DateTimeFormat.getFormat(format).parse(str.stringValue());
} else {
// We need to provide time zone information to the GWT date parser.
// Unfortunately, DateTimeFormat has no overload specifying a TimeZone,
// so the only way is to extend the format string.
return DateTimeFormat.getFormat(format + " v").parse(
str.stringValue() + " " + Defaults.getTimeZone().getID());
}
}

@Override
public JSONValue encode(Date value) throws EncodingException {
if (value == null) {
return getNullType();
}

String format = Defaults.getDateFormat();

if (format == null) {
return new JSONNumber(value.getTime());
}
return new JSONString(DateTimeFormat.getFormat(format).format(value));

if (Defaults.getTimeZone() == null || Defaults.dateFormatHasTimeZone()) {
return new JSONString(DateTimeFormat.getFormat(format).format(value));
} else {
return new JSONString(DateTimeFormat.getFormat(format).format(value, Defaults.getTimeZone()));
}
}
};

Expand Down
Expand Up @@ -22,6 +22,7 @@
import org.fusesource.restygwt.client.dispatcher.DefaultDispatcher;

import com.google.gwt.core.client.GWT;
import com.google.gwt.i18n.shared.TimeZone;

/**
* Provides ability to set the default date format and service root (defaults to
Expand All @@ -37,6 +38,8 @@ public class Defaults {

private static String serviceRoot = GWT.getModuleBaseURL();
private static String dateFormat = "yyyy-MM-dd'T'HH:mm:ss.SSSZ";
private static boolean dateFormatHasTimeZone = true;
private static TimeZone timeZone = null;
private static boolean ignoreJsonNulls = false;
// patch TNY: timeout ms,
// if >-1, used in Method class to set timeout
Expand All @@ -61,17 +64,72 @@ public static void setServiceRoot(String serviceRoot) {
Defaults.serviceRoot = serviceRoot;
}

/**
* Gets the format used when encoding and decoding Dates. Defaults to
* {@code "yyyy-MM-dd'T'HH:mm:ss.SSSZ"}. If the date format is set to
* {@code null}, dates will be encoded and decoded as timestamps
* (the number of milliseconds since the Unix epoch, 1970-01-01).
*
* @return the date format string
* @see com.google.gwt.i18n.shared.DateTimeFormat DateTimeFormat
*/
public static String getDateFormat() {
return dateFormat;
}

/**
* Sets the format used when encoding and decoding Dates.
* Sets the format used when encoding and decoding Dates. If the date
* format is set to {@code null}, dates will be encoded and decoded as
* timestamps (the number of milliseconds since the Unix epoch,
* 1970-01-01).
*
* @param dateFormat
* @param dateFormat the date format string
* @see com.google.gwt.i18n.shared.DateTimeFormat DateTimeFormat
*/
public static void setDateFormat(String dateFormat) {
Defaults.dateFormat = dateFormat;
dateFormatHasTimeZone = false;

if (dateFormat != null) {
for (int i = 0; i < dateFormat.length(); i++) {
char ch = dateFormat.charAt(i);

if (ch == 'Z' || ch == 'z' || ch == 'V' || ch == 'v') {
dateFormatHasTimeZone = true;
break;
}
}
}
}

/* package */ static boolean dateFormatHasTimeZone() {
return dateFormatHasTimeZone;
}

/**
* Gets the timezone used when encoding and decoding Dates.
* <p>
* The timezone is only taken into consideration if the date format string
* does not contain a timezone field. If the timezone is set to
* {@code null}, the browser's default (local) timezone will be used.
*
* @return the date format timezone (null for local timezone)
*/
public static TimeZone getTimeZone() {
return timeZone;
}

/**
* Gets the timezone used when encoding and decoding Dates.
* <p>
* The timezone is only taken into consideration if the date format string
* does not contain a timezone field. If the timezone is set to null, the
* browser's default (local) timezone will be used.
*
* @param timeZone the new timezone (use null for local timezone)
*/
public static void setDateFormat(TimeZone timeZone) {
Defaults.timeZone = timeZone;
}

/**
Expand Down

0 comments on commit fa60743

Please sign in to comment.