1 |
/* |
2 |
* Copyright (c) 2002 Erik Fears |
3 |
* Copyright (c) 2014-2015 ircd-hybrid development team |
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 |
16 |
* along with this program; if not, write to the Free Software |
17 |
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 |
18 |
* USA |
19 |
*/ |
20 |
|
21 |
#include "setup.h" |
22 |
|
23 |
#include <stdio.h> |
24 |
#include <time.h> |
25 |
|
26 |
#include "misc.h" |
27 |
|
28 |
|
29 |
/* |
30 |
* Split a time_t into an English-language explanation of how |
31 |
* much time it represents, e.g. "2 hours 45 minutes 8 seconds" |
32 |
*/ |
33 |
const char * |
34 |
dissect_time(time_t time) |
35 |
{ |
36 |
static char buf[64]; |
37 |
unsigned int years, weeks, days, hours, minutes, seconds; |
38 |
|
39 |
years = weeks = days = hours = minutes = seconds = 0; |
40 |
|
41 |
while (time >= 60 * 60 * 24 * 365) |
42 |
{ |
43 |
time -= 60 * 60 * 24 * 365; |
44 |
years++; |
45 |
} |
46 |
|
47 |
while (time >= 60 * 60 * 24 * 7) |
48 |
{ |
49 |
time -= 60 * 60 * 24 * 7; |
50 |
weeks++; |
51 |
} |
52 |
|
53 |
while (time >= 60 * 60 * 24) |
54 |
{ |
55 |
time -= 60 * 60 * 24; |
56 |
days++; |
57 |
} |
58 |
|
59 |
while (time >= 60 * 60) |
60 |
{ |
61 |
time -= 60 * 60; |
62 |
hours++; |
63 |
} |
64 |
|
65 |
while (time >= 60) |
66 |
{ |
67 |
time -= 60; |
68 |
minutes++; |
69 |
} |
70 |
|
71 |
seconds = time; |
72 |
|
73 |
if (years) |
74 |
{ |
75 |
snprintf(buf, sizeof(buf), |
76 |
"%d year%s, %d week%s, %d day%s, %02d:%02d:%02d", |
77 |
years, years == 1 ? "" : "s", weeks, |
78 |
weeks == 1 ? "" : "s", days, days == 1 ? "" : "s", |
79 |
hours, minutes, seconds); |
80 |
} |
81 |
else if (weeks) |
82 |
{ |
83 |
snprintf(buf, sizeof(buf), |
84 |
"%d week%s, %d day%s, %02d:%02d:%02d", weeks, |
85 |
weeks == 1 ? "" : "s", days, days == 1 ? "" : "s", |
86 |
hours, minutes, seconds); |
87 |
} |
88 |
else if (days) |
89 |
{ |
90 |
snprintf(buf, sizeof(buf), "%d day%s, %02d:%02d:%02d", |
91 |
days, days == 1 ? "" : "s", hours, minutes, seconds); |
92 |
} |
93 |
else if (hours) |
94 |
{ |
95 |
if (minutes || seconds) |
96 |
{ |
97 |
snprintf(buf, sizeof(buf), |
98 |
"%d hour%s, %d minute%s, %d second%s", hours, |
99 |
hours == 1 ? "" : "s", minutes, |
100 |
minutes == 1 ? "" : "s", seconds, |
101 |
seconds == 1 ? "" : "s"); |
102 |
} |
103 |
else |
104 |
{ |
105 |
snprintf(buf, sizeof(buf), "%d hour%s", hours, |
106 |
hours == 1 ? "" : "s"); |
107 |
} |
108 |
} |
109 |
else if (minutes) |
110 |
{ |
111 |
snprintf(buf, sizeof(buf), "%d minute%s, %d second%s", |
112 |
minutes, minutes == 1 ? "" : "s", seconds, |
113 |
seconds == 1 ? "" : "s"); |
114 |
} |
115 |
else |
116 |
{ |
117 |
snprintf(buf, sizeof(buf), "%d second%s", seconds, |
118 |
seconds == 1 ? "" : "s"); |
119 |
} |
120 |
|
121 |
return buf; |
122 |
} |