Пример #1
0
size_t cdtime_to_iso8601 (char *s, size_t max, cdtime_t t) /* {{{ */
{
  struct timespec t_spec;
  struct tm t_tm;

  size_t len;

  CDTIME_T_TO_TIMESPEC (t, &t_spec);
  NORMALIZE_TIMESPEC (t_spec);

  if (localtime_r ((time_t *)&t_spec.tv_sec, &t_tm) == NULL) {
    char errbuf[1024];
    ERROR ("cdtime_to_iso8601: localtime_r failed: %s",
        sstrerror (errno, errbuf, sizeof (errbuf)));
    return (0);
  }

  len = strftime (s, max, "%Y-%m-%dT%H:%M:%S", &t_tm);
  if (len == 0)
    return 0;

  if (max - len > 2) {
    int n = snprintf (s + len, max - len, ".%09i", (int)t_spec.tv_nsec);
    len += (n < max - len) ? n : max - len;
  }

  if (max - len > 3) {
    int n = strftime (s + len, max - len, "%z", &t_tm);
    len += (n < max - len) ? n : max - len;
  }

  s[max - 1] = '\0';
  return len;
} /* }}} size_t cdtime_to_iso8601 */
Пример #2
0
static int format_rfc3339 (char *buffer, size_t buffer_size, cdtime_t t, _Bool print_nano) /* {{{ */
{
  struct timespec t_spec;
  struct tm t_tm;
  char base[20]; /* 2006-01-02T15:04:05 */
  char nano[11]; /* .999999999 */
  char zone[7];  /* +00:00 */
  char *fields[] = {base, nano, zone};
  size_t len;

  CDTIME_T_TO_TIMESPEC (t, &t_spec);
  NORMALIZE_TIMESPEC (t_spec);

  if (localtime_r (&t_spec.tv_sec, &t_tm) == NULL) {
    char errbuf[1024];
    int status = errno;
    ERROR ("format_rfc3339: localtime_r failed: %s",
        sstrerror (status, errbuf, sizeof (errbuf)));
    return (status);
  }

  len = strftime (base, sizeof (base), "%Y-%m-%dT%H:%M:%S", &t_tm);
  if (len == 0)
    return ENOMEM;

  if (print_nano)
    ssnprintf (nano, sizeof (nano), ".%09ld", (long) t_spec.tv_nsec);
  else
    sstrncpy (nano, "", sizeof (nano));

  format_zone (zone, sizeof (zone));

  if (strjoin (buffer, buffer_size, fields, STATIC_ARRAY_SIZE (fields), "") < 0)
    return ENOMEM;
  return 0;
} /* }}} int cdtime_to_rfc3339nano */