You are viewing a plain text version of this content. The canonical link for it is here.
Posted to dev@olingo.apache.org by GitBox <gi...@apache.org> on 2019/11/06 07:48:48 UTC

[GitHub] [olingo-odata4] i050510 commented on a change in pull request #57: Support new date time API

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

 ##########
 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, and java.sql.Time
   I think we should do 
   Timestamp timestamp = Timestamp.valueOf(zdt.toLocalDateTime());
   Time time = Timestamp.from(zdt.toInstant()).getTime();

----------------------------------------------------------------
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:
users@infra.apache.org


With regards,
Apache Git Services