view src/nabble/view/web/template/DateNamespace.java @ 0:7ecd1a4ef557

add content
author Franklin Schmidt <fschmidt@gmail.com>
date Thu, 21 Mar 2019 19:15:52 -0600
parents
children
line wrap: on
line source

package nabble.view.web.template;

import nabble.naml.compiler.Command;
import nabble.naml.compiler.CommandSpec;
import nabble.naml.compiler.IPrintWriter;
import nabble.naml.compiler.Interpreter;
import nabble.naml.compiler.Namespace;

import java.text.SimpleDateFormat;
import java.util.Date;


@Namespace (
	name = "date",
	global = false
)
public final class DateNamespace {
	private final Date date;

	public DateNamespace(Date date) {
		this.date = date;
	}

	@Command public void raw_time(IPrintWriter out,Interpreter interp) {
		out.print( date.getTime() );
	}

	public static final CommandSpec custom_format = new CommandSpec.Builder()
		.parameters("format")
		.build()
	;

	@Command public void custom_format(IPrintWriter out, Interpreter interp) {
		String format = interp.getArgString("format");
		out.print(new SimpleDateFormat(format).format(date));
	}

	public static final CommandSpec is_older_than = new CommandSpec.Builder()
		.parameters("days")
		.build()
	;

	@Command public void is_older_than(IPrintWriter out, Interpreter interp) {
		int days = interp.getArgAsInt("days");
		long diffFromNow = System.currentTimeMillis() - date.getTime();
		float daysFromNow = diffFromNow / (24 * 60 * 60 * 1000);
		out.print(daysFromNow > days);
	}

}