This is an automated email from the ASF dual-hosted git repository.

zhonghongsheng pushed a commit to branch master
in repository https://gitbox.apache.org/repos/asf/shardingsphere.git


The following commit(s) were added to refs/heads/master by this push:
     new 46d6722535a Add LocalDateTimeConvertor (#24550)
46d6722535a is described below

commit 46d6722535ae9d6c6487247a2776f7d96a3d28e4
Author: Liang Zhang <[email protected]>
AuthorDate: Sat Mar 11 17:40:26 2023 +0800

    Add LocalDateTimeConvertor (#24550)
    
    * Refactor AbstractCosIdIntervalShardingAlgorithm
    
    * Refactor AbstractCosIdIntervalShardingAlgorithm
    
    * Add LocalDateTimeConvertor
---
 .../AbstractCosIdIntervalShardingAlgorithm.java    | 43 +++++------
 .../interval/CosIdIntervalShardingAlgorithm.java   | 76 +++-----------------
 .../CosIdSnowflakeIntervalShardingAlgorithm.java   | 37 +++-------
 .../interval/convertor/LocalDateTimeConvertor.java | 34 +++++++++
 .../impl/CosIDLocalDateTimeConvertor.java          | 84 ++++++++++++++++++++++
 .../impl/SnowflakeLocalDateTimeConvertor.java      | 50 +++++++++++++
 ...osIdSnowflakeIntervalShardingAlgorithmTest.java |  2 +-
 .../IntervalShardingAlgorithmDataFixture.java      |  2 +-
 8 files changed, 207 insertions(+), 121 deletions(-)

diff --git 
a/features/sharding/plugin/cosid/src/main/java/org/apache/shardingsphere/sharding/cosid/algorithm/sharding/interval/AbstractCosIdIntervalShardingAlgorithm.java
 
b/features/sharding/plugin/cosid/src/main/java/org/apache/shardingsphere/sharding/cosid/algorithm/sharding/interval/AbstractCosIdIntervalShardingAlgorithm.java
index b4219752a9e..8f2ef582671 100644
--- 
a/features/sharding/plugin/cosid/src/main/java/org/apache/shardingsphere/sharding/cosid/algorithm/sharding/interval/AbstractCosIdIntervalShardingAlgorithm.java
+++ 
b/features/sharding/plugin/cosid/src/main/java/org/apache/shardingsphere/sharding/cosid/algorithm/sharding/interval/AbstractCosIdIntervalShardingAlgorithm.java
@@ -19,8 +19,6 @@ package 
org.apache.shardingsphere.sharding.cosid.algorithm.sharding.interval;
 
 import com.google.common.collect.BoundType;
 import com.google.common.collect.Range;
-import lombok.AccessLevel;
-import lombok.Getter;
 import me.ahoo.cosid.sharding.IntervalStep;
 import me.ahoo.cosid.sharding.IntervalTimeline;
 import 
org.apache.shardingsphere.infra.util.exception.ShardingSpherePreconditions;
@@ -28,10 +26,10 @@ import 
org.apache.shardingsphere.sharding.api.sharding.standard.PreciseShardingV
 import 
org.apache.shardingsphere.sharding.api.sharding.standard.RangeShardingValue;
 import 
org.apache.shardingsphere.sharding.api.sharding.standard.StandardShardingAlgorithm;
 import 
org.apache.shardingsphere.sharding.cosid.algorithm.CosIdAlgorithmConstants;
+import 
org.apache.shardingsphere.sharding.cosid.algorithm.sharding.interval.convertor.LocalDateTimeConvertor;
 import org.apache.shardingsphere.sharding.exception.ShardingPluginException;
 
 import java.time.LocalDateTime;
-import java.time.ZoneId;
 import java.time.format.DateTimeFormatter;
 import java.time.temporal.ChronoUnit;
 import java.util.Collection;
@@ -56,30 +54,23 @@ public abstract class 
AbstractCosIdIntervalShardingAlgorithm<T extends Comparabl
     
     public static final String INTERVAL_AMOUNT_KEY = 
"datetime-interval-amount";
     
-    public static final String ZONE_ID_KEY = "zone-id";
-    
-    @Getter(AccessLevel.PROTECTED)
-    private ZoneId zoneId;
-    
     private IntervalTimeline intervalTimeline;
     
+    private LocalDateTimeConvertor localDateTimeConvertor;
+    
     @Override
     public void init(final Properties props) {
-        zoneId = getZoneId(props);
-        intervalTimeline = getIntervalTimeline(props);
-    }
-    
-    private ZoneId getZoneId(final Properties props) {
-        return props.containsKey(ZONE_ID_KEY) ? 
ZoneId.of(props.getProperty(ZONE_ID_KEY)) : ZoneId.systemDefault();
+        intervalTimeline = createIntervalTimeline(props);
+        localDateTimeConvertor = createLocalDateTimeConvertor(props);
     }
     
-    private IntervalTimeline getIntervalTimeline(final Properties props) {
+    private IntervalTimeline createIntervalTimeline(final Properties props) {
         String logicNamePrefix = getRequiredValue(props, 
CosIdAlgorithmConstants.LOGIC_NAME_PREFIX_KEY);
         LocalDateTime effectiveLower = 
LocalDateTime.parse(getRequiredValue(props, DATE_TIME_LOWER_KEY), 
DEFAULT_DATE_TIME_FORMATTER);
         LocalDateTime effectiveUpper = 
LocalDateTime.parse(getRequiredValue(props, DATE_TIME_UPPER_KEY), 
DEFAULT_DATE_TIME_FORMATTER);
-        DateTimeFormatter suffixFormatter = 
DateTimeFormatter.ofPattern(getRequiredValue(props, 
SHARDING_SUFFIX_FORMAT_KEY));
         ChronoUnit stepUnit = ChronoUnit.valueOf(getRequiredValue(props, 
INTERVAL_UNIT_KEY));
         int stepAmount = 
Integer.parseInt(props.getOrDefault(INTERVAL_AMOUNT_KEY, 1).toString());
+        DateTimeFormatter suffixFormatter = 
DateTimeFormatter.ofPattern(getRequiredValue(props, 
SHARDING_SUFFIX_FORMAT_KEY));
         return new IntervalTimeline(logicNamePrefix, 
Range.closed(effectiveLower, effectiveUpper), IntervalStep.of(stepUnit, 
stepAmount), suffixFormatter);
     }
     
@@ -88,20 +79,20 @@ public abstract class 
AbstractCosIdIntervalShardingAlgorithm<T extends Comparabl
         return props.getProperty(key);
     }
     
+    protected abstract LocalDateTimeConvertor 
createLocalDateTimeConvertor(Properties props);
+    
     @Override
     public String doSharding(final Collection<String> availableTargetNames, 
final PreciseShardingValue<T> shardingValue) {
-        LocalDateTime shardingTime = 
convertShardingValue(shardingValue.getValue());
-        return intervalTimeline.sharding(shardingTime);
+        return 
intervalTimeline.sharding(localDateTimeConvertor.toLocalDateTime(shardingValue.getValue()));
     }
     
     @Override
     public Collection<String> doSharding(final Collection<String> 
availableTargetNames, final RangeShardingValue<T> shardingValue) {
-        Range<LocalDateTime> shardingRangeTime = 
convertRangeShardingValue(shardingValue.getValueRange());
-        return intervalTimeline.sharding(shardingRangeTime);
+        return 
intervalTimeline.sharding(toLocalDateTimeRange(shardingValue.getValueRange()));
     }
     
     @SuppressWarnings("unchecked")
-    private Range<LocalDateTime> convertRangeShardingValue(final Range<T> 
shardingValue) {
+    private Range<LocalDateTime> toLocalDateTimeRange(final Range<T> 
shardingValue) {
         if (Range.all().equals(shardingValue)) {
             return Range.all();
         }
@@ -110,17 +101,15 @@ public abstract class 
AbstractCosIdIntervalShardingAlgorithm<T extends Comparabl
             return (Range<LocalDateTime>) shardingValue;
         }
         if (shardingValue.hasLowerBound() && shardingValue.hasUpperBound()) {
-            LocalDateTime lower = 
convertShardingValue(shardingValue.lowerEndpoint());
-            LocalDateTime upper = 
convertShardingValue(shardingValue.upperEndpoint());
+            LocalDateTime lower = 
localDateTimeConvertor.toLocalDateTime(shardingValue.lowerEndpoint());
+            LocalDateTime upper = 
localDateTimeConvertor.toLocalDateTime(shardingValue.upperEndpoint());
             return Range.range(lower, shardingValue.lowerBoundType(), upper, 
shardingValue.upperBoundType());
         }
         if (shardingValue.hasLowerBound()) {
-            LocalDateTime lower = 
convertShardingValue(shardingValue.lowerEndpoint());
+            LocalDateTime lower = 
localDateTimeConvertor.toLocalDateTime(shardingValue.lowerEndpoint());
             return BoundType.OPEN.equals(shardingValue.lowerBoundType()) ? 
Range.greaterThan(lower) : Range.atLeast(lower);
         }
-        LocalDateTime upper = 
convertShardingValue(shardingValue.upperEndpoint());
+        LocalDateTime upper = 
localDateTimeConvertor.toLocalDateTime(shardingValue.upperEndpoint());
         return BoundType.OPEN.equals(shardingValue.upperBoundType()) ? 
Range.lessThan(upper) : Range.atMost(upper);
     }
-    
-    protected abstract LocalDateTime convertShardingValue(T shardingValue);
 }
diff --git 
a/features/sharding/plugin/cosid/src/main/java/org/apache/shardingsphere/sharding/cosid/algorithm/sharding/interval/CosIdIntervalShardingAlgorithm.java
 
b/features/sharding/plugin/cosid/src/main/java/org/apache/shardingsphere/sharding/cosid/algorithm/sharding/interval/CosIdIntervalShardingAlgorithm.java
index 7c4d060d6cf..ca25eb4f752 100644
--- 
a/features/sharding/plugin/cosid/src/main/java/org/apache/shardingsphere/sharding/cosid/algorithm/sharding/interval/CosIdIntervalShardingAlgorithm.java
+++ 
b/features/sharding/plugin/cosid/src/main/java/org/apache/shardingsphere/sharding/cosid/algorithm/sharding/interval/CosIdIntervalShardingAlgorithm.java
@@ -17,87 +17,33 @@
 
 package org.apache.shardingsphere.sharding.cosid.algorithm.sharding.interval;
 
-import me.ahoo.cosid.util.LocalDateTimeConvert;
 import 
org.apache.shardingsphere.sharding.cosid.algorithm.CosIdAlgorithmConstants;
-import org.apache.shardingsphere.sharding.exception.ShardingPluginException;
+import 
org.apache.shardingsphere.sharding.cosid.algorithm.sharding.interval.convertor.impl.CosIDLocalDateTimeConvertor;
+import 
org.apache.shardingsphere.sharding.cosid.algorithm.sharding.interval.convertor.LocalDateTimeConvertor;
 
-import java.time.Instant;
-import java.time.LocalDate;
-import java.time.LocalDateTime;
-import java.time.LocalTime;
-import java.time.OffsetDateTime;
-import java.time.Year;
-import java.time.YearMonth;
-import java.time.ZonedDateTime;
+import java.time.ZoneId;
 import java.time.format.DateTimeFormatter;
-import java.util.Date;
 import java.util.Properties;
 
 /**
- * Interval range sharding algorithm with CosId.
+ * Interval sharding algorithm with CosId.
  */
 public final class CosIdIntervalShardingAlgorithm extends 
AbstractCosIdIntervalShardingAlgorithm<Comparable<?>> {
     
+    private static final String ZONE_ID_KEY = "zone-id";
+    
     private static final String DATE_TIME_PATTERN_KEY = "datetime-pattern";
     
     private static final String TIMESTAMP_SECOND_UNIT = "SECOND";
     
     private static final String TIMESTAMP_UNIT_KEY = "ts-unit";
     
-    private boolean isSecondTs;
-    
-    private DateTimeFormatter dateTimeFormatter;
-    
-    @Override
-    public void init(final Properties props) {
-        super.init(props);
-        isSecondTs = getIsSecondTs(props);
-        dateTimeFormatter = getDateTimeFormatter(props);
-    }
-    
-    private boolean getIsSecondTs(final Properties props) {
-        return props.containsKey(TIMESTAMP_UNIT_KEY) && 
TIMESTAMP_SECOND_UNIT.equalsIgnoreCase(props.getProperty(TIMESTAMP_UNIT_KEY));
-    }
-    
-    private DateTimeFormatter getDateTimeFormatter(final Properties props) {
-        return 
DateTimeFormatter.ofPattern(props.getProperty(DATE_TIME_PATTERN_KEY, 
DEFAULT_DATE_TIME_PATTERN));
-    }
-    
-    @SuppressWarnings("checkstyle:CyclomaticComplexity")
     @Override
-    protected LocalDateTime convertShardingValue(final Comparable<?> 
shardingValue) {
-        if (shardingValue instanceof LocalDateTime) {
-            return (LocalDateTime) shardingValue;
-        }
-        if (shardingValue instanceof ZonedDateTime) {
-            return ((ZonedDateTime) shardingValue).toLocalDateTime();
-        }
-        if (shardingValue instanceof OffsetDateTime) {
-            return ((OffsetDateTime) shardingValue).toLocalDateTime();
-        }
-        if (shardingValue instanceof Instant) {
-            return LocalDateTimeConvert.fromInstant((Instant) shardingValue, 
getZoneId());
-        }
-        if (shardingValue instanceof LocalDate) {
-            return LocalDateTime.of((LocalDate) shardingValue, LocalTime.MIN);
-        }
-        if (shardingValue instanceof Date) {
-            return LocalDateTimeConvert.fromDate((Date) shardingValue, 
getZoneId());
-        }
-        if (shardingValue instanceof YearMonth) {
-            YearMonth yearMonth = (YearMonth) shardingValue;
-            return LocalDateTime.of(yearMonth.getYear(), 
yearMonth.getMonthValue(), 1, 0, 0);
-        }
-        if (shardingValue instanceof Year) {
-            return LocalDateTime.of(((Year) shardingValue).getValue(), 1, 1, 
0, 0);
-        }
-        if (shardingValue instanceof Long) {
-            return isSecondTs ? 
LocalDateTimeConvert.fromTimestampSecond((Long) shardingValue, getZoneId()) : 
LocalDateTimeConvert.fromTimestamp((Long) shardingValue, getZoneId());
-        }
-        if (shardingValue instanceof String) {
-            return LocalDateTimeConvert.fromString((String) shardingValue, 
dateTimeFormatter);
-        }
-        throw new ShardingPluginException("Unsupported sharding value type 
`%s`.", shardingValue);
+    protected LocalDateTimeConvertor createLocalDateTimeConvertor(final 
Properties props) {
+        ZoneId zoneId = props.containsKey(ZONE_ID_KEY) ? 
ZoneId.of(props.getProperty(ZONE_ID_KEY)) : ZoneId.systemDefault();
+        boolean isSecondTs = props.containsKey(TIMESTAMP_UNIT_KEY) && 
TIMESTAMP_SECOND_UNIT.equalsIgnoreCase(props.getProperty(TIMESTAMP_UNIT_KEY));
+        DateTimeFormatter dateTimeFormatter = 
DateTimeFormatter.ofPattern(props.getProperty(DATE_TIME_PATTERN_KEY, 
DEFAULT_DATE_TIME_PATTERN));
+        return new CosIDLocalDateTimeConvertor(zoneId, isSecondTs, 
dateTimeFormatter);
     }
     
     @Override
diff --git 
a/features/sharding/plugin/cosid/src/main/java/org/apache/shardingsphere/sharding/cosid/algorithm/sharding/interval/CosIdSnowflakeIntervalShardingAlgorithm.java
 
b/features/sharding/plugin/cosid/src/main/java/org/apache/shardingsphere/sharding/cosid/algorithm/sharding/interval/CosIdSnowflakeIntervalShardingAlgorithm.java
index e5d6d3e11c0..03924328a76 100644
--- 
a/features/sharding/plugin/cosid/src/main/java/org/apache/shardingsphere/sharding/cosid/algorithm/sharding/interval/CosIdSnowflakeIntervalShardingAlgorithm.java
+++ 
b/features/sharding/plugin/cosid/src/main/java/org/apache/shardingsphere/sharding/cosid/algorithm/sharding/interval/CosIdSnowflakeIntervalShardingAlgorithm.java
@@ -17,15 +17,15 @@
 
 package org.apache.shardingsphere.sharding.cosid.algorithm.sharding.interval;
 
-import me.ahoo.cosid.converter.Radix62IdConverter;
 import me.ahoo.cosid.snowflake.MillisecondSnowflakeId;
 import me.ahoo.cosid.snowflake.MillisecondSnowflakeIdStateParser;
 import me.ahoo.cosid.snowflake.SnowflakeIdStateParser;
 import 
org.apache.shardingsphere.sharding.cosid.algorithm.CosIdAlgorithmConstants;
 import 
org.apache.shardingsphere.sharding.cosid.algorithm.keygen.CosIdSnowflakeKeyGenerateAlgorithm;
-import org.apache.shardingsphere.sharding.exception.ShardingPluginException;
+import 
org.apache.shardingsphere.sharding.cosid.algorithm.sharding.interval.convertor.LocalDateTimeConvertor;
+import 
org.apache.shardingsphere.sharding.cosid.algorithm.sharding.interval.convertor.impl.SnowflakeLocalDateTimeConvertor;
 
-import java.time.LocalDateTime;
+import java.time.ZoneId;
 import java.util.Properties;
 
 /**
@@ -33,37 +33,20 @@ import java.util.Properties;
  */
 public final class CosIdSnowflakeIntervalShardingAlgorithm extends 
AbstractCosIdIntervalShardingAlgorithm<Comparable<?>> {
     
-    public static final String EPOCH_KEY = "epoch";
+    private static final String EPOCH_KEY = "epoch";
     
-    private SnowflakeIdStateParser snowflakeIdStateParser;
+    private static final String ZONE_ID_KEY = "zone-id";
     
     @Override
-    public void init(final Properties props) {
-        super.init(props);
-        snowflakeIdStateParser = getSnowflakeIdStateParser(props);
+    protected LocalDateTimeConvertor createLocalDateTimeConvertor(final 
Properties props) {
+        return new 
SnowflakeLocalDateTimeConvertor(createSnowflakeIdStateParser(props));
     }
     
-    private SnowflakeIdStateParser getSnowflakeIdStateParser(final Properties 
props) {
+    private SnowflakeIdStateParser createSnowflakeIdStateParser(final 
Properties props) {
         long epoch = Long.parseLong(props.getProperty(EPOCH_KEY, 
CosIdSnowflakeKeyGenerateAlgorithm.DEFAULT_EPOCH + ""));
+        ZoneId zoneId = props.containsKey(ZONE_ID_KEY) ? 
ZoneId.of(props.getProperty(ZONE_ID_KEY)) : ZoneId.systemDefault();
         return new MillisecondSnowflakeIdStateParser(
-                epoch, MillisecondSnowflakeId.DEFAULT_TIMESTAMP_BIT, 
MillisecondSnowflakeId.DEFAULT_MACHINE_BIT, 
MillisecondSnowflakeId.DEFAULT_SEQUENCE_BIT, getZoneId());
-    }
-    
-    @Override
-    protected LocalDateTime convertShardingValue(final Comparable<?> 
shardingValue) {
-        Long snowflakeId = convertToSnowflakeId(shardingValue);
-        return snowflakeIdStateParser.parseTimestamp(snowflakeId);
-    }
-    
-    private Long convertToSnowflakeId(final Comparable<?> shardingValue) {
-        if (shardingValue instanceof Long) {
-            return (Long) shardingValue;
-        }
-        if (shardingValue instanceof String) {
-            String shardingValueStr = (String) shardingValue;
-            return Radix62IdConverter.PAD_START.asLong(shardingValueStr);
-        }
-        throw new ShardingPluginException("Unsupported sharding value type 
`%s`.", shardingValue);
+                epoch, MillisecondSnowflakeId.DEFAULT_TIMESTAMP_BIT, 
MillisecondSnowflakeId.DEFAULT_MACHINE_BIT, 
MillisecondSnowflakeId.DEFAULT_SEQUENCE_BIT, zoneId);
     }
     
     @Override
diff --git 
a/features/sharding/plugin/cosid/src/main/java/org/apache/shardingsphere/sharding/cosid/algorithm/sharding/interval/convertor/LocalDateTimeConvertor.java
 
b/features/sharding/plugin/cosid/src/main/java/org/apache/shardingsphere/sharding/cosid/algorithm/sharding/interval/convertor/LocalDateTimeConvertor.java
new file mode 100644
index 00000000000..41921d9ce90
--- /dev/null
+++ 
b/features/sharding/plugin/cosid/src/main/java/org/apache/shardingsphere/sharding/cosid/algorithm/sharding/interval/convertor/LocalDateTimeConvertor.java
@@ -0,0 +1,34 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one or more
+ * contributor license agreements.  See the NOTICE file distributed with
+ * this work for additional information regarding copyright ownership.
+ * The ASF licenses this file to You 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.apache.shardingsphere.sharding.cosid.algorithm.sharding.interval.convertor;
+
+import java.time.LocalDateTime;
+
+/**
+ * Local date time convertor.
+ */
+public interface LocalDateTimeConvertor {
+    
+    /**
+     * To local date time.
+     * 
+     * @param value value to be converted
+     * @return converted local date time
+     */
+    LocalDateTime toLocalDateTime(Comparable<?> value);
+}
diff --git 
a/features/sharding/plugin/cosid/src/main/java/org/apache/shardingsphere/sharding/cosid/algorithm/sharding/interval/convertor/impl/CosIDLocalDateTimeConvertor.java
 
b/features/sharding/plugin/cosid/src/main/java/org/apache/shardingsphere/sharding/cosid/algorithm/sharding/interval/convertor/impl/CosIDLocalDateTimeConvertor.java
new file mode 100644
index 00000000000..a6154152e29
--- /dev/null
+++ 
b/features/sharding/plugin/cosid/src/main/java/org/apache/shardingsphere/sharding/cosid/algorithm/sharding/interval/convertor/impl/CosIDLocalDateTimeConvertor.java
@@ -0,0 +1,84 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one or more
+ * contributor license agreements.  See the NOTICE file distributed with
+ * this work for additional information regarding copyright ownership.
+ * The ASF licenses this file to You 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.apache.shardingsphere.sharding.cosid.algorithm.sharding.interval.convertor.impl;
+
+import lombok.RequiredArgsConstructor;
+import me.ahoo.cosid.util.LocalDateTimeConvert;
+import 
org.apache.shardingsphere.sharding.cosid.algorithm.sharding.interval.convertor.LocalDateTimeConvertor;
+import org.apache.shardingsphere.sharding.exception.ShardingPluginException;
+
+import java.time.Instant;
+import java.time.LocalDate;
+import java.time.LocalDateTime;
+import java.time.LocalTime;
+import java.time.OffsetDateTime;
+import java.time.Year;
+import java.time.YearMonth;
+import java.time.ZoneId;
+import java.time.ZonedDateTime;
+import java.time.format.DateTimeFormatter;
+import java.util.Date;
+
+/**
+ * Local date time convertor for CosId.
+ */
+@RequiredArgsConstructor
+public final class CosIDLocalDateTimeConvertor implements 
LocalDateTimeConvertor {
+    
+    private final ZoneId zoneId;
+    
+    private final boolean isSecondTs;
+    
+    private final DateTimeFormatter dateTimeFormatter;
+    
+    @Override
+    public LocalDateTime toLocalDateTime(final Comparable<?> value) {
+        if (value instanceof LocalDateTime) {
+            return (LocalDateTime) value;
+        }
+        if (value instanceof ZonedDateTime) {
+            return ((ZonedDateTime) value).toLocalDateTime();
+        }
+        if (value instanceof OffsetDateTime) {
+            return ((OffsetDateTime) value).toLocalDateTime();
+        }
+        if (value instanceof Instant) {
+            return LocalDateTimeConvert.fromInstant((Instant) value, zoneId);
+        }
+        if (value instanceof LocalDate) {
+            return LocalDateTime.of((LocalDate) value, LocalTime.MIN);
+        }
+        if (value instanceof Date) {
+            return LocalDateTimeConvert.fromDate((Date) value, zoneId);
+        }
+        if (value instanceof YearMonth) {
+            YearMonth yearMonth = (YearMonth) value;
+            return LocalDateTime.of(yearMonth.getYear(), 
yearMonth.getMonthValue(), 1, 0, 0);
+        }
+        if (value instanceof Year) {
+            return LocalDateTime.of(((Year) value).getValue(), 1, 1, 0, 0);
+        }
+        if (value instanceof Long) {
+            return isSecondTs ? 
LocalDateTimeConvert.fromTimestampSecond((Long) value, zoneId) : 
LocalDateTimeConvert.fromTimestamp((Long) value, zoneId);
+        }
+        if (value instanceof String) {
+            return LocalDateTimeConvert.fromString((String) value, 
dateTimeFormatter);
+        }
+        throw new ShardingPluginException("Unsupported sharding value type 
`%s`.", value);
+    }
+}
diff --git 
a/features/sharding/plugin/cosid/src/main/java/org/apache/shardingsphere/sharding/cosid/algorithm/sharding/interval/convertor/impl/SnowflakeLocalDateTimeConvertor.java
 
b/features/sharding/plugin/cosid/src/main/java/org/apache/shardingsphere/sharding/cosid/algorithm/sharding/interval/convertor/impl/SnowflakeLocalDateTimeConvertor.java
new file mode 100644
index 00000000000..68e970268f3
--- /dev/null
+++ 
b/features/sharding/plugin/cosid/src/main/java/org/apache/shardingsphere/sharding/cosid/algorithm/sharding/interval/convertor/impl/SnowflakeLocalDateTimeConvertor.java
@@ -0,0 +1,50 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one or more
+ * contributor license agreements.  See the NOTICE file distributed with
+ * this work for additional information regarding copyright ownership.
+ * The ASF licenses this file to You 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.apache.shardingsphere.sharding.cosid.algorithm.sharding.interval.convertor.impl;
+
+import lombok.RequiredArgsConstructor;
+import me.ahoo.cosid.converter.Radix62IdConverter;
+import me.ahoo.cosid.snowflake.SnowflakeIdStateParser;
+import 
org.apache.shardingsphere.sharding.cosid.algorithm.sharding.interval.convertor.LocalDateTimeConvertor;
+import org.apache.shardingsphere.sharding.exception.ShardingPluginException;
+
+import java.time.LocalDateTime;
+
+/**
+ * Local date time convertor for Snowflake.
+ */
+@RequiredArgsConstructor
+public final class SnowflakeLocalDateTimeConvertor implements 
LocalDateTimeConvertor {
+    
+    private final SnowflakeIdStateParser snowflakeIdStateParser;
+    
+    @Override
+    public LocalDateTime toLocalDateTime(final Comparable<?> value) {
+        return 
snowflakeIdStateParser.parseTimestamp(convertToSnowflakeId(value));
+    }
+    
+    private Long convertToSnowflakeId(final Comparable<?> value) {
+        if (value instanceof Long) {
+            return (Long) value;
+        }
+        if (value instanceof String) {
+            return Radix62IdConverter.PAD_START.asLong((String) value);
+        }
+        throw new ShardingPluginException("Unsupported sharding value type 
`%s`.", value);
+    }
+}
diff --git 
a/features/sharding/plugin/cosid/src/test/java/org/apache/shardingsphere/sharding/cosid/algorithm/sharding/interval/CosIdSnowflakeIntervalShardingAlgorithmTest.java
 
b/features/sharding/plugin/cosid/src/test/java/org/apache/shardingsphere/sharding/cosid/algorithm/sharding/interval/CosIdSnowflakeIntervalShardingAlgorithmTest.java
index 9beb248cfa8..f4945bb0f02 100644
--- 
a/features/sharding/plugin/cosid/src/test/java/org/apache/shardingsphere/sharding/cosid/algorithm/sharding/interval/CosIdSnowflakeIntervalShardingAlgorithmTest.java
+++ 
b/features/sharding/plugin/cosid/src/test/java/org/apache/shardingsphere/sharding/cosid/algorithm/sharding/interval/CosIdSnowflakeIntervalShardingAlgorithmTest.java
@@ -52,7 +52,7 @@ public final class 
CosIdSnowflakeIntervalShardingAlgorithmTest {
     
     private static Properties createProperties() {
         return PropertiesBuilder.build(
-                new Property(CosIdIntervalShardingAlgorithm.ZONE_ID_KEY, 
"Asia/Shanghai"),
+                new Property("zone-id", "Asia/Shanghai"),
                 new Property(CosIdAlgorithmConstants.LOGIC_NAME_PREFIX_KEY, 
IntervalShardingAlgorithmDataFixture.LOGIC_NAME_PREFIX),
                 new 
Property(CosIdIntervalShardingAlgorithm.DATE_TIME_LOWER_KEY,
                         
IntervalShardingAlgorithmDataFixture.LOWER_DATE_TIME.format(CosIdIntervalShardingAlgorithm.DEFAULT_DATE_TIME_FORMATTER)),
diff --git 
a/features/sharding/plugin/cosid/src/test/java/org/apache/shardingsphere/sharding/cosid/algorithm/sharding/interval/fixture/IntervalShardingAlgorithmDataFixture.java
 
b/features/sharding/plugin/cosid/src/test/java/org/apache/shardingsphere/sharding/cosid/algorithm/sharding/interval/fixture/IntervalShardingAlgorithmDataFixture.java
index ffc3162195b..5d507c8cfdb 100644
--- 
a/features/sharding/plugin/cosid/src/test/java/org/apache/shardingsphere/sharding/cosid/algorithm/sharding/interval/fixture/IntervalShardingAlgorithmDataFixture.java
+++ 
b/features/sharding/plugin/cosid/src/test/java/org/apache/shardingsphere/sharding/cosid/algorithm/sharding/interval/fixture/IntervalShardingAlgorithmDataFixture.java
@@ -128,7 +128,7 @@ public final class IntervalShardingAlgorithmDataFixture {
      */
     public static CosIdIntervalShardingAlgorithm createShardingAlgorithm() {
         Properties props = PropertiesBuilder.build(
-                new Property(CosIdIntervalShardingAlgorithm.ZONE_ID_KEY, 
"Asia/Shanghai"),
+                new Property("zone-id", "Asia/Shanghai"),
                 new Property(CosIdAlgorithmConstants.LOGIC_NAME_PREFIX_KEY, 
LOGIC_NAME_PREFIX),
                 new 
Property(CosIdIntervalShardingAlgorithm.DATE_TIME_LOWER_KEY, 
LOWER_DATE_TIME.format(CosIdIntervalShardingAlgorithm.DEFAULT_DATE_TIME_FORMATTER)),
                 new 
Property(CosIdIntervalShardingAlgorithm.DATE_TIME_UPPER_KEY, 
UPPER_DATE_TIME.format(CosIdIntervalShardingAlgorithm.DEFAULT_DATE_TIME_FORMATTER)),

Reply via email to