StringToJavaTimeConverter.java

/*
 * Copyright 2022 Global Crop Diversity Trust
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *   http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
package org.genesys.blocks.util;

import org.springframework.context.i18n.LocaleContextHolder;
import org.springframework.core.convert.converter.Converter;

import java.time.Instant;
import java.time.LocalDate;
import java.time.LocalDateTime;
import java.time.OffsetDateTime;
import java.time.ZoneId;
import java.time.ZoneOffset;
import java.time.ZonedDateTime;
import java.time.format.DateTimeFormatter;
import java.time.format.DateTimeParseException;
import java.time.temporal.TemporalAccessor;
import java.util.TimeZone;

public abstract class StringToJavaTimeConverter {

	public static class StringToInstantConverter implements Converter<String, Instant> {
		public static StringToInstantConverter INSTANCE = new StringToInstantConverter();

		private StringToInstantConverter() {
			super();
		}

		@Override
		public Instant convert(final String source) {
			if (source.length() == 10 ) {
				return convertDateToInstantWithTimeZone(source);
			}
			
			TemporalAccessor dt;
			try {
				dt = DateTimeFormatter.ISO_DATE_TIME.parseBest(source, OffsetDateTime::from, ZonedDateTime::from, LocalDateTime::from, LocalDate::from);
			} catch (Exception e) {
				return LocalDate.ofInstant(Instant.ofEpochMilli(Long.parseLong(source)), ZoneId.systemDefault()).atStartOfDay().toInstant(ZoneOffset.UTC);
			}
			
			if (dt instanceof OffsetDateTime) {
				return ((OffsetDateTime) dt).toInstant();
			} else if (dt instanceof LocalDateTime) {
				return ((LocalDateTime) dt).toInstant(ZoneOffset.UTC);
			} else if (dt instanceof ZonedDateTime) {
				return ((ZonedDateTime) dt).toInstant();
			} else if (dt instanceof LocalDate) {
				return ((LocalDate) dt).atStartOfDay().toInstant(ZoneOffset.UTC);
			} else {
				throw new DateTimeParseException("IDK", source, 0);
			}
		}

		private Instant convertDateToInstantWithTimeZone(String date) {
			TimeZone timeZone = LocaleContextHolder.getTimeZone();
			return LocalDate.parse(date).atStartOfDay(timeZone.toZoneId()).withZoneSameInstant(ZoneId.of("UTC")).toInstant();
		}
	}

	public static class StringToLocalDateConverter implements Converter<String, LocalDate> {
		public static StringToLocalDateConverter INSTANCE = new StringToLocalDateConverter();

		private StringToLocalDateConverter() {
			super();
		}

		@Override
		public LocalDate convert(String source) {
			if (source.length() == 10 ) {
				return LocalDate.parse(source);
			}
			return LocalDate.parse(source.subSequence(0, 10));
		}
	}
	
}