comparison mcabber/src/utils.c @ 241:8584f919d9b9

[/trunk] Changeset 254 by mikael * Use ISO8601 for time format in log file * Update TODO
author mikael
date Mon, 13 Jun 2005 19:29:09 +0000
parents 250f872c722f
children f562b9af2de7
comparison
equal deleted inserted replaced
240:723433a677f0 241:8584f919d9b9
1 /*
2 * utils.c -- Various utility functions
3 *
4 * Copyright (C) 2005 Mikael Berthe <bmikael@lists.lilotux.net>
5 * ut_* functions are derived from Cabber debug/log code.
6 * from_iso8601() comes from the Gaim project.
7 *
8 * This program is free software; you can redistribute it and/or modify
9 * it under the terms of the GNU General Public License as published by
10 * the Free Software Foundation; either version 2 of the License, or (at
11 * your option) any later version.
12 *
13 * This program is distributed in the hope that it will be useful, but
14 * WITHOUT ANY WARRANTY; without even the implied warranty of
15 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
16 * General Public License for more details.
17 *
18 * You should have received a copy of the GNU General Public License
19 * along with this program; if not, write to the Free Software
20 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307
21 * USA
22 */
23
1 #include <stdio.h> 24 #include <stdio.h>
2 #include <stdlib.h> 25 #include <stdlib.h>
3 #include <string.h> 26 #include <string.h>
4 #include <stdarg.h> 27 #include <stdarg.h>
5 #include <time.h> 28 #include <time.h>
29
30 #include <config.h>
6 31
7 static int DebugEnabled; 32 static int DebugEnabled;
8 static char *FName; 33 static char *FName;
9 34
10 void ut_InitDebug(unsigned int level, char *filename) 35 void ut_InitDebug(unsigned int level, char *filename)
63 free(buffer); 88 free(buffer);
64 fclose(fp); 89 fclose(fp);
65 } 90 }
66 } 91 }
67 92
93 // to_iso8601(dststr, timestamp)
94 // Convert timestamp to iso8601 format, and store it in dststr.
95 // NOTE: dststr should be at last 19 chars long.
96 // Return should be 0
97 int to_iso8601(char *dststr, time_t timestamp)
98 {
99 struct tm *tm_time;
100 int ret;
101
102 tm_time = gmtime(&timestamp);
103
104 ret = snprintf(dststr, 19, "%.4d%02d%02dT%02d:%02d:%02dZ",
105 1900+tm_time->tm_year, tm_time->tm_mon+1, tm_time->tm_mday,
106 tm_time->tm_hour, tm_time->tm_min, tm_time->tm_sec);
107
108 return ((ret == -1) ? -1 : 0);
109 }
110
111 // from_iso8601(timestamp, utc)
112 // This function comes from the Gaim project, gaim_str_to_time().
113 // (Actually date may not be pure iso-8601)
114 // Thanks, guys!
115 time_t from_iso8601(const char *timestamp, int utc)
116 {
117 struct tm t;
118 time_t retval = 0;
119 char buf[32];
120 char *c;
121 int tzoff = 0;
122
123 time(&retval);
124 localtime_r(&retval, &t);
125
126 snprintf(buf, sizeof(buf), "%s", timestamp);
127 c = buf;
128
129 /* 4 digit year */
130 if (!sscanf(c, "%04d", &t.tm_year)) return 0;
131 c+=4;
132 if (*c == '-')
133 c++;
134
135 t.tm_year -= 1900;
136
137 /* 2 digit month */
138 if (!sscanf(c, "%02d", &t.tm_mon)) return 0;
139 c+=2;
140 if (*c == '-')
141 c++;
142
143 t.tm_mon -= 1;
144
145 /* 2 digit day */
146 if (!sscanf(c, "%02d", &t.tm_mday)) return 0;
147 c+=2;
148 if (*c == 'T' || *c == '.') { /* we have more than a date, keep going */
149 c++; /* skip the "T" */
150
151 /* 2 digit hour */
152 if (sscanf(c, "%02d:%02d:%02d", &t.tm_hour, &t.tm_min, &t.tm_sec) == 3 ||
153 sscanf(c, "%02d%02d%02d", &t.tm_hour, &t.tm_min, &t.tm_sec) == 3) {
154 int tzhrs, tzmins;
155 c+=8;
156 if (*c == '.') /* dealing with precision we don't care about */
157 c += 4;
158
159 if ((*c == '+' || *c == '-') &&
160 sscanf(c+1, "%02d:%02d", &tzhrs, &tzmins)) {
161 tzoff = tzhrs*60*60 + tzmins*60;
162 if (*c == '+')
163 tzoff *= -1;
164 }
165
166 if (tzoff || utc) {
167
168 //#ifdef HAVE_TM_GMTOFF
169 tzoff += t.tm_gmtoff;
170 //#else
171 //# ifdef HAVE_TIMEZONE
172 // tzset(); /* making sure */
173 // tzoff -= timezone;
174 //# endif
175 //#endif
176 }
177 }
178 }
179
180 t.tm_isdst = -1;
181
182 retval = mktime(&t);
183
184 retval += tzoff;
185
186 return retval;
187 }
188