Skip to content

Commit

Permalink
Have DateTime implement TemporalAccessor (#20)
Browse files Browse the repository at this point in the history
* Have DateTime implement TemporalAccessor

---------

Co-authored-by: Morten Haraldsen <dev@ethlo.com>
  • Loading branch information
ethlo and Morten Haraldsen committed Jan 5, 2024
1 parent 7b862ca commit a78549d
Show file tree
Hide file tree
Showing 5 changed files with 258 additions and 85 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 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);
}
}
36 changes: 36 additions & 0 deletions src/main/java/com/ethlo/time/Field.java
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,10 @@
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
Expand Down Expand Up @@ -72,4 +75,37 @@ public int getRequiredLength()
{
return requiredLength;
}

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);
}
}
14 changes: 7 additions & 7 deletions src/main/java/com/ethlo/time/internal/EthloITU.java
Original file line number Diff line number Diff line change
Expand Up @@ -130,14 +130,11 @@ private static Object handleTime(String chars, int year, int month, int day, int
case ZULU_UPPER:
case ZULU_LOWER:
final TimezoneOffset zoneOffset = parseTimezone(chars, 16);
if (! raw)
if (!raw)
{
throw raiseMissingField(Field.SECOND);
}
return DateTime.of(year, month, day, hour, minute, zoneOffset);

default:
assertPositionContains(chars, 16, TIME_SEPARATOR, PLUS, MINUS, ZULU_UPPER);
}
throw new DateTimeException(chars);
}
Expand Down Expand Up @@ -373,7 +370,7 @@ private static Object parse(String chars, boolean raw)
final int months = parsePositiveInt(chars, 5, 7);
if (7 == len)
{
if (! raw)
if (!raw)
{
throw raiseMissingField(Field.SECOND);
}
Expand All @@ -385,7 +382,7 @@ private static Object parse(String chars, boolean raw)
final int days = parsePositiveInt(chars, 8, 10);
if (10 == len)
{
if (! raw)
if (!raw)
{
throw raiseMissingField(Field.SECOND);
}
Expand Down Expand Up @@ -466,7 +463,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
Original file line number Diff line number Diff line change
Expand Up @@ -129,17 +129,4 @@ private static void copy(char[] buf, int srcPos, char[] target, int offset, int
{
System.arraycopy(buf, srcPos, target, offset, length);
}

public static int indexOfNonDigit(final char[] text, int offset)
{
for (int i = offset; i < text.length; i++)
{
char c = text[i];
if (c < ZERO || c > DIGIT_9)
{
return i;
}
}
return -1;
}
}
Loading

0 comments on commit a78549d

Please sign in to comment.