private ExprCastNodeDateDesc ValidateDateFormat( ExprNamedParameterNode dateFormatParameter, ExprValidationContext validationContext) { var iso8601Format = false; var formatExpr = dateFormatParameter.ChildNodes[0]; var formatForge = formatExpr.Forge; var formatReturnType = formatExpr.Forge.EvaluationType; string staticFormatString = null; if (formatReturnType == typeof(string)) { if (formatExpr.Forge.ForgeConstantType.IsCompileTimeConstant) { staticFormatString = (string) formatForge.ExprEvaluator.Evaluate(null, true, null); if (staticFormatString.ToLowerInvariant().Trim().Equals("iso")) { iso8601Format = true; } else { try { DateTimeFormat.For(staticFormatString); } catch (EPException) { throw; } catch (Exception ex) { throw new ExprValidationException( "Invalid date format '" + staticFormatString + "' (as obtained from DateTimeFormatter.For): " + ex.Message, ex); } } } } else { if ((typeof(DateFormat) != formatReturnType) && (!typeof(DateFormat).IsAssignableFrom(formatReturnType)) && (typeof(DateTimeFormat) != formatReturnType) && (!typeof(DateTimeFormat).IsAssignableFrom(formatReturnType))) { throw GetFailedExpected(typeof(DateFormat), formatReturnType); } } return new ExprCastNodeDateDesc( iso8601Format, formatForge, staticFormatString, formatForge.ForgeConstantType.IsConstant); }
public static DateTimeFormat StringToDateTimeFormatterSafe(object format) { if (format == null) { throw new EPException("Null date format returned by 'dateformat' expression"); } try { return DateTimeFormat.For(format.ToString()); } catch (EPException) { throw; } catch (Exception ex) { throw new EPException("Invalid date format '" + format + "': " + ex.Message, ex); } }
public override object Parse(string input) { return(StringToDateTimeWStaticFormatParse(input, DateTimeFormat.For(format))); }
private static ParseMakePropertiesDesc SetupProperties(bool requireOneMatch, string[] propertyNamesOffered, EventType outputEventType, StatementContext statementContext, string dateFormat) { var writeables = EventTypeUtility.GetWriteableProperties(outputEventType, false, false); IList <int> indexesList = new List <int>(); IList <SimpleTypeParser> parserList = new List <SimpleTypeParser>(); IList <WriteablePropertyDescriptor> writablesList = new List <WriteablePropertyDescriptor>(); for (var i = 0; i < propertyNamesOffered.Length; i++) { var propertyName = propertyNamesOffered[i]; Type propertyType; try { propertyType = outputEventType.GetPropertyType(propertyName); } catch (PropertyAccessException ex) { throw new EPException("Invalid property name '" + propertyName + "': " + ex.Message, ex); } if (propertyType == null) { continue; } SimpleTypeParser parser; if (propertyType.IsDateTime() && !propertyType.IsInt64()) { var dateTimeFormat = dateFormat != null ? DateTimeFormat.For(dateFormat) : DateTimeFormat.ISO_DATE_TIME; if (propertyType == typeof(DateTime?)) { parser = new ProxySimpleTypeParser( text => (dateTimeFormat.Parse(text)?.DateTime)?.DateTime); } else if (propertyType == typeof(DateTime)) { parser = new ProxySimpleTypeParser( text => dateTimeFormat.Parse(text).DateTime.DateTime); } else if (propertyType == typeof(DateTimeOffset?)) { parser = new ProxySimpleTypeParser( text => dateTimeFormat.Parse(text)?.DateTime); } else if (propertyType == typeof(DateTimeOffset)) { parser = new ProxySimpleTypeParser( text => dateTimeFormat.Parse(text).DateTime); } else { parser = new ProxySimpleTypeParser( text => dateTimeFormat.Parse(text)); } } else { parser = SimpleTypeParserFactory.GetParser(propertyType); } var writable = EventTypeUtility.FindWritable(propertyName, writeables); if (writable == null) { continue; } indexesList.Add(i); parserList.Add(parser); writablesList.Add(writable); } if (indexesList.IsEmpty() && requireOneMatch) { throw new EPException( "Failed to match any of the properties " + CompatExtensions.RenderAny(propertyNamesOffered) + " to the event type properties of event type '" + outputEventType.Name + "'"); } var parsers = parserList.ToArray(); var writables = writablesList.ToArray(); var indexes = CollectionUtil.IntArray(indexesList); EventBeanManufacturer manufacturer; try { manufacturer = EventTypeUtility.GetManufacturer( outputEventType, writables, statementContext.ImportServiceRuntime, false, statementContext.EventTypeAvroHandler) .GetManufacturer(statementContext.EventBeanTypedEventFactory); } catch (EventBeanManufactureException e) { throw new EPException("Event type '" + outputEventType.Name + "' cannot be written to: " + e.Message, e); } return(new ParseMakePropertiesDesc(indexes, parsers, manufacturer)); }
public StringWStaticFormatComputer(string format) { this.format = format; this.dateFormat = DateTimeFormat.For(format); }
public static ReformatFormatForgeDesc ValidateGetFormatterType( EPType inputType, string methodName, ExprNode exprNode) { if (!(inputType is ClassEPType)) { throw new ExprValidationException( GetMessage(methodName) + " requires a datetime input value but received " + inputType); } if (!exprNode.Forge.ForgeConstantType.IsConstant) { throw new ExprValidationException(GetMessage(methodName) + " requires a constant-value format"); } var formatForge = exprNode.Forge; var formatType = formatForge.EvaluationType; if (formatType == null) { throw new ExprValidationException(GetMessage(methodName) + " invalid null format object"); } object format = null; if (formatForge.ForgeConstantType.IsCompileTimeConstant) { format = ExprNodeUtilityEvaluate.EvaluateValidationTimeNoStreams( exprNode.Forge.ExprEvaluator, null, "date format"); if (format == null) { throw new ExprValidationException(GetMessage(methodName) + " invalid null format object"); } } // handle legacy date var input = (ClassEPType) inputType; if (input.Clazz.GetBoxedType() == typeof(long?) || TypeHelper.IsSubclassOrImplementsInterface(input.Clazz, typeof(DateTime)) || TypeHelper.IsSubclassOrImplementsInterface(input.Clazz, typeof(DateTimeOffset)) || TypeHelper.IsSubclassOrImplementsInterface(input.Clazz, typeof(DateTimeEx))) { if (TypeHelper.IsSubclassOrImplementsInterface(formatType, typeof(DateFormat))) { return new ReformatFormatForgeDesc(typeof(DateFormat)); } if (TypeHelper.IsSubclassOrImplementsInterface(formatType, typeof(string))) { if (format != null) { try { new SimpleDateFormat((string) format); } catch (Exception ex) { throw new ExprValidationException( GetMessage(methodName) + " invalid format string (SimpleDateFormat): " + ex.Message, ex); } } return new ReformatFormatForgeDesc(typeof(string)); } throw GetFailedExpected(methodName, typeof(DateFormat), formatType); } if (TypeHelper.IsSubclassOrImplementsInterface(formatType, typeof(DateTimeFormat))) { return new ReformatFormatForgeDesc(typeof(DateTimeFormat)); } if (TypeHelper.IsSubclassOrImplementsInterface(formatType, typeof(string))) { if (format != null) { try { DateTimeFormat.For((string) format); } catch (Exception ex) { throw new ExprValidationException( GetMessage(methodName) + " invalid format string (DateTimeFormatter): " + ex.Message, ex); } } return new ReformatFormatForgeDesc(typeof(string)); } throw GetFailedExpected(methodName, typeof(DateTimeFormat), formatType); }