Skip to content

Commit

Permalink
Cleanup
Browse files Browse the repository at this point in the history
  • Loading branch information
ethlo committed Mar 10, 2022
1 parent fccbb49 commit ccd4c0d
Show file tree
Hide file tree
Showing 22 changed files with 303 additions and 140 deletions.
102 changes: 89 additions & 13 deletions src/main/java/com/ethlo/time/DateTime.java
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
* #%L
* Internet Time Utility
* %%
* Copyright (C) 2017 - 2022 Morten Haraldsen (ethlo)
* Copyright (C) 2017 Morten Haraldsen (ethlo)
* %%
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
Expand All @@ -31,12 +31,13 @@
import java.time.OffsetDateTime;
import java.time.Year;
import java.time.YearMonth;
import java.util.Objects;
import java.util.Optional;

import com.ethlo.time.internal.LimitedCharArrayIntegerUtil;

/**
* Container class for parsed date/date-time data. The {@link #getField()} contains the highest granularity field found, like MONTH, MINUTE, SECOND.
* Container class for parsed date/date-time data. The {@link #getMostGranularField()} contains the highest granularity field found, like MONTH, MINUTE, SECOND.
*/
public class DateTime
{
Expand All @@ -63,36 +64,60 @@ public DateTime(final Field field, final int year, final int month, final int da
this.offset = offset;
}

/**
* 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, TimezoneOffset offset)
{
return new DateTime(Field.SECOND, year, month, day, hour, minute, second, 0, offset);
}

/**
* 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, TimezoneOffset offset)
{
return new DateTime(Field.NANO, year, month, day, hour, minute, second, nanos, offset);
}

/**
* Create a new instance with year granularity from the input parameters
*/
public static DateTime ofYear(int year)
{
return new DateTime(Field.YEAR, year, 0, 0, 0, 0, 0, 0, null);
}

/**
* Create a new instance with year-month granularity from the input parameters
*/
public static DateTime ofYearMonth(int years, int months)
{
return new DateTime(Field.MONTH, years, months, 0, 0, 0, 0, 0, null);
}

/**
* Create a new instance with day granularity from the input parameters
*/
public static DateTime ofDate(int years, int months, int days)
{
return new DateTime(Field.DAY, years, months, days, 0, 0, 0, 0, null);
}

/**
* Create a new instance with minute granularity from the input parameters
*/
public static DateTime of(int years, int months, int days, int hours, int minute, TimezoneOffset offset)
{
return new DateTime(Field.MINUTE, years, months, days, hours, minute, 0, 0, offset);
}

/**
* Create a new instance with data from the specified date-time.
*
* @param dateTime The date-time to copy data from
* @return A new instance
*/
public static DateTime of(OffsetDateTime dateTime)
{
return DateTime.of(dateTime.getYear(), dateTime.getMonthValue(), dateTime.getDayOfMonth(), dateTime.getHour(), dateTime.getMinute(), dateTime.getSecond(), dateTime.getNano(), TimezoneOffset.of(dateTime.getOffset()));
Expand Down Expand Up @@ -164,7 +189,7 @@ public Optional<TimezoneOffset> getOffset()
}

/**
* Creates a {@link Year} discarding any higher resolution fields
* Creates a {@link Year} discarding any higher granularity fields
*
* @return the {@link Year}
*/
Expand All @@ -174,7 +199,7 @@ public Year toYear()
}

/**
* Creates a {@link YearMonth} discarding any higher resolution fields
* Creates a {@link YearMonth} discarding any higher granularity fields
*
* @return the {@link YearMonth}
*/
Expand Down Expand Up @@ -205,13 +230,13 @@ public OffsetDateTime toOffsetDatetime()
assertMinGranularity(Field.MINUTE);
if (offset != null)
{
return OffsetDateTime.of(year, month, day, hour, minute, second, nano, offset.asJavaTimeOffset());
return OffsetDateTime.of(year, month, day, hour, minute, second, nano, offset.toZoneOffset());
}
throw new DateTimeException("No zone offset information found");
}

