} \
}
-static short unsigned get_duration(int seconds_total, char *buf, short unsigned max_width)
+static short unsigned get_duration_width(int seconds)
{
short unsigned width;
- int s = seconds_total;
- unsigned hours = s / 3600, mins = (s % 3600) / 60, secs = s % 60;
-
- if (s < 3600) { /* less than one hour => m:ss or mm:ss */
- GET_NUM_DIGITS(mins, &width); /* 1 or 2 */
- width += 3; /* 4 or 5 */
- if (buf)
- sprintf(buf, "%*u:%02u", max_width - width + 1, mins, secs);
- return width;
- }
+ unsigned hours = seconds / 3600, mins = (seconds % 3600) / 60;
+
+ if (!hours) /* less than one hour => m:ss or mm:ss => 4 or 5 digits */
+ return 4 + (mins > 9);
/* more than one hour => h:mm:ss, hh:mm:ss, hhh:mm:ss, ... */
GET_NUM_DIGITS(hours, &width);
- width += 6;
- if (buf)
- sprintf(buf, "%*u:%02u:%02u", max_width - width + 1, hours, mins, secs);
- return width;
+ return width + 6;
+}
+
+static void get_duration_buf(int seconds, char *buf, short unsigned max_width)
+{
+ unsigned hours = seconds / 3600, mins = (seconds % 3600) / 60;
+
+ if (!hours) /* m:ss or mm:ss */
+ sprintf(buf, "%*u:%02u", max_width - 3, mins, seconds % 60);
+ else /* more than one hour => h:mm:ss, hh:mm:ss, hhh:mm:ss, ... */
+ sprintf(buf, "%*u:%02u:%02u", max_width - 6, hours, mins,
+ seconds % 60);
}
static char *make_attribute_line(const char *att_bitmap, struct afs_info *afsi)
sizeof(last_played_time));
if (ret < 0)
return ret;
- get_duration(afhi->seconds_total, duration_buf, w->duration_width);
+ get_duration_buf(afhi->seconds_total, duration_buf, w->duration_width);
if (have_score) {
if (opts->mode == LS_MODE_LONG)
sprintf(score_buf, "%*li ", w->score_width, d->score);
w->frequency_width = PARA_MAX(w->frequency_width, num_digits);
GET_NUM_DIGITS(d->afsi.num_played, &num_digits);
w->num_played_width = PARA_MAX(w->num_played_width, num_digits);
- /* just get the number of chars to print this amount of time */
- tmp = get_duration(d->afhi.seconds_total, NULL, 0);
+ /* get the number of chars to print this amount of time */
+ tmp = get_duration_width(d->afhi.seconds_total);
w->duration_width = PARA_MAX(w->duration_width, tmp);
if (options->flags & LS_FLAG_ADMISSIBLE_ONLY) {
GET_NUM_DIGITS(score, &num_digits);