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