1 |
/* |
2 |
Copyright (C) 2002 Erik Fears |
3 |
|
4 |
This program is free software; you can redistribute it and/or |
5 |
modify it under the terms of the GNU General Public License |
6 |
as published by the Free Software Foundation; either version 2 |
7 |
of the License, or (at your option) any later version. |
8 |
|
9 |
This program is distributed in the hope that it will be useful, |
10 |
but WITHOUT ANY WARRANTY; without even the implied warranty of |
11 |
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
12 |
GNU General Public License for more details. |
13 |
|
14 |
You should have received a copy of the GNU General Public License |
15 |
along with this program; if not, write to the Free Software |
16 |
|
17 |
Foundation, Inc. |
18 |
59 Temple Place - Suite 330 |
19 |
Boston, MA 02111-1307, USA. |
20 |
|
21 |
*/ |
22 |
|
23 |
#include "setup.h" |
24 |
|
25 |
#include <stdio.h> |
26 |
#include <time.h> |
27 |
|
28 |
#ifdef STDC_HEADERS |
29 |
# include <string.h> |
30 |
#endif |
31 |
|
32 |
#include "compat.h" |
33 |
#include "config.h" |
34 |
#include "misc.h" |
35 |
|
36 |
|
37 |
/* |
38 |
* Split a time_t into an English-language explanation of how |
39 |
* much time it represents, e.g. "2 hours 45 minutes 8 seconds" |
40 |
*/ |
41 |
char *dissect_time(time_t time) |
42 |
{ |
43 |
static char buf[64]; |
44 |
unsigned int years, weeks, days, hours, minutes, seconds; |
45 |
|
46 |
years = weeks = days = hours = minutes = seconds = 0; |
47 |
|
48 |
while (time >= 60 * 60 * 24 * 365) |
49 |
{ |
50 |
time -= 60 * 60 * 24 * 365; |
51 |
years++; |
52 |
} |
53 |
|
54 |
while (time >= 60 * 60 * 24 * 7) |
55 |
{ |
56 |
time -= 60 * 60 * 24 * 7; |
57 |
weeks++; |
58 |
} |
59 |
|
60 |
while (time >= 60 * 60 * 24) |
61 |
{ |
62 |
time -= 60 * 60 * 24; |
63 |
days++; |
64 |
} |
65 |
|
66 |
while (time >= 60 * 60) |
67 |
{ |
68 |
time -= 60 * 60; |
69 |
hours++; |
70 |
} |
71 |
|
72 |
while (time >= 60) |
73 |
{ |
74 |
time -= 60; |
75 |
minutes++; |
76 |
} |
77 |
|
78 |
seconds = time; |
79 |
|
80 |
if (years) |
81 |
{ |
82 |
snprintf(buf, sizeof(buf), |
83 |
"%d year%s, %d week%s, %d day%s, %02d:%02d:%02d", |
84 |
years, years == 1 ? "" : "s", weeks, |
85 |
weeks == 1 ? "" : "s", days, days == 1 ? "" : "s", |
86 |
hours, minutes, seconds); |
87 |
} |
88 |
else if (weeks) |
89 |
{ |
90 |
snprintf(buf, sizeof(buf), |
91 |
"%d week%s, %d day%s, %02d:%02d:%02d", weeks, |
92 |
weeks == 1 ? "" : "s", days, days == 1 ? "" : "s", |
93 |
hours, minutes, seconds); |
94 |
} |
95 |
else if (days) |
96 |
{ |
97 |
snprintf(buf, sizeof(buf), "%d day%s, %02d:%02d:%02d", |
98 |
days, days == 1 ? "" : "s", hours, minutes, seconds); |
99 |
} |
100 |
else if (hours) |
101 |
{ |
102 |
if (minutes || seconds) |
103 |
{ |
104 |
snprintf(buf, sizeof(buf), |
105 |
"%d hour%s, %d minute%s, %d second%s", hours, |
106 |
hours == 1 ? "" : "s", minutes, |
107 |
minutes == 1 ? "" : "s", seconds, |
108 |
seconds == 1 ? "" : "s"); |
109 |
} |
110 |
else |
111 |
{ |
112 |
snprintf(buf, sizeof(buf), "%d hour%s", hours, |
113 |
hours == 1 ? "" : "s"); |
114 |
} |
115 |
} |
116 |
else if (minutes) |
117 |
{ |
118 |
snprintf(buf, sizeof(buf), "%d minute%s, %d second%s", |
119 |
minutes, minutes == 1 ? "" : "s", seconds, |
120 |
seconds == 1 ? "" : "s"); |
121 |
} |
122 |
else |
123 |
{ |
124 |
snprintf(buf, sizeof(buf), "%d second%s", seconds, |
125 |
seconds == 1 ? "" : "s"); |
126 |
} |
127 |
|
128 |
return(buf); |
129 |
} |