From: Max Kellermann Date: Tue, 21 Mar 2017 21:41:58 +0000 (+0100) Subject: utils: move format_duration_*() to time_format.c X-Git-Tag: v0.27~8 X-Git-Url: https://git.tokkee.org/?a=commitdiff_plain;h=f2b8b4378b3760e8ba5a1ed54a5f8635d3dac85e;p=ncmpc.git utils: move format_duration_*() to time_format.c --- diff --git a/Makefile.am b/Makefile.am index a70f478..ac11c69 100644 --- a/Makefile.am +++ b/Makefile.am @@ -62,6 +62,7 @@ src_ncmpc_SOURCES = \ src/window.h \ src/wreadln.c src/wreadln.h \ src/strfsong.c src/strfsong.h \ + src/time_format.c src/time_format.h \ src/utils.c src/utils.h if ENABLE_ASYNC_CONNECT diff --git a/src/screen_song.c b/src/screen_song.c index 086768c..61c77d5 100644 --- a/src/screen_song.c +++ b/src/screen_song.c @@ -25,7 +25,7 @@ #include "i18n.h" #include "screen.h" #include "charset.h" -#include "utils.h" +#include "time_format.h" #include "mpdclient.h" #include diff --git a/src/song_paint.c b/src/song_paint.c index 3b55eaf..56b80a3 100644 --- a/src/song_paint.c +++ b/src/song_paint.c @@ -20,7 +20,7 @@ #include "song_paint.h" #include "paint.h" #include "strfsong.h" -#include "utils.h" +#include "time_format.h" #include "hscroll.h" #include "charset.h" #include "config.h" diff --git a/src/status_bar.c b/src/status_bar.c index bdf3bcd..fc63d20 100644 --- a/src/status_bar.c +++ b/src/status_bar.c @@ -24,7 +24,7 @@ #include "charset.h" #include "strfsong.h" #include "player_command.h" -#include "utils.h" +#include "time_format.h" #include diff --git a/src/strfsong.c b/src/strfsong.c index 8fe1016..b64c350 100644 --- a/src/strfsong.c +++ b/src/strfsong.c @@ -19,7 +19,7 @@ #include "strfsong.h" #include "charset.h" -#include "utils.h" +#include "time_format.h" #include diff --git a/src/time_format.c b/src/time_format.c new file mode 100644 index 0000000..8db5cbd --- /dev/null +++ b/src/time_format.c @@ -0,0 +1,76 @@ +/* ncmpc (Ncurses MPD Client) + * (c) 2004-2017 The Music Player Daemon Project + * Project homepage: http://musicpd.org + * + * This program is free software; you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation; either version 2 of the License, or + * (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License along + * with this program; if not, write to the Free Software Foundation, Inc., + * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA. + */ + +#include "time_format.h" +#include "i18n.h" + +#include + +void +format_duration_short(char *buffer, size_t length, unsigned duration) +{ + if (duration < 3600) + g_snprintf(buffer, length, + "%i:%02i", duration / 60, duration % 60); + else + g_snprintf(buffer, length, + "%i:%02i:%02i", duration / 3600, + (duration % 3600) / 60, duration % 60); +} + +void +format_duration_long(char *p, size_t length, unsigned long duration) +{ + unsigned bytes_written = 0; + + if (duration / 31536000 > 0) { + if (duration / 31536000 == 1) + bytes_written = g_snprintf(p, length, "%d %s, ", 1, _("year")); + else + bytes_written = g_snprintf(p, length, "%lu %s, ", duration / 31536000, _("years")); + duration %= 31536000; + length -= bytes_written; + p += bytes_written; + } + if (duration / 604800 > 0) { + if (duration / 604800 == 1) + bytes_written = g_snprintf(p, length, "%d %s, ", + 1, _("week")); + else + bytes_written = g_snprintf(p, length, "%lu %s, ", + duration / 604800, _("weeks")); + duration %= 604800; + length -= bytes_written; + p += bytes_written; + } + if (duration / 86400 > 0) { + if (duration / 86400 == 1) + bytes_written = g_snprintf(p, length, "%d %s, ", + 1, _("day")); + else + bytes_written = g_snprintf(p, length, "%lu %s, ", + duration / 86400, _("days")); + duration %= 86400; + length -= bytes_written; + p += bytes_written; + } + + g_snprintf(p, length, "%02lu:%02lu:%02lu", duration / 3600, + duration % 3600 / 60, duration % 3600 % 60); +} diff --git a/src/time_format.h b/src/time_format.h new file mode 100644 index 0000000..c3a4b54 --- /dev/null +++ b/src/time_format.h @@ -0,0 +1,31 @@ +/* ncmpc (Ncurses MPD Client) + * (c) 2004-2017 The Music Player Daemon Project + * Project homepage: http://musicpd.org + * + * This program is free software; you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation; either version 2 of the License, or + * (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License along + * with this program; if not, write to the Free Software Foundation, Inc., + * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA. + */ + +#ifndef TIME_FORMAT_H +#define TIME_FORMAT_H + +#include + +void +format_duration_short(char *buffer, size_t length, unsigned duration); + +void +format_duration_long(char *buffer, size_t length, unsigned long duration); + +#endif diff --git a/src/utils.c b/src/utils.c index 8f867ad..0c35c9e 100644 --- a/src/utils.c +++ b/src/utils.c @@ -18,7 +18,6 @@ */ #include "utils.h" -#include "i18n.h" #include #include @@ -58,56 +57,3 @@ string_list_remove(GList *string_list, const gchar *str) } return list; } - -void -format_duration_short(char *buffer, size_t length, unsigned duration) -{ - if (duration < 3600) - g_snprintf(buffer, length, - "%i:%02i", duration / 60, duration % 60); - else - g_snprintf(buffer, length, - "%i:%02i:%02i", duration / 3600, - (duration % 3600) / 60, duration % 60); -} - -void -format_duration_long(char *p, size_t length, unsigned long duration) -{ - unsigned bytes_written = 0; - - if (duration / 31536000 > 0) { - if (duration / 31536000 == 1) - bytes_written = g_snprintf(p, length, "%d %s, ", 1, _("year")); - else - bytes_written = g_snprintf(p, length, "%lu %s, ", duration / 31536000, _("years")); - duration %= 31536000; - length -= bytes_written; - p += bytes_written; - } - if (duration / 604800 > 0) { - if (duration / 604800 == 1) - bytes_written = g_snprintf(p, length, "%d %s, ", - 1, _("week")); - else - bytes_written = g_snprintf(p, length, "%lu %s, ", - duration / 604800, _("weeks")); - duration %= 604800; - length -= bytes_written; - p += bytes_written; - } - if (duration / 86400 > 0) { - if (duration / 86400 == 1) - bytes_written = g_snprintf(p, length, "%d %s, ", - 1, _("day")); - else - bytes_written = g_snprintf(p, length, "%lu %s, ", - duration / 86400, _("days")); - duration %= 86400; - length -= bytes_written; - p += bytes_written; - } - - g_snprintf(p, length, "%02lu:%02lu:%02lu", duration / 3600, - duration % 3600 / 60, duration % 3600 % 60); -} diff --git a/src/utils.h b/src/utils.h index ecd68bb..010cfee 100644 --- a/src/utils.h +++ b/src/utils.h @@ -27,10 +27,4 @@ GList *string_list_free(GList *string_list); GList *string_list_find(GList *string_list, const gchar *str); GList *string_list_remove(GList *string_list, const gchar *str); -void -format_duration_short(char *buffer, size_t length, unsigned duration); - -void -format_duration_long(char *buffer, size_t length, unsigned long duration); - #endif