Skip to content

Commit 953189f

Browse files
Update DateWrapper to store an Instant
1 parent 5b4feb9 commit 953189f

13 files changed

Lines changed: 65 additions & 85 deletions

extractor/src/main/java/org/schabi/newpipe/extractor/localization/DateWrapper.java

Lines changed: 16 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -1,65 +1,52 @@
11
package org.schabi.newpipe.extractor.localization;
22

3-
43
import javax.annotation.Nonnull;
54
import java.io.Serializable;
5+
import java.time.Instant;
66
import java.time.OffsetDateTime;
77
import java.time.ZoneOffset;
8-
import java.util.Calendar;
9-
import java.util.GregorianCalendar;
108

119
/**
1210
* A wrapper class that provides a field to describe if the date/time is precise or just an
1311
* approximation.
1412
*/
1513
public class DateWrapper implements Serializable {
1614
@Nonnull
17-
private final OffsetDateTime offsetDateTime;
15+
private final Instant instant;
1816
private final boolean isApproximation;
1917

20-
/**
21-
* @deprecated Use {@link #DateWrapper(OffsetDateTime)} instead.
22-
*/
23-
@Deprecated
24-
public DateWrapper(@Nonnull final Calendar calendar) {
25-
//noinspection deprecation
26-
this(calendar, false);
27-
}
28-
29-
/**
30-
* @deprecated Use {@link #DateWrapper(OffsetDateTime, boolean)} instead.
31-
*/
32-
@Deprecated
33-
public DateWrapper(@Nonnull final Calendar calendar, final boolean isApproximation) {
34-
this(OffsetDateTime.ofInstant(calendar.toInstant(), ZoneOffset.UTC), isApproximation);
35-
}
36-
3718
public DateWrapper(@Nonnull final OffsetDateTime offsetDateTime) {
3819
this(offsetDateTime, false);
3920
}
4021

4122
public DateWrapper(@Nonnull final OffsetDateTime offsetDateTime,
4223
final boolean isApproximation) {
43-
this.offsetDateTime = offsetDateTime.withOffsetSameInstant(ZoneOffset.UTC);
24+
this(offsetDateTime.toInstant(), isApproximation);
25+
}
26+
27+
public DateWrapper(@Nonnull final Instant instant) {
28+
this(instant, false);
29+
}
30+
31+
public DateWrapper(@Nonnull final Instant instant, final boolean isApproximation) {
32+
this.instant = instant;
4433
this.isApproximation = isApproximation;
4534
}
4635

4736
/**
48-
* @return the wrapped date/time as a {@link Calendar}.
49-
* @deprecated use {@link #offsetDateTime()} instead.
37+
* @return the wrapped {@link Instant}
5038
*/
51-
@Deprecated
5239
@Nonnull
53-
public Calendar date() {
54-
return GregorianCalendar.from(offsetDateTime.toZonedDateTime());
40+
public Instant getInstant() {
41+
return instant;
5542
}
5643

5744
/**
58-
* @return the wrapped date/time.
45+
* @return the wrapped {@link Instant} as an {@link OffsetDateTime} set to UTC.
5946
*/
6047
@Nonnull
6148
public OffsetDateTime offsetDateTime() {
62-
return offsetDateTime;
49+
return instant.atOffset(ZoneOffset.UTC);
6350
}
6451

6552
/**

extractor/src/main/java/org/schabi/newpipe/extractor/localization/TimeAgoParser.java

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,6 @@
55
import org.schabi.newpipe.extractor.utils.Parser;
66

77
import java.time.OffsetDateTime;
8-
import java.time.ZoneOffset;
98
import java.time.temporal.ChronoUnit;
109
import java.util.Map;
1110
import java.util.regex.Pattern;
@@ -28,7 +27,7 @@ public class TimeAgoParser {
2827
* language word separator.
2928
*/
3029
public TimeAgoParser(final PatternsHolder patternsHolder) {
31-
this(patternsHolder, OffsetDateTime.now(ZoneOffset.UTC));
30+
this(patternsHolder, OffsetDateTime.now());
3231
}
3332

3433
/**

extractor/src/main/java/org/schabi/newpipe/extractor/services/bandcamp/extractors/BandcampExtractorHelper.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -204,7 +204,7 @@ public static DateWrapper parseDate(final String textDate) throws ParsingExcepti
204204
try {
205205
final ZonedDateTime zonedDateTime = ZonedDateTime.parse(textDate,
206206
DateTimeFormatter.ofPattern("dd MMM yyyy HH:mm:ss zzz", Locale.ENGLISH));
207-
return new DateWrapper(zonedDateTime.toOffsetDateTime(), false);
207+
return new DateWrapper(zonedDateTime.toInstant());
208208
} catch (final DateTimeException e) {
209209
throw new ParsingException("Could not parse date '" + textDate + "'", e);
210210
}

extractor/src/main/java/org/schabi/newpipe/extractor/services/media_ccc/extractors/MediaCCCRecentKiosk.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -54,7 +54,7 @@ public InfoItemsPage<StreamInfoItem> getInitialPage() throws IOException, Extrac
5454
// Sort them to have the latest stream at the beginning of the list.
5555
final Comparator<StreamInfoItem> comparator = Comparator
5656
.comparing(StreamInfoItem::getUploadDate, Comparator
57-
.nullsLast(Comparator.comparing(DateWrapper::offsetDateTime)))
57+
.nullsLast(Comparator.comparing(DateWrapper::getInstant)))
5858
.reversed();
5959
final StreamInfoItemsCollector collector = new StreamInfoItemsCollector(getServiceId(),
6060
comparator);

extractor/src/main/java/org/schabi/newpipe/extractor/services/media_ccc/extractors/MediaCCCRecentKioskExtractor.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -92,6 +92,6 @@ public String getTextualUploadDate() throws ParsingException {
9292
public DateWrapper getUploadDate() throws ParsingException {
9393
final ZonedDateTime zonedDateTime = ZonedDateTime.parse(event.getString("date"),
9494
DateTimeFormatter.ofPattern("yyyy-MM-dd'T'HH:mm:ss.SSSzzzz"));
95-
return new DateWrapper(zonedDateTime.toOffsetDateTime(), false);
95+
return new DateWrapper(zonedDateTime.toInstant());
9696
}
9797
}

extractor/src/main/java/org/schabi/newpipe/extractor/services/peertube/PeertubeParsingHelper.java

Lines changed: 2 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -19,8 +19,6 @@
1919

2020
import javax.annotation.Nonnull;
2121
import java.time.Instant;
22-
import java.time.OffsetDateTime;
23-
import java.time.ZoneOffset;
2422
import java.time.format.DateTimeParseException;
2523
import java.util.ArrayList;
2624
import java.util.Collections;
@@ -48,10 +46,9 @@ public static void validate(final JsonObject json) throws ContentNotAvailableExc
4846
}
4947
}
5048

51-
public static OffsetDateTime parseDateFrom(final String textualUploadDate)
52-
throws ParsingException {
49+
public static Instant parseInstantFrom(final String textualUploadDate) throws ParsingException {
5350
try {
54-
return OffsetDateTime.ofInstant(Instant.parse(textualUploadDate), ZoneOffset.UTC);
51+
return Instant.parse(textualUploadDate);
5552
} catch (final DateTimeParseException e) {
5653
throw new ParsingException("Could not parse date: \"" + textualUploadDate + "\"", e);
5754
}

extractor/src/main/java/org/schabi/newpipe/extractor/services/peertube/extractors/PeertubeCommentsInfoItemExtractor.java

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,7 @@
2323

2424
import static org.schabi.newpipe.extractor.services.peertube.extractors.PeertubeCommentsExtractor.CHILDREN;
2525
import static org.schabi.newpipe.extractor.services.peertube.PeertubeParsingHelper.getAvatarsFromOwnerAccountOrVideoChannelObject;
26-
import static org.schabi.newpipe.extractor.services.peertube.PeertubeParsingHelper.parseDateFrom;
26+
import static org.schabi.newpipe.extractor.services.peertube.PeertubeParsingHelper.parseInstantFrom;
2727

2828
public class PeertubeCommentsInfoItemExtractor implements CommentsInfoItemExtractor {
2929
@Nonnull
@@ -73,8 +73,7 @@ public String getTextualUploadDate() throws ParsingException {
7373

7474
@Override
7575
public DateWrapper getUploadDate() throws ParsingException {
76-
final String textualUploadDate = getTextualUploadDate();
77-
return new DateWrapper(parseDateFrom(textualUploadDate));
76+
return new DateWrapper(parseInstantFrom(getTextualUploadDate()));
7877
}
7978

8079
@Nonnull

extractor/src/main/java/org/schabi/newpipe/extractor/services/peertube/extractors/PeertubeStreamExtractor.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -85,7 +85,7 @@ public DateWrapper getUploadDate() throws ParsingException {
8585
return null;
8686
}
8787

88-
return new DateWrapper(PeertubeParsingHelper.parseDateFrom(textualUploadDate));
88+
return new DateWrapper(PeertubeParsingHelper.parseInstantFrom(textualUploadDate));
8989
}
9090

9191
@Nonnull

extractor/src/main/java/org/schabi/newpipe/extractor/services/peertube/extractors/PeertubeStreamInfoItemExtractor.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@
1414

1515
import static org.schabi.newpipe.extractor.services.peertube.PeertubeParsingHelper.getAvatarsFromOwnerAccountOrVideoChannelObject;
1616
import static org.schabi.newpipe.extractor.services.peertube.PeertubeParsingHelper.getThumbnailsFromPlaylistOrVideoItem;
17-
import static org.schabi.newpipe.extractor.services.peertube.PeertubeParsingHelper.parseDateFrom;
17+
import static org.schabi.newpipe.extractor.services.peertube.PeertubeParsingHelper.parseInstantFrom;
1818

1919
public class PeertubeStreamInfoItemExtractor implements StreamInfoItemExtractor {
2020

@@ -91,7 +91,7 @@ public DateWrapper getUploadDate() throws ParsingException {
9191
return null;
9292
}
9393

94-
return new DateWrapper(parseDateFrom(textualUploadDate));
94+
return new DateWrapper(parseInstantFrom(textualUploadDate));
9595
}
9696

9797
@Override

extractor/src/main/java/org/schabi/newpipe/extractor/services/youtube/YoutubeParsingHelper.java

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -67,9 +67,10 @@
6767
import java.net.MalformedURLException;
6868
import java.net.URL;
6969
import java.nio.charset.StandardCharsets;
70+
import java.time.Instant;
7071
import java.time.LocalDate;
7172
import java.time.OffsetDateTime;
72-
import java.time.ZoneOffset;
73+
import java.time.ZoneId;
7374
import java.time.format.DateTimeParseException;
7475
import java.util.HashMap;
7576
import java.util.List;
@@ -294,13 +295,14 @@ public static String getFeedUrlFrom(@Nonnull final String channelIdOrUser) {
294295
}
295296
}
296297

297-
public static OffsetDateTime parseDateFrom(final String textualUploadDate)
298+
public static Instant parseInstantFrom(final String textualUploadDate)
298299
throws ParsingException {
299300
try {
300-
return OffsetDateTime.parse(textualUploadDate);
301+
return OffsetDateTime.parse(textualUploadDate).toInstant();
301302
} catch (final DateTimeParseException e) {
302303
try {
303-
return LocalDate.parse(textualUploadDate).atStartOfDay().atOffset(ZoneOffset.UTC);
304+
return LocalDate.parse(textualUploadDate).atStartOfDay(ZoneId.systemDefault())
305+
.toInstant();
304306
} catch (final DateTimeParseException e1) {
305307
throw new ParsingException("Could not parse date: \"" + textualUploadDate + "\"",
306308
e1);

0 commit comments

Comments
 (0)