Skip to content

Commit

Permalink
fix #185: improve the accuracy of datetime to milliseconds (v1.1.5) #186
Browse files Browse the repository at this point in the history
 from CorvusYe/master

fix #185: improve the accuracy of datetime to milliseconds (v1.1.5)
  • Loading branch information
wey-gu authored Jul 20, 2023
2 parents cf6dacb + ca1dfbd commit ef7c714
Show file tree
Hide file tree
Showing 9 changed files with 55 additions and 20 deletions.
2 changes: 2 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@ This source code is licensed under Apache 2.0 License.
## Dependencies upgrade
- [ ] Springboot 3.x support.

# 1.1.5
## Bugfix
- fix: [#176](https://github.com/nebula-contrib/ngbatis/issues/176) use double quote instead of the original single quote in valuaFmt function
- fix: [#181](https://github.com/nebula-contrib/ngbatis/issues/181) when node has multi tag, can not update by subclass
Expand All @@ -31,6 +32,7 @@ This source code is licensed under Apache 2.0 License.
- updateByIdBatchSelective
- updateByIdBatchSelective
- upsertByIdSelective
- fix: [#185](https://github.com/nebula-contrib/ngbatis/issues/185) improve the accuracy of datetime to milliseconds

# 1.1.4
## Develop behavior change.
Expand Down
4 changes: 2 additions & 2 deletions README-CN.md
Original file line number Diff line number Diff line change
Expand Up @@ -34,12 +34,12 @@ This source code is licensed under Apache 2.0 License.
<dependency>
<groupId>org.nebula-contrib</groupId>
<artifactId>ngbatis</artifactId>
<version>1.1.4</version>
<version>1.1.5</version>
</dependency>
```
- Gradle
```groovy
implementation 'org.nebula-contrib:ngbatis:1.1.4'
implementation 'org.nebula-contrib:ngbatis:1.1.5'
```
### 参考 [【ngbatis-demo】](./ngbatis-demo),与springboot无缝集成。在该项目的 test 中还有api的样例。在开发过程中每增加一个特性也都会同步更新ngbatis-demo的用例。

Expand Down
4 changes: 2 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -38,12 +38,12 @@ See [EXECUTION-PROCESS.md](./EXECUTION-PROCESS.md)
<dependency>
<groupId>org.nebula-contrib</groupId>
<artifactId>ngbatis</artifactId>
<version>1.1.4</version>
<version>1.1.5</version>
</dependency>
```
- Gradle
```groovy
implementation 'org.nebula-contrib:ngbatis:1.1.4'
implementation 'org.nebula-contrib:ngbatis:1.1.5'
```

- Referring to [ngbatis-demo](./ngbatis-demo), which was smoothly integrated with spring-boot. The API examples could be found under the test of it for all features of ngbatis.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -65,7 +65,7 @@ public void selectById() {
);

Assert.isTrue(
Objects.equals(timeTest.getDatetime().toString(), datetime.toString()),
Objects.equals(timeTest.getDatetime().getTime(), datetime.getTime()),
"Datetime must be equal to the value before insertion"
);

Expand Down
2 changes: 1 addition & 1 deletion pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@
<name>ngbatis</name>
<groupId>org.nebula-contrib</groupId>
<artifactId>ngbatis</artifactId>
<version>1.2.0-SNAPSHOT</version>
<version>1.1.5</version>

<developers>
<developer>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@
* @since 2022-08-29 8:06
* Now is history!
*/
@Deprecated
public class DateDeserializer implements ObjectSerializer {

@Override
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,10 @@
*/
public class ValueFmtFn extends AbstractFunction<Object, Boolean, Boolean, Void, Void, Void> {

private static final String DATE_FMT = "yyyy-MM-dd";
private static final String TIME_FMT = "HH:mm:ss.SSS";
private static final String DATETIME_FMT = String.format("%s'T'%s", DATE_FMT, TIME_FMT);

private static boolean escape = true;

private static String parameterQuote = "\"";
Expand Down Expand Up @@ -64,10 +68,10 @@ public Object call(Object value, Boolean ifStringLike, Boolean escape) {
return String.format("%s(%d)", "timestamp", (((Timestamp) value).getTime() / 1000));
}

String timePattern = objClass == java.util.Date.class ? "yyyy-MM-dd'T'HH:mm:ss.sss"
: objClass == java.sql.Date.class ? "yyyy-MM-dd"
: objClass == java.sql.Time.class ? "HH:mm:ss.sss"
: "yyyy-MM-dd'T'HH:mm:ss.sss";
String timePattern = objClass == java.util.Date.class ? DATETIME_FMT
: objClass == java.sql.Date.class ? DATE_FMT
: objClass == java.sql.Time.class ? TIME_FMT
: DATETIME_FMT;
SimpleDateFormat sdf = new SimpleDateFormat(timePattern);

String fn = "datetime";
Expand Down
47 changes: 37 additions & 10 deletions src/main/java/org/nebula/contrib/ngbatis/utils/ResultSetUtil.java
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@
import com.vesoft.nebula.client.graph.data.TimeWrapper;
import com.vesoft.nebula.client.graph.data.ValueWrapper;
import java.io.UnsupportedEncodingException;
import java.lang.reflect.Constructor;
import java.lang.reflect.Field;
import java.util.ArrayList;
import java.util.GregorianCalendar;
Expand Down Expand Up @@ -66,7 +67,19 @@ public class ResultSetUtil {
public static String type_vertex_value = "vertex";

public static String type_edge_value = "edge";


private static final Constructor<GregorianCalendar> CALENDAR_CONSTRUCTOR;

static {
try {
CALENDAR_CONSTRUCTOR = GregorianCalendar.class.getDeclaredConstructor(
int.class, int.class, int.class, int.class, int.class, int.class, int.class
);
} catch (NoSuchMethodException e) {
throw new RuntimeException(e);
}
}

/**
* <p>根据nebula graph本身的类型说明,获取对应的 java对象值。</p>
* @param value nebula graph 类型数据,(结果集的元素)
Expand Down Expand Up @@ -112,16 +125,30 @@ public static <T> T getValue(ValueWrapper valueWrapper, Class<T> resultType) {
return value;
}


private static Object transformDateTime(DateTimeWrapper dateTime) {
return new GregorianCalendar(
dateTime.getYear(),
dateTime.getMonth() - 1,
dateTime.getDay(),
dateTime.getHour(),
dateTime.getMinute(),
dateTime.getSecond()
).getTime();
try {
CALENDAR_CONSTRUCTOR.setAccessible(true);
GregorianCalendar calendar = CALENDAR_CONSTRUCTOR.newInstance(
dateTime.getYear(),
dateTime.getMonth() - 1,
dateTime.getDay(),
dateTime.getHour(),
dateTime.getMinute(),
dateTime.getSecond(),
Math.floorDiv(dateTime.getMicrosec(), 1000)
);
CALENDAR_CONSTRUCTOR.setAccessible(false);
return calendar.getTime();
} catch (Exception e) {
return new GregorianCalendar(
dateTime.getYear(),
dateTime.getMonth() - 1,
dateTime.getDay(),
dateTime.getHour(),
dateTime.getMinute(),
dateTime.getSecond()
).getTime();
}
}

private static Object transformDate(DateWrapper date) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@
* @since 2022-06-22 4:44
* <br>Now is history!
*/
@Deprecated
class DateDeserializerTest {

@Test
Expand Down

0 comments on commit ef7c714

Please sign in to comment.