TemplateModel _getAsTemplateModel(Environment env) throws TemplateException {
TemplateModel model = target.getAsTemplateModel(env);
if (model instanceof TemplateDateModel) {
TemplateDateModel dmodel = (TemplateDateModel)model;
int dtype = dmodel.getDateType();
// Any date model can be coerced into its own type
if(dateType == dtype) {
return model;
}
// unknown and datetime can be coerced into any date type
if(dtype == TemplateDateModel.UNKNOWN || dtype == TemplateDateModel.DATETIME) {
return new SimpleDate(dmodel.getAsDate(), dateType);
}
throw new TemplateException(
"Cannot convert " + TemplateDateModel.TYPE_NAMES.get(dtype)
+ " into " + TemplateDateModel.TYPE_NAMES.get(dateType), env);
}
// Otherwise, interpret as a string and attempt
// to parse it into a date.
String s = target.getStringValue(env);
return new DateParser(s, env);
}
|