X-Git-Url: http://www.project-moonshot.org/gitweb/?a=blobdiff_plain;f=common%2Ftr_util.c;fp=common%2Ftr_util.c;h=ef85776ba23b0a6df08721ccac18636b2d91c4c6;hb=32112416992e9619622b5e9b7a6a7fe4eda992c2;hp=2ce3c82d421c6241feaf689df50e886769f229fd;hpb=eeb92f9286aef72af1dec0f24cdc36adb935deb4;p=trust_router.git diff --git a/common/tr_util.c b/common/tr_util.c index 2ce3c82..ef85776 100644 --- a/common/tr_util.c +++ b/common/tr_util.c @@ -36,8 +36,8 @@ #include #include #include -#include #include +#include void tr_bin_to_hex(const unsigned char * bin, size_t bin_len, char * hex_out, size_t hex_len) @@ -71,3 +71,28 @@ int tr_cmp_timespec(struct timespec *ts1, struct timespec *ts2) return 0; } + +/** + * Convert a struct timespec to a string representation + * @param ts + * @return + */ +char *timespec_to_str(struct timespec *ts) +{ + struct tm tm; + char *s=NULL; + + if (localtime_r(&(ts->tv_sec), &tm)==NULL) + return NULL; + + s=malloc(40); /* long enough to contain strftime result */ + if (s==NULL) + return NULL; + + if (strftime(s, 40, "%F %T", &tm)==0) { + free(s); + return NULL; + } + return s; +} +