Code

screen_song: Added some statistics about mpd to the song screen.
[ncmpc.git] / src / utils.c
index cd125099e67a72edd245608b411bcf0dbf7f14c0..32707eb48901ac164ee8d23e4e9fdde377a8c89d 100644 (file)
@@ -20,6 +20,7 @@
 #include "utils.h"
 #include "options.h"
 #include "charset.h"
+#include "i18n.h"
 
 #include <ctype.h>
 #include <stdlib.h>
@@ -113,3 +114,56 @@ gcmp_list_from_path(mpdclient_t *c, const gchar *path, GList *list, gint types)
        filelist_free(filelist);
        return list;
 }
+
+char *
+time_seconds_to_durationstr(unsigned long time_seconds)
+{
+       const char *year = _("year");
+       const char *years = _("years");
+       const char *week = _("week");
+       const char *weeks = _("weeks");
+       const char *day = _("day");
+       const char *days = _("days");
+       char *duration;
+       char *iter;
+       unsigned bytes_written = 0;
+       unsigned length = utf8_width(years) +
+               utf8_width(weeks) + utf8_width(days) + 32;
+
+       duration = g_malloc(length);
+       iter = duration;
+       if (time_seconds / 31536000 > 0) {
+               if (time_seconds % 31536000 == 1)
+                       bytes_written = g_snprintf(iter, length, "%lu %s, ", time_seconds / 31536000, year);
+               else
+                       bytes_written = g_snprintf(iter, length, "%lu %s, ", time_seconds / 31536000, years);
+               time_seconds %= 31536000;
+               length -= bytes_written;
+               iter += bytes_written;
+       }
+       if (time_seconds / 604800 > 0) {
+               if (time_seconds % 604800 == 1)
+                       bytes_written = g_snprintf(iter, length, "%lu %s, ", time_seconds / 604800, week);
+               else
+                       bytes_written = g_snprintf(iter, length, "%lu %s, ", time_seconds / 604800, weeks);
+               time_seconds %= 604800;
+               length -= bytes_written;
+               iter += bytes_written;
+       }
+       if (time_seconds / 86400 > 0) {
+               if (time_seconds % 86400 == 1)
+                       bytes_written = g_snprintf(iter, length, "%lu %s, ", time_seconds / 86400, day);
+               else
+                       bytes_written = g_snprintf(iter, length, "%lu %s, ", time_seconds / 86400, days);
+               time_seconds %= 86400;
+               length -= bytes_written;
+               iter += bytes_written;
+       }
+       g_snprintf(iter, length, "%02lu:%02lu:%02lu",
+                       time_seconds / 3600,
+                       time_seconds % 3600 / 60,
+                       time_seconds % 3600 % 60);
+       return duration;
+}
+
+