29int string_to_int64(
const char *str, int64_t *num)
33 uint64_t limit = INT64_MAX;
42 if (*str && (*str ==
'-'))
47 while (*str && !isspace(*str))
52 val += ((*str) -
'0');
62 *num = (int64_t) (sign * val);
66int string_to_int32(
const char *str, int32_t *num)
70 uint64_t limit = INT32_MAX;
79 if (*str && (*str ==
'-'))
84 while (*str && !isspace(*str))
89 val += ((*str) -
'0');
99 *num = (int32_t) (sign * val);
103int string_to_int16(
const char *str, int16_t *num)
108 if ((err = string_to_int32(str, &val)))
110 if (val > 32767 || val < -32768)
113 *num = (int16_t) val;
117int string_to_int8(
const char *str, int8_t *num)
122 if ((err = string_to_int32(str, &val)))
124 if (val > 127 || val < -128)
131int string_to_uint32(
const char *str, uint32_t *num)
134 uint64_t limit = UINT32_MAX;
138 while (isspace(*str))
143 while (*str && !isspace(*str))
148 val += ((*str) -
'0');
158 *num = (uint32_t) val;
162int string_to_uint16(
const char *str, uint16_t *num)
167 if ((err = string_to_uint32(str, &val)))
172 *num = (uint16_t) val;
176int string_to_uint8(
const char *str, uint8_t *num)
181 if ((err = string_to_uint32(str, &val)))
186 *num = (uint8_t) val;
190int string_to_double(
const char *str,
double *dbl)
193 *dbl = strtod(str, &pe);
199int string_to_timeout(
const char *str,
unsigned *ms)
202 char u[2] = { 0, 0 };
203 int n = sscanf(str,
"%f%c%c", &q, u, u+1);
206 if (memcmp(
"ms", u, 2) == 0)
208 else if (memcmp(
"s", u, 2) == 0)
209 *ms = (unsigned)(q * 1000);
210 else if (memcmp(
"m", u, 2) == 0)
211 *ms = (unsigned)(q * 60000);
212 else if (memcmp(
"h", u, 2) == 0)
213 *ms = (unsigned)(q * 3600000);
214 else if (memcmp(
"d", u, 2) == 0)
215 *ms = (unsigned)(q * 86400000);
221int string_to_size(
const char *str, int64_t *sz)
224 char u[2] = { 0, 0 };
225 int n = sscanf(str,
"%f%c%c", &q, u, u+1);
230 else if (memcmp(
"k", u, 2) == 0)
231 *sz = (int64_t)(q * (1<<10));
232 else if (memcmp(
"m", u, 2) == 0)
233 *sz = (int64_t)(q * (1<<20));
234 else if (memcmp(
"g", u, 2) == 0)
235 *sz = (int64_t)(q * (1<<30));
241int hex_to_num(
const char *str,
int *num)
246 if (*str >=
'0' && *str <=
'9')
247 val = (val << 4) + (*str -
'0');
248 else if (*str >=
'A' && *str <=
'F')
249 val = (val << 4) + (*str -
'A' + 10);
250 else if (*str >=
'a' && *str <=
'f')
251 val = (val << 4) + (*str -
'a' + 10);
262 static const char g[16] = {
263 '0',
'1',
'2',
'3',
'4',
'5',
'6',
'7',
264 '8',
'9',
'a',
'b',
'c',
'd',
'e',
'f',
266 char * p = str->data;
267 p[0] = g[(0xf & (c >> 4))];
268 p[1] = g[(0xf & (c))];
274 static const char g[16] = {
275 '0',
'1',
'2',
'3',
'4',
'5',
'6',
'7',
276 '8',
'9',
'A',
'B',
'C',
'D',
'E',
'F',
278 char * p = str->data;
279 p[0] = g[(0xf & (c >> 4))];
280 p[1] = g[(0xf & (c))];
284unsigned uint_to_strdec(
unsigned u,
char *str,
unsigned len,
int pad)
286 static const char g[10] = {
287 '0',
'1',
'2',
'3',
'4',
'5',
'6',
'7',
'8',
'9',
291 char *ptr = str, *end = str + len;
292 unsigned n = u, n10 = u / 10;
295 *ptr++ = g[n - n10 * 10];
297 }
while ((n10 = n / 10) > 0 && ptr < end);
305 while (ptr < end) *ptr++ =
'0';
320unsigned uint64_to_strdec(uint64_t ul,
char *str,
unsigned len,
int pad)
322 static const char g[10] = {
323 '0',
'1',
'2',
'3',
'4',
'5',
'6',
'7',
'8',
'9',
327 char *ptr = str, *end = str + len;
328 uint64_t n = ul, n10 = ul / 10;
331 *ptr++ = g[n - n10 * 10];
333 }
while ((n10 = n / 10) > 0 && ptr < end);
341 while (ptr < end) *ptr++ =
'0';
356time_t __timegm(
struct tm *utctime_tm)
359 struct tm adj_tm, chk_tm;
361 adj_tm.tm_year = utctime_tm->tm_year;
362 adj_tm.tm_mon = utctime_tm->tm_mon;
363 adj_tm.tm_mday = utctime_tm->tm_mday;
364 adj_tm.tm_hour = utctime_tm->tm_hour;
365 adj_tm.tm_min = utctime_tm->tm_min;
366 adj_tm.tm_sec = utctime_tm->tm_sec;
367 adj_tm.tm_isdst = -1;
371 time = mktime(&adj_tm);
372 if (time == INVALID_TIME)
374 if (NULL == gmtime_r(&time, &chk_tm))
376 if (chk_tm.tm_min == utctime_tm->tm_min &&
377 chk_tm.tm_hour == utctime_tm->tm_hour &&
378 chk_tm.tm_mday == utctime_tm->tm_mday &&
379 chk_tm.tm_mon == utctime_tm->tm_mon &&
380 chk_tm.tm_year == utctime_tm->tm_year)
382 adj_tm.tm_min += utctime_tm->tm_min - chk_tm.tm_min;
383 adj_tm.tm_hour += utctime_tm->tm_hour - chk_tm.tm_hour;
384 adj_tm.tm_mday += utctime_tm->tm_mday - chk_tm.tm_mday;
385 adj_tm.tm_mon += utctime_tm->tm_mon - chk_tm.tm_mon;
386 adj_tm.tm_year += utctime_tm->tm_year - chk_tm.tm_year;
391int string_to_time(
const char *str, time_t *time)
394 int i, len, format, isutc;
395 char *yyyy, *MM, *dd, *hh, *mm, *ss;
396 char buf[TIMESTAMP_UTC_LEN + 1];
401 *time = INVALID_TIME;
404 memset(buf, 0,
sizeof(buf));
405 strncpy(buf, str,
sizeof(buf) - 1);
410 case TIMESTAMP_UTC_LEN:
411 if (((buf[4] !=
'-') || (buf[7] !=
'-') || (buf[10] !=
'T') ||
412 (buf[13] !=
':') || (buf[16] !=
':') || (buf[19] !=
'Z')))
414 fprintf(stderr,
"%s: string is badly formed '%s'\n", __func__, buf);
421 if (((buf[4] !=
'-') || (buf[7] !=
'-') || (buf[10] !=
'T') ||
422 (buf[13] !=
':') || (buf[16] !=
':')))
424 fprintf(stderr,
"%s: string is badly formed '%s'\n", __func__, buf);
431 if (((buf[4] !=
'-') || (buf[7] !=
'-')))
433 fprintf(stderr,
"%s: string is badly formed '%s'\n", __func__, buf);
440 fprintf(stderr,
"%s: string is not a timestamp '%s'\n", __func__, buf);
452 yyyy[4] = MM[2] = dd[2] = hh[2] = mm[2] = ss[2] =
'\0';
454 for (i = 0; i < len; ++i)
456 if (buf[i] && !isdigit(buf[i]))
458 fprintf(stderr,
"%s: expected numeral at '%s'[%d]\n", __func__, str, i);
463 time_tm.tm_isdst = -1;
464 time_tm.tm_year = atoi(yyyy) - 1900;
465 time_tm.tm_mon = atoi(MM) - 1;
466 if (time_tm.tm_mon > 11)
468 fprintf(stderr,
"%s: month value too big '%s'\n", __func__, str);
471 time_tm.tm_mday = atoi(dd);
472 if (time_tm.tm_mday > 31)
474 fprintf(stderr,
"%s: day value too big '%s'\n", __func__, str);
481 time_tm.tm_hour = time_tm.tm_min = time_tm.tm_sec = 0;
482 *time = mktime(&time_tm);
486 time_tm.tm_hour = atoi(hh);
487 if (time_tm.tm_hour > 23)
489 fprintf(stderr,
"%s: hour value too big '%s'\n", __func__, str);
492 time_tm.tm_min = atoi(mm);
493 if (time_tm.tm_min > 59)
495 fprintf(stderr,
"%s: minute value too big '%s'\n", __func__, str);
498 time_tm.tm_sec = atoi(ss);
499 if (time_tm.tm_sec > 59)
501 fprintf(stderr,
"%s: second value too big '%s'\n", __func__, str);
506 *time = timegm(&time_tm);
508 *time = mktime(&time_tm);
512 *time = INVALID_TIME;
520 if (time == INVALID_TIME || NULL == gmtime_r(&time, &time_tm))
526 uint_to_strdec(time_tm.tm_year + 1900, str->data, 4, 1);
528 uint_to_strdec(time_tm.tm_mon + 1, str->data + 5, 2, 1);
530 uint_to_strdec(time_tm.tm_mday, str->data + 8, 2, 1);
532 uint_to_strdec(time_tm.tm_hour, str->data + 11, 2, 1);
534 uint_to_strdec(time_tm.tm_min, str->data + 14, 2, 1);
536 uint_to_strdec(time_tm.tm_sec, str->data + 17, 2, 1);
538 str->data[20] =
'\0';
545 if (time == INVALID_TIME || NULL == localtime_r(&time, &time_tm))
551 uint_to_strdec(time_tm.tm_year + 1900, str->data, 4, 1);
553 uint_to_strdec(time_tm.tm_mon + 1, str->data + 5, 2, 1);
555 uint_to_strdec(time_tm.tm_mday, str->data + 8, 2, 1);
557 uint_to_strdec(time_tm.tm_hour, str->data + 11, 2, 1);
559 uint_to_strdec(time_tm.tm_min, str->data + 14, 2, 1);
561 uint_to_strdec(time_tm.tm_sec, str->data + 17, 2, 1);
562 str->data[19] =
'\0';
569 if (time == INVALID_TIME || NULL == localtime_r(&time, &time_tm))
574 uint_to_strdec(time_tm.tm_year + 1900, str->data, 4, 1);
576 uint_to_strdec(time_tm.tm_mon + 1, str->data + 5, 2, 1);
578 uint_to_strdec(time_tm.tm_mday, str->data + 8, 2, 1);
579 str->data[10] =
'\0';
584 static const char* dw[] = {
585 "Sun",
"Mon",
"Tue",
"Wed",
"Thu",
"Fri",
"Sat"
587 static const char* my[] = {
588 "Jan",
"Feb",
"Mar",
"Apr",
"May",
"Jun",
589 "Jul",
"Aug",
"Sep",
"Oct",
"Nov",
"Dec"
594 if (time == INVALID_TIME || NULL == gmtime_r(&time, &time_tm))
599 sprintf(str->data,
"%s, %2.2d %s %4.4d %2.2d:%2.2d:%2.2d GMT",
600 dw[time_tm.tm_wday], time_tm.tm_mday, my[time_tm.tm_mon],
601 time_tm.tm_year + 1900, time_tm.tm_hour, time_tm.tm_min,
605tz_t *time_tz(time_t time,
tz_t* tz) {
607 localtime_r(&time, &loc);
609 gmtime_r(&time, &gmt);
610 int minutes = ((loc.tm_hour * 60 + loc.tm_min) - (gmt.tm_hour * 60 + gmt.tm_min)) % 720;
611 tz->tz_dir = minutes < 0 ? (-1) : 1;
612 tz->tz_hour = tz->tz_dir * minutes / 60;
613 tz->tz_min = ( tz->tz_dir * minutes ) - tz->tz_hour * 60;
614 sprintf(tz->tz_str,
"%+2.2d:%2.2d", tz->tz_dir * tz->tz_hour, (
unsigned)(tz->tz_min) % 60);