p≡p engine fork for my own dirty testing of stuff
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

64 lines
1.2 KiB

7 years ago
  1. /** @file */
  2. /** @brief File description for doxygen missing. FIXME */
  3. // This file is under GNU General Public License 3.0
  4. // see LICENSE.txt
  5. #include "pEp_internal.h"
  6. #include "timestamp.h"
  7. #include <stdlib.h>
  8. #include <string.h>
  9. #include <assert.h>
  10. DYNAMIC_API time_t timegm_with_gmtoff(const timestamp* ts)
  11. {
  12. time_t retval = -1;
  13. timestamp *_ts = NULL;
  14. if (ts) {
  15. _ts = timestamp_dup(ts);
  16. if (_ts) {
  17. const time_t raw_time = timegm(_ts);
  18. if (raw_time != -1) {
  19. retval = raw_time - ts->tm_gmtoff;
  20. }
  21. }
  22. }
  23. free_timestamp(_ts); // Safe if NULL
  24. return retval;
  25. }
  26. DYNAMIC_API timestamp * new_timestamp(time_t clock)
  27. {
  28. timestamp *ts = calloc(1, sizeof(timestamp));
  29. assert(ts);
  30. if (ts == NULL)
  31. return NULL;
  32. if (clock)
  33. gmtime_r(&clock, (struct tm *) ts);
  34. return ts;
  35. }
  36. DYNAMIC_API void free_timestamp(timestamp *ts)
  37. {
  38. free(ts);
  39. }
  40. DYNAMIC_API timestamp * timestamp_dup(const timestamp *src)
  41. {
  42. if (!src)
  43. return NULL;
  44. timestamp *dst = (timestamp *) malloc(sizeof(timestamp));
  45. memcpy(dst, src, sizeof(timestamp));
  46. return dst;
  47. }