Skip to content

Commit

Permalink
Merge pull request #92 from kpartlow/master
Browse files Browse the repository at this point in the history
Single ZoneId
  • Loading branch information
jdereg committed Feb 6, 2024
2 parents dd40b6e + 38b60c7 commit c2a4a6b
Show file tree
Hide file tree
Showing 8 changed files with 375 additions and 352 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -43,11 +43,13 @@ static long toLong(Object from) {
}

static Instant toInstant(Object from) {
return ((Calendar)from).toInstant();
Calendar calendar = (Calendar)from;
return calendar.toInstant();
}

static ZonedDateTime toZonedDateTime(Object from, ConverterOptions options) {
return toInstant(from).atZone(options.getZoneId());
Calendar calendar = (Calendar)from;
return calendar.toInstant().atZone(calendar.getTimeZone().toZoneId());
}

static ZonedDateTime toZonedDateTime(Object from, Converter converter, ConverterOptions options) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,11 @@

import java.nio.charset.Charset;
import java.nio.charset.StandardCharsets;
import java.time.LocalDate;
import java.time.LocalDateTime;
import java.time.ZoneId;
import java.util.Locale;
import java.util.TimeZone;
import java.util.concurrent.ConcurrentHashMap;

/**
* @author Kenny Partlow ([email protected])
Expand All @@ -28,12 +29,8 @@ public interface ConverterOptions {


/**
* @return zoneId to use for source conversion when on is not provided on the source (Date, Instant, etc.)
*/
default ZoneId getSourceZoneIdForLocalDates() { return ZoneId.systemDefault(); }

