xs_time.h 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184
  1. /* copyright (c) 2022 - 2025 grunfink et al. / MIT license */
  2. #ifndef _XS_TIME_H
  3. #define _XS_TIME_H
  4. #include <time.h>
  5. xs_str *xs_str_time(time_t t, const char *fmt, int local);
  6. #define xs_str_localtime(t, fmt) xs_str_time(t, fmt, 1)
  7. #define xs_str_utctime(t, fmt) xs_str_time(t, fmt, 0)
  8. #define xs_str_iso_date(t) xs_str_time(t, "%Y-%m-%dT%H:%M:%SZ", 0)
  9. time_t xs_parse_iso_date(const char *iso_date, int local);
  10. time_t xs_parse_time(const char *str, const char *fmt, int local);
  11. #define xs_parse_localtime(str, fmt) xs_parse_time(str, fmt, 1)
  12. #define xs_parse_utctime(str, fmt) xs_parse_time(str, fmt, 0)
  13. xs_str *xs_str_time_diff(time_t time_diff);
  14. xs_list *xs_tz_list(void);
  15. int xs_tz_offset(const char *tz);
  16. #ifdef XS_IMPLEMENTATION
  17. xs_str *xs_str_time(time_t t, const char *fmt, int local)
  18. /* returns a string with a formated time */
  19. {
  20. struct tm tm;
  21. char tmp[64];
  22. if (t == 0)
  23. t = time(NULL);
  24. if (local)
  25. localtime_r(&t, &tm);
  26. else
  27. gmtime_r(&t, &tm);
  28. strftime(tmp, sizeof(tmp), fmt, &tm);
  29. return xs_str_new(tmp);
  30. }
  31. xs_str *xs_str_time_diff(time_t time_diff)
  32. /* returns time_diff in seconds to 'human' units (d:hh:mm:ss) */
  33. {
  34. int secs = time_diff % 60;
  35. int mins = (time_diff /= 60) % 60;
  36. int hours = (time_diff /= 60) % 24;
  37. int days = (time_diff /= 24);
  38. return xs_fmt("%d:%02d:%02d:%02d", days, hours, mins, secs);
  39. }
  40. char *strptime(const char *s, const char *format, struct tm *tm);
  41. time_t xs_parse_time(const char *str, const char *fmt, int local)
  42. {
  43. time_t t = 0;
  44. #ifndef WITHOUT_STRPTIME
  45. struct tm tm = {0};
  46. strptime(str, fmt, &tm);
  47. /* try to guess the Daylight Saving Time */
  48. if (local)
  49. tm.tm_isdst = -1;
  50. t = local ? mktime(&tm) : timegm(&tm);
  51. #endif /* WITHOUT_STRPTIME */
  52. return t;
  53. }
  54. time_t xs_parse_iso_date(const char *iso_date, int local)
  55. /* parses a YYYY-MM-DDTHH:MM:SS date string */
  56. {
  57. time_t t = 0;
  58. #ifndef WITHOUT_STRPTIME
  59. t = xs_parse_time(iso_date, "%Y-%m-%dT%H:%M:%S", local);
  60. #else /* WITHOUT_STRPTIME */
  61. struct tm tm = {0};
  62. if (sscanf(iso_date, "%d-%d-%dT%d:%d:%d",
  63. &tm.tm_year, &tm.tm_mon, &tm.tm_mday,
  64. &tm.tm_hour, &tm.tm_min, &tm.tm_sec) == 6) {
  65. tm.tm_year -= 1900;
  66. tm.tm_mon -= 1;
  67. if (local)
  68. tm.tm_isdst = -1;
  69. t = local ? mktime(&tm) : timegm(&tm);
  70. }
  71. #endif /* WITHOUT_STRPTIME */
  72. return t;
  73. }
  74. /** timezones **/
  75. /* intentionally dead simple */
  76. struct {
  77. const char *tz; /* timezone name */
  78. float h_offset; /* hour offset */
  79. } xs_tz[] = {
  80. { "UTC", 0 },
  81. { "GMT", 0 },
  82. { "GMT+1", -1 },
  83. { "GMT+2", -2 },
  84. { "GMT+3", -3 },
  85. { "GMT+4", -4 },
  86. { "GMT+5", -5 },
  87. { "GMT+6", -6 },
  88. { "GMT+7", -7 },
  89. { "GMT+8", -8 },
  90. { "GMT+9", -9 },
  91. { "GMT+10", -10 },
  92. { "GMT+11", -11 },
  93. { "GMT+12", -12 },
  94. { "GMT-1", 1 },
  95. { "GMT-2", 2 },
  96. { "GMT-3", 3 },
  97. { "GMT-4", 4 },
  98. { "GMT-5", 5 },
  99. { "GMT-6", 6 },
  100. { "GMT-7", 7 },
  101. { "GMT-8", 8 },
  102. { "GMT-9", 9 },
  103. { "GMT-10", 10 },
  104. { "GMT-11", 11 },
  105. { "GMT-12", 12 },
  106. { "GMT-13", 13 },
  107. { "GMT-14", 14 },
  108. { "GMT-15", 15 },
  109. { "WET", 0 },
  110. { "CET", -1 },
  111. { "AST", -4 },
  112. { "CST", -6 },
  113. { "MST", -7 },
  114. { "PST", -8 },
  115. { NULL, 0 }
  116. };
  117. xs_list *xs_tz_list(void)
  118. /* returns the list of supported timezones */
  119. {
  120. xs_list *l = xs_list_new();
  121. for (int n = 0; xs_tz[n].tz != NULL; n++)
  122. l = xs_list_append(l, xs_tz[n].tz);
  123. return l;
  124. }
  125. int xs_tz_offset(const char *tz)
  126. /* returns the offset in seconds from the specified Time Zone to UTC */
  127. {
  128. for (int n = 0; xs_tz[n].tz != NULL; n++) {
  129. if (strcmp(xs_tz[n].tz, tz) == 0)
  130. return xs_tz[n].h_offset * 3600;
  131. }
  132. return 0;
  133. }
  134. #endif /* XS_IMPLEMENTATION */
  135. #endif /* _XS_TIME_H */