Skip to content

Commit

Permalink
Cleanup
Browse files Browse the repository at this point in the history
  • Loading branch information
Morten Haraldsen committed Jan 22, 2024
1 parent ad450e9 commit d78d989
Show file tree
Hide file tree
Showing 7 changed files with 139 additions and 141 deletions.
2 changes: 1 addition & 1 deletion pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ limitations under the License.
<groupId>com.ethlo.time</groupId>
<packaging>bundle</packaging>
<artifactId>itu</artifactId>
<version>1.7.6</version>
<version>1.7.7-SNAPSHOT</version>
<name>Internet Time Utility</name>
<description>Very fast date-time parser and formatter - RFC 3339 (ISO 8601 profile) and W3C format
</description>
Expand Down
10 changes: 5 additions & 5 deletions src/main/java/com/ethlo/time/Field.java
Original file line number Diff line number Diff line change
Expand Up @@ -71,11 +71,6 @@ else if (OffsetDateTime.class.equals(type))
throw new IllegalArgumentException("Type " + type.getSimpleName() + " is not supported");
}

public int getRequiredLength()
{
return requiredLength;
}

public static Field of(TemporalField temporalField)
{
if (temporalField.equals(ChronoField.YEAR))
Expand Down Expand Up @@ -108,4 +103,9 @@ else if (temporalField.equals(ChronoField.NANO_OF_SECOND))
}
throw new UnsupportedTemporalTypeException("Unsupported field: " + temporalField);
}

public int getRequiredLength()
{
return requiredLength;
}
}
250 changes: 125 additions & 125 deletions src/main/java/com/ethlo/time/internal/EthloITU.java
Original file line number Diff line number Diff line change
Expand Up @@ -153,15 +153,15 @@ private static void assertPositionContains(String chars, int offset, char expect
}
}

private static void assertPositionContains(String chars, int offset, char... expected)
private static void assertPositionContains(String chars, char... expected)
{
if (offset >= chars.length())
if (10 >= chars.length())
{
raiseDateTimeException(chars, "Unexpected end of input");
}

boolean found = false;
final char needle = chars.charAt(offset);
final char needle = chars.charAt(10);
for (char e : expected)
{
if (needle == e)
Expand All @@ -173,7 +173,7 @@ private static void assertPositionContains(String chars, int offset, char... exp
if (!found)
{
throw new DateTimeException("Expected character " + Arrays.toString(expected)
+ " at position " + (offset + 1) + " '" + chars + "'");
+ " at position " + (10 + 1) + " '" + chars + "'");
}
}

Expand Down Expand Up @@ -227,126 +227,6 @@ private static void assertNoMoreChars(String chars, int lastUsed)
}
}

@Override
public String formatUtc(OffsetDateTime date, int fractionDigits)
{
return doFormat(date, ZoneOffset.UTC, Field.SECOND, fractionDigits);
}

@Override
public String formatUtc(OffsetDateTime date, Field lastIncluded)
{
return doFormat(date, ZoneOffset.UTC, lastIncluded, 0);
}

@Override
public String format(OffsetDateTime date, ZoneOffset adjustTo, final int fractionDigits)
{
return doFormat(date, adjustTo, Field.NANO, fractionDigits);
}

private String doFormat(OffsetDateTime date, ZoneOffset adjustTo, Field lastIncluded, int fractionDigits)
{
assertMaxFractionDigits(fractionDigits);

OffsetDateTime adjusted = date;
if (!date.getOffset().equals(adjustTo))
{
adjusted = date.atZoneSameInstant(adjustTo).toOffsetDateTime();
}
final TimezoneOffset tz = TimezoneOffset.of(adjustTo);

final char[] buffer = new char[31];

if (handleDatePart(lastIncluded, buffer, adjusted.getYear(), 0, 4, Field.YEAR))
{
return finish(buffer, Field.YEAR.getRequiredLength(), null);
}

buffer[4] = DATE_SEPARATOR;
if (handleDatePart(lastIncluded, buffer, adjusted.getMonthValue(), 5, 2, Field.MONTH))
{
return finish(buffer, Field.MONTH.getRequiredLength(), null);
}

buffer[7] = DATE_SEPARATOR;
if (handleDatePart(lastIncluded, buffer, adjusted.getDayOfMonth(), 8, 2, Field.DAY))
{
return finish(buffer, Field.DAY.getRequiredLength(), null);
}

// T separator
buffer[10] = SEPARATOR_UPPER;

// Time
LimitedCharArrayIntegerUtil.toString(adjusted.getHour(), buffer, 11, 2);
buffer[13] = TIME_SEPARATOR;
if (handleDatePart(lastIncluded, buffer, adjusted.getMinute(), 14, 2, Field.MINUTE))
{
return finish(buffer, Field.MINUTE.getRequiredLength(), tz);
}
buffer[16] = TIME_SEPARATOR;
LimitedCharArrayIntegerUtil.toString(adjusted.getSecond(), buffer, 17, 2);

// Second fractions
final boolean hasFractionDigits = fractionDigits > 0;
if (hasFractionDigits)
{
buffer[19] = FRACTION_SEPARATOR;
addFractions(buffer, fractionDigits, adjusted.getNano());
return finish(buffer, 20 + fractionDigits, tz);
}
return finish(buffer, 19, tz);
}

