com.codetaco.math.function.FuncDateFmt Maven / Gradle / Ivy
package com.codetaco.math.function;
import com.codetaco.date.CalendarFactory;
import com.codetaco.math.Function;
import com.codetaco.math.ValueStack;
import com.codetaco.math.token.TokVariable;
import java.text.ParseException;
import java.time.LocalDate;
import java.time.LocalDateTime;
import java.time.LocalTime;
import java.time.format.DateTimeFormatter;
/**
*
* FuncDateTimeFmt class.
*
*
* @author Chris DeGreef [email protected]
*/
public class FuncDateFmt extends Function {
/**
*
* Constructor for FuncDate.
*
*/
public FuncDateFmt() {
super();
}
/**
*
* Constructor for FuncDateTimeFmt.
*
*
* @param var a {@link com.codetaco.math.token.TokVariable} object.
*/
public FuncDateFmt(final TokVariable var) {
super(var);
}
/**
* {@inheritDoc}
*/
@Override
public void resolve(final ValueStack values) throws Exception {
if (values.size() < 2) {
throw new Exception("missing operands for " + toString());
}
try {
String adjustments = "";
if (getParameterCount() == 3) {
adjustments = values.popString();
}
final DateTimeFormatter dtf = DateTimeFormatter.ofPattern(values.popString());
final String dateInputObject = values.popString();
final LocalDate convertedInputDate = LocalDate.parse(dateInputObject, dtf);
final LocalDateTime adjLdt = CalendarFactory.modify(convertedInputDate, adjustments);
if (adjLdt.toLocalTime() != LocalTime.MIN) {
throw new ParseException("Adjustments to time are not allowed", 0);
}
values.push(adjLdt);
} catch (final ParseException e) {
e.fillInStackTrace();
throw new Exception(toString() + "; " + e.getMessage(), e);
}
}
/**
* {@inheritDoc}
*/
@Override
public String toString() {
return "function(dateFmt)";
}
}