Skip to content

Commit

Permalink
Merge pull request #107 from w3stling/default-timezone
Browse files Browse the repository at this point in the history
Make it possible to change the default timezone
  • Loading branch information
w3stling authored Aug 11, 2023
2 parents 91c72dc + 96899f7 commit b64e184
Show file tree
Hide file tree
Showing 4 changed files with 189 additions and 129 deletions.
31 changes: 18 additions & 13 deletions src/main/java/com/apptasticsoftware/rssreader/DateTime.java
Original file line number Diff line number Diff line change
Expand Up @@ -33,10 +33,10 @@
import static java.time.format.DateTimeFormatter.ISO_LOCAL_TIME;

/**
* Date Time util class for converting date time strings
* Class for converting date time strings
*/
public class DateTime implements DateTimeParser {
public static ZoneId defaultZone = ZoneId.of("UTC");
private final ZoneId defaultZone;

public static final DateTimeFormatter BASIC_ISO_DATE;
public static final DateTimeFormatter ISO_LOCAL_DATE;
Expand Down Expand Up @@ -136,24 +136,28 @@ public class DateTime implements DateTimeParser {
RFC_1123_DATE_TIME_SPECIAL_PST_NO_EOW = DateTimeFormatter.ofPattern("d LLL yyyy HH:mm:ss 'PST'", Locale.ENGLISH).withZone(ZoneOffset.ofHours(-8));
}

/**
* Creates DateTime object for converting timestamps.
* Using default timezone UTC if no timezone information if found in timestamp.
*/
public DateTime() {

defaultZone = ZoneId.of("UTC");
}

/**
* Time zone to use if now zone information if found in date time string
* @param defaultZone time zone to use
* Creates DateTime object for converting timestamps.
* @param defaultZone time zone to use if no timezone information if found in timestamp
*/
public static void setDefaultZone(ZoneId defaultZone) {
DateTime.defaultZone = defaultZone;
public DateTime(ZoneId defaultZone) {
this.defaultZone = defaultZone;
}

/**
* Converts date time string to LocalDateTime object. Note any time zone information in date time string is ignored.
* @param dateTime date time string
* @return local date time object
*/
public static LocalDateTime toLocalDateTime(String dateTime) {
public LocalDateTime toLocalDateTime(String dateTime) {
var zonedDateTime = toZonedDateTime(dateTime);
if (zonedDateTime == null) {
return null;
Expand All @@ -167,7 +171,7 @@ public static LocalDateTime toLocalDateTime(String dateTime) {
* @return zoned date time object
*/
@SuppressWarnings("java:S3776")
public static ZonedDateTime toZonedDateTime(String dateTime) {
public ZonedDateTime toZonedDateTime(String dateTime) {
if (dateTime == null)
return null;

Expand Down Expand Up @@ -356,7 +360,7 @@ else if (dateTime.contains("-") ||dateTime.contains("+"))
* @param dateTime date time string
* @return time in milliseconds
*/
public static Long toEpochMilli(String dateTime) {
public Long toEpochMilli(String dateTime) {
ZonedDateTime zonedDateTime = toZonedDateTime(dateTime);

if (zonedDateTime == null)
Expand All @@ -365,7 +369,7 @@ public static Long toEpochMilli(String dateTime) {
return zonedDateTime.toInstant().toEpochMilli();
}

public static Instant toInstant(String dateTime) {
public Instant toInstant(String dateTime) {
ZonedDateTime zonedDateTime = toZonedDateTime(dateTime);

if (zonedDateTime == null)
Expand All @@ -386,7 +390,8 @@ public static Instant toInstant(String dateTime) {
@SuppressWarnings("java:S1133")
@Deprecated(since="3.3.0", forRemoval=true)
public static Comparator<Item> pubDateComparator() {
return Comparator.comparing(i -> i.getPubDate().map(DateTime::toInstant).orElse(Instant.EPOCH));
var dateTime = new DateTime();
return Comparator.comparing(i -> i.getPubDate().map(dateTime::toInstant).orElse(Instant.EPOCH));
}

/**
Expand All @@ -396,6 +401,6 @@ public static Comparator<Item> pubDateComparator() {
*/
@Override
public ZonedDateTime parse(String timestamp) {
return DateTime.toZonedDateTime(timestamp);
return toZonedDateTime(timestamp);
}
}
Original file line number Diff line number Diff line change
@@ -1,3 +1,26 @@
/*
* MIT License
*
* Copyright (c) 2022, Apptastic Software
*
* Permission is hereby granted, free of charge, to any person obtaining a copy
* of this software and associated documentation files (the "Software"), to deal
* in the Software without restriction, including without limitation the rights
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
* copies of the Software, and to permit persons to whom the Software is
* furnished to do so, subject to the following conditions:
*
* The above copyright notice and this permission notice shall be included in all
* copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
* SOFTWARE.
*/
package com.apptasticsoftware.rssreader.util;

import com.apptasticsoftware.rssreader.DateTime;
Expand All @@ -24,7 +47,8 @@ private ItemComparator() {
* @return comparator
*/
public static <I extends Item> Comparator<I> oldestItemFirst() {
return Comparator.comparing((I i) -> i.getPubDate().map(DateTime::toInstant).orElse(Instant.EPOCH));
var dateTime = new DateTime();
return Comparator.comparing((I i) -> i.getPubDate().map(dateTime::toInstant).orElse(Instant.EPOCH));
}

/**
Expand All @@ -44,7 +68,8 @@ public static <I extends Item> Comparator<I> oldestItemFirst(DateTimeParser date
* @return comparator
*/
public static <I extends Item> Comparator<I> newestItemFirst() {
return Comparator.comparing((I i) -> i.getPubDate().map(DateTime::toInstant).orElse(Instant.EPOCH)).reversed();
var dateTime = new DateTime();
return Comparator.comparing((I i) -> i.getPubDate().map(dateTime::toInstant).orElse(Instant.EPOCH)).reversed();
}

/**
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -418,8 +418,7 @@ void zonedDateTime() throws IOException {
ZonedDateTime dateTime = items.stream()
.sorted()
.findFirst()
.flatMap(Item::getPubDate)
.map(DateTime::toZonedDateTime)
.flatMap(Item::getPubDateZonedDateTime)
.orElse(null);
assertNotNull(dateTime);
}
Expand All @@ -434,8 +433,7 @@ void dateTime() throws IOException {

Optional<ZonedDateTime> dateTime = items.stream()
.findFirst()
.flatMap(Item::getPubDate)
.map(DateTime::toZonedDateTime);
.flatMap(Item::getPubDateZonedDateTime);

assertThat(dateTime, isPresent());
}
Expand Down
Loading

0 comments on commit b64e184

Please sign in to comment.