|
| 1 | +/* |
| 2 | + * Copyright 2021 the original author or authors. |
| 3 | + * |
| 4 | + * Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 | + * you may not use this file except in compliance with the License. |
| 6 | + * You may obtain a copy of the License at |
| 7 | + * |
| 8 | + * https://www.apache.org/licenses/LICENSE-2.0 |
| 9 | + * |
| 10 | + * Unless required by applicable law or agreed to in writing, software |
| 11 | + * distributed under the License is distributed on an "AS IS" BASIS, |
| 12 | + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 | + * See the License for the specific language governing permissions and |
| 14 | + * limitations under the License. |
| 15 | + */ |
| 16 | +package org.springframework.data.jdbc.core.dialect; |
| 17 | + |
| 18 | +import org.h2.api.TimestampWithTimeZone; |
| 19 | +import org.springframework.core.convert.converter.Converter; |
| 20 | +import org.springframework.data.convert.ReadingConverter; |
| 21 | +import org.springframework.data.relational.core.dialect.Db2Dialect; |
| 22 | +import org.springframework.data.relational.core.dialect.H2Dialect; |
| 23 | + |
| 24 | +import java.time.OffsetDateTime; |
| 25 | +import java.time.ZoneOffset; |
| 26 | +import java.util.Collection; |
| 27 | +import java.util.Collections; |
| 28 | + |
| 29 | +/** |
| 30 | + * {@link Db2Dialect} that registers JDBC specific converters. |
| 31 | + * |
| 32 | + * @author Jens Schauder |
| 33 | + * @since 2.3 |
| 34 | + */ |
| 35 | +public class JdbcH2Dialect extends H2Dialect { |
| 36 | + |
| 37 | + public static JdbcH2Dialect INSTANCE = new JdbcH2Dialect(); |
| 38 | + |
| 39 | + @Override |
| 40 | + public Collection<Object> getConverters() { |
| 41 | + return Collections.singletonList(TimestampWithTimeZone2OffsetDateTimeConverter.INSTANCE); |
| 42 | + } |
| 43 | + |
| 44 | + @ReadingConverter |
| 45 | + enum TimestampWithTimeZone2OffsetDateTimeConverter implements Converter<TimestampWithTimeZone, OffsetDateTime> { |
| 46 | + INSTANCE; |
| 47 | + |
| 48 | + |
| 49 | + @Override |
| 50 | + public OffsetDateTime convert(TimestampWithTimeZone source) { |
| 51 | + |
| 52 | + long nanosInSecond = 1_000_000_000; |
| 53 | + long nanosInMinute = nanosInSecond * 60; |
| 54 | + long nanosInHour = nanosInMinute * 60; |
| 55 | + |
| 56 | + long hours = (source.getNanosSinceMidnight() / nanosInHour); |
| 57 | + |
| 58 | + long nanosInHours = hours * nanosInHour; |
| 59 | + long nanosLeft = source.getNanosSinceMidnight() - nanosInHours; |
| 60 | + long minutes = nanosLeft / nanosInMinute; |
| 61 | + |
| 62 | + long nanosInMinutes = minutes * nanosInMinute; |
| 63 | + nanosLeft -= nanosInMinutes; |
| 64 | + long seconds = nanosLeft / nanosInSecond; |
| 65 | + |
| 66 | + long nanosInSeconds = seconds * nanosInSecond; |
| 67 | + nanosLeft -= nanosInSeconds; |
| 68 | + ZoneOffset offset = ZoneOffset.ofTotalSeconds(source.getTimeZoneOffsetSeconds()); |
| 69 | + |
| 70 | + return OffsetDateTime.of(source.getYear(), source.getMonth(), source.getDay(), (int)hours, (int)minutes, (int)seconds, (int)nanosLeft, offset ); |
| 71 | + |
| 72 | + } |
| 73 | + } |
| 74 | +} |
0 commit comments