## Time and Variable Math

With build 2.1.0.116 Rule 3.0 gets another feature addition: the ability to perform math operations and comparisons on Global Variables. Also, it is now possible to set the current time in seconds or milliseconds of Unix Epoch Time.

Dealing with dates and time is always a messy affair for computer software. There are so many formats, so many variations, and so many elements to the defining a moment in time. Unix simplified all of this into a single large number, the number of milliseconds since 00:00:00 UTC January 1, 1970. Using this number one can derive other representations of times, and find differences between two times by simply subtracting one number from another. Milliseconds of time divided by 1000 is seconds, divided again by 60 is minutes, etc.

First came the addition of setting a Global variable to `now()`

. `now()`

is a system function that returns Unix Time. Suppose I want to know how long a door has been left open. If I capture the time it opens by setting a variable called Open to `now()`

, and then later set another variable called Now the same way, then Now minus Open is the number of milliseconds the door has been open. I could use that information to trigger some automation.

As soon as one has numbers flying around like Unix Time (as I write this the time is 1558307477, a super obvious representation -- not!), one wants to manipulate them with simple arithmetic. Do to that in Rule Machine, we need mathematical operators, we need to do a little math.

Rule 3.0 now can do this with Set Variable. One choice is Variable Math. It allows you to do simple math on two numbers, and put the result of the calculation into a Global Variable. The operators supported are:

`+ addition`

`- subtraction`

`* multiplication`

`/ division`

`% remainder`

negate

absolute

round

random

The first five operators use two operands. The last four operators use a single operand. The operands can each be a Global Variable or a constant number. Numbers can be whole numbers or decimal fractions.

## Comparisons and Logic (again)

When setting a Boolean Global Variable, there are now available comparison operations and logical operators, the Boolean equivalent of math. For comparisons, we have the normal set of comparison operators:

```
=
!=
<
<=
>
>=
```

These each expect two operands, that can be either Global Variables (Numbers or Decimals) or constants.

For logic we can do operations on Boolean Global Variables. The operators supported are:

AND

OR

XOR

NAND

NOR

XNOR

NOT

All of these except NOT use two operands. If you are wondering about these, XOR (exclusive OR) is essential the same as 'not equal'. XOR is true if either but not both operands are true. XNOR, the opposite of XOR, is essentially the same as 'equal'. XNOR is true if both operands or true or both are false. NAND is the same as NOT AND, and NOR is the same as NOT OR. These six operators represent the six types of logic gates that all digital electronics are built from.