1 |
/* |
2 |
* Copyright (c) 2002 Erik Fears |
3 |
* Copyright (c) 2014-2021 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 <string.h> |
25 |
#include <time.h> |
26 |
|
27 |
#include "misc.h" |
28 |
|
29 |
|
30 |
const char * |
31 |
date_iso8601(time_t lclock) |
32 |
{ |
33 |
static char buf[32]; |
34 |
static time_t lclock_last; |
35 |
|
36 |
if (lclock == 0) |
37 |
lclock = time(0); |
38 |
|
39 |
if (lclock_last != lclock) |
40 |
{ |
41 |
lclock_last = lclock; |
42 |
strftime(buf, sizeof(buf), "%FT%T%z", localtime(&lclock)); |
43 |
} |
44 |
|
45 |
return buf; |
46 |
} |
47 |
|
48 |
/* |
49 |
* Split a time_t into an English-language explanation of how |
50 |
* much time it represents, e.g. "2 hours 45 minutes 8 seconds" |
51 |
*/ |
52 |
const char * |
53 |
time_dissect(time_t duration) |
54 |
{ |
55 |
static char buf[32]; /* 32 = sizeof("9999999999999999 days, 23:59:59") */ |
56 |
unsigned int days = 0, hours = 0, minutes = 0, seconds = 0; |
57 |
|
58 |
while (duration >= 60 * 60 * 24) |
59 |
{ |
60 |
duration -= 60 * 60 * 24; |
61 |
++days; |
62 |
} |
63 |
|
64 |
while (duration >= 60 * 60) |
65 |
{ |
66 |
duration -= 60 * 60; |
67 |
++hours; |
68 |
} |
69 |
|
70 |
while (duration >= 60) |
71 |
{ |
72 |
duration -= 60; |
73 |
++minutes; |
74 |
} |
75 |
|
76 |
seconds = duration; |
77 |
|
78 |
snprintf(buf, sizeof(buf), "%u day%s, %02u:%02u:%02u", |
79 |
days, days == 1 ? "" : "s", hours, minutes, seconds); |
80 |
return buf; |
81 |
} |
82 |
|
83 |
const char * |
84 |
stripws(char *txt) |
85 |
{ |
86 |
while (*txt == '\t' || *txt == ' ') |
87 |
++txt; |
88 |
|
89 |
char *tmp = txt + strlen(txt) - 1; |
90 |
while (tmp >= txt && (*tmp == '\t' || *tmp == ' ')) |
91 |
--tmp; |
92 |
|
93 |
*(tmp + 1) = '\0'; |
94 |
|
95 |
return txt; |
96 |
} |