Skip to content

Commit

Permalink
Merge pull request #1127 from anto-ac/backward-compatibility-for-Z-UT…
Browse files Browse the repository at this point in the history
…C-designator

fix: backward compatible support for Z UTC designator in timestamps
  • Loading branch information
anto-ac authored Jun 16, 2020
2 parents d96859f + 7ae9e7f commit 64cb151
Show file tree
Hide file tree
Showing 2 changed files with 24 additions and 7 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -323,12 +323,21 @@ fun <M : Mismatch> matchDateTime(
return if (isCollection(actual)) {
emptyList()
} else {
var newPattern = pattern
try {
DateTimeFormatter.ofPattern(pattern).parse(safeToString(actual))
/**
Replacing 'Z' with 'X' in order to offer backward compatibility for consumers who might be
using ISO 8601 the UTC designator 'Z'
**/
if (pattern.endsWith('Z')) {
newPattern = pattern.replace('Z', 'X')
logger.warn { "Found unsupported UTC designator in pattern '$pattern'. Replacing non quote 'Z's with 'X's" }
}
DateTimeFormatter.ofPattern(newPattern).parse(safeToString(actual))
emptyList<M>()
} catch (e: DateTimeParseException) {
listOf(mismatchFactory.create(expected, actual,
"Expected ${valueOf(actual)} to match a datetime of '$pattern': " +
"Expected ${valueOf(actual)} to match a datetime of '$newPattern': " +
"${e.message}", path))
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -149,6 +149,14 @@ class MatcherExecutorSpec extends Specification {
'2014-01-01 14:00:00+10:00' | '2013#12#01#14#00#00' | "yyyy'#'MM'#'dd'#'HH'#'mm'#'ss" || true
'2014-01-01 14:00:00+10:00' | null | null || false
'2014-01-01T10:00+10:00[Australia/Melbourne]' | '2020-01-01T10:00+01:00[Europe/Warsaw]' | "yyyy-MM-dd'T'HH:mmXXX'['zzz']'" || true
'2019-11-25T13:45:00+02:00' | '2019-11-25T11:45:00Z' | "yyyy-MM-dd'T'HH:mm:ssX" || true
'2019-11-25T13:45:00+02:00' | '2019-11-25T11:45:00Z' | "yyyy-MM-dd'T'HH:mm:ssZZ" || true
'2019-11-25T13:45:00+02:00' | '2019-11-25T11:45Z' | "yyyy-MM-dd'T'HH:mmZZ" || true
'2019-11-25T13:45:00+02:00' | '2019-11-25T11Z' | "yyyy-MM-dd'T'HHZZ" || true
'2019-11-25T13:45:00+0200' | '2019-11-25T11:45:00Z' | "yyyy-MM-dd'T'HH:mm:ssZ" || true
'2019-11-25T13:45:00+0200' | '2019-11-25T11:45Z' | "yyyy-MM-dd'T'HH:mmZ" || true
'2019-11-25T13:45:00+0200' | '2019-11-25T11Z' | "yyyy-MM-dd'T'HHZ" || true
'2019-11-25T13:45:00+0200' | '2019-11-25T11:45:00Z' | "yyyy-MM-dd'T'HH:mm:ss'Z'" || true

matcher = pattern ? new TimestampMatcher(pattern) : new TimestampMatcher()
}
Expand All @@ -159,11 +167,11 @@ class MatcherExecutorSpec extends Specification {
MatcherExecutorKt.domatch(matcher, path, expected, actual, mismatchFactory).empty == mustBeEmpty

where:
expected | actual | pattern || mustBeEmpty
'14:00:00' | '14:00:00' | null || true
'00:00' | '14:01:02' | 'mm:ss' || false
'00:00:14' | '05:10:14' | 'ss:mm:HH' || true
'14:00:00+10:00' | null | null || false
expected | actual | pattern || mustBeEmpty
'14:00:00' | '14:00:00' | null || true
'00:00' | '14:01:02' | 'mm:ss' || false
'00:00:14' | '05:10:14' | 'ss:mm:HH' || true
'14:00:00+10:00' | null | null || false

matcher = pattern ? new TimeMatcher(pattern) : new TimeMatcher()
}
Expand Down

0 comments on commit 64cb151

Please sign in to comment.