Skip to content

Commit 8727659

Browse files
Use LocalDateTime instead of LocalDate
1 parent f8d6f94 commit 8727659

2 files changed

Lines changed: 12 additions & 14 deletions

File tree

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

Lines changed: 7 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44
import java.io.Serializable;
55
import java.time.Instant;
66
import java.time.LocalDate;
7+
import java.time.LocalDateTime;
78
import java.time.OffsetDateTime;
89
import java.time.ZoneId;
910
import java.time.ZoneOffset;
@@ -35,12 +36,12 @@ public DateWrapper(@Nonnull final Instant instant, final boolean isApproximation
3536
this.isApproximation = isApproximation;
3637
}
3738

38-
public DateWrapper(@Nonnull final LocalDate localDate) {
39-
this(localDate, true);
39+
public DateWrapper(@Nonnull final LocalDateTime dateTime) {
40+
this(dateTime, false);
4041
}
4142

42-
public DateWrapper(@Nonnull final LocalDate localDate, final boolean isApproximation) {
43-
this(localDate.atStartOfDay(ZoneId.systemDefault()).toInstant(), isApproximation);
43+
public DateWrapper(@Nonnull final LocalDateTime dateTime, final boolean isApproximation) {
44+
this(dateTime.atZone(ZoneId.systemDefault()).toInstant(), isApproximation);
4445
}
4546

4647
/**
@@ -63,8 +64,8 @@ public OffsetDateTime offsetDateTime() {
6364
* @return the wrapped {@link Instant} as a {@link LocalDate} in the current time zone.
6465
*/
6566
@Nonnull
66-
public LocalDate getLocalDate() {
67-
return LocalDate.ofInstant(instant, ZoneId.systemDefault());
67+
public LocalDateTime getLocalDateTime() {
68+
return LocalDateTime.ofInstant(instant, ZoneId.systemDefault());
6869
}
6970

7071
/**

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

Lines changed: 5 additions & 8 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.LocalDateTime;
8-
import java.time.ZoneId;
98
import java.time.temporal.ChronoUnit;
109
import java.util.Map;
1110
import java.util.regex.Pattern;
@@ -105,15 +104,13 @@ private boolean textualDateMatches(final String textualDate, final String agoPhr
105104
}
106105

107106
private DateWrapper getResultFor(final int timeAgoAmount, final ChronoUnit chronoUnit) {
108-
final var resolvedDateTime = chronoUnit == ChronoUnit.YEARS
107+
final var localDateTime = chronoUnit == ChronoUnit.YEARS
109108
// minusDays is needed to prevent `PrettyTime` from showing '12 months ago'.
110109
? now.minusYears(timeAgoAmount).minusDays(1)
111110
: now.minus(timeAgoAmount, chronoUnit);
112-
113-
if (chronoUnit.isDateBased()) {
114-
return new DateWrapper(resolvedDateTime.toLocalDate());
115-
} else {
116-
return new DateWrapper(resolvedDateTime.atZone(ZoneId.systemDefault()).toInstant());
117-
}
111+
final boolean isApproximate = chronoUnit.isDateBased();
112+
final var resolvedDateTime =
113+
isApproximate ? localDateTime.truncatedTo(ChronoUnit.DAYS) : localDateTime;
114+
return new DateWrapper(resolvedDateTime, isApproximate);
118115
}
119116
}

0 commit comments

Comments
 (0)