Skip to content

Commit

Permalink
Have DateTime implement TemporalAccessor
Browse files Browse the repository at this point in the history
  • Loading branch information
Morten Haraldsen committed Jan 5, 2024
1 parent 7b862ca commit a7b26ca
Show file tree
Hide file tree
Showing 4 changed files with 244 additions and 69 deletions.
89 changes: 69 additions & 20 deletions src/main/java/com/ethlo/time/DateTime.java
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,10 @@
import java.time.OffsetDateTime;
import java.time.Year;
import java.time.YearMonth;
import java.time.temporal.ChronoField;
import java.time.temporal.TemporalAccessor;
import java.time.temporal.TemporalField;
import java.time.temporal.UnsupportedTemporalTypeException;
import java.util.Objects;
import java.util.Optional;

Expand All @@ -39,7 +43,7 @@
/**
* Container class for parsed date/date-time data. The {@link #getMostGranularField()} contains the highest granularity field found, like MONTH, MINUTE, SECOND.
*/
public class DateTime
public class DateTime implements TemporalAccessor
{
private final Field field;
private final int year;
Expand Down Expand Up @@ -69,10 +73,10 @@ public DateTime(final Field field, final int year, final int month, final int da
/**
* Create a new instance with second granularity from the input parameters
*
* @param year year
* @param month month
* @param day day
* @param hour hour
* @param year year
* @param month month
* @param day day
* @param hour hour
* @param minute minute
* @param second second
* @param offset timezone offset
Expand All @@ -86,14 +90,14 @@ public static DateTime of(int year, int month, int day, int hour, int minute, in
/**
* Create a new instance with nanosecond granularity from the input parameters
*
* @param year year
* @param month month
* @param day day
* @param hour hour
* @param minute minute
* @param second second
* @param nanos nanos
* @param offset timezone offset
* @param year year
* @param month month
* @param day day
* @param hour hour
* @param minute minute
* @param second second
* @param nanos nanos
* @param offset timezone offset
* @param fractionDigits The granularity of the fractional seconds field
* @return A DateTime with nanosecond granularity
*/
Expand All @@ -104,6 +108,7 @@ public static DateTime of(int year, int month, int day, int hour, int minute, in

/**
* Create a new instance with year granularity from the input parameters
*
* @param year The year
* @return a new instance with year granularity from the input parameters
*/
Expand All @@ -114,7 +119,8 @@ public static DateTime ofYear(int year)

/**
* Create a new instance with year-month granularity from the input parameters
* @param year The year
*
* @param year The year
* @param month The month
* @return a new instance with year-month granularity from the input parameters
*/
Expand All @@ -125,9 +131,10 @@ public static DateTime ofYearMonth(int year, int month)

/**
* Create a new instance with day granularity from the input parameters
* @param year The year
*
* @param year The year
* @param month The month
* @param day The day
* @param day The day
* @return a new instance with day granularity from the input parameters
*/
public static DateTime ofDate(int year, int month, int day)
Expand All @@ -137,10 +144,11 @@ public static DateTime ofDate(int year, int month, int day)

/**
* Create a new instance with minute granularity from the input parameters
* @param year The year
* @param month The month
* @param day The day
* @param hour The hour
*
* @param year The year
* @param month The month
* @param day The day
* @param hour The hour
* @param minute The minute
* @param offset The timezone offset
* @return a new instance with minute granularity from the input parameters
Expand Down Expand Up @@ -459,4 +467,45 @@ public int hashCode()
{
return Objects.hash(field.ordinal(), year, month, day, hour, minute, second, nano, offset, fractionDigits);
}

@Override
public boolean isSupported(final TemporalField field)
{
return Field.of(field).ordinal() <= this.field.ordinal();
}

@SuppressWarnings("DuplicatedCode")
@Override
public long getLong(final TemporalField temporalField)
{
if (temporalField.equals(ChronoField.YEAR))
{
return year;
}
else if (temporalField.equals(ChronoField.MONTH_OF_YEAR))
{
return month;
}
else if (temporalField.equals(ChronoField.DAY_OF_MONTH))
{
return day;
}
else if (temporalField.equals(ChronoField.HOUR_OF_DAY))
{
return hour;
}
else if (temporalField.equals(ChronoField.MINUTE_OF_HOUR))
{
return year;
}
else if (temporalField.equals(ChronoField.SECOND_OF_MINUTE))
{
return second;
}
else if (temporalField.equals(ChronoField.NANO_OF_SECOND))
{
return nano;
}
throw new UnsupportedTemporalTypeException("Unsupported field: " + temporalField);
}
}
59 changes: 51 additions & 8 deletions src/main/java/com/ethlo/time/Field.java
Original file line number Diff line number Diff line change
Expand Up @@ -24,26 +24,31 @@
import java.time.OffsetDateTime;
import java.time.Year;
import java.time.YearMonth;
import java.time.temporal.ChronoField;
import java.time.temporal.Temporal;
import java.time.temporal.TemporalField;
import java.time.temporal.UnsupportedTemporalTypeException;

/**
* Enumeration of the fields that makes up the date/date-time
*/
public enum Field
{
YEAR(4),
MONTH(7),
DAY(10),
HOUR(13),
MINUTE(16),
SECOND(19),
NANO(20);
YEAR(4, ChronoField.YEAR),
MONTH(7, ChronoField.MONTH_OF_YEAR),
DAY(10, ChronoField.DAY_OF_MONTH),
HOUR(13, ChronoField.HOUR_OF_DAY),
MINUTE(16, ChronoField.MINUTE_OF_HOUR),
SECOND(19, ChronoField.SECOND_OF_MINUTE),
NANO(20, ChronoField.NANO_OF_SECOND);

private final int requiredLength;
private final ChronoField chronoField;

Field(int requiredLength)
Field(int requiredLength, final ChronoField chronoField)
{
this.requiredLength = requiredLength;
this.chronoField = chronoField;
}

public static Field valueOf(Class<? extends Temporal> type)
Expand All @@ -68,8 +73,46 @@ else if (OffsetDateTime.class.equals(type))
throw new IllegalArgumentException("Type " + type.getSimpleName() + " is not supported");
}

public static Field of(TemporalField temporalField)
{
if (temporalField.equals(ChronoField.YEAR))
{
return YEAR;
}
else if (temporalField.equals(ChronoField.MONTH_OF_YEAR))
{
return MONTH;
}
else if (temporalField.equals(ChronoField.DAY_OF_MONTH))
{
return DAY;
}
else if (temporalField.equals(ChronoField.HOUR_OF_DAY))
{
return HOUR;
}
else if (temporalField.equals(ChronoField.MINUTE_OF_HOUR))
{
return MINUTE;
}
else if (temporalField.equals(ChronoField.SECOND_OF_MINUTE))
{
return SECOND;
}
else if (temporalField.equals(ChronoField.NANO_OF_SECOND))
{
return NANO;
}
throw new UnsupportedTemporalTypeException("Unsupported field: " + temporalField);
}

public int getRequiredLength()
{
return requiredLength;
}

public TemporalField toTemporalField()
{
return chronoField;
}
}
5 changes: 4 additions & 1 deletion src/main/java/com/ethlo/time/internal/EthloITU.java
Original file line number Diff line number Diff line change
Expand Up @@ -466,7 +466,10 @@ private static Object handleTime(int year, int month, int day, int hour, int min
{
fractionDigits = idx - 20;
fractions = scale(-result, fractionDigits);
offset = parseTimezone(chars, idx);
if (! raw)
{
offset = parseTimezone(chars, idx);
}
}
}
else if (remaining == 1 && (c == ZULU_UPPER || c == ZULU_LOWER))
Expand Down
Loading

0 comments on commit a7b26ca

Please sign in to comment.