2828import javax .annotation .Nonnull ;
2929
3030import java .io .IOException ;
31- import java .time .LocalDateTime ;
32- import java .time .ZoneId ;
33- import java .time .ZonedDateTime ;
31+ import java .time .Instant ;
3432import java .time .format .DateTimeFormatter ;
3533import java .time .format .DateTimeParseException ;
34+ import java .time .temporal .ChronoUnit ;
3635import java .util .HashMap ;
3736import java .util .List ;
3837import java .util .Map ;
@@ -74,8 +73,6 @@ public final class RSSHandlerRoutine implements Routine {
7473
7574 private static final Logger logger = LoggerFactory .getLogger (RSSHandlerRoutine .class );
7675 private static final int MAX_CONTENTS = 1000 ;
77- private static final ZonedDateTime ZONED_TIME_MIN =
78- ZonedDateTime .of (LocalDateTime .MIN , ZoneId .systemDefault ());
7976 private static final String HTTP_USER_AGENT =
8077 "TJ-Bot/1.0 (+https://github.com/Together-Java/TJ-Bot)" ;
8178 private final RssReader rssReader ;
@@ -181,7 +178,7 @@ private void sendRSS(JDA jda, RSSFeed feedConfig) {
181178 private Optional <Predicate <Item >> prepareItemPostPredicate (RSSFeed feedConfig ,
182179 List <Item > rssItems ) {
183180 Optional <RssFeedRecord > rssFeedRecord = getRssFeedRecordFromDatabase (feedConfig );
184- Optional <ZonedDateTime > lastPostedDate =
181+ Optional <Instant > lastPostedDate =
185182 getLatestPostDateFromItems (rssItems , feedConfig .dateFormatterPattern ());
186183
187184 lastPostedDate .ifPresent (
@@ -191,16 +188,15 @@ private Optional<Predicate<Item>> prepareItemPostPredicate(RSSFeed feedConfig,
191188 return Optional .empty ();
192189 }
193190
194- Optional <ZonedDateTime > lastSavedDate = getLastSavedDateFromDatabaseRecord (
195- rssFeedRecord .orElseThrow (), feedConfig . dateFormatterPattern ());
191+ Optional <Instant > lastSavedDate =
192+ getLastSavedDateFromDatabaseRecord ( rssFeedRecord .orElseThrow ());
196193
197194 if (lastSavedDate .isEmpty ()) {
198195 return Optional .empty ();
199196 }
200197
201198 return Optional .of (item -> {
202- ZonedDateTime itemPubDate =
203- getDateTimeFromItem (item , feedConfig .dateFormatterPattern ());
199+ Instant itemPubDate = getDateTimeFromItem (item , feedConfig .dateFormatterPattern ());
204200 return itemPubDate .isAfter (lastSavedDate .orElseThrow ());
205201 });
206202 }
@@ -223,16 +219,13 @@ private Optional<RssFeedRecord> getRssFeedRecordFromDatabase(RSSFeed feedConfig)
223219 * record.
224220 *
225221 * @param rssRecord an existing RSS feed record to retrieve the last saved date from
226- * @param dateFormatterPattern the pattern used to parse the date from the database record
227222 * @return An {@link Optional} containing the last saved date if it could be retrieved and
228223 * parsed successfully, otherwise an empty {@link Optional}
229224 */
230- private Optional <ZonedDateTime > getLastSavedDateFromDatabaseRecord (RssFeedRecord rssRecord ,
231- String dateFormatterPattern ) throws DateTimeParseException {
225+ private Optional <Instant > getLastSavedDateFromDatabaseRecord (RssFeedRecord rssRecord )
226+ throws DateTimeParseException {
232227 try {
233- ZonedDateTime savedDate =
234- getZonedDateTime (rssRecord .getLastDate (), dateFormatterPattern );
235- return Optional .of (savedDate );
228+ return Optional .of (Instant .parse (rssRecord .getLastDate ()));
236229 } catch (DateTimeParseException _) {
237230 return Optional .empty ();
238231 }
@@ -243,13 +236,13 @@ private Optional<ZonedDateTime> getLastSavedDateFromDatabaseRecord(RssFeedRecord
243236 *
244237 * @param items the list of items to retrieve the latest post date from
245238 * @param dateFormatterPattern the pattern used to parse the date from the database record
246- * @return the latest post date as a {@link ZonedDateTime } object, or null if the list is empty
239+ * @return the latest post date as a {@link Instant } object, or null if the list is empty
247240 */
248- private Optional <ZonedDateTime > getLatestPostDateFromItems (List <Item > items ,
241+ private Optional <Instant > getLatestPostDateFromItems (List <Item > items ,
249242 String dateFormatterPattern ) {
250243 return items .stream ()
251244 .map (item -> getDateTimeFromItem (item , dateFormatterPattern ))
252- .max (ZonedDateTime ::compareTo );
245+ .max (Instant ::compareTo );
253246 }
254247
255248 /**
@@ -277,10 +270,10 @@ private void postItem(List<TextChannel> textChannels, Item rssItem, RSSFeed feed
277270 * @throws DateTimeParseException if the date cannot be parsed
278271 */
279272 private void updateLastDateToDatabase (RSSFeed feedConfig , @ Nullable RssFeedRecord rssFeedRecord ,
280- ZonedDateTime lastPostedDate ) {
273+ Instant lastPostedDate ) {
281274 DateTimeFormatter dateTimeFormatter =
282275 DateTimeFormatter .ofPattern (feedConfig .dateFormatterPattern ());
283- String lastDateStr = lastPostedDate .format ( dateTimeFormatter );
276+ String lastDateStr = lastPostedDate .toString ( );
284277
285278 if (rssFeedRecord == null ) {
286279 database .write (context -> context .newRecord (RSS_FEED )
@@ -297,22 +290,22 @@ private void updateLastDateToDatabase(RSSFeed feedConfig, @Nullable RssFeedRecor
297290 }
298291
299292 /**
300- * Attempts to get a {@link ZonedDateTime } from an {@link Item} with a provided string date time
293+ * Attempts to get a {@link Instant } from an {@link Item} with a provided string date time
301294 * format.
302295 * <p>
303296 * If either of the function inputs are null or a {@link DateTimeParseException} is caught, the
304- * oldest-possible {@link ZonedDateTime } will get returned instead.
297+ * oldest-possible {@link Instant } will get returned instead.
305298 *
306299 * @param item The {@link Item} from which to extract the date.
307300 * @param dateTimeFormat The format of the date time string.
308- * @return The computed {@link ZonedDateTime }
301+ * @return The computed {@link Instant }
309302 * @throws DateTimeParseException if the date cannot be parsed
310303 */
311- private static ZonedDateTime getDateTimeFromItem (Item item , String dateTimeFormat )
304+ private static Instant getDateTimeFromItem (Item item , String dateTimeFormat )
312305 throws DateTimeParseException {
313306 Optional <String > pubDate = item .getPubDate ();
314307
315- return pubDate .map (s -> getZonedDateTime (s , dateTimeFormat )).orElse (ZONED_TIME_MIN );
308+ return pubDate .map (s -> getInstantTimeFromFormat (s , dateTimeFormat )).orElse (Instant . MIN );
316309
317310 }
318311
@@ -334,7 +327,7 @@ private static boolean isValidDateFormat(Item rssItem, RSSFeed feedConfig) {
334327 // If this throws a DateTimeParseException then it's certain
335328 // that the format pattern defined in the config and the
336329 // feed's actual format differ.
337- getZonedDateTime (firstRssFeedPubDate .get (), feedConfig .dateFormatterPattern ());
330+ getInstantTimeFromFormat (firstRssFeedPubDate .get (), feedConfig .dateFormatterPattern ());
338331 } catch (DateTimeParseException _) {
339332 return false ;
340333 }
@@ -384,7 +377,7 @@ private MessageCreateData constructMessage(Item item, RSSFeed feedConfig) {
384377 // Set the item's timestamp to the embed if found
385378 item .getPubDate ()
386379 .ifPresent (date -> embedBuilder
387- .setTimestamp (getZonedDateTime (date , feedConfig .dateFormatterPattern ())));
380+ .setTimestamp (getInstantTimeFromFormat (date , feedConfig .dateFormatterPattern ())));
388381
389382 embedBuilder .setTitle (title , titleLink );
390383 embedBuilder .setAuthor (item .getChannel ().getLink ());
@@ -428,7 +421,7 @@ private List<Item> fetchRSSItemsFromURL(String rssUrl) {
428421 "Possibly dead RSS feed URL: {} - Failed {} times. Please remove it from config." ,
429422 rssUrl , newCount );
430423 }
431- circuitBreaker .put (rssUrl , new FailureState (newCount , ZonedDateTime .now ()));
424+ circuitBreaker .put (rssUrl , new FailureState (newCount , Instant .now ()));
432425
433426 long blacklistedHours = calculateWaitHours (newCount );
434427
@@ -441,21 +434,19 @@ private List<Item> fetchRSSItemsFromURL(String rssUrl) {
441434 }
442435
443436 /**
444- * Helper function for parsing a given date value to a {@link ZonedDateTime} with a given
445- * format.
437+ * Helper function for parsing a given date value to a {@link Instant} with a given format.
446438 *
447439 * @param date the date value to parse, can be null
448- * @param format the format pattern to use for parsing
449- * @return the parsed {@link ZonedDateTime} object
440+ * @return the parsed {@link Instant} object
450441 * @throws DateTimeParseException if the date cannot be parsed
451442 */
452- private static ZonedDateTime getZonedDateTime (@ Nullable String date , String format )
443+ private static Instant getInstantTimeFromFormat (@ Nullable String date , String datePattern )
453444 throws DateTimeParseException {
454445 if (date == null ) {
455- return ZONED_TIME_MIN ;
446+ return Instant . MIN ;
456447 }
457448
458- return ZonedDateTime . parse ( date , DateTimeFormatter .ofPattern (format ));
449+ return Instant . from ( DateTimeFormatter .ofPattern (datePattern ). parse ( date ));
459450 }
460451
461452 private long calculateWaitHours (int failureCount ) {
@@ -469,8 +460,8 @@ private boolean isBackingOff(String url) {
469460 return false ;
470461
471462 long waitHours = calculateWaitHours (state .count ());
472- ZonedDateTime retryAt = state .lastFailure ().plusHours (waitHours );
463+ Instant retryAt = state .lastFailure ().plus (waitHours , ChronoUnit . HOURS );
473464
474- return ZonedDateTime .now ().isBefore (retryAt );
465+ return Instant .now ().isBefore (retryAt );
475466 }
476467}
0 commit comments