Skip to content

Commit

Permalink
Added localized date time parsers (fixes ruippeixotog#65).
Browse files Browse the repository at this point in the history
It provides localized datetime parsers in the extraction API similar to the handling of local time zones.

A test case is also provided. The pattern and example comes from ruippeixotog/ebay-snipe-server ruippeixotog#7.
  • Loading branch information
DrDub committed Dec 14, 2022
1 parent 3770786 commit b37ad5b
Show file tree
Hide file tree
Showing 3 changed files with 21 additions and 0 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -110,6 +110,15 @@ object ContentParsers {
* a content parser for parsing text content as a `DateTime` using `tz` as default time zone.
*/
def withZone(tz: DateTimeZone): String => DateTime = formatter.withZone(tz).parseDateTime(_).withZone(tz)

/** Parses text content as a `DateTime` using a provided locale.
*
* @param locale
* the locale to be used by the parser
* @return
* a content parser for parsing text content as a `DateTime` using `locale` as the locale.
*/
def withLocale(locale: java.util.Locale): String => DateTime = formatter.withLocale(locale).parseDateTime(_)
}

/** A content parser with extra options for the retrieval of the first match of a regex.
Expand Down
3 changes: 3 additions & 0 deletions core/src/test/resources/test2.html
Original file line number Diff line number Diff line change
Expand Up @@ -64,6 +64,9 @@ <h3>Section 3 h3</h3>
</table>
</section>
</div>
<div id="locale">
<span id="dutchdate">okt. 1, 2022 15:22:10 PDT</span>
</div>

<div id="footer">
<span>No copyright 2014</span>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -129,6 +129,15 @@ class DSLExtractingSpec extends Specification with BrowserHelper {
// when there is time zone in the string to be parsed, the datetime is just changed to the provided time zone
doc >> extractor("#datefull", stext, asDateTime("yyyy-MM-dd'T'HH:mm:ssZ").withZone(customTz)) mustEqual
"2014-10-26T12:30:05Z".toDateTime.withZone(customTz)

// when there is locale provided, it is used
val dutch = java.util.Locale.forLanguageTag("nl")
(doc >> extractor(
"#dutchdate",
stext,
asDateTime("MMM dd, yyyy HH:mm:ss zzz").withLocale(dutch)
)).toString mustEqual
"2022-10-01T15:22:10.000-07:00"
}

"allow immediate parsing with a regex after extraction" in {
Expand Down

0 comments on commit b37ad5b

Please sign in to comment.