/**
* @return zoneId expected on the target when finished (only for types that support ZoneId or TimeZone)
* @return {@link ZoneId} to use for source conversion when one is not provided and is required on the target
* type. ie. {@link LocalDateTime}, {@link LocalDate}, or {@link String} when no zone is provided.
*/
default ZoneId getZoneId() { return ZoneId.systemDefault(); }

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@ public final class LocalDateConversions {
private LocalDateConversions() {}

private static ZonedDateTime toZonedDateTime(Object from, ConverterOptions options) {
return ((LocalDate)from).atStartOfDay(options.getSourceZoneIdForLocalDates()).withZoneSameInstant(options.getZoneId());
return ((LocalDate)from).atStartOfDay(options.getZoneId());
}

static Instant toInstant(Object from, ConverterOptions options) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@ public final class LocalDateTimeConversions {
private LocalDateTimeConversions() {}

private static ZonedDateTime toZonedDateTime(Object from, ConverterOptions options) {
return ((LocalDateTime)from).atZone(options.getSourceZoneIdForLocalDates()).withZoneSameInstant(options.getZoneId());
return ((LocalDateTime)from).atZone(options.getZoneId());
}

private static Instant toInstant(Object from, ConverterOptions options) {
Expand Down
102 changes: 39 additions & 63 deletions src/main/java/com/cedarsoftware/util/convert/StringConversions.java
Original file line number Diff line number Diff line change
@@ -1,5 +1,9 @@
package com.cedarsoftware.util.convert;

import com.cedarsoftware.util.ClassUtilities;
import com.cedarsoftware.util.DateUtilities;
import com.cedarsoftware.util.StringUtilities;

import java.math.BigDecimal;
import java.math.BigInteger;
import java.math.RoundingMode;
Expand All @@ -25,17 +29,14 @@
import java.util.Calendar;
import java.util.Date;
import java.util.GregorianCalendar;
import java.util.Optional;
import java.util.UUID;
import java.util.concurrent.atomic.AtomicBoolean;
import java.util.concurrent.atomic.AtomicInteger;
import java.util.concurrent.atomic.AtomicLong;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

import com.cedarsoftware.util.ClassUtilities;
import com.cedarsoftware.util.DateUtilities;
import com.cedarsoftware.util.StringUtilities;

import static com.cedarsoftware.util.ArrayUtilities.EMPTY_BYTE_ARRAY;
import static com.cedarsoftware.util.ArrayUtilities.EMPTY_CHAR_ARRAY;

Expand Down Expand Up @@ -329,43 +330,30 @@ static Period toPeriod(Object from, Converter converter, ConverterOptions option
}

static Date toDate(Object from, Converter converter, ConverterOptions options) {
Instant instant = getInstant((String) from, options);
if (instant == null) {
return null;
}
// Bring the zonedDateTime to a user-specifiable timezone
return Date.from(instant);
Instant instant = toInstant(from, options);
return instant == null ? null : Date.from(instant);
}

static java.sql.Date toSqlDate(Object from, Converter converter, ConverterOptions options) {
Instant instant = getInstant((String) from, options);
if (instant == null) {
return null;
}
return new java.sql.Date(instant.toEpochMilli());
Instant instant = toInstant(from, options);
return instant == null ? null : new java.sql.Date(instant.toEpochMilli());
}

static Timestamp toTimestamp(Object from, Converter converter, ConverterOptions options) {
Instant instant = getInstant((String) from, options);
if (instant == null) {
return null;
}
return Timestamp.from(instant);
Instant instant = toInstant(from, options);
return instant == null ? null : new Timestamp(instant.toEpochMilli());
}

static Calendar toCalendar(Object from, Converter converter, ConverterOptions options) {
ZonedDateTime time = toZonedDateTime(from, options);
return time == null ? null : GregorianCalendar.from(time);
return parseDate(from, options).map(GregorianCalendar::from).orElse(null);
}

static LocalDate toLocalDate(Object from, Converter converter, ConverterOptions options) {
ZonedDateTime time = toZonedDateTime(from, options);
return time == null ? null : time.toLocalDate();
return parseDate(from, options).map(ZonedDateTime::toLocalDate).orElse(null);
}

static LocalDateTime toLocalDateTime(Object from, Converter converter, ConverterOptions options) {
ZonedDateTime time = toZonedDateTime(from, options);
return time == null ? null : time.toLocalDateTime();
return parseDate(from, options).map(ZonedDateTime::toLocalDateTime).orElse(null);
}

static LocalTime toLocalTime(Object from, Converter converter, ConverterOptions options) {
Expand All @@ -377,22 +365,29 @@ static LocalTime toLocalTime(Object from, Converter converter, ConverterOptions
try {
return LocalTime.parse(str);
} catch (Exception e) {
ZonedDateTime zdt = DateUtilities.parseDate(str, options.getSourceZoneIdForLocalDates(), true);
return zdt.toLocalTime();
return parseDate(str, options).map(ZonedDateTime::toLocalTime).orElse(null);
}
}

static ZonedDateTime toZonedDateTime(Object from, ConverterOptions options) {
Instant instant = getInstant((String) from, options);
if (instant == null) {
return null;
private static Optional<ZonedDateTime> parseDate(Object from, ConverterOptions options) {
String str = StringUtilities.trimToNull(asString(from));

if (str == null) {
return Optional.empty();
}
return instant.atZone(options.getZoneId());

ZonedDateTime zonedDateTime = DateUtilities.parseDate(str, options.getZoneId(), true);

if (zonedDateTime == null) {
return Optional.empty();
}

return Optional.of(zonedDateTime);
}


static ZonedDateTime toZonedDateTime(Object from, Converter converter, ConverterOptions options) {
return toZonedDateTime(from, options);
return parseDate(from, options).orElse(null);
}

static ZoneId toZoneId(Object from, Converter converter, ConverterOptions options) {
Expand Down Expand Up @@ -422,16 +417,7 @@ static ZoneOffset toZoneOffset(Object from, Converter converter, ConverterOption
}

static OffsetDateTime toOffsetDateTime(Object from, Converter converter, ConverterOptions options) {
String s = StringUtilities.trimToNull(asString(from));
if (s == null) {
return null;
}

try {
return OffsetDateTime.parse(s, DateTimeFormatter.ISO_OFFSET_DATE_TIME);
} catch (Exception e) {
return toZonedDateTime(from, options).toOffsetDateTime();
}
return parseDate(from, options).map(ZonedDateTime::toOffsetDateTime).orElse(null);
}

static OffsetTime toOffsetTime(Object from, Converter converter, ConverterOptions options) {
Expand All @@ -443,30 +429,20 @@ static OffsetTime toOffsetTime(Object from, Converter converter, ConverterOption
try {
return OffsetTime.parse(s, DateTimeFormatter.ISO_OFFSET_TIME);
} catch (Exception e) {
return toZonedDateTime(from, options).toOffsetDateTime().toOffsetTime();
OffsetDateTime dateTime = toOffsetDateTime(from, converter, options);
if (dateTime == null) {
return null;
}
return dateTime.toOffsetTime();
}
}

static Instant toInstant(Object from, Converter converter, ConverterOptions options) {
String s = StringUtilities.trimToNull(asString(from));
if (s == null) {
return null;
}

try {
return Instant.parse(s);
} catch (Exception e) {
return getInstant(s, options);
}
private static Instant toInstant(Object from, ConverterOptions options) {
return parseDate(from, options).map(ZonedDateTime::toInstant).orElse(null);
}

private static Instant getInstant(String from, ConverterOptions options) {
String str = StringUtilities.trimToNull(from);
if (str == null) {
return null;
}
ZonedDateTime dateTime = DateUtilities.parseDate(str, options.getSourceZoneIdForLocalDates(), true);
return dateTime.toInstant();
static Instant toInstant(Object from, Converter converter, ConverterOptions options) {
return toInstant(from, options);
}

static char[] toCharArray(Object from, Converter converter, ConverterOptions options) {
Expand Down
27 changes: 13 additions & 14 deletions src/test/java/com/cedarsoftware/util/TestDateUtilities.java
Original file line number Diff line number Diff line change
@@ -1,5 +1,11 @@
package com.cedarsoftware.util;

import org.junit.jupiter.api.Test;
import org.junit.jupiter.params.ParameterizedTest;
import org.junit.jupiter.params.provider.Arguments;
import org.junit.jupiter.params.provider.MethodSource;
import org.junit.jupiter.params.provider.ValueSource;

import java.lang.reflect.Constructor;
import java.lang.reflect.Modifier;
import java.text.SimpleDateFormat;
Expand All @@ -11,12 +17,6 @@
import java.util.TimeZone;
import java.util.stream.Stream;

import org.junit.jupiter.api.Test;
import org.junit.jupiter.params.ParameterizedTest;
import org.junit.jupiter.params.provider.Arguments;
import org.junit.jupiter.params.provider.MethodSource;
import org.junit.jupiter.params.provider.ValueSource;

import static org.assertj.core.api.Assertions.assertThatThrownBy;
import static org.junit.jupiter.api.Assertions.assertEquals;
import static org.junit.jupiter.api.Assertions.assertNotEquals;
Expand Down Expand Up @@ -843,15 +843,14 @@ private static Stream provideTimeZones()
@MethodSource("provideTimeZones")
void testTimeZoneParsing(String exampleZone, Long epochMilli)
{
for (int i=0; i < 1; i++) {
Date date = DateUtilities.parseDate(exampleZone);
assertEquals(date.getTime(), epochMilli);
Date date = DateUtilities.parseDate(exampleZone);
assertEquals(date.getTime(), epochMilli);

TemporalAccessor dateTime = DateUtilities.parseDate(exampleZone, ZoneId.systemDefault(), true);
ZonedDateTime zdt = (ZonedDateTime) dateTime;

assertEquals(zdt.toInstant().toEpochMilli(), epochMilli);
}
TemporalAccessor dateTime = DateUtilities.parseDate(exampleZone, ZoneId.systemDefault(), true);
ZonedDateTime zdt = (ZonedDateTime) dateTime;

assertEquals(zdt.toInstant().toEpochMilli(), epochMilli);
}

@Test
Expand Down Expand Up @@ -936,4 +935,4 @@ void testFormatsThatShouldNotWork(String badFormat)
{
DateUtilities.parseDate(badFormat, ZoneId.systemDefault(), true);
}
}
}
Loading

0 comments on commit c2a4a6b

Please sign in to comment.