private boolean handleDatePart(final Field lastIncluded, final char[] buffer, final int value, final int offset, final int length, final Field field)
{
LimitedCharArrayIntegerUtil.toString(value, buffer, offset, length);
return lastIncluded == field;
}

private void addFractions(char[] buf, int fractionDigits, int nano)
{
final double d = widths[fractionDigits - 1];
LimitedCharArrayIntegerUtil.toString((int) (nano / d), buf, 20, fractionDigits);
}

@Override
public OffsetDateTime parseDateTime(final String dateTime)
{
return (OffsetDateTime) parse(dateTime, false);
}

@Override
public String formatUtcMilli(OffsetDateTime date)
{
return formatUtc(date, 3);
}

@Override
public String formatUtcMicro(OffsetDateTime date)
{
return formatUtc(date, 6);
}

@Override
public String formatUtcNano(OffsetDateTime date)
{
return formatUtc(date, 9);
}

@Override
public String formatUtc(OffsetDateTime date)
{
return formatUtc(date, 0);
}

@Override
public DateTime parse(String chars)
{
return (DateTime) parse(chars, true);
}

private static Object parse(String chars, boolean raw)
{
if (chars == null)
Expand Down Expand Up @@ -390,7 +270,7 @@ private static Object parse(String chars, boolean raw)
}

// HOURS
assertPositionContains(chars, 10, SEPARATOR_UPPER, SEPARATOR_LOWER, SEPARATOR_SPACE);
assertPositionContains(chars, SEPARATOR_UPPER, SEPARATOR_LOWER, SEPARATOR_SPACE);
final int hours = parsePositiveInt(chars, 11, 13);

// MINUTES
Expand Down Expand Up @@ -522,4 +402,124 @@ private static void raiseDateTimeException(String chars, String message)
{
throw new DateTimeException(message + ": " + chars);
}

@Override
public String formatUtc(OffsetDateTime date, int fractionDigits)
{
return doFormat(date, ZoneOffset.UTC, Field.SECOND, fractionDigits);
}

@Override
public String formatUtc(OffsetDateTime date, Field lastIncluded)
{
return doFormat(date, ZoneOffset.UTC, lastIncluded, 0);
}

@Override
public String format(OffsetDateTime date, ZoneOffset adjustTo, final int fractionDigits)
{
return doFormat(date, adjustTo, Field.NANO, fractionDigits);
}

private String doFormat(OffsetDateTime date, ZoneOffset adjustTo, Field lastIncluded, int fractionDigits)
{
assertMaxFractionDigits(fractionDigits);

OffsetDateTime adjusted = date;
if (!date.getOffset().equals(adjustTo))
{
adjusted = date.atZoneSameInstant(adjustTo).toOffsetDateTime();
}
final TimezoneOffset tz = TimezoneOffset.of(adjustTo);

final char[] buffer = new char[31];

if (handleDatePart(lastIncluded, buffer, adjusted.getYear(), 0, 4, Field.YEAR))
{
return finish(buffer, Field.YEAR.getRequiredLength(), null);
}

buffer[4] = DATE_SEPARATOR;
if (handleDatePart(lastIncluded, buffer, adjusted.getMonthValue(), 5, 2, Field.MONTH))
{
return finish(buffer, Field.MONTH.getRequiredLength(), null);
}

buffer[7] = DATE_SEPARATOR;
if (handleDatePart(lastIncluded, buffer, adjusted.getDayOfMonth(), 8, 2, Field.DAY))
{
return finish(buffer, Field.DAY.getRequiredLength(), null);
}

// T separator
buffer[10] = SEPARATOR_UPPER;

// Time
LimitedCharArrayIntegerUtil.toString(adjusted.getHour(), buffer, 11, 2);
buffer[13] = TIME_SEPARATOR;
if (handleDatePart(lastIncluded, buffer, adjusted.getMinute(), 14, 2, Field.MINUTE))
{
return finish(buffer, Field.MINUTE.getRequiredLength(), tz);
}
buffer[16] = TIME_SEPARATOR;
LimitedCharArrayIntegerUtil.toString(adjusted.getSecond(), buffer, 17, 2);

// Second fractions
final boolean hasFractionDigits = fractionDigits > 0;
if (hasFractionDigits)
{
buffer[19] = FRACTION_SEPARATOR;
addFractions(buffer, fractionDigits, adjusted.getNano());
return finish(buffer, 20 + fractionDigits, tz);
}
return finish(buffer, 19, tz);
}

