From 9371ad88dcae5e6694a84d11b54ad0c472edc1c4 Mon Sep 17 00:00:00 2001 From: Arnaud Roques Date: Thu, 21 Jul 2022 15:40:34 +0200 Subject: [PATCH] Adding %now function --- .../sourceforge/plantuml/tim/TContext.java | 2 + .../plantuml/tim/stdlib/DateFunction.java | 15 +++-- .../sourceforge/plantuml/tim/stdlib/Now.java | 64 +++++++++++++++++++ .../sourceforge/plantuml/version/Version.java | 2 +- 4 files changed, 77 insertions(+), 6 deletions(-) create mode 100644 src/net/sourceforge/plantuml/tim/stdlib/Now.java diff --git a/src/net/sourceforge/plantuml/tim/TContext.java b/src/net/sourceforge/plantuml/tim/TContext.java index 4de69da68..37c8a2efd 100644 --- a/src/net/sourceforge/plantuml/tim/TContext.java +++ b/src/net/sourceforge/plantuml/tim/TContext.java @@ -113,6 +113,7 @@ import net.sourceforge.plantuml.tim.stdlib.LoadJson; import net.sourceforge.plantuml.tim.stdlib.LogicalNot; import net.sourceforge.plantuml.tim.stdlib.Lower; import net.sourceforge.plantuml.tim.stdlib.Newline; +import net.sourceforge.plantuml.tim.stdlib.Now; import net.sourceforge.plantuml.tim.stdlib.RetrieveProcedure; import net.sourceforge.plantuml.tim.stdlib.ReverseColor; import net.sourceforge.plantuml.tim.stdlib.ReverseHsluvColor; @@ -189,6 +190,7 @@ public class TContext { functionsSet.addFunction(new GetJsonType()); functionsSet.addFunction(new SplitStr()); functionsSet.addFunction(new JsonKeyExists()); + functionsSet.addFunction(new Now()); // %standard_exists_function // %str_replace // !exit diff --git a/src/net/sourceforge/plantuml/tim/stdlib/DateFunction.java b/src/net/sourceforge/plantuml/tim/stdlib/DateFunction.java index 715719b26..7e45da94f 100644 --- a/src/net/sourceforge/plantuml/tim/stdlib/DateFunction.java +++ b/src/net/sourceforge/plantuml/tim/stdlib/DateFunction.java @@ -51,21 +51,26 @@ import net.sourceforge.plantuml.tim.expression.TValue; public class DateFunction extends SimpleReturnFunction { public TFunctionSignature getSignature() { - return new TFunctionSignature("%date", 1); + return new TFunctionSignature("%date", 2); } public boolean canCover(int nbArg, Set namedArgument) { - return nbArg == 0 || nbArg == 1; + return nbArg == 0 || nbArg == 1 || nbArg == 2; } public TValue executeReturnFunction(TContext context, TMemory memory, LineLocation location, List values, Map named) throws EaterException, EaterExceptionLocated { - if (values.size() == 0) { + if (values.size() == 0) return TValue.fromString(new Date().toString()); - } + final String format = values.get(0).toString(); + final long now; + if (values.size() == 2) + now = 1000L * values.get(1).toInt(); + else + now = System.currentTimeMillis(); try { - return TValue.fromString(new SimpleDateFormat(format).format(new Date())); + return TValue.fromString(new SimpleDateFormat(format).format(now)); } catch (Exception e) { throw EaterException.unlocated("Bad date pattern"); } diff --git a/src/net/sourceforge/plantuml/tim/stdlib/Now.java b/src/net/sourceforge/plantuml/tim/stdlib/Now.java new file mode 100644 index 000000000..206ca8437 --- /dev/null +++ b/src/net/sourceforge/plantuml/tim/stdlib/Now.java @@ -0,0 +1,64 @@ +/* ======================================================================== + * PlantUML : a free UML diagram generator + * ======================================================================== + * + * (C) Copyright 2009-2023, Arnaud Roques + * + * Project Info: http://plantuml.com + * + * If you like this project or if you find it useful, you can support us at: + * + * http://plantuml.com/patreon (only 1$ per month!) + * http://plantuml.com/paypal + * + * This file is part of PlantUML. + * + * PlantUML is free software; you can redistribute it and/or modify it + * under the terms of the GNU General Public License as published by + * the Free Software Foundation, either version 3 of the License, or + * (at your option) any later version. + * + * PlantUML distributed in the hope that it will be useful, but + * WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY + * or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public + * License for more details. + * + * You should have received a copy of the GNU General Public + * License along with this library; if not, write to the Free Software + * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, + * USA. + * + * + * Original Author: Arnaud Roques + * + */ +package net.sourceforge.plantuml.tim.stdlib; + +import java.util.List; +import java.util.Map; +import java.util.Set; + +import net.sourceforge.plantuml.LineLocation; +import net.sourceforge.plantuml.tim.EaterException; +import net.sourceforge.plantuml.tim.EaterExceptionLocated; +import net.sourceforge.plantuml.tim.TContext; +import net.sourceforge.plantuml.tim.TFunctionSignature; +import net.sourceforge.plantuml.tim.TMemory; +import net.sourceforge.plantuml.tim.expression.TValue; + +public class Now extends SimpleReturnFunction { + + public TFunctionSignature getSignature() { + return new TFunctionSignature("%now", 0); + } + + public boolean canCover(int nbArg, Set namedArgument) { + return nbArg == 0; + } + + public TValue executeReturnFunction(TContext context, TMemory memory, LineLocation location, List values, + Map named) throws EaterException, EaterExceptionLocated { + final long now = System.currentTimeMillis() / 1000L; + return TValue.fromInt((int) now); + } +} diff --git a/src/net/sourceforge/plantuml/version/Version.java b/src/net/sourceforge/plantuml/version/Version.java index 3de4de91b..b79bbc77b 100644 --- a/src/net/sourceforge/plantuml/version/Version.java +++ b/src/net/sourceforge/plantuml/version/Version.java @@ -80,7 +80,7 @@ public class Version { } public static int beta() { - final int beta = 3; + final int beta = 4; return beta; }