Convert Number into Time (HH:MM:SS)

Hello,
I'm refering to my previous post : Convert Number (elapsed_time) into Time (HH:MM:SS)

I have some issue that i don't understand with the scripted field from angelo response. This time I have a number in Seconds and I want it to be a duration like that hh:mm:ss (in my previous topic i had number in milliseconds)

I have come with this

if (doc['duration'].size()==0) {
    return ""
} else {
    long elapsed_time=(long)doc["duration"].value;
    
    
    long minutesInseconds = 60;
    long hoursInseconds = minutesInseconds * 60;
    // long daysInMilli = hoursInMilli * 24;

    // long elapsedDays = elapsed_time / daysInMilli;
    // elapsed_time = elapsed_time % daysInMilli;
	
    long elapsedHours = elapsed_time / hoursInseconds;
    elapsed_time = elapsed_time % hoursInseconds;

    long elapsedMinutes = elapsed_time / minutesInseconds;
    elapsed_time = elapsed_time % minutesInseconds;
    
    long elapsedSeconds = elapsed_time;

    String my_answer = String.format("%02d:%02d:%02d", new def[] {(int)elapsedHours, (int)elapsedMinutes, (int)elapsedSeconds});
    return my_answer;
}

But for example if i set 7200 second (2 hours) it's displayed like that 00:00:02 instead of 02:00:00

Can tell me where is my mistake here because I don't understand ?

In my scripted field i have correctly set up my number like this :

Thank you for help
Thibaut

You are really close. Just set format as string instead of number since your script is returning a string and not a number.

Here is the sample data I was using

PUT test
{}

PUT test/_doc/1
{
    "duration": 7200
}

PUT test/_doc/2
{
    "duration": 3900
}

Hi Nathan,

I tried what you suggest but as you can see in my screenshot

image

It convert in Minute:seconds:milliseconds I don' have the hours... Do you know if there is a mistake in my scripted field ?

Thibaut

I'm not sure where this is going wrong. In my case, a 'duration' of 7200 always returns "02:00:00" given the format specified at the end for my_answer. This is regardless of asking Kibana to format it as a Number or String (with Number.js formatting of 00:00:00).

However, your last screenshot is a bit curious - if the duration value is 7200, you are showing a field of duration_min, which on its own would be equal to 120 minutes if you wanted to report it that way. If you do want it in HH:mm:ss format, then that should come out to 02:00:00 which is done via the string formatting of my_answer.

As a test, if you run the following in Kibana Dev Tools, do you get values of "02:00:00" and "01:05:00" using Nathan's example?

PUT test
{}
PUT test/_doc/1
{"elapsed_time": 7200}
PUT test/_doc/2
{"elapsed_time": 3900}
GET test/_search
{
  "script_fields": {
    "diff": {
      "script": """if (doc['elapsed_time'].size()==0) {
    return ""
} else {
    long elapsed_time=(long)doc["elapsed_time"].value;
    
    long minutesInseconds = 60;
    long hoursInseconds = minutesInseconds * 60;
    // long daysInMilli = hoursInMilli * 24;

    // long elapsedDays = elapsed_time / daysInMilli;
    // elapsed_time = elapsed_time % daysInMilli;
	
    long elapsedHours = elapsed_time / hoursInseconds;
    elapsed_time = elapsed_time % hoursInseconds;

    long elapsedMinutes = elapsed_time / minutesInseconds;
    elapsed_time = elapsed_time % minutesInseconds;
    
    long elapsedSeconds = elapsed_time;

    String my_answer = String.format("%02d:%02d:%02d", new def[] {(int)elapsedHours, (int)elapsedMinutes, (int)elapsedSeconds});
    return my_answer;
}"""
    }
  }
}

This topic was automatically closed 28 days after the last reply. New replies are no longer allowed.