*/
#include <string.h>
+#include <time.h>
#include "types.h"
{PARSE_ERROR, "PARSE_ERROR"},
{VERIFY_ERROR, "VERIFY_ERROR"},
{INVALID_STATE, "INVALID_STATE"},
- {DELETE_ME, "DELETE_ME"},
+ {DESTROY_ME, "DESTROY_ME"},
{CREATED, "CREATED"},
{MAPPING_END, NULL}
};
+#define UNDEFINED_TIME 0
+
+/**
+ * @brief Display a date either in local or UTC time
+ *
+ * @param buf buffer where displayed time will be written
+ * @param buflen buffer length
+ * @param time time to be displayed
+ * @param utc UTC (TRUE) or local time (FALSE)
+ *
+ */
+void timetoa(char *buf, size_t buflen, const time_t *time, bool utc);
+
/**
* Empty chunk.
*/
*/
bool chunk_equals(chunk_t a, chunk_t b)
{
- if (a.ptr == NULL || b.ptr == NULL ||
- a.len != b.len ||
- memcmp(a.ptr, b.ptr, a.len) != 0)
- {
- return FALSE;
- }
- return TRUE;
+ return a.len == b.len &&
+ a.ptr != NULL &&
+ b.ptr != NULL &&
+ memcmp(a.ptr, b.ptr, a.len) == 0;
}
/**
buflen--; /* reserve space for null termination */
- while (chunk.len >0 && buflen > 2)
+ while (chunk.len > 0 && buflen > 2)
{
static char hexdig[] = "0123456789abcdef";
return (data);
}
+
+/*
+ * Names of the months used by timetoa()
+ */
+static const char* months[] = {
+ "Jan", "Feb", "Mar", "Apr", "May", "Jun",
+ "Jul", "Aug", "Sep", "Oct", "Nov", "Dec"
+};
+
+/*
+ * Described in header file
+ */
+void timetoa(char *buf, size_t buflen, const time_t *time, bool utc)
+{
+ if (*time == UNDEFINED_TIME)
+ snprintf(buf, buflen, "--- -- --:--:--%s----", (utc)?" UTC ":" ");
+ else
+ {
+ struct tm *t = (utc)? gmtime(time) : localtime(time);
+
+ snprintf(buf, buflen, "%s %02d %02d:%02d:%02d%s%04d",
+ months[t->tm_mon], t->tm_mday, t->tm_hour, t->tm_min, t->tm_sec,
+ (utc)?" UTC ":" ", t->tm_year + 1900);
+ }
+}
+