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 |
#include <string.h> |
26 |
|
27 |
#include "compat.h" |
28 |
#include "config.h" |
29 |
#include "misc.h" |
30 |
|
31 |
|
32 |
/* |
33 |
* Split a time_t into an English-language explanation of how |
34 |
* much time it represents, e.g. "2 hours 45 minutes 8 seconds" |
35 |
*/ |
36 |
char *dissect_time(time_t time) |
37 |
{ |
38 |
static char buf[64]; |
39 |
unsigned int years, weeks, days, hours, minutes, seconds; |
40 |
|
41 |
years = weeks = days = hours = minutes = seconds = 0; |
42 |
|
43 |
while (time >= 60 * 60 * 24 * 365) |
44 |
{ |
45 |
time -= 60 * 60 * 24 * 365; |
46 |
years++; |
47 |
} |
48 |
|
49 |
while (time >= 60 * 60 * 24 * 7) |
50 |
{ |
51 |
time -= 60 * 60 * 24 * 7; |
52 |
weeks++; |
53 |
} |
54 |
|
55 |
while (time >= 60 * 60 * 24) |
56 |
{ |
57 |
time -= 60 * 60 * 24; |
58 |
days++; |
59 |
} |
60 |
|
61 |
while (time >= 60 * 60) |
62 |
{ |
63 |
time -= 60 * 60; |
64 |
hours++; |
65 |
} |
66 |
|
67 |
while (time >= 60) |
68 |
{ |
69 |
time -= 60; |
70 |
minutes++; |
71 |
} |
72 |
|
73 |
seconds = time; |
74 |
|
75 |
if (years) |
76 |
{ |
77 |
snprintf(buf, sizeof(buf), |
78 |
"%d year%s, %d week%s, %d day%s, %02d:%02d:%02d", |
79 |
years, years == 1 ? "" : "s", weeks, |
80 |
weeks == 1 ? "" : "s", days, days == 1 ? "" : "s", |
81 |
hours, minutes, seconds); |
82 |
} |
83 |
else if (weeks) |
84 |
{ |
85 |
snprintf(buf, sizeof(buf), |
86 |
"%d week%s, %d day%s, %02d:%02d:%02d", weeks, |
87 |
weeks == 1 ? "" : "s", days, days == 1 ? "" : "s", |
88 |
hours, minutes, seconds); |
89 |
} |
90 |
else if (days) |
91 |
{ |
92 |
snprintf(buf, sizeof(buf), "%d day%s, %02d:%02d:%02d", |
93 |
days, days == 1 ? "" : "s", hours, minutes, seconds); |
94 |
} |
95 |
else if (hours) |
96 |
{ |
97 |
if (minutes || seconds) |
98 |
{ |
99 |
snprintf(buf, sizeof(buf), |
100 |
"%d hour%s, %d minute%s, %d second%s", hours, |
101 |
hours == 1 ? "" : "s", minutes, |
102 |
minutes == 1 ? "" : "s", seconds, |
103 |
seconds == 1 ? "" : "s"); |
104 |
} |
105 |
else |
106 |
{ |
107 |
snprintf(buf, sizeof(buf), "%d hour%s", hours, |
108 |
hours == 1 ? "" : "s"); |
109 |
} |
110 |
} |
111 |
else if (minutes) |
112 |
{ |
113 |
snprintf(buf, sizeof(buf), "%d minute%s, %d second%s", |
114 |
minutes, minutes == 1 ? "" : "s", seconds, |
115 |
seconds == 1 ? "" : "s"); |
116 |
} |
117 |
else |
118 |
{ |
119 |
snprintf(buf, sizeof(buf), "%d second%s", seconds, |
120 |
seconds == 1 ? "" : "s"); |
121 |
} |
122 |
|
123 |
return(buf); |
124 |
} |