X-Git-Url: https://git.strongswan.org/?p=strongswan.git;a=blobdiff_plain;f=src%2Flibstrongswan%2Ftypes.c;h=5b9245cf9ebf4632c6e7591de6143db9514837c2;hp=6ba2a008b5d7fa92195f695cf17271d3401fdbc4;hb=299dbc604f48ed5a44029600b01d77477ecc1fd4;hpb=4a5bba25e28eee22bb81ac8faecfff33a0839885 diff --git a/src/libstrongswan/types.c b/src/libstrongswan/types.c index 6ba2a00..5b9245c 100644 --- a/src/libstrongswan/types.c +++ b/src/libstrongswan/types.c @@ -21,6 +21,7 @@ */ #include +#include #include "types.h" @@ -44,6 +45,19 @@ mapping_t status_m[] = { {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. */ @@ -92,13 +106,10 @@ chunk_t chunk_alloc(size_t bytes) */ 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; } /** @@ -110,7 +121,7 @@ void chunk_to_hex(char *buf, size_t buflen, chunk_t chunk) buflen--; /* reserve space for null termination */ - while (chunk.len >0 && buflen > 2) + while (chunk.len > 0 && buflen > 2) { static char hexdig[] = "0123456789abcdef"; @@ -141,3 +152,29 @@ void *clalloc(void * pointer, size_t size) 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); + } +} +