adding timestamp
authorVolker Birk <vb@pep-project.org>
Wed, 25 Mar 2015 12:55:15 +0100
changeset 124e2930c783d3c
parent 123 4466282b36e9
child 125 5119178815d8
child 134 53e0802d1d38
adding timestamp
src/timestamp.c
src/timestamp.h
     1.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
     1.2 +++ b/src/timestamp.c	Wed Mar 25 12:55:15 2015 +0100
     1.3 @@ -0,0 +1,36 @@
     1.4 +#include "timestamp.h"
     1.5 +
     1.6 +#include <stdlib.h>
     1.7 +#include <string.h>
     1.8 +#include <assert.h>
     1.9 +
    1.10 +DYNAMIC_API timestamp * new_timestamp(time_t clock)
    1.11 +{
    1.12 +    timestamp *ts = calloc(1, sizeof(timestamp));
    1.13 +    assert(ts);
    1.14 +    if (ts == NULL)
    1.15 +        return NULL;
    1.16 +
    1.17 +    if (clock)
    1.18 +        gmtime_r(&clock, ts);
    1.19 +
    1.20 +    return ts;
    1.21 +}
    1.22 +
    1.23 +
    1.24 +DYNAMIC_API void free_timestamp(timestamp *ts)
    1.25 +{
    1.26 +    free(ts);
    1.27 +}
    1.28 +
    1.29 +DYNAMIC_API timestamp * timestamp_dup(const timestamp *src)
    1.30 +{
    1.31 +    timestamp *dst = malloc(sizeof(timestamp));
    1.32 +    assert(dst);
    1.33 +    if (dst == NULL)
    1.34 +        return NULL;
    1.35 +
    1.36 +    memcpy(dst, src, sizeof(timestamp));
    1.37 +    return dst;
    1.38 +}
    1.39 +
     2.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
     2.2 +++ b/src/timestamp.h	Wed Mar 25 12:55:15 2015 +0100
     2.3 @@ -0,0 +1,49 @@
     2.4 +#pragma once
     2.5 +
     2.6 +#include <time.h>
     2.7 +#include "dynamic_api.h"
     2.8 +
     2.9 +#ifdef __cplusplus
    2.10 +extern "C" {
    2.11 +#endif
    2.12 +
    2.13 +
    2.14 +// for time values all functions are using POSIX struct tm
    2.15 +
    2.16 +typedef struct tm timestamp;
    2.17 +
    2.18 +
    2.19 +// new_timestamp() - allocate a new timestamp
    2.20 +//
    2.21 +//  parameters:
    2.22 +//      clock (in)      initial value or 0 if not available
    2.23 +//
    2.24 +//  return value:
    2.25 +//      pointer to timestamp object or NULL if out of memory
    2.26 +
    2.27 +DYNAMIC_API timestamp * new_timestamp(time_t clock);
    2.28 +
    2.29 +
    2.30 +// free_timestamp() - free memory occupied by timestamp
    2.31 +//
    2.32 +//  parameters:
    2.33 +//      ts (in)         pointer to timestamp to free
    2.34 +
    2.35 +DYNAMIC_API void free_timestamp(timestamp *ts);
    2.36 +
    2.37 +
    2.38 +// timestamp_dup() - duplicate a timestamp
    2.39 +//
    2.40 +//  parameters:
    2.41 +//      src (in)        pointer to timestamp to duplicate
    2.42 +//
    2.43 +//  return value:
    2.44 +//      pointer to copy or NULL if out of memory
    2.45 +
    2.46 +DYNAMIC_API timestamp * timestamp_dup(const timestamp *src);
    2.47 +
    2.48 +
    2.49 +#ifdef __cplusplus
    2.50 +}
    2.51 +#endif
    2.52 +