static int get_utc_time (cdtime_t t, struct tm *t_tm, long *nsec) /* {{{ */
{
- struct timespec t_spec;
- int status;
-
- CDTIME_T_TO_TIMESPEC (t, &t_spec);
+ struct timespec t_spec = CDTIME_T_TO_TIMESPEC (t);
NORMALIZE_TIMESPEC (t_spec);
if (gmtime_r (&t_spec.tv_sec, t_tm) == NULL) {
char errbuf[1024];
- status = errno;
+ int status = errno;
ERROR ("get_utc_time: gmtime_r failed: %s",
sstrerror (status, errbuf, sizeof (errbuf)));
return status;
static int get_local_time (cdtime_t t, struct tm *t_tm, long *nsec) /* {{{ */
{
- struct timespec t_spec;
- int status;
-
- CDTIME_T_TO_TIMESPEC (t, &t_spec);
+ struct timespec t_spec = CDTIME_T_TO_TIMESPEC (t);
NORMALIZE_TIMESPEC (t_spec);
if (localtime_r (&t_spec.tv_sec, t_tm) == NULL) {
char errbuf[1024];
- status = errno;
+ int status = errno;
ERROR ("get_local_time: localtime_r failed: %s",
sstrerror (status, errbuf, sizeof (errbuf)));
return status;
Formatting functions
***********************************************************************/
-static const char utc_zone[] = "+00:00";
static const char zulu_zone[] = "Z";
/* format_zone reads time zone information from "extern long timezone", exported
* by <time.h>, and formats it according to RFC 3339. This differs from
* strftime()'s "%z" format by including a colon between hour and minute. */
-static int format_zone (char *buffer, size_t buffer_size) /* {{{ */
+static int format_zone (char *buffer, size_t buffer_size, struct tm const *tm) /* {{{ */
{
- struct tm t_tm = { 0 }; /* The value doesn't matter. */
char tmp[7];
size_t sz;
if ((buffer == NULL) || (buffer_size < 7))
return EINVAL;
- sz = strftime (tmp, sizeof (tmp), "%z", &t_tm);
+ sz = strftime (tmp, sizeof (tmp), "%z", tm);
if (sz == 0)
return ENOMEM;
if (sz != 5)
size_left -= len;
}
- sstrncpy (buffer, zone, buffer_size);
+ sstrncpy (pos, zone, size_left);
return 0;
} /* }}} int format_rfc3339 */
-int format_rfc3339_utc (char *buffer, size_t buffer_size, cdtime_t t, _Bool print_nano, char const *zone) /* {{{ */
+int format_rfc3339_utc (char *buffer, size_t buffer_size, cdtime_t t, _Bool print_nano) /* {{{ */
{
struct tm t_tm;
- long nsec;
+ long nsec = 0;
int status;
if ((status = get_utc_time (t, &t_tm, &nsec)) != 0)
return status; /* The error should have already be reported. */
- return format_rfc3339 (buffer, buffer_size, &t_tm, nsec, print_nano, zone);
+ return format_rfc3339 (buffer, buffer_size, &t_tm, nsec, print_nano, zulu_zone);
} /* }}} int format_rfc3339_utc */
int format_rfc3339_local (char *buffer, size_t buffer_size, cdtime_t t, _Bool print_nano) /* {{{ */
{
struct tm t_tm;
- long nsec;
+ long nsec = 0;
int status;
char zone[7]; /* +00:00 */
if ((status = get_local_time (t, &t_tm, &nsec)) != 0)
return status; /* The error should have already be reported. */
- if ((status = format_zone (zone, sizeof (zone))) != 0)
+ if ((status = format_zone (zone, sizeof (zone), &t_tm)) != 0)
return status;
return format_rfc3339 (buffer, buffer_size, &t_tm, nsec, print_nano, zone);
if (buffer_size < RFC3339_SIZE)
return ENOMEM;
- return format_rfc3339_utc (buffer, buffer_size, t, 0, utc_zone);
+ return format_rfc3339_utc (buffer, buffer_size, t, 0);
} /* }}} int rfc3339 */
int rfc3339nano (char *buffer, size_t buffer_size, cdtime_t t) /* {{{ */
if (buffer_size < RFC3339NANO_SIZE)
return ENOMEM;
- return format_rfc3339_utc (buffer, buffer_size, t, 1, utc_zone);
+ return format_rfc3339_utc (buffer, buffer_size, t, 1);
} /* }}} int rfc3339nano */
-int rfc3339_zulu (char *buffer, size_t buffer_size, cdtime_t t) /* {{{ */
-{
- if (buffer_size < RFC3339_ZULU_SIZE)
- return ENOMEM;
-
- return format_rfc3339_utc (buffer, buffer_size, t, 0, zulu_zone);
-} /* }}} int rfc3339_zulu */
-
-int rfc3339nano_zulu (char *buffer, size_t buffer_size, cdtime_t t) /* {{{ */
-{
- if (buffer_size < RFC3339NANO_ZULU_SIZE)
- return ENOMEM;
-
- return format_rfc3339_utc (buffer, buffer_size, t, 1, zulu_zone);
-} /* }}} int rfc3339nano_zulu */
-
int rfc3339_local (char *buffer, size_t buffer_size, cdtime_t t) /* {{{ */
{
if (buffer_size < RFC3339_SIZE)