private boolean handleDatePart(final Field lastIncluded, final char[] buffer, final int value, final int offset, final int length, final Field field)
{
LimitedCharArrayIntegerUtil.toString(value, buffer, offset, length);
return lastIncluded == field;
}

private void addFractions(char[] buf, int fractionDigits, int nano)
{
final double d = widths[fractionDigits - 1];
LimitedCharArrayIntegerUtil.toString((int) (nano / d), buf, 20, fractionDigits);
}

@Override
public OffsetDateTime parseDateTime(final String dateTime)
{
return (OffsetDateTime) parse(dateTime, false);
}

@Override
public String formatUtcMilli(OffsetDateTime date)
{
return formatUtc(date, 3);
}

@Override
public String formatUtcMicro(OffsetDateTime date)
{
return formatUtc(date, 6);
}

@Override
public String formatUtcNano(OffsetDateTime date)
{
return formatUtc(date, 9);
}

@Override
public String formatUtc(OffsetDateTime date)
{
return formatUtc(date, 0);
}

@Override
public DateTime parse(String chars)
{
return (DateTime) parse(chars, true);
}
}
4 changes: 2 additions & 2 deletions src/test/java/com/ethlo/time/AbstractTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -20,11 +20,11 @@
* #L%
*/

import org.junit.jupiter.api.BeforeEach;

import com.ethlo.time.internal.Rfc3339Formatter;
import com.ethlo.time.internal.Rfc3339Parser;

import org.junit.jupiter.api.BeforeEach;

public abstract class AbstractTest
{
protected Rfc3339Parser parser;
Expand Down
8 changes: 4 additions & 4 deletions src/test/java/com/ethlo/time/CharArrayUtilTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -20,13 +20,13 @@
* #L%
*/

import static org.assertj.core.api.Assertions.assertThat;

import java.util.Arrays;
import com.ethlo.time.internal.LimitedCharArrayIntegerUtil;

import org.junit.jupiter.api.Test;

import com.ethlo.time.internal.LimitedCharArrayIntegerUtil;
import java.util.Arrays;

import static org.assertj.core.api.Assertions.assertThat;

public class CharArrayUtilTest
{
Expand Down
1 change: 0 additions & 1 deletion src/test/java/com/ethlo/time/CorrectnessTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,6 @@
import java.time.OffsetDateTime;
import java.time.temporal.ChronoField;
import java.time.temporal.TemporalAccessor;
import java.time.temporal.TemporalField;

import org.junit.jupiter.api.Tag;
import org.junit.jupiter.api.Test;
Expand Down
5 changes: 2 additions & 3 deletions src/test/java/com/ethlo/time/W3cCorrectnessTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -31,14 +31,13 @@
import java.time.YearMonth;
import java.time.ZoneOffset;

import com.ethlo.time.internal.Rfc3339;
import com.ethlo.time.internal.Rfc3339Formatter;

import org.junit.jupiter.api.Assertions;
import org.junit.jupiter.api.Tag;
import org.junit.jupiter.api.Test;

import com.ethlo.time.internal.EthloITU;
import com.ethlo.time.internal.Rfc3339;
import com.ethlo.time.internal.Rfc3339Formatter;
import com.ethlo.time.internal.W3cDateTimeUtil;

@Tag("CorrectnessTest")
Expand Down

0 comments on commit d78d989

Please sign in to comment.