rzo1 commented on code in PR #239:
URL: https://github.com/apache/openjpa/pull/239#discussion_r4404248436
##########
openjpa-kernel/src/main/java/org/apache/openjpa/kernel/exps/ExtractDateTimeField.java:
##########
@@ -64,30 +68,47 @@ protected Object eval(Object candidate, Object orig,
StoreContext ctx, Object[] params) {
Object r = _val.eval(candidate, orig, ctx, params);
- Temporal t = null;
- if (Date.class.isAssignableFrom(r.getClass())) {
- t = ((Date) r).toLocalDate();
- } else if (Time.class.isAssignableFrom(r.getClass())) {
- t = ((Time) r).toLocalTime();
- } else if (Timestamp.class.isAssignableFrom(r.getClass())) {
- t = ((Timestamp) r).toInstant();
- } else if (Temporal.class.isAssignableFrom(r.getClass())) {
- t = (Temporal) r;
+ if (r == null) {
+ return null;
}
+ Temporal t = toTemporal(r);
if (t == null) {
throw new IllegalArgumentException();
}
- switch (_field) {
- case QUARTER:
- int month = t.get(ChronoField.MONTH_OF_YEAR);
- return (int) Math.round(Math.ceil(month/3f));
- case SECOND:
- int seconds = t.get(ChronoField.SECOND_OF_MINUTE);
- int mili = t.get(ChronoField.MILLI_OF_SECOND);
- return (float) seconds + (float) (mili/1000f);
- default:
- return t.get(_field.getEquivalent());
+ if (_field == DateTimeExtractField.SECOND) {
+ int seconds = t.get(ChronoField.SECOND_OF_MINUTE);
+ int nanos = t.get(ChronoField.NANO_OF_SECOND);
+ return (float) (seconds + nanos / 1_000_000_000d);
+ }
+ return t.get(_field.getTemporalField());
+ }
+
+ /**
+ * Convert the given value to a temporal whose date and time fields are
+ * the local date and time the value stands for. A value that is an
+ * instant on the time line is read in the default time zone, as the
+ * JDBC driver writes it, a calendar in its own time zone.
+ */
+ static Temporal toTemporal(Object r) {
+ if (r instanceof Date) {
Review Comment:
Done, and the same for the other branches in toTemporal.
--
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.
To unsubscribe, e-mail: [email protected]
For queries about this service, please contact Infrastructure at:
[email protected]