w3stling / rssreader

A simple Java library for reading RSS and Atom feeds
MIT License
152 stars 25 forks source link

getPubDateZonedDateTime() throws an Exception instead of returning an empty Optional #76

Closed b3r4t closed 1 year ago

b3r4t commented 1 year ago

The method getPubDateZonedDateTime() returns an Optional<ZonedDateTime>. For some reason it cannot parse the date shown in the example below:

Item item = items.get(0);
item.setPubDate("Sat, 21 Jan 2023 11:12:30 GMT");

// Throws an Exception
Optional<ZonedDateTime> pubDate = item.getPubDateZonedDateTime();

An exception is thrown

Exception in thread "main" java.time.format.DateTimeParseException: Text 'Sat, 21 Jan 2023 11:12:30 GMT' could not be parsed at index 0
    at java.base/java.time.format.DateTimeFormatter.parseResolved0(DateTimeFormatter.java:2052)
    at java.base/java.time.format.DateTimeFormatter.parse(DateTimeFormatter.java:1954)
    at java.base/java.time.ZonedDateTime.parse(ZonedDateTime.java:600)
    at com.apptasticsoftware.rssreader/com.apptasticsoftware.rssreader.DateTime.toZonedDateTime(DateTime.java:179)
    at java.base/java.util.Optional.map(Optional.java:260)
    at com.apptasticsoftware.rssreader/com.apptasticsoftware.rssreader.Item.getPubDateZonedDateTime(Item.java:235)

I wonder that the pubDate can't be parsed. Expected behavior: Returning an empty Optional if parsing fails. Tested with version 3.4.1.

w3stling commented 1 year ago

What output do you get if you run the following code?

System.out.println(Locale.getDefault());
var formatter = DateTimeFormatter.ofPattern("E, d LLL yyyy HH:mm:ss O");
System.out.println(formatter);
var timestamp = ZonedDateTime.parse("Sat, 21 Jan 2023 11:12:30 GMT", formatter);
System.out.println(timestamp);
b3r4t commented 1 year ago

An exception is thrown:

de_DE
Text(DayOfWeek,SHORT)','' 'Value(DayOfMonth)' 'Text(MonthOfYear,SHORT_STANDALONE)' 'Value(YearOfEra,4,19,EXCEEDS_PAD)' 'Value(HourOfDay,2)':'Value(MinuteOfHour,2)':'Value(SecondOfMinute,2)' 'LocalizedOffset(SHORT)
Exception in thread "main" java.time.format.DateTimeParseException: Text 'Sat, 21 Jan 2023 11:12:30 GMT' could not be parsed at index 0
    at java.base/java.time.format.DateTimeFormatter.parseResolved0(DateTimeFormatter.java:2052)
    at java.base/java.time.format.DateTimeFormatter.parse(DateTimeFormatter.java:1954)
    at java.base/java.time.ZonedDateTime.parse(ZonedDateTime.java:600)
    at main.Main.main(Main.java:14)

If I change the locale to English it works fine: Locale.setDefault(Locale.ENGLISH);

I don't know the implementation details of the parser, but it could respect the language property of the RSS feed: <language>en-us</language> if available. Otherwise the parser defaults to the English locale.

w3stling commented 1 year ago

DateTimeFormatter was using system default locale. Changed it to use Locale.ENGLISH formatting when parsing timestamp strings.

Fixed by PR #77

w3stling commented 1 year ago

Fixed in release 3.4.2

b3r4t commented 1 year ago

Thank you. Everything works now.