|
| 1 | +/* |
| 2 | + * Copyright 2021 DataCanvas |
| 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 | + * http://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 | + |
| 17 | +package io.dingodb.expr.common.timezone.converter; |
| 18 | + |
| 19 | +import io.dingodb.expr.common.timezone.core.DateTimeType; |
| 20 | +import io.dingodb.expr.common.timezone.core.DingoDateTime; |
| 21 | +import io.dingodb.expr.common.timezone.core.SimpleTimeZoneConfig; |
| 22 | + |
| 23 | +import java.time.Instant; |
| 24 | +import java.time.LocalDate; |
| 25 | +import java.time.LocalDateTime; |
| 26 | +import java.time.LocalTime; |
| 27 | +import java.time.ZoneId; |
| 28 | +import java.time.ZonedDateTime; |
| 29 | + |
| 30 | +public class DateTimeConverter { |
| 31 | + |
| 32 | + private final ZoneId defaultZone; |
| 33 | + |
| 34 | + public DateTimeConverter(ZoneId defaultZone) { |
| 35 | + this.defaultZone = defaultZone; |
| 36 | + } |
| 37 | + |
| 38 | + public DateTimeConverter() { |
| 39 | + this(SimpleTimeZoneConfig.getApplicationZone()); |
| 40 | + } |
| 41 | + |
| 42 | + public DingoDateTime convertInput(Object input, DateTimeType targetType) { |
| 43 | + if (input == null || targetType == null) { |
| 44 | + return null; |
| 45 | + } |
| 46 | + |
| 47 | + if (input instanceof java.sql.Date) { |
| 48 | + return convertSqlDate((java.sql.Date) input, targetType); |
| 49 | + } else if (input instanceof java.sql.Time) { |
| 50 | + return convertSqlTime((java.sql.Time) input, targetType); |
| 51 | + } else if (input instanceof java.sql.Timestamp) { |
| 52 | + return convertSqlTimestamp((java.sql.Timestamp) input, targetType); |
| 53 | + } else if (input instanceof java.util.Date) { |
| 54 | + return convertUtilDate((java.util.Date) input, targetType); |
| 55 | + } else if (input instanceof String) { |
| 56 | + StringDateTimeParser parser = new StringDateTimeParser(); |
| 57 | + return parser.parseString((String) input, targetType, defaultZone); |
| 58 | + } else if (input instanceof DingoDateTime) { |
| 59 | + return convertDingoDateTime((DingoDateTime) input, targetType); |
| 60 | + } else { |
| 61 | + throw new IllegalArgumentException("Unsupported input type: " + input.getClass()); |
| 62 | + } |
| 63 | + } |
| 64 | + |
| 65 | + private DingoDateTime convertUtilDate(java.util.Date utilDate, DateTimeType targetType) { |
| 66 | + Instant instant = utilDate.toInstant(); |
| 67 | + |
| 68 | + if (targetType.isTimeZoneSensitive()) { |
| 69 | + return new DingoDateTime.DingoTimestampTZ(instant, defaultZone); |
| 70 | + } else { |
| 71 | + LocalDateTime localDateTime = instant.atZone(defaultZone).toLocalDateTime(); |
| 72 | + |
| 73 | + switch (targetType) { |
| 74 | + case DATE: |
| 75 | + return new DingoDateTime.DingoLocalDate(localDateTime.toLocalDate()); |
| 76 | + case TIME: |
| 77 | + return new DingoDateTime.DingoLocalTime(localDateTime.toLocalTime()); |
| 78 | + case TIMESTAMP: |
| 79 | + return new DingoDateTime.DingoLocalDateTime(localDateTime); |
| 80 | + default: |
| 81 | + throw new IllegalArgumentException("Unsupported target type: " + targetType); |
| 82 | + } |
| 83 | + } |
| 84 | + } |
| 85 | + |
| 86 | + private DingoDateTime convertSqlDate(java.sql.Date sqlDate, DateTimeType targetType) { |
| 87 | + if (targetType.isTimeZoneSensitive()) { |
| 88 | + Instant instant = Instant.ofEpochMilli(sqlDate.getTime()); |
| 89 | + return new DingoDateTime.DingoTimestampTZ(instant, defaultZone); |
| 90 | + } else { |
| 91 | + LocalDate localDate = sqlDate.toLocalDate(); |
| 92 | + |
| 93 | + switch (targetType) { |
| 94 | + case DATE: |
| 95 | + return new DingoDateTime.DingoLocalDate(localDate); |
| 96 | + case TIME: |
| 97 | + throw new IllegalArgumentException("Cannot convert DATE to TIME"); |
| 98 | + case TIMESTAMP: |
| 99 | + LocalDateTime localDateTime = localDate.atStartOfDay(); |
| 100 | + return new DingoDateTime.DingoLocalDateTime(localDateTime); |
| 101 | + default: |
| 102 | + throw new IllegalArgumentException("Unsupported target type: " + targetType); |
| 103 | + } |
| 104 | + } |
| 105 | + } |
| 106 | + |
| 107 | + private DingoDateTime convertSqlTime(java.sql.Time sqlTime, DateTimeType targetType) { |
| 108 | + if (targetType.isTimeZoneSensitive()) { |
| 109 | + Instant instant = Instant.ofEpochMilli(sqlTime.getTime()); |
| 110 | + return new DingoDateTime.DingoTimestampTZ(instant, defaultZone); |
| 111 | + } else { |
| 112 | + LocalTime localTime = sqlTime.toLocalTime(); |
| 113 | + |
| 114 | + switch (targetType) { |
| 115 | + case DATE: |
| 116 | + // throw new IllegalArgumentException("Cannot convert TIME to DATE"); |
| 117 | + return null; |
| 118 | + case TIME: |
| 119 | + return new DingoDateTime.DingoLocalTime(localTime); |
| 120 | + case TIMESTAMP: |
| 121 | + /*LocalDateTime localDateTime = LocalDateTime.of( |
| 122 | + LocalDate.of(1970, 1, 1), localTime); |
| 123 | + return new DingoDateTime.DingoLocalDateTime(localDateTime);*/ |
| 124 | + return null; |
| 125 | + default: |
| 126 | + throw new IllegalArgumentException("Unsupported target type: " + targetType); |
| 127 | + } |
| 128 | + } |
| 129 | + } |
| 130 | + |
| 131 | + private DingoDateTime convertSqlTimestamp(java.sql.Timestamp timestamp, DateTimeType targetType) { |
| 132 | + Instant instant = timestamp.toInstant(); |
| 133 | + |
| 134 | + if (targetType.isTimeZoneSensitive()) { |
| 135 | + return new DingoDateTime.DingoTimestampTZ(instant, defaultZone); |
| 136 | + } else { |
| 137 | + LocalDateTime localDateTime = instant.atZone(defaultZone).toLocalDateTime(); |
| 138 | + |
| 139 | + switch (targetType) { |
| 140 | + case DATE: |
| 141 | + return new DingoDateTime.DingoLocalDate(localDateTime.toLocalDate()); |
| 142 | + case TIME: |
| 143 | + return new DingoDateTime.DingoLocalTime(localDateTime.toLocalTime()); |
| 144 | + case TIMESTAMP: |
| 145 | + return new DingoDateTime.DingoLocalDateTime(localDateTime); |
| 146 | + default: |
| 147 | + throw new IllegalArgumentException("Unsupported target type: " + targetType); |
| 148 | + } |
| 149 | + } |
| 150 | + } |
| 151 | + |
| 152 | + private DingoDateTime convertDingoDateTime(DingoDateTime dingoDateTime, DateTimeType targetType) { |
| 153 | + if (dingoDateTime.getType() == targetType) { |
| 154 | + return dingoDateTime; |
| 155 | + } |
| 156 | + |
| 157 | + if (dingoDateTime.isTimeZoneSensitive()) { |
| 158 | + DingoDateTime.DingoTimestampTZ tzValue = (DingoDateTime.DingoTimestampTZ) dingoDateTime; |
| 159 | + |
| 160 | + if (targetType.isTimeZoneSensitive()) { |
| 161 | + return dingoDateTime; |
| 162 | + } else { |
| 163 | + switch (targetType) { |
| 164 | + case DATE: |
| 165 | + LocalDate localDate = tzValue.getUtcValue() |
| 166 | + .atZone(tzValue.getOriginalZone()) |
| 167 | + .toLocalDate(); |
| 168 | + return new DingoDateTime.DingoLocalDate(localDate); |
| 169 | + case TIME: |
| 170 | + LocalTime localTime = tzValue.getUtcValue() |
| 171 | + .atZone(tzValue.getOriginalZone()) |
| 172 | + .toLocalTime(); |
| 173 | + return new DingoDateTime.DingoLocalTime(localTime); |
| 174 | + case TIMESTAMP: |
| 175 | + LocalDateTime localDateTime = tzValue.getUtcValue() |
| 176 | + .atZone(tzValue.getOriginalZone()) |
| 177 | + .toLocalDateTime(); |
| 178 | + return new DingoDateTime.DingoLocalDateTime(localDateTime); |
| 179 | + default: |
| 180 | + throw new IllegalArgumentException("Unsupported target type: " + targetType); |
| 181 | + } |
| 182 | + } |
| 183 | + } else { |
| 184 | + if (targetType.isTimeZoneSensitive()) { |
| 185 | + ZoneId targetZone = defaultZone; |
| 186 | + |
| 187 | + if (dingoDateTime instanceof DingoDateTime.DingoLocalDate) { |
| 188 | + DingoDateTime.DingoLocalDate localDate = (DingoDateTime.DingoLocalDate) dingoDateTime; |
| 189 | + LocalDateTime localDateTime = localDate.getValue().atStartOfDay(); |
| 190 | + ZonedDateTime zdt = localDateTime.atZone(targetZone); |
| 191 | + return new DingoDateTime.DingoTimestampTZ(zdt.toInstant(), targetZone); |
| 192 | + } else if (dingoDateTime instanceof DingoDateTime.DingoLocalTime) { |
| 193 | + DingoDateTime.DingoLocalTime localTime = (DingoDateTime.DingoLocalTime) dingoDateTime; |
| 194 | + LocalDateTime localDateTime = LocalDate.now().atTime(localTime.getValue()); |
| 195 | + ZonedDateTime zdt = localDateTime.atZone(targetZone); |
| 196 | + return new DingoDateTime.DingoTimestampTZ(zdt.toInstant(), targetZone); |
| 197 | + } else if (dingoDateTime instanceof DingoDateTime.DingoLocalDateTime) { |
| 198 | + DingoDateTime.DingoLocalDateTime localDateTime = (DingoDateTime.DingoLocalDateTime) dingoDateTime; |
| 199 | + ZonedDateTime zdt = localDateTime.getValue().atZone(targetZone); |
| 200 | + return new DingoDateTime.DingoTimestampTZ(zdt.toInstant(), targetZone); |
| 201 | + } |
| 202 | + } else { |
| 203 | + if (dingoDateTime instanceof DingoDateTime.DingoLocalDate) { |
| 204 | + DingoDateTime.DingoLocalDate localDate = (DingoDateTime.DingoLocalDate) dingoDateTime; |
| 205 | + switch (targetType) { |
| 206 | + case DATE: |
| 207 | + return dingoDateTime; |
| 208 | + case TIMESTAMP: |
| 209 | + return new DingoDateTime.DingoLocalDateTime(localDate.getValue().atStartOfDay()); |
| 210 | + default: |
| 211 | + throw new IllegalArgumentException("Unsupported conversion: DATE to " + targetType); |
| 212 | + } |
| 213 | + } else if (dingoDateTime instanceof DingoDateTime.DingoLocalTime) { |
| 214 | + DingoDateTime.DingoLocalTime localTime = (DingoDateTime.DingoLocalTime) dingoDateTime; |
| 215 | + switch (targetType) { |
| 216 | + case TIME: |
| 217 | + return dingoDateTime; |
| 218 | + case TIMESTAMP: |
| 219 | + LocalDateTime localDateTime = LocalDate.of(1970, 1, 1).atTime(localTime.getValue()); |
| 220 | + return new DingoDateTime.DingoLocalDateTime(localDateTime); |
| 221 | + default: |
| 222 | + throw new IllegalArgumentException("Unsupported conversion: TIME to " + targetType); |
| 223 | + } |
| 224 | + } else if (dingoDateTime instanceof DingoDateTime.DingoLocalDateTime) { |
| 225 | + DingoDateTime.DingoLocalDateTime localDateTime = (DingoDateTime.DingoLocalDateTime) dingoDateTime; |
| 226 | + switch (targetType) { |
| 227 | + case DATE: |
| 228 | + return new DingoDateTime.DingoLocalDate(localDateTime.getValue().toLocalDate()); |
| 229 | + case TIME: |
| 230 | + return new DingoDateTime.DingoLocalTime(localDateTime.getValue().toLocalTime()); |
| 231 | + case TIMESTAMP: |
| 232 | + return dingoDateTime; |
| 233 | + default: |
| 234 | + throw new IllegalArgumentException("Unsupported conversion: TIMESTAMP to " + targetType); |
| 235 | + } |
| 236 | + } |
| 237 | + } |
| 238 | + } |
| 239 | + |
| 240 | + throw new IllegalArgumentException("Unsupported DingoDateTime conversion: " |
| 241 | + + dingoDateTime.getType() + " to " + targetType); |
| 242 | + } |
| 243 | +} |
0 commit comments