agoerler commented on a change in pull request #57: Support new date time API
URL: https://github.com/apache/olingo-odata4/pull/57#discussion_r343263777
 
 

 ##########
 File path: 
lib/commons-core/src/main/java/org/apache/olingo/commons/core/edm/primitivetype/EdmDate.java
 ##########
 @@ -29,65 +35,76 @@
  */
 public final class EdmDate extends SingletonPrimitiveType {
 
-  private static final Pattern PATTERN = 
Pattern.compile("(-?\\p{Digit}{4,})-(\\p{Digit}{2})-(\\p{Digit}{2})");
+       private static final EdmDate INSTANCE = new EdmDate();
 
-  private static final EdmDate INSTANCE = new EdmDate();
+       public static EdmDate getInstance() {
+               return INSTANCE;
+       }
 
-  public static EdmDate getInstance() {
-    return INSTANCE;
-  }
+       @Override
+       public Class<?> getDefaultType() {
+               return Calendar.class;
+       }
 
-  @Override
-  public Class<?> getDefaultType() {
-    return Calendar.class;
-  }
+       @SuppressWarnings("unchecked")
+       @Override
+       protected <T> T internalValueOfString(final String value, final Boolean 
isNullable, final Integer maxLength,
+                       final Integer precision, final Integer scale, final 
Boolean isUnicode, final Class<T> returnType)
+                       throws EdmPrimitiveTypeException {
+               LocalDate date;
+               try {
+                       date = LocalDate.parse(value);
+               } catch (DateTimeParseException ex) {
+                       throw new EdmPrimitiveTypeException("The literal '" + 
value + "' has illegal content.");
+               }
 
-  @Override
-  protected <T> T internalValueOfString(final String value,
-      final Boolean isNullable, final Integer maxLength, final Integer 
precision,
-      final Integer scale, final Boolean isUnicode, final Class<T> returnType) 
throws EdmPrimitiveTypeException {
+               // appropriate types
+               if (returnType.isAssignableFrom(LocalDate.class)) {
+                       return (T) date;
+               } else if (returnType.isAssignableFrom(java.sql.Date.class)) {
+                       return (T) java.sql.Date.valueOf(date);
+               }
 
-    final Calendar dateTimeValue = Calendar.getInstance();
-    dateTimeValue.clear();
+               // inappropriate types, which need to be supported for backward 
compatibility
+               ZonedDateTime zdt = LocalDateTime.of(date, 
LocalTime.MIDNIGHT).atZone(ZoneId.systemDefault());
+               if (returnType.isAssignableFrom(Calendar.class)) {
+                       return (T) GregorianCalendar.from(zdt);
+               } else if (returnType.isAssignableFrom(Long.class)) {
+                       return (T) Long.valueOf(zdt.toInstant().toEpochMilli());
+               } else if (returnType.isAssignableFrom(java.util.Date.class)) {
+                       return (T) java.util.Date.from(zdt.toInstant());
+               } else {
+                       throw new EdmPrimitiveTypeException("The value type " + 
returnType + " is not supported.");
+               }
+       }
 
 Review comment:
   > What if return type is java.sql.Timestamp
   
   > Timestamp timestamp = Timestamp.valueOf(zdt.toLocalDateTime());
   Timestamp is covered by `java.sql.Timestamp.from(zdt.toInstant());`
   
   > , and java.sql.Time
   Technically we can do this. But semantically it does not make sense: a EDM 
date must not have time components whereas a java.sql.Time must not have date 
components. This really does not make any sense.
   
   > Time time = Timestamp.from(zdt.toInstant()).getTime();
   This would not work as it would give you a `long`.
   
   You just cannot convert a date into a time. It does not make any sense.
   

----------------------------------------------------------------
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.
 
For queries about this service, please contact Infrastructure at:
us...@infra.apache.org


With regards,
Apache Git Services

Reply via email to