It is very common in my interactive flows, that the user can input a general time offset as string (eg. "2h 30m"), and the flow adds it to the current time, or to any specific date time value.
Therefore I've implemented it as the following script:
Code: Select all
<action type="script">
<useDefaultName>false</useDefaultName>
<name>result=addOffset(date=now, offset=´0m´)</name>
<script>result = getValue("date", getDate());
offset = getValue("offset", "0m");
for (item in split(offset, ' +'))
{
groups = newList();
valid = matches(item, "([-0-9.]+)([smhd])", groups);
if (valid)
{
unit = getElement(groups, 2);
count = toNumber(getElement(groups, 1));
if (unit == "s")
{result = addSeconds(result, count);}
else if (unit == "m")
{result = addMinutes(result, count);}
else if (unit == "h")
{result = addHours(result, count);}
else if (unit == "d")
{result = addDays(result, count);}
//log("result {result, dateformat, HH:mm:ss}");
}
else
{
log("Assertion error: offset is {_offset}");
}
}</script>
</action>
I think, this function would be useful for the other Automagic users too. What do you think about adding it to the library?
Regards:
Tamás