/**
* Creates a {@link LocalDate}, discarding any higher resolution fields
* Creates a {@link LocalDate}, discarding any higher granularity fields
*
* @return the {@link LocalDate}
*/
Expand All @@ -222,39 +247,50 @@ public LocalDate toLocalDate()
}

/**
* Returns the minimum field found during parsing
* Returns the most granular field found during parsing
*
* @return The minimum field found
* @return The field found
*/
public Field getField()
public Field getMostGranularField()
{
return field;
}

public DateTime assertMinGranularity(Field field)
private void assertMinGranularity(Field field)
{
if (!includesGranularity(field))
{
throw new DateTimeException("No " + field.name() + " field found");
}
return this;
}

/**
* Formats this date-time as an ISO formatted string with the last included field as specified.
*
* @param lastIncluded The last specified field to include
* @return The formatted date/date-time string
*/
public String toString(final Field lastIncluded)
{
return toString(this, lastIncluded, 0);
}

/**
* Formats this date-time as an RFC-3339 compatible string with the specified number of fractions in the second.
*
* @param fractionDigits The number of fractions to include
* @return The formatted date/date-time string
*/
public String toString(final int fractionDigits)
{
return toString(this, Field.NANO, fractionDigits);
}

private String toString(final DateTime date, final Field lastIncluded, final int fractionDigits)
{
if (lastIncluded.ordinal() > date.getField().ordinal())
if (lastIncluded.ordinal() > date.getMostGranularField().ordinal())
{
throw new DateTimeException("Requested granularity was " + lastIncluded.name() + ", but contains only granularity " + date.getField().name());
throw new DateTimeException("Requested granularity was " + lastIncluded.name() + ", but contains only granularity " + date.getMostGranularField().name());
}
final TimezoneOffset tz = date.getOffset().orElse(null);
final char[] buffer = new char[35];
Expand Down Expand Up @@ -330,9 +366,49 @@ private String toString(final DateTime date, final Field lastIncluded, final int
return finish(buffer, 20 + fractionDigits, tz);
}

/**
* Formats this date-time as a date/date-time with the same fields as was parsed and no fractions in the second.
*
* @return The formatted date/date-time string
*/
@Override
public String toString()
{
return toString(field);
}

/**
* * @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
&& field == dateTime.field
&& Objects.equals(offset, dateTime.offset);
}

/**
* @hidden
*/
@Override
public int hashCode()
{
return Objects.hash(field.ordinal(), year, month, day, hour, minute, second, nano, offset);
}
}
100 changes: 85 additions & 15 deletions src/main/java/com/ethlo/time/ITU.java
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
* #%L
* Internet Time Utility
* %%
* Copyright (C) 2017 - 2018 Morten Haraldsen (ethlo)
* Copyright (C) 2017 Morten Haraldsen (ethlo)
* %%
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
Expand All @@ -30,7 +30,7 @@
import com.ethlo.time.internal.EthloITU;

