| 1 |
/* vim: set shiftwidth=3 softtabstop=3 expandtab: */ |
| 2 |
|
| 3 |
/* |
| 4 |
Copyright (C) 2002 Erik Fears |
| 5 |
|
| 6 |
This program is free software; you can redistribute it and/or |
| 7 |
modify it under the terms of the GNU General Public License |
| 8 |
as published by the Free Software Foundation; either version 2 |
| 9 |
of the License, or (at your option) any later version. |
| 10 |
|
| 11 |
This program is distributed in the hope that it will be useful, |
| 12 |
but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 13 |
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 14 |
GNU General Public License for more details. |
| 15 |
|
| 16 |
You should have received a copy of the GNU General Public License |
| 17 |
along with this program; if not, write to the Free Software |
| 18 |
|
| 19 |
Foundation, Inc. |
| 20 |
59 Temple Place - Suite 330 |
| 21 |
Boston, MA 02111-1307, USA. |
| 22 |
|
| 23 |
*/ |
| 24 |
|
| 25 |
#include "setup.h" |
| 26 |
|
| 27 |
#include <stdio.h> |
| 28 |
#include <time.h> |
| 29 |
|
| 30 |
#ifdef STDC_HEADERS |
| 31 |
# include <string.h> |
| 32 |
#endif |
| 33 |
|
| 34 |
#include "compat.h" |
| 35 |
#include "config.h" |
| 36 |
#include "extern.h" |
| 37 |
#include "misc.h" |
| 38 |
|
| 39 |
|
| 40 |
/* |
| 41 |
* Split a time_t into an English-language explanation of how |
| 42 |
* much time it represents, e.g. "2 hours 45 minutes 8 seconds" |
| 43 |
*/ |
| 44 |
char *dissect_time(time_t time) |
| 45 |
{ |
| 46 |
static char buf[64]; |
| 47 |
unsigned int years, weeks, days, hours, minutes, seconds; |
| 48 |
|
| 49 |
years = weeks = days = hours = minutes = seconds = 0; |
| 50 |
|
| 51 |
while (time >= 60 * 60 * 24 * 365) |
| 52 |
{ |
| 53 |
time -= 60 * 60 * 24 * 365; |
| 54 |
years++; |
| 55 |
} |
| 56 |
|
| 57 |
while (time >= 60 * 60 * 24 * 7) |
| 58 |
{ |
| 59 |
time -= 60 * 60 * 24 * 7; |
| 60 |
weeks++; |
| 61 |
} |
| 62 |
|
| 63 |
while (time >= 60 * 60 * 24) |
| 64 |
{ |
| 65 |
time -= 60 * 60 * 24; |
| 66 |
days++; |
| 67 |
} |
| 68 |
|
| 69 |
while (time >= 60 * 60) |
| 70 |
{ |
| 71 |
time -= 60 * 60; |
| 72 |
hours++; |
| 73 |
} |
| 74 |
|
| 75 |
while (time >= 60) |
| 76 |
{ |
| 77 |
time -= 60; |
| 78 |
minutes++; |
| 79 |
} |
| 80 |
|
| 81 |
seconds = time; |
| 82 |
|
| 83 |
if (years) |
| 84 |
{ |
| 85 |
snprintf(buf, sizeof(buf), |
| 86 |
"%d year%s, %d week%s, %d day%s, %02d:%02d:%02d", |
| 87 |
years, years == 1 ? "" : "s", weeks, |
| 88 |
weeks == 1 ? "" : "s", days, days == 1 ? "" : "s", |
| 89 |
hours, minutes, seconds); |
| 90 |
} |
| 91 |
else if (weeks) |
| 92 |
{ |
| 93 |
snprintf(buf, sizeof(buf), |
| 94 |
"%d week%s, %d day%s, %02d:%02d:%02d", weeks, |
| 95 |
weeks == 1 ? "" : "s", days, days == 1 ? "" : "s", |
| 96 |
hours, minutes, seconds); |
| 97 |
} |
| 98 |
else if (days) |
| 99 |
{ |
| 100 |
snprintf(buf, sizeof(buf), "%d day%s, %02d:%02d:%02d", |
| 101 |
days, days == 1 ? "" : "s", hours, minutes, seconds); |
| 102 |
} |
| 103 |
else if (hours) |
| 104 |
{ |
| 105 |
if (minutes || seconds) |
| 106 |
{ |
| 107 |
snprintf(buf, sizeof(buf), |
| 108 |
"%d hour%s, %d minute%s, %d second%s", hours, |
| 109 |
hours == 1 ? "" : "s", minutes, |
| 110 |
minutes == 1 ? "" : "s", seconds, |
| 111 |
seconds == 1 ? "" : "s"); |
| 112 |
} |
| 113 |
else |
| 114 |
{ |
| 115 |
snprintf(buf, sizeof(buf), "%d hour%s", hours, |
| 116 |
hours == 1 ? "" : "s"); |
| 117 |
} |
| 118 |
} |
| 119 |
else if (minutes) |
| 120 |
{ |
| 121 |
snprintf(buf, sizeof(buf), "%d minute%s, %d second%s", |
| 122 |
minutes, minutes == 1 ? "" : "s", seconds, |
| 123 |
seconds == 1 ? "" : "s"); |
| 124 |
} |
| 125 |
else |
| 126 |
{ |
| 127 |
snprintf(buf, sizeof(buf), "%d second%s", seconds, |
| 128 |
seconds == 1 ? "" : "s"); |
| 129 |
} |
| 130 |
|
| 131 |
return(buf); |
| 132 |
} |
| 133 |
|
| 134 |
/* |
| 135 |
* Strip leading/tailing characters from null terminated str and return a |
| 136 |
* pointer to the new string. |
| 137 |
*/ |
| 138 |
|
| 139 |
char *clean(char *str) |
| 140 |
{ |
| 141 |
size_t i; |
| 142 |
/* Position of last non space. */ |
| 143 |
int ln; |
| 144 |
/* Position of first non space. */ |
| 145 |
int fn; |
| 146 |
int gf; |
| 147 |
|
| 148 |
ln = 0; |
| 149 |
fn = 0; |
| 150 |
gf = 0; |
| 151 |
|
| 152 |
/* Dont need to deal with 1 character */ |
| 153 |
if (strlen(str) <= 1) |
| 154 |
return str; |
| 155 |
|
| 156 |
for (i = 0; i < strlen(str); i++) |
| 157 |
{ |
| 158 |
if (fn == 0 && str[i] != ' ' && !gf) |
| 159 |
{ |
| 160 |
fn = i; |
| 161 |
gf = 1; |
| 162 |
} |
| 163 |
if (str[i] != ' ') |
| 164 |
ln = i; |
| 165 |
} |
| 166 |
|
| 167 |
/* Null terminate before the tailing spaces. */ |
| 168 |
str[ln + 1] = 0; |
| 169 |
|
| 170 |
/* Return pointer to point after leading spaces. */ |
| 171 |
return(str + (fn)); |
| 172 |
} |