Workshop: How to populate time diff

I have created dataset in coderepo with two columns in my table with a timestamp data type:

  • start_time
  • end_time

I’ve created a new column called time_diff to calculate the difference between these timestamps.

Logic:

time_diff = start_time - end_time

Expected outcome:

User can input timestamp in above mentioned columns. While in registration using form the time_diff column should automatically display the time difference in a human-readable format, suitable for both the “Workshop” and “ObjectExplorer” . Here are some examples:

  • 5 min 13 sec (for a short duration)
  • 10 days, 15 hours, 13 sec (for a longer duration)

This way, the time_diff column provides a clear and easy-to-understand representation of the elapsed time between the start_time and end_time.

Please provide suggestions on how to best solve this in Ontology app or Workshop or in Functions

Hello!

Try something like this. For this to work, your difference should be in milliseconds. The output is a FunctionsMap with key as the object and value as the difference in human readable format (string) so you can add it as a column in an object table in Workshop.

    @Function()
    public readableTime(somethingwithtimestamps: ObjectSet<YourObjectSet>) : FunctionsMap<YourObjectSet, string> {
        const allObj = somethingwithtimestamps.all()
        const res = new FunctionsMap<YourObjectSet, string>();
    
        for (const entry of allObj) {
            const diff = Math.abs(entry.startTime!.getTime() - entry.endTime!.getTime())
            const days =  Math.floor(diff / (24*60*60*1000));
            const daysms = diff % (24*60*60*1000);
            const hours = Math.floor(daysms / (60*60*1000));
            const hoursms = diff % (60*60*1000);
            const minutes = Math.floor(hoursms / (60*1000));
            const minutesms = diff % (60*1000);
            const sec = Math.floor(minutesms / 1000);
            const concat = days + " days, " + hours + " hours " + minutes + " minutes " + sec +" seconds";
            res.set(entry, concat);
        }
       
        return res;
    }

Nicole