Convert Seconds to date format

Hello everyone, I have a request for you :

I have a value, exemple value = 60000, which is the uptime in second of a device. This value is a child parameter.
I want to view this value into a regular date format (HH:MM:SS).
I want to know if you have fonctions or methods to do it.

And can you tell me if all the display config is in YAML please.

Thank you in advance for your replies.

You donā€™t mention what version of GenieACS you are using. In v1.0 and v1.1 (not sure about v1.2 as we havenā€™t migrated yet) there is support. In v1.0 and v1.1 look in the gui/config/parameter_renders.yml file. You will see a mapping between parameters and parameter renderers. You can even write your own by in ruby storing them in gui/config/parameter_renderers.

Example from my environment:

InternetGatewayDevice.DeviceInfo.UpTime: duration
Device.DeviceInfo.UpTime: duration
VirtualParameters.UpTime: duration
VirtualParameters.pppoeUsername: username_link
_deviceId._SerialNumber: serial_number_link
_tags: tags
_lastBoot: date_local

The two renderers username_link and serial_number_link are custom ones Iā€™ve written.

Thanks for your reply.

I use the last version : the 1.2 beta, so do you know if this version keeps the files you mentioned ?

Thereā€™s the function DATE_STRING() but that renders a datetime, not a duration. Maybe create a custom component to render a numeric parameter in that format? See ui/components/parameter.ts for an example component.

I do this via a virtual parameter. For example I convert UpTime to a format DDd hh:mm:ss with a Virtual Parameter called UpTimeDHMS.

There may be more efficient ways to write the code, my aim is for simplicity and clarity.

// Unified across Device and InternetGatewayDevice
let v1 = declare("Device.DeviceInfo.UpTime", {value: Date.now()});
let v2 = declare("InternetGatewayDevice.DeviceInfo.UpTime", {value: Date.now()});
let totalSecs = 0
if (typeof v1.value !== "undefined") {
  totalSecs = v1.value[0];
} else {
  totalSecs = v2.value[0];
}
let days = Math.floor(totalSecs / 86400);
let rem  = totalSecs % 86400;
let hrs  = Math.floor(rem / 3600);
if (hrs < 10) {
	hrs = "0" + hrs;
}

rem  = rem % 3600;
let mins = Math.floor(rem / 60);
if (mins < 10) {
	mins = "0" + mins;
}
let secs = rem % 60;
if (secs < 10) {
	secs = "0" + secs;
}

let uptime = days + "d " + hrs + ":" + mins + ":" + secs;
return {writable: false, value: uptime};

This is how it renders:

2 Likes

Hello,

Thank you for your reply and your solution. This is exactly what I am looking for ^^

So the ideal would be to include this in the genie code at ā€œparameter.tsā€:

if (e[0] === "FUNC" && e[1] === "DATE_STRING" && !Array.isArray(e[2])) {
  return new Date(e[2]).toLocaleString();
}
else if (e[0] === "FUNC" && e[1] === "SECONDS" && !Array.isArray(e[2])) {
    let totalSecs = e[2];
    let days = Math.floor(totalSecs / 86400);
    let rem  = totalSecs % 86400;
    let hrs  = Math.floor(rem / 3600);
    if (hrs < 10) {
            hrs = "0" + hrs;
    }

    rem  = rem % 3600;
    let mins = Math.floor(rem / 60);
    if (mins < 10) {
            mins = "0" + mins;
    }
    let secs = rem % 60;
    if (secs < 10) {
            secs = "0" + secs;
    }
    return days + "d " + hrs + ":" + mins + ":" + secs;
}
return e;

});

hello, yes it will be good as jriera say. i was try to get the connection time from some wifi stations.
I tryed to add that on parameter.ts but it complain during the run build. I revert back the file since i dont have a programming skills (also for create a external function or sort of):

/usr/src/genieacs# npm run build

genieacs@1.2.3 build /usr/src/genieacs
node -r esm -r ts-node/register/transpile-only build/build

@rollup/plugin-typescript TS2322: Type ā€˜stringā€™ is not assignable to type ā€˜numberā€™.
@rollup/plugin-typescript TS2322: Type ā€˜stringā€™ is not assignable to type ā€˜numberā€™.
@rollup/plugin-typescript TS2322: Type ā€˜stringā€™ is not assignable to type ā€˜numberā€™.

regards

hii, how to configure like this ? can you explain me ? thanks

Thank you for this extremely helpful post @KGcasa