-
Notifications
You must be signed in to change notification settings - Fork 1.9k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Added New DateTime parser implementation
Signed-off-by: Prabhat Sharma <[email protected]>
- Loading branch information
Prabhat Sharma
committed
Dec 10, 2023
1 parent
3c14cc1
commit c01debd
Showing
9 changed files
with
963 additions
and
36 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
98 changes: 98 additions & 0 deletions
98
server/src/main/java/org/opensearch/common/time/CustomDateTimeFormatter.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,98 @@ | ||
/* | ||
* SPDX-License-Identifier: Apache-2.0 | ||
* | ||
* The OpenSearch Contributors require contributions made to | ||
* this file be licensed under the Apache-2.0 license or a | ||
* compatible open source license. | ||
*/ | ||
|
||
package org.opensearch.common.time; | ||
|
||
import java.text.Format; | ||
import java.text.ParsePosition; | ||
import java.time.ZoneId; | ||
import java.time.chrono.Chronology; | ||
import java.time.format.DateTimeFormatter; | ||
import java.time.temporal.TemporalAccessor; | ||
import java.time.temporal.TemporalQuery; | ||
import java.util.Locale; | ||
|
||
/** | ||
* Wrapper class for DateTimeFormatter{@link java.time.format.DateTimeFormatter} | ||
* to allow for custom implementations for datetime parsing/formatting | ||
*/ | ||
public class CustomDateTimeFormatter { | ||
private final DateTimeFormatter formatter; | ||
|
||
public CustomDateTimeFormatter(String pattern) { | ||
this.formatter = DateTimeFormatter.ofPattern(pattern, Locale.ROOT); | ||
} | ||
|
||
public CustomDateTimeFormatter(String pattern, Locale locale) { | ||
this.formatter = DateTimeFormatter.ofPattern(pattern, locale); | ||
} | ||
|
||
public CustomDateTimeFormatter(DateTimeFormatter formatter) { | ||
this.formatter = formatter; | ||
} | ||
|
||
public static CustomDateTimeFormatter ofPattern(String pattern) { | ||
return new CustomDateTimeFormatter(pattern); | ||
} | ||
|
||
public static CustomDateTimeFormatter ofPattern(String pattern, Locale locale) { | ||
return new CustomDateTimeFormatter(pattern, locale); | ||
} | ||
|
||
public CustomDateTimeFormatter withLocale(Locale locale) { | ||
return new CustomDateTimeFormatter(getFormatter().withLocale(locale)); | ||
} | ||
|
||
public CustomDateTimeFormatter withZone(ZoneId zoneId) { | ||
return new CustomDateTimeFormatter(getFormatter().withZone(zoneId)); | ||
} | ||
|
||
public CustomDateTimeFormatter withChronology(Chronology chrono) { | ||
return new CustomDateTimeFormatter(getFormatter().withChronology(chrono)); | ||
} | ||
|
||
public String format(TemporalAccessor temporal) { | ||
return this.getFormatter().format(temporal); | ||
} | ||
|
||
public TemporalAccessor parse(CharSequence text, ParsePosition position) { | ||
return this.getFormatter().parse(text, position); | ||
} | ||
|
||
public TemporalAccessor parse(CharSequence text) { | ||
return this.getFormatter().parse(text); | ||
} | ||
|
||
public <T> T parse(CharSequence text, TemporalQuery<T> query) { | ||
return this.getFormatter().parse(text, query); | ||
} | ||
|
||
public ZoneId getZone() { | ||
return this.getFormatter().getZone(); | ||
} | ||
|
||
public Locale getLocale() { | ||
return this.getFormatter().getLocale(); | ||
} | ||
|
||
public TemporalAccessor parse(String input) { | ||
return formatter.parse(input); | ||
} | ||
|
||
public DateTimeFormatter getFormatter() { | ||
return formatter; | ||
} | ||
|
||
public Format toFormat() { | ||
return getFormatter().toFormat(); | ||
} | ||
|
||
public Object parseObject(String text, ParsePosition pos) { | ||
return getFormatter().toFormat().parseObject(text, pos); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
192 changes: 192 additions & 0 deletions
192
server/src/main/java/org/opensearch/common/time/DateTime.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,192 @@ | ||
/* | ||
* SPDX-License-Identifier: Apache-2.0 | ||
* | ||
* The OpenSearch Contributors require contributions made to | ||
* this file be licensed under the Apache-2.0 license or a | ||
* compatible open source license. | ||
*/ | ||
|
||
package org.opensearch.common.time; | ||
|
||
import java.time.DateTimeException; | ||
import java.time.OffsetDateTime; | ||
import java.time.ZoneOffset; | ||
import java.time.temporal.ChronoField; | ||
import java.util.Objects; | ||
import java.util.Optional; | ||
|
||
/** | ||
* Container class for parsed date/date-time data. | ||
*/ | ||
class DateTime { | ||
private final int year; | ||
private final int month; | ||
private final int day; | ||
private final int hour; | ||
private final int minute; | ||
private final int second; | ||
private final int nano; | ||
private final ZoneOffset offset; | ||
private final int fractionDigits; | ||
|
||
public DateTime( | ||
final int year, | ||
final int month, | ||
final int day, | ||
final int hour, | ||
final int minute, | ||
final int second, | ||
final int nano, | ||
final ZoneOffset offset, | ||
final int fractionDigits | ||
) { | ||
this.year = year; | ||
this.month = assertSize(month, 1, 12, ChronoField.MONTH_OF_YEAR); | ||
this.day = assertSize(day, 1, 31, ChronoField.DAY_OF_MONTH); | ||
this.hour = assertSize(hour, 0, 23, ChronoField.HOUR_OF_DAY); | ||
this.minute = assertSize(minute, 0, 59, ChronoField.MINUTE_OF_HOUR); | ||
this.second = assertSize(second, 0, 60, ChronoField.SECOND_OF_MINUTE); | ||
this.nano = assertSize(nano, 0, 999_999_999, ChronoField.NANO_OF_SECOND); | ||
this.offset = offset; | ||
this.fractionDigits = fractionDigits; | ||
} | ||
|
||
/** | ||
* Create a new instance with minute granularity from the input parameters | ||
*/ | ||
public static DateTime of(int year, int month, int day, int hour, int minute, ZoneOffset offset) { | ||
return new DateTime(year, month, day, hour, minute, 0, 0, offset, 0); | ||
} | ||
|
||
/** | ||
* Create a new instance with second granularity from the input parameters | ||
*/ | ||
public static DateTime of(int year, int month, int day, int hour, int minute, int second, ZoneOffset offset) { | ||
return new DateTime(year, month, day, hour, minute, second, 0, offset, 0); | ||
} | ||
|
||
/** | ||
* Create a new instance with nanosecond granularity from the input parameters | ||
*/ | ||
public static DateTime of( | ||
int year, | ||
int month, | ||
int day, | ||
int hour, | ||
int minute, | ||
int second, | ||
int nanos, | ||
ZoneOffset offset, | ||
final int fractionDigits | ||
) { | ||
return new DateTime(year, month, day, hour, minute, second, nanos, offset, fractionDigits); | ||
} | ||
|
||
/** | ||
* Create a new instance with year granularity from the input parameters | ||
*/ | ||
public static DateTime ofYear(int year) { | ||
return new DateTime(year, 1, 1, 0, 0, 0, 0, ZoneOffset.UTC, 0); | ||
} | ||
|
||
/** | ||
* Create a new instance with year-month granularity from the input parameters | ||
*/ | ||
public static DateTime ofYearMonth(int years, int months) { | ||
return new DateTime(years, months, 1, 0, 0, 0, 0, ZoneOffset.UTC, 0); | ||
} | ||
|
||
/** | ||
* Create a new instance with day granularity from the input parameters | ||
*/ | ||
public static DateTime ofDate(int years, int months, int days) { | ||
return new DateTime(years, months, days, 0, 0, 0, 0, ZoneOffset.UTC, 0); | ||
} | ||
|
||
private int assertSize(int value, int min, int max, ChronoField field) { | ||
if (value > max) { | ||
throw new DateTimeException("Field " + field.name() + " out of bounds. Expected " + min + "-" + max + ", got " + value); | ||
} | ||
return value; | ||
} | ||
|
||
public int getYear() { | ||
return year; | ||
} | ||
|
||
public int getMonth() { | ||
return month; | ||
} | ||
|
||
public int getDayOfMonth() { | ||
return day; | ||
} | ||
|
||
public int getHour() { | ||
return hour; | ||
} | ||
|
||
public int getMinute() { | ||
return minute; | ||
} | ||
|
||
public int getSecond() { | ||
return second; | ||
} | ||
|
||
public int getNano() { | ||
return nano; | ||
} | ||
|
||
/** | ||
* Returns the time offset, if available | ||
* | ||
* @return the time offset, if available | ||
*/ | ||
public Optional<ZoneOffset> getOffset() { | ||
return Optional.ofNullable(offset); | ||
} | ||
|
||
/** | ||
* Creates an {@link OffsetDateTime} | ||
* | ||
* @return the {@link OffsetDateTime} | ||
*/ | ||
public OffsetDateTime toOffsetDatetime() { | ||
if (offset != null) { | ||
return OffsetDateTime.of(year, month, day, hour, minute, second, nano, offset); | ||
} | ||
throw new DateTimeException("No zone offset information found"); | ||
} | ||
|
||
/** | ||
* * @hidden | ||
*/ | ||
@Override | ||
public boolean equals(final Object o) { | ||
if (this == o) { | ||
return true; | ||
} | ||
if (o == null || getClass() != o.getClass()) { | ||
return false; | ||
} | ||
DateTime dateTime = (DateTime) o; | ||
return year == dateTime.year | ||
&& month == dateTime.month | ||
&& day == dateTime.day | ||
&& hour == dateTime.hour | ||
&& minute == dateTime.minute | ||
&& second == dateTime.second | ||
&& nano == dateTime.nano | ||
&& fractionDigits == dateTime.fractionDigits | ||
&& Objects.equals(offset, dateTime.offset); | ||
} | ||
|
||
/** | ||
* @hidden | ||
*/ | ||
@Override | ||
public int hashCode() { | ||
return Objects.hash(year, month, day, hour, minute, second, nano, offset, fractionDigits); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.