From 2f010b181d1e990beb6eaca3a134840b5622fbb1 Mon Sep 17 00:00:00 2001 From: alexbobp Date: Sat, 17 Jul 2021 07:46:43 -0500 Subject: [PATCH] expose PatternDateFormat regex string (#173) * expose PatternDateFormat regex string * comment matchingRegexString and add test case * extra-escape in the regex for javascript build target to work --- .../com/soywiz/klock/PatternDateFormat.kt | 11 +++++++--- .../kotlin/com/soywiz/klock/DateTimeTest.kt | 21 +++++++++++++++++++ 2 files changed, 29 insertions(+), 3 deletions(-) diff --git a/klock/src/commonMain/kotlin/com/soywiz/klock/PatternDateFormat.kt b/klock/src/commonMain/kotlin/com/soywiz/klock/PatternDateFormat.kt index cecbe97..2831781 100644 --- a/klock/src/commonMain/kotlin/com/soywiz/klock/PatternDateFormat.kt +++ b/klock/src/commonMain/kotlin/com/soywiz/klock/PatternDateFormat.kt @@ -104,8 +104,10 @@ data class PatternDateFormat @JvmOverloads constructor( } } - //val escapedFormat = Regex.escape(format) - internal val rx2: Regex = Regex("^" + regexChunks.mapIndexed { index, it -> + /** + * @return the regular expression string used for matching this format, able to be composed into another regex + */ + fun matchingRegexString() = regexChunks.mapIndexed { index, it -> if (options.optionalSupport) { val opens = openOffsets.getOrElse(index) { 0 } val closes = closeOffsets.getOrElse(index) { 0 } @@ -117,7 +119,10 @@ data class PatternDateFormat @JvmOverloads constructor( } else { it } - }.joinToString("") + "$") + }.joinToString("") + + //val escapedFormat = Regex.escape(format) + internal val rx2: Regex = Regex("^" + matchingRegexString() + "$") // EEE, dd MMM yyyy HH:mm:ss z -- > Sun, 06 Nov 1994 08:49:37 GMT diff --git a/klock/src/commonTest/kotlin/com/soywiz/klock/DateTimeTest.kt b/klock/src/commonTest/kotlin/com/soywiz/klock/DateTimeTest.kt index 91e48b4..faed8f5 100644 --- a/klock/src/commonTest/kotlin/com/soywiz/klock/DateTimeTest.kt +++ b/klock/src/commonTest/kotlin/com/soywiz/klock/DateTimeTest.kt @@ -475,4 +475,25 @@ class DateTimeTest { assertEquals(TimezoneOffset((-15).hours), DateFormat("z").parse("-15:00").offset) assertEquals(TimezoneOffset(0.hours), DateFormat("z").parse("+00:00").offset) } + + @Test + fun testPatternFormatRegex() { + val dtmilli = 1536379689000L + assertEquals(dtmilli, DateTime(2018, 9, 8, 4, 8, 9).unixMillisLong) + + val fmt = DateFormat("EEE, dd MMM yyyy HH:mm:ss z") + val msg = "[Sat, 08 Sep 2018 04:08:09 UTC] Example log message" + val nomsg = "[Sat, 08 Sep 20G8 04:08:09 UTC] Example log message" // 20G8 + + val logPattern = Regex("""^\[(""" + fmt.matchingRegexString() + """)\] """) + + assertTrue(logPattern.containsMatchIn(msg), message = "correct datestamp should match") + assertFalse(logPattern.containsMatchIn(nomsg), message = "incorrect datestamp shouldn't match") + + val match = logPattern.find(msg) + assertNotNull(match) + + assertEquals(dtmilli, fmt.parseLong(match.groups[1]!!.value), message = "datestamp parsed from log line has correct value") + assertEquals("Example log message", msg.drop(match.value.length), message = "total match length for composed regex") + } }