Code

utils: move format_duration_*() to time_format.c
[ncmpc.git] / src / time_format.c
1 /* ncmpc (Ncurses MPD Client)
2  * (c) 2004-2017 The Music Player Daemon Project
3  * Project homepage: http://musicpd.org
4  *
5  * This program is free software; you can redistribute it and/or modify
6  * it under the terms of the GNU General Public License as published by
7  * the Free Software Foundation; either version 2 of the License, or
8  * (at your option) any later version.
9  *
10  * This program is distributed in the hope that it will be useful,
11  * but WITHOUT ANY WARRANTY; without even the implied warranty of
12  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
13  * GNU General Public License for more details.
14  *
15  * You should have received a copy of the GNU General Public License along
16  * with this program; if not, write to the Free Software Foundation, Inc.,
17  * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
18  */
20 #include "time_format.h"
21 #include "i18n.h"
23 #include <glib.h>
25 void
26 format_duration_short(char *buffer, size_t length, unsigned duration)
27 {
28         if (duration < 3600)
29                 g_snprintf(buffer, length,
30                            "%i:%02i", duration / 60, duration % 60);
31         else
32                 g_snprintf(buffer, length,
33                            "%i:%02i:%02i", duration / 3600,
34                            (duration % 3600) / 60, duration % 60);
35 }
37 void
38 format_duration_long(char *p, size_t length, unsigned long duration)
39 {
40         unsigned bytes_written = 0;
42         if (duration / 31536000 > 0) {
43                 if (duration / 31536000 == 1)
44                         bytes_written = g_snprintf(p, length, "%d %s, ", 1, _("year"));
45                 else
46                         bytes_written = g_snprintf(p, length, "%lu %s, ", duration / 31536000, _("years"));
47                 duration %= 31536000;
48                 length -= bytes_written;
49                 p += bytes_written;
50         }
51         if (duration / 604800 > 0) {
52                 if (duration / 604800 == 1)
53                         bytes_written = g_snprintf(p, length, "%d %s, ",
54                                                    1, _("week"));
55                 else
56                         bytes_written = g_snprintf(p, length, "%lu %s, ",
57                                                    duration / 604800, _("weeks"));
58                 duration %= 604800;
59                 length -= bytes_written;
60                 p += bytes_written;
61         }
62         if (duration / 86400 > 0) {
63                 if (duration / 86400 == 1)
64                         bytes_written = g_snprintf(p, length, "%d %s, ",
65                                                    1, _("day"));
66                 else
67                         bytes_written = g_snprintf(p, length, "%lu %s, ",
68                                                    duration / 86400, _("days"));
69                 duration %= 86400;
70                 length -= bytes_written;
71                 p += bytes_written;
72         }
74         g_snprintf(p, length, "%02lu:%02lu:%02lu", duration / 3600,
75                    duration % 3600 / 60, duration % 3600 % 60);
76 }