Date and Time - SurrealTools/Documentation GitHub Wiki
The duration units are: ns, µs, ms, s, m, h, d, w, y There isn't any duration unit for months, because a month can be a variable length. So instead you could use 4w or 31d or something like that. The same goes for years y, but because it's relatively the same each year, we added it in for simple calculations.
largest time unit is years. It doesn't support months. So you can specify it in y (years), w (weeks), d (days), h (hours), m (minutes), s (seconds), ms (milliseconds), µs (microseconds), ns (nanoseconds). The largest possible duration is 584,942,417,355y3w5d7h15s.
SELECT math::sum(amount) AS balance FROM payment WHERE userID=$userID AND date < (time::now() - 4w) GROUP BY amount
SELECT * FROM time::format("2021-11-01T08:30:17+00:00", "%Y-%m-%d");
-- "2021-11-01"
DEFINE FIELD dob ON user TYPE datetime
DEFINE FIELD age ON user VALUE (duration::years(time::now() - dob))
--- Future value
DEFINE FIELD age ON test TYPE any VALUE <future> { duration::years(time::now() - dob) }
create user set dob = <datetime>"1987-09-12"
RETURN time::now() + 3d;
-- or
RETURN time::now() + 1h30m;
- The createdAt field will be updated, each time the record is updated. To stop this from happening you need to check whether the value is already set or not, and only set it if it isn't...
-- Check if there was a $before value, effectively making this field write-once, and then readonly.
-- Also we don't need an ASSERT clause, because we never allow a user-supplied value to be inserted.
DEFINE FIELD createdAt ON class TYPE datetime VALUE $before OR time::now();
- The updatedAt field will be updated on every record modification. So you don't need the DEFINE EVENT clause. The following field will ensure that the field is updated every time... ``sql -- We don't need an ASSERT clause, because we never allow a user-supplied value to be inserted. DEFINE FIELD updatedAt ON class TYPE datetime VALUE time::now();
3. What you are doing with the DEFINE EVENT clause is updating the current record whenever the current record is updated. This event modifies the record, meaning that the record is actually updated. This will therefore in-turn mean that the event is run in a never-ending loop, as it updates itself, and initiates a new event to update itself. This is therefore causing a 'too much depth' error!
(my application has to permit importing data from external sources, so they should be settable on insert instead of using the defaults)
```sql
DEFINE FIELD createdAt ON class TYPE datetime VALUE $value OR $before OR time::now() ASSERT $value != NONE;
DEFINE FIELD updatedAt ON class TYPE datetime VALUE $value OR time::now() ASSERT $value != NONE;