evalinkdocs

Time functions

In evalink analytics, every time is a number: milliseconds since 1 January 1970, UTC. The time.* functions turn these numbers into dates, filter by periods such as "today" or "last week", and group by day or hour.

The time functions work in UTC: time.start_of('day') is midnight UTC. For your own time zone, see Use your own time zone.

Filter by a period

Function What it does
time.now() The current time.
time.ago('5 minutes') The time 5 minutes ago. Units: minute, hour, day, week, month, year.
time.last('5 minutes') Same as time.ago.
time.last(ms, '3 days') True if the time lies within the last 3 days.
time.in_last(ms, '3 days') Same as time.last(ms, …).
time.since_start_of(ms, 'week') True if the time lies between the start of this week and now.
time.until_end_of(ms, 'week') True if the time lies between now and the end of this week.
time.between(ms, from, to) True if the time lies between two dates, both included. Write the dates as text, for example '2025-01-01 00:00:00'.
SELECT * FROM analytics.alarm a
WHERE a.time BETWEEN time.ago('2 days') AND time.now();

SELECT * FROM analytics.alarm a
WHERE time.since_start_of(a.time, 'week');

Start and end of a period

Function What it does
time.start_of('day') The start of the current day.
time.start_of(ms, 'hour') The start of the hour that contains the time.
time.end_of('day') The last millisecond of the current day.
time.end_of(ms, 'month') The last millisecond of the month that contains the time.
time.truncate(ms, 'day') Rounds the time down to the unit. Use it to group by day, hour and so on.

Units: millisecond, second, minute, hour, day, week, month, quarter, year. Weeks start on Monday. Upper and lower case both work. Days, weeks and months start and end in UTC.

-- Alarms per day (UTC days)
SELECT time.format(time.truncate(a.time, 'day'), 'YYYY-MM-DD') AS day,
       COUNT(*)
FROM analytics.alarm a
WHERE time.last(a.time, '30 days')
GROUP BY 1
ORDER BY 1;

Show a time as a date

Function What it does
time.ts(ms) The time as a timestamp in UTC.
time.ts(ms, 'America/New_York') The time as a timestamp in the given time zone.
time.format(ms) The time as text in UTC: 2025-09-23 15:42:17.000.
time.format(ms, pattern) The time as text in UTC, in your own pattern.
time.ms('2024-01-01 00:00:00') A UTC date, as text or timestamp, turned into milliseconds.

Pattern examples:

Pattern Result
'YYYY-MM-DD HH24:MI:SS' 2025-09-23 15:42:17
'DD.MM.YYYY' 23.09.2025
'Dy, DD Mon YYYY' Tue, 23 Sep 2025

Use your own time zone

To see a time in your time zone, pass the zone to time.ts. Use time zone names such as Europe/Zurich, Europe/Berlin or America/New_York:

SELECT a.alarm_code, time.ts(a.time, 'Europe/Zurich')
FROM analytics.alarm a
WHERE time.last(a.time, '5 days')
ORDER BY a.time;

To group by local day or hour, round the converted time with date_trunc:

-- Alarms per day, Zurich time
SELECT date_trunc('day', time.ts(a.time, 'Europe/Zurich')) AS day,
       COUNT(*)
FROM analytics.alarm a
WHERE time.last(a.time, '30 days')
GROUP BY 1
ORDER BY 1;

To use each site's own time zone, join the site and take its time_zone:

SELECT date_trunc('day', time.ts(a.time, COALESCE(d.time_zone, 'UTC'))) AS day,
       d.name,
       COUNT(*)
FROM analytics.alarm a
  JOIN analytics.device d USING (device_id)
WHERE time.last(a.time, '30 days')
GROUP BY 1, 2
ORDER BY 1, 2;

Durations

These functions return a length in milliseconds, for calculations such as a.time + time.hours(1):

time.seconds(n), time.minutes(n), time.hours(n), time.days(n), time.weeks(n), time.months(n), time.years(n)

time.months counts 30 days and time.years 365 days. For exact calendar months, use time.ago('1 month') or dateadd.

To get a duration between two times, subtract them: (w.closed_date - w.created_date) / 1000 gives seconds.