Web Interface: Where's Last Update Logic to calculate relative time?

Martha5 years ago

I've create a web page using Traccar's API and basic HTML, JS/jQuery, and CSS. In this web page, I'm displaying some information about a tracking device similar to the information you see in the Traccar's Web Interface: https://i.stack.imgur.com/b4Yss.jpg

I'm receiving the device's last update time in a ISO format: 2019-01-11T15:55:47.377+0000 from Traccar's API.

I need to display the relative time between the device's last update and the time I open the web page like the Web interface is doing (even adjusting to my Time Zone) and showing last update 28 days ago, or 1 hour or 10 minutes ago.

I've looked through the source code of the Web interface https://github.com/traccar/traccar-web but I haven't found where exactly it indicates this logic for Last Update. Which file is it? or what libraries should I use to get the same results? I'm not familiar yet on ExtJS web app structures, so if anyone is familiar with these type of web apps, please advise!

Any help would be appreciated!

Anton Tananaev5 years ago

It's in AttributeFormatter.js.

Martha5 years ago

So I'm looking at the "lastUpdateFormatter" function:

lastUpdateFormatter: function (value) {
        var seconds, interval;
        if (value) {
            seconds = Math.floor((new Date() - value) / 1000);
            if (seconds < 0) {
                seconds = 0;
            }
            interval = Math.floor(seconds / 86400);
            if (interval > 1) {
                return interval + ' ' + Strings.sharedDays;
            }
            interval = Math.floor(seconds / 3600);
            if (interval > 1) {
                return interval + ' ' + Strings.sharedHours;
            }
            return Math.floor(seconds / 60) + ' ' + Strings.sharedMinutes;
        }
        return null;
    }

Where does "Strings.sharedDays" come from? Where are you declaring "Strings"?

Thank you for your help!

Anton Tananaev5 years ago

There are a bunch of JSON files in the l10n folder.