Merge pull request #357 from jwtk/291-sdf-utc

Fix claim assertion exception message to reflect UTC timestamps
This commit is contained in:
Les Hazlewood 2018-07-20 19:58:58 -04:00 committed by GitHub
commit ac6703541e
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 32 additions and 2 deletions

View File

@ -4,6 +4,7 @@ import java.text.DateFormat;
import java.text.ParseException; import java.text.ParseException;
import java.text.SimpleDateFormat; import java.text.SimpleDateFormat;
import java.util.Date; import java.util.Date;
import java.util.TimeZone;
/** /**
* @since 0.10.0 * @since 0.10.0
@ -17,14 +18,18 @@ public class DateFormats {
private static final ThreadLocal<DateFormat> ISO_8601 = new ThreadLocal<DateFormat>() { private static final ThreadLocal<DateFormat> ISO_8601 = new ThreadLocal<DateFormat>() {
@Override @Override
protected DateFormat initialValue() { protected DateFormat initialValue() {
return new SimpleDateFormat(ISO_8601_PATTERN); SimpleDateFormat format = new SimpleDateFormat(ISO_8601_PATTERN);
format.setTimeZone(TimeZone.getTimeZone("UTC"));
return format;
} }
}; };
private static final ThreadLocal<DateFormat> ISO_8601_MILLIS = new ThreadLocal<DateFormat>() { private static final ThreadLocal<DateFormat> ISO_8601_MILLIS = new ThreadLocal<DateFormat>() {
@Override @Override
protected DateFormat initialValue() { protected DateFormat initialValue() {
return new SimpleDateFormat(ISO_8601_MILLIS_PATTERN); SimpleDateFormat format = new SimpleDateFormat(ISO_8601_MILLIS_PATTERN);
format.setTimeZone(TimeZone.getTimeZone("UTC"));
return format;
} }
}; };

View File

@ -0,0 +1,25 @@
package io.jsonwebtoken.lang
import org.junit.Test
import java.text.SimpleDateFormat
import static org.junit.Assert.*
class DateFormatsTest {
@Test //https://github.com/jwtk/jjwt/issues/291
void testUtcTimezone() {
def iso8601 = DateFormats.ISO_8601.get()
def iso8601Millis = DateFormats.ISO_8601_MILLIS.get()
assertTrue iso8601 instanceof SimpleDateFormat
assertTrue iso8601Millis instanceof SimpleDateFormat
def utc = TimeZone.getTimeZone("UTC")
assertEquals utc, iso8601.getTimeZone()
assertEquals utc, iso8601Millis.getTimeZone()
}
}