ViewVC Help
View File | Revision Log | Show Annotations | View Changeset | Root Listing
root/svn/hopm/branches/1.1.x/src/misc.c
Revision: 6228
Committed: Thu Jul 2 13:10:18 2015 UTC (8 years, 9 months ago) by michael
Content type: text/x-csrc
File size: 2986 byte(s)
Log Message:
- Create 1.1.x branch

File Contents

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

Properties

Name Value
svn:eol-style native
svn:keywords Id