# Human readable display of hours:minutes

**URL:** <https://community.unix.com/t/human-readable-display-of-hours-minutes/388570>\
**Category:** Programming\
**Created:** [October 26, 2022, 1:29pm UTC](https://community.unix.com/t/human-readable-display-of-hours-minutes/388570 "2022-10-26T13:29:24Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![technossomy](https://community.unix.com/user_avatar/community.unix.com/technossomy/32/14676_2.png) [@technossomy](https://community.unix.com/u/technossomy)\
**Post date:** [October 26, 2022, 1:29pm UTC](https://community.unix.com/t/human-readable-display-of-hours-minutes/388570/1 "2022-10-26T13:29:24Z")

</div>

I have a C program that logs the duration of a process (substituted with `sleep` for illustrative purposes):

```nohighlight
// Compilation: gcc time.c -lm -Wall -o time
// Execution: ./time
#include <stdio.h>
#include <string.h>
#include <time.h>
#include <math.h>
#include <unistd.h>

int main (void) {
  time_t begin = time(NULL);
  sleep(4);
  time_t end = time(NULL);

  char buffer_hours[3];
  int hours = floor((end-begin) / 3600);
  sprintf(buffer_hours, "%d", hours);
  if (hours < 10) strcat(buffer_hours, "0");

  char buffer_minutes[3];
  int minutes = round(hours / 60);
  sprintf(buffer_minutes, "%d", minutes);
  if (minutes < 10) strcat(buffer_minutes, "0");

  printf("Time elapsed: %ld seconds (%s:%s hh:mm).\n\a", (end - begin), buffer_hours, buffer_minutes);

  return 0;
}

```

The purpose is to show time in seconds as well as hours and minutes, but with a zero prefixed for when either is less than 10. This seems like an inefficient way of doing it, as 4 new variables are introduced as well as 4 lines of string handling. Can someone suggest a refactored way of how this could be done?

---

<div class="post-metadata">

**Author:** ![bendingrodriguez](https://community.unix.com/user_avatar/community.unix.com/bendingrodriguez/32/21971_2.png) [@bendingrodriguez](https://community.unix.com/u/bendingrodriguez)\
**Post date:** [October 26, 2022, 2:44pm UTC](https://community.unix.com/t/human-readable-display-of-hours-minutes/388570/2 "2022-10-26T14:44:33Z")

</div>

Hi @technossomy,

you can use `printf`'s format capacity, see `man 3 printf`. Here is a simple version that also prints the other time intervals:

```c
#include <stdio.h> // printf()
#include <stdlib.h> // atoi()
#include <time.h> // time()
#include <unistd.h> // sleep()

int main(int argc, char **argv) {
    time_t beg = time(NULL);
    sleep(atoi(argv[1]));
    time_t end = time(NULL);

    int secs = end - beg;
    printf("Time elapsed: %d seconds (%d-%02d:%02d:%02d d-hh:mm:ss).\n", \
           secs, secs/86400, secs/3600%24, secs/60%60, secs%60);

    return 0;
}

```

For testing different time intervals, comment out the `sleep` call and replace `int secs ...` by `int secs = atoi(argv[1]);`. Start the prog via `./time seconds`.

---

<div class="post-metadata">

**Author:** ![munkeHoller](https://community.unix.com/user_avatar/community.unix.com/munkeholler/32/7431_2.png) [@munkeHoller](https://community.unix.com/u/munkeHoller)\
**Post date:** [October 26, 2022, 2:55pm UTC](https://community.unix.com/t/human-readable-display-of-hours-minutes/388570/3 "2022-10-26T14:55:55Z")

</div>

see the below .... , with or without minute/hour variables, no intermediate strings, and minimum 2 digit precision on number of ... seconds/minutes/hours.  
you can pass a sleep delay on the command line ... if none supplied it'll waste 5 seconds of your valuable time 🙂

```nohighlight
#include <stdlib.h>
#include <stdio.h>
#include <stdio.h>
#include <string.h>
#include <time.h>
#include <math.h>
#include <unistd.h>

int main (int argc, char **argv )
{
        int secs=5;

        secs = (argc == 2) ? atoi(argv[1]) : 5;
        time_t begin = time(NULL);
        printf(" sleeping for %d seconds \n", secs );
        sleep(secs);
        time_t end = time(NULL);

        printf( " without any additional variables \n" );
        printf("Time elapsed: %.2ld seconds (%.2ld:%.2ld hh:mm).\n\a", (end - begin), (long)floor((end-begin) / 3600), (long)( floor((end-begin) / 3600) / 60 ) );

        int hours = floor((end-begin) / 3600);
        int minutes = round(hours / 60);
        printf( "\nwith minute and hour variables \n" );
        printf("Time elapsed: %.2ld seconds (%.2d:%.2d hh:mm).\n\a", (end - begin), hours, minutes);

        return 0;
}

```

use of "%.2ld" or "%.2d" - guarantees at least 2 digits

---

<div class="post-metadata">

**Author:** ![munkeHoller](https://community.unix.com/user_avatar/community.unix.com/munkeholler/32/7431_2.png) [@munkeHoller](https://community.unix.com/u/munkeHoller)\
**Post date:** [October 26, 2022, 6:33pm UTC](https://community.unix.com/t/human-readable-display-of-hours-minutes/388570/4 "2022-10-26T18:33:54Z")

</div>

@technossomy,

fyi -  
you can do away with the sleep (and save yourself some time 🙂 ... just add the #seconds passed in to the **end** variable.

`time_t end = time(NULL) + atoi(argv[1]);`

---

<div class="post-metadata">

**Author:** ![system](https://community.unix.com/uploads/default/original/2X/c/c7e9e16a595259edafe88615719d5c5dcda035c2.png) [@system](https://community.unix.com/u/system)\
**Post date:** [November 5, 2022, 6:34pm UTC](https://community.unix.com/t/human-readable-display-of-hours-minutes/388570/5 "2022-11-05T18:34:05Z")

</div>

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