ViewVC Help
View File | Revision Log | Show Annotations | View Changeset | Root Listing
root/svn/ircd-hybrid/trunk/src/misc.c
Revision: 6419
Committed: Wed Aug 26 17:51:30 2015 UTC (10 years ago) by michael
Content type: text/x-csrc
File size: 4471 byte(s)
Log Message:
- misc.c: for consistency, have myctime() to behave like the other date/time functions when called with an argument being 0

File Contents

# Content
1 /*
2 * ircd-hybrid: an advanced, lightweight Internet Relay Chat Daemon (ircd)
3 *
4 * Copyright (c) 1997-2015 ircd-hybrid development team
5 *
6 * This program is free software; you can redistribute it and/or modify
7 * it under the terms of the GNU General Public License as published by
8 * the Free Software Foundation; either version 2 of the License, or
9 * (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 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301
19 * USA
20 */
21
22 /*! \file s_misc.c
23 * \brief Yet another miscellaneous functions file.
24 * \version $Id$
25 */
26
27 #include "stdinc.h"
28 #include "misc.h"
29 #include "irc_string.h"
30 #include "ircd.h"
31
32
33 static const char *const months[] =
34 {
35 "January", "February", "March", "April",
36 "May", "June", "July", "August",
37 "September", "October", "November","December"
38 };
39
40 static const char *const weekdays[] =
41 {
42 "Sunday", "Monday", "Tuesday", "Wednesday",
43 "Thursday", "Friday", "Saturday"
44 };
45
46 const char *
47 date(time_t lclock)
48 {
49 static char buf[80], plus;
50 struct tm *lt, *gm;
51 struct tm gmbuf;
52 int minswest;
53 static time_t lclock_last;
54
55 if (!lclock)
56 lclock = CurrentTime;
57
58 if (lclock_last == lclock)
59 return buf;
60
61 lclock_last = lclock;
62 gm = gmtime(&lclock);
63 memcpy(&gmbuf, gm, sizeof(gmbuf));
64 gm = &gmbuf;
65 lt = localtime(&lclock);
66
67 /*
68 * There is unfortunately no clean portable way to extract time zone
69 * offset information, so do ugly things.
70 */
71 minswest = (gm->tm_hour - lt->tm_hour) * 60 + (gm->tm_min - lt->tm_min);
72
73 if (lt->tm_yday != gm->tm_yday)
74 {
75 if ((lt->tm_yday > gm->tm_yday && lt->tm_year == gm->tm_year) ||
76 (lt->tm_yday < gm->tm_yday && lt->tm_year != gm->tm_year))
77 minswest -= 24 * 60;
78 else
79 minswest += 24 * 60;
80 }
81
82 plus = (minswest > 0) ? '-' : '+';
83 if (minswest < 0)
84 minswest = -minswest;
85
86 snprintf(buf, sizeof(buf), "%s %s %d %d -- %02u:%02u:%02u %c%02u:%02u",
87 weekdays[lt->tm_wday], months[lt->tm_mon],lt->tm_mday,
88 lt->tm_year + 1900, lt->tm_hour, lt->tm_min, lt->tm_sec,
89 plus, minswest/60, minswest%60);
90 return buf;
91 }
92
93 const char *
94 date_iso8601(time_t lclock)
95 {
96 static char buf[MAX_DATE_STRING];
97 static time_t lclock_last;
98
99 if (!lclock)
100 lclock = CurrentTime;
101
102 if (lclock_last != lclock)
103 {
104 lclock_last = lclock;
105 strftime(buf, sizeof(buf), "%FT%H:%M:%S%z", localtime(&lclock));
106 }
107
108 return buf;
109 }
110
111 const char *
112 smalldate(time_t lclock)
113 {
114 static char buf[MAX_DATE_STRING];
115 struct tm *lt, *gm;
116 struct tm gmbuf;
117 static time_t lclock_last;
118
119 if (!lclock)
120 lclock = CurrentTime;
121
122 if (lclock_last == lclock)
123 return buf;
124
125 lclock_last = lclock;
126 gm = gmtime(&lclock);
127 memcpy(&gmbuf, gm, sizeof(gmbuf));
128 gm = &gmbuf;
129 lt = localtime(&lclock);
130
131 snprintf(buf, sizeof(buf), "%d/%d/%d %02d.%02d",
132 lt->tm_year + 1900, lt->tm_mon + 1, lt->tm_mday,
133 lt->tm_hour, lt->tm_min);
134
135 return buf;
136 }
137
138 /*
139 * myctime - This is like standard ctime()-function, but it zaps away
140 * the newline from the end of that string. Also, it takes
141 * the time value as parameter, instead of pointer to it.
142 * Note that it is necessary to copy the string to alternate
143 * buffer (who knows how ctime() implements it, maybe it statically
144 * has newline there and never 'refreshes' it -- zapping that
145 * might break things in other places...)
146 *
147 *
148 * Thu Nov 24 18:22:48 1986
149 */
150 const char *
151 myctime(time_t lclock)
152 {
153 static char buf[32];
154 char *p;
155 static time_t lclock_last;
156
157 if (!lclock)
158 lclock = CurrentTime;
159
160 if (lclock_last == lclock)
161 return buf;
162
163 lclock_last = lclock;
164 strlcpy(buf, ctime(&lclock), sizeof(buf));
165
166 if ((p = strchr(buf, '\n')))
167 *p = '\0';
168 return buf;
169 }
170
171 #ifdef HAVE_LIBCRYPTO
172 const char *
173 ssl_get_cipher(const SSL *ssl)
174 {
175 static char buffer[IRCD_BUFSIZE];
176 int bits = 0;
177
178 SSL_CIPHER_get_bits(SSL_get_current_cipher(ssl), &bits);
179
180 snprintf(buffer, sizeof(buffer), "%s-%s-%d", SSL_get_version(ssl),
181 SSL_get_cipher(ssl), bits);
182 return buffer;
183 }
184 #endif

Properties

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