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