version 1.126, 2018/03/07 23:53:08 |
version 1.127, 2018/03/12 00:52:01 |
|
|
*val = (int)num; |
*val = (int)num; |
return errstr; |
return errstr; |
} |
} |
|
|
|
int |
|
parse_absolute_time(const char *s, uint64_t *tp) |
|
{ |
|
struct tm tm; |
|
time_t tt; |
|
char buf[32], *fmt; |
|
|
|
*tp = 0; |
|
|
|
/* |
|
* POSIX strptime says "The application shall ensure that there |
|
* is white-space or other non-alphanumeric characters between |
|
* any two conversion specifications" so arrange things this way. |
|
*/ |
|
switch (strlen(s)) { |
|
case 8: /* YYYYMMDD */ |
|
fmt = "%Y-%m-%d"; |
|
snprintf(buf, sizeof(buf), "%.4s-%.2s-%.2s", s, s + 4, s + 6); |
|
break; |
|
case 12: /* YYYYMMDDHHMM */ |
|
fmt = "%Y-%m-%dT%H:%M"; |
|
snprintf(buf, sizeof(buf), "%.4s-%.2s-%.2sT%.2s:%.2s", |
|
s, s + 4, s + 6, s + 8, s + 10); |
|
break; |
|
case 14: /* YYYYMMDDHHMMSS */ |
|
fmt = "%Y-%m-%dT%H:%M:%S"; |
|
snprintf(buf, sizeof(buf), "%.4s-%.2s-%.2sT%.2s:%.2s:%.2s", |
|
s, s + 4, s + 6, s + 8, s + 10, s + 12); |
|
break; |
|
default: |
|
return SSH_ERR_INVALID_FORMAT; |
|
} |
|
|
|
memset(&tm, 0, sizeof(tm)); |
|
if (strptime(buf, fmt, &tm) == NULL) |
|
return SSH_ERR_INVALID_FORMAT; |
|
if ((tt = mktime(&tm)) < 0) |
|
return SSH_ERR_INVALID_FORMAT; |
|
/* success */ |
|
*tp = (uint64_t)tt; |
|
return 0; |
|
} |
|
|
|
void |
|
format_absolute_time(uint64_t t, char *buf, size_t len) |
|
{ |
|
time_t tt = t > INT_MAX ? INT_MAX : t; /* XXX revisit in 2038 :P */ |
|
struct tm tm; |
|
|
|
localtime_r(&tt, &tm); |
|
strftime(buf, len, "%Y-%m-%dT%H:%M:%S", &tm); |
|
} |