22
33import org .schabi .newpipe .extractor .exceptions .ParsingException ;
44import org .schabi .newpipe .extractor .timeago .PatternsHolder ;
5- import org .schabi .newpipe .extractor .timeago .TimeAgoUnit ;
65import org .schabi .newpipe .extractor .utils .Parser ;
76
8- import java .util .Calendar ;
7+ import java .time .OffsetDateTime ;
8+ import java .time .ZoneOffset ;
9+ import java .time .temporal .ChronoUnit ;
910import java .util .Collection ;
1011import java .util .Map ;
1112import java .util .regex .Pattern ;
1617 */
1718public class TimeAgoParser {
1819 private final PatternsHolder patternsHolder ;
19- private final Calendar consistentNow ;
20+ private final OffsetDateTime now ;
2021
2122 /**
2223 * Creates a helper to parse upload dates in the format '2 days ago'.
@@ -28,7 +29,7 @@ public class TimeAgoParser {
2829 */
2930 public TimeAgoParser (PatternsHolder patternsHolder ) {
3031 this .patternsHolder = patternsHolder ;
31- consistentNow = Calendar . getInstance ( );
32+ now = OffsetDateTime . now ( ZoneOffset . UTC );
3233 }
3334
3435 /**
@@ -42,14 +43,14 @@ public TimeAgoParser(PatternsHolder patternsHolder) {
4243 * @throws ParsingException if the time unit could not be recognized
4344 */
4445 public DateWrapper parse (String textualDate ) throws ParsingException {
45- for (Map .Entry <TimeAgoUnit , Map <String , Integer >> caseUnitEntry : patternsHolder .specialCases ().entrySet ()) {
46- final TimeAgoUnit timeAgoUnit = caseUnitEntry .getKey ();
46+ for (Map .Entry <ChronoUnit , Map <String , Integer >> caseUnitEntry : patternsHolder .specialCases ().entrySet ()) {
47+ final ChronoUnit chronoUnit = caseUnitEntry .getKey ();
4748 for (Map .Entry <String , Integer > caseMapToAmountEntry : caseUnitEntry .getValue ().entrySet ()) {
4849 final String caseText = caseMapToAmountEntry .getKey ();
4950 final Integer caseAmount = caseMapToAmountEntry .getValue ();
5051
5152 if (textualDateMatches (textualDate , caseText )) {
52- return getResultFor (caseAmount , timeAgoUnit );
53+ return getResultFor (caseAmount , chronoUnit );
5354 }
5455 }
5556 }
@@ -63,22 +64,22 @@ public DateWrapper parse(String textualDate) throws ParsingException {
6364 timeAgoAmount = 1 ;
6465 }
6566
66- final TimeAgoUnit timeAgoUnit = parseTimeAgoUnit (textualDate );
67- return getResultFor (timeAgoAmount , timeAgoUnit );
67+ final ChronoUnit chronoUnit = parseChronoUnit (textualDate );
68+ return getResultFor (timeAgoAmount , chronoUnit );
6869 }
6970
7071 private int parseTimeAgoAmount (String textualDate ) throws NumberFormatException {
7172 String timeValueStr = textualDate .replaceAll ("\\ D+" , "" );
7273 return Integer .parseInt (timeValueStr );
7374 }
7475
75- private TimeAgoUnit parseTimeAgoUnit (String textualDate ) throws ParsingException {
76- for (Map .Entry <TimeAgoUnit , Collection <String >> entry : patternsHolder .asMap ().entrySet ()) {
77- final TimeAgoUnit timeAgoUnit = entry .getKey ();
76+ private ChronoUnit parseChronoUnit (String textualDate ) throws ParsingException {
77+ for (Map .Entry <ChronoUnit , Collection <String >> entry : patternsHolder .asMap ().entrySet ()) {
78+ final ChronoUnit chronoUnit = entry .getKey ();
7879
7980 for (String agoPhrase : entry .getValue ()) {
8081 if (textualDateMatches (textualDate , agoPhrase )) {
81- return timeAgoUnit ;
82+ return chronoUnit ;
8283 }
8384 }
8485 }
@@ -112,65 +113,35 @@ private boolean textualDateMatches(String textualDate, String agoPhrase) {
112113 }
113114 }
114115
115- private DateWrapper getResultFor (int timeAgoAmount , TimeAgoUnit timeAgoUnit ) {
116- final Calendar calendarTime = getNow () ;
116+ private DateWrapper getResultFor (int timeAgoAmount , ChronoUnit chronoUnit ) {
117+ OffsetDateTime offsetDateTime = now ;
117118 boolean isApproximation = false ;
118119
119- switch (timeAgoUnit ) {
120+ switch (chronoUnit ) {
120121 case SECONDS :
121- calendarTime .add (Calendar .SECOND , -timeAgoAmount );
122- break ;
123-
124122 case MINUTES :
125- calendarTime .add (Calendar .MINUTE , -timeAgoAmount );
126- break ;
127-
128123 case HOURS :
129- calendarTime . add ( Calendar . HOUR_OF_DAY , - timeAgoAmount );
124+ offsetDateTime = offsetDateTime . minus ( timeAgoAmount , chronoUnit );
130125 break ;
131126
132127 case DAYS :
133- calendarTime .add (Calendar .DAY_OF_MONTH , -timeAgoAmount );
134- isApproximation = true ;
135- break ;
136-
137128 case WEEKS :
138- calendarTime .add (Calendar .WEEK_OF_YEAR , -timeAgoAmount );
139- isApproximation = true ;
140- break ;
141-
142129 case MONTHS :
143- calendarTime . add ( Calendar . MONTH , - timeAgoAmount );
130+ offsetDateTime = offsetDateTime . minus ( timeAgoAmount , chronoUnit );
144131 isApproximation = true ;
145132 break ;
146133
147134 case YEARS :
148- calendarTime .add (Calendar .YEAR , -timeAgoAmount );
149- // Prevent `PrettyTime` from showing '12 months ago'.
150- calendarTime .add (Calendar .DAY_OF_MONTH , -1 );
135+ // minusDays is needed to prevent `PrettyTime` from showing '12 months ago'.
136+ offsetDateTime = offsetDateTime .minusYears (timeAgoAmount ).minusDays (1 );
151137 isApproximation = true ;
152138 break ;
153139 }
154140
155141 if (isApproximation ) {
156- markApproximatedTime ( calendarTime );
142+ offsetDateTime = offsetDateTime . truncatedTo ( ChronoUnit . HOURS );
157143 }
158144
159- return new DateWrapper (calendarTime , isApproximation );
160- }
161-
162- private Calendar getNow () {
163- return (Calendar ) consistentNow .clone ();
164- }
165-
166- /**
167- * Marks the time as approximated by setting minutes, seconds and milliseconds to 0.
168- *
169- * @param calendarTime Time to be marked as approximated
170- */
171- private void markApproximatedTime (Calendar calendarTime ) {
172- calendarTime .set (Calendar .MINUTE , 0 );
173- calendarTime .set (Calendar .SECOND , 0 );
174- calendarTime .set (Calendar .MILLISECOND , 0 );
145+ return new DateWrapper (offsetDateTime , isApproximation );
175146 }
176147}
0 commit comments