/**
* The main class for accessing the functions in this library
* The main access to the parse and formatting functions in this library.
*/
public class ITU
{
Expand Down Expand Up @@ -86,40 +86,98 @@ public static boolean isValid(String text)
* Format the input as an RFC-3339 formatted date-time in the UTC timezone
*
* @param offsetDateTime The date-time to format
* @param fractionDigits The Nuber of fraction digits in the second
* @return A formatted string
* @param fractionDigits The number of fraction digits in the second field
* @return The formatted string
*/
public static String formatUtc(OffsetDateTime offsetDateTime, int fractionDigits)
{
return delegate.formatUtc(offsetDateTime, fractionDigits);
}

public static String formatUtc(OffsetDateTime date, Field lastIncluded)
/**
* Format the input as an ISO format string, limited to the granularity of the specified field, in the UTC timezone.
* @param offsetDateTime The date-time to format
* @param lastIncluded The last included field
* @return The formatted string
*/
public static String formatUtc(OffsetDateTime offsetDateTime, Field lastIncluded)
{
return delegate.formatUtc(date, lastIncluded);
return delegate.formatUtc(offsetDateTime, lastIncluded);
}

public static String formatUtc(OffsetDateTime date)
/**
* Format the input as an RFC-3339 formatted date-time in the timezone of the input.
*
* @param offsetDateTime The date-time to format
* @return The formatted string
*/
public static String format(OffsetDateTime offsetDateTime)
{
return delegate.formatUtc(date);
return delegate.format(offsetDateTime, offsetDateTime.getOffset(), 0);
}

public static String formatUtcMilli(OffsetDateTime date)
/**
* Format the input as an RFC-3339 formatted date-time in the timezone of the input, with the specified number of fraction digits.
*
* @param offsetDateTime The date-time to format
* @param fractionDigits The number of fraction digits in the second field
* @return The formatted string
*/
public static String format(OffsetDateTime offsetDateTime, int fractionDigits)
{
return delegate.formatUtcMilli(date);
return delegate.format(offsetDateTime, offsetDateTime.getOffset(), fractionDigits);
}

public static String formatUtcMicro(OffsetDateTime date)
/**
* Format the input as an RFC-3339 formatted date-time in the UTC timezone with second resolution.
*
* @param offsetDateTime The date-time to format.
* @return The formatted string with second resolution.
*/
public static String formatUtc(OffsetDateTime offsetDateTime)
{
return delegate.formatUtcMicro(date);
return delegate.formatUtc(offsetDateTime);
}

public static String formatUtcNano(OffsetDateTime date)
/**
* Format the input as an RFC-3339 formatted date-time in the UTC timezone with millisecond resolution.
*
* @param offsetDateTime The date-time to format.
* @return The formatted string with millisecond resolution.
*/
public static String formatUtcMilli(final OffsetDateTime offsetDateTime)
{
return delegate.formatUtcNano(date);
return delegate.formatUtcMilli(offsetDateTime);
}

public static void parse(String text, TemporalConsumer temporalConsumer)
/**
* Format the input as an RFC-3339 formatted date-time in the UTC timezone with microsecond resolution.
*
* @param offsetDateTime The date-time to format
* @return The formatted string with microsecond resolution
*/
public static String formatUtcMicro(final OffsetDateTime offsetDateTime)
{
return delegate.formatUtcMicro(offsetDateTime);
}

/**
* Format the input as an RFC-3339 formatted date-time in the UTC timezone with nanosecond resolution
*
* @param offsetDateTime The date-time to format
* @return The formatted string with nanosecond resolution
*/
public static String formatUtcNano(final OffsetDateTime offsetDateTime)
{
return delegate.formatUtcNano(offsetDateTime);
}

/**
* Parse the input, and use callbacks for the type of date/date-time it contains. This allows you to handle different granularity inputs with ease!
* @param text The text to parse as a date/date-time
* @param temporalConsumer The consumer of the found date/date-time
*/
public static void parse(final String text, final TemporalConsumer temporalConsumer)
{
final DateTime dateTime = delegate.parse(text);
if (dateTime.includesGranularity(Field.MINUTE))
Expand Down Expand Up @@ -147,6 +205,12 @@ else if (dateTime.includesGranularity(Field.MONTH))
}
}

/**
* Parse the input, and use callbacks for the type of date/date-time it contains. This allows you to handle different granularity inputs with ease!
* @param text The text to parse as a date/date-time
* @param temporalHandler The handler of the found date/date-time
*/

public static <T> T parse(String text, TemporalHandler<T> temporalHandler)
{
final DateTime dateTime = delegate.parse(text);
Expand Down Expand Up @@ -175,6 +239,12 @@ else if (dateTime.includesGranularity(Field.MONTH))
}
}

/**
* Check if the input is valid for one of the specified types
* @param text The input to check
* @param types The types that are considered valid
* @return True if valid, otherwise false
*/
public static boolean isValid(final String text, TemporalType... types)
{
try
Expand Down
2 changes: 1 addition & 1 deletion src/main/java/com/ethlo/time/LeapSecondException.java
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
* #%L
* Internet Time Utility
* %%
* Copyright (C) 2017 - 2019 Morten Haraldsen (ethlo)
* Copyright (C) 2017 Morten Haraldsen (ethlo)
* %%
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
Expand Down
Loading

0 comments on commit ccd4c0d

Please sign in to comment.