Skip to content

Fix parsing timestamps with zone 'Z' #17

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

Merged
merged 2 commits into from
Apr 12, 2021
Merged
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
4 changes: 2 additions & 2 deletions api/src/main/java/dev/paseto/jpaseto/lang/DateFormats.java
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ private DateFormats() {}
public static final DateTimeFormatter ISO_OFFSET_DATE_TIME = new DateTimeFormatterBuilder()
.parseCaseInsensitive()
.append(DateTimeFormatter.ISO_LOCAL_DATE_TIME)
.appendOffset("+HH:MM", "+00:00")
.appendOffsetId() // append UTC designator "Z"
.toFormatter()
.withZone(ZoneOffset.UTC);

Expand All @@ -43,4 +43,4 @@ public static Instant parseIso8601Date(String s) throws DateTimeException {
Assert.notNull(s, "String argument cannot be null.");
return Instant.from(ISO_OFFSET_DATE_TIME.parse(s));
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -96,7 +96,7 @@ class JacksonSerializerTest {
@Test
void testDateTime() {
DateTimeFormatter dateTimeFormatter = DateFormats.ISO_OFFSET_DATE_TIME
def anInstantString = "2019-01-01T00:00:00+00:00"
def anInstantString = "2019-01-01T00:00:00Z"
def dateTime = Instant.from(dateTimeFormatter.parse(anInstantString))

def expected = '{"hello":"' + anInstantString + '"}'
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,54 @@
/*
* Copyright (C) 2015 jsonwebtoken.io
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package dev.paseto.jpaseto.impl

import dev.paseto.jpaseto.RequiredTypeException
import dev.paseto.jpaseto.lang.DateFormats
import org.hamcrest.MatcherAssert
import org.testng.annotations.Test

import java.time.Instant
import java.time.ZoneId
import java.time.ZonedDateTime

import static org.hamcrest.MatcherAssert.assertThat
import static org.hamcrest.Matchers.*

class DateFormatsTest {
@Test
void testParseIso8601String() {
def d = ZonedDateTime.of(2015, 1, 1, 12, 0, 0, 0, ZoneId.of("UTC")).toInstant()
String s = "2015-01-01T12:00:00+00:00"
Instant instant = DateFormats.parseIso8601Date(s)
assertThat instant, is(d)
}

@Test
void testParseIso8601ZString() {
def d = ZonedDateTime.of(2015, 1, 1, 12, 0, 0, 0, ZoneId.of("UTC")).toInstant()
String s = "2015-01-01T12:00:00Z"
Instant instant = DateFormats.parseIso8601Date(s)
assertThat instant, is(d)
}

@Test
void testFormatDateWithIso8601String() {
def d = ZonedDateTime.of(2015, 1, 1, 12, 0, 0, 0, ZoneId.of("UTC")).toInstant()
String exp = "2015-01-01T12:00:00Z"
String s = DateFormats.formatIso8601(d)
assertThat s, is(exp)
}
}