All Downloads are FREE. Search and download functionalities are using the official Maven repository.

de.bytefish.pgbulkinsert.pgsql.handlers.ZonedDateTimeValueHandler Maven / Gradle / Ivy

There is a newer version: 7.0.1
Show newest version
package de.bytefish.pgbulkinsert.pgsql.handlers;

import de.bytefish.pgbulkinsert.pgsql.converter.IValueConverter;
import de.bytefish.pgbulkinsert.pgsql.converter.LocalDateTimeConverter;

import java.io.DataOutputStream;
import java.time.LocalDateTime;
import java.time.ZoneOffset;
import java.time.ZonedDateTime;

public class ZonedDateTimeValueHandler extends BaseValueHandler {
    private IValueConverter dateTimeConverter;

    public ZonedDateTimeValueHandler() {
        this(new ToUTCStripTimezone());
    }

    public ZonedDateTimeValueHandler(IValueConverter dateTimeConverter) {
        this.dateTimeConverter = dateTimeConverter;
    }

    @Override
    protected void internalHandle(DataOutputStream buffer, ZonedDateTime value) throws Exception {
        buffer.writeInt(8);
        buffer.writeLong(dateTimeConverter.convert(value));
    }

    @Override
    public int getLength(ZonedDateTime value) {
        return 8;
    }

    private static final class ToUTCStripTimezone implements IValueConverter {
        private final IValueConverter converter = new LocalDateTimeConverter();

        @Override
        public Long convert(final ZonedDateTime value) {
            return converter.convert(value.withZoneSameInstant(ZoneOffset.UTC).toLocalDateTime());
        }
    }
}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy