xs_time.h 6.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235
  1. /* copyright (c) 2022 - 2026 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. { "WET (Western European Time)", 0 },
  82. { "WEST (Western European Summer Time)", 1 },
  83. { "CET (Central European Time)", 1 },
  84. { "CEST (Central European Summer Time)", 2 },
  85. { "EET (Eastern European Time)", 2 },
  86. { "EEST (Eastern European Summer Time)", 3 },
  87. { "MSK (Moskow Time Zone)", 3 },
  88. { "EST (Eastern Time Zone)", -5 },
  89. { "AST (Atlantic Time Zone)", -4 },
  90. { "ADT (Atlantic Daylight Time Zone)", -3 },
  91. { "CST (Central Time Zone)", -6 },
  92. { "CDT (Central Daylight Time Zone)", -5 },
  93. { "MST (Mountain Time Zone)", -7 },
  94. { "MDT (Mountain Daylight Time Zone)", -6 },
  95. { "PST (Pacific Time Zone)", -8 },
  96. { "PDT (Pacific Daylight Time Zone)", -7 },
  97. { "AKST (Alaska Time Zone)", -9 },
  98. { "AKDT (Alaska Daylight Time Zone)", -8 },
  99. { "China Time Zone", 8 },
  100. { "IST (Indian Standard Time)", 5.5 },
  101. { "IDT (Israel Daylight Standard Time)", 3 },
  102. { "WIB (Western Indonesia Time)", 7 },
  103. { "WITA (Central Indonesia Time)", 8 },
  104. { "WIT (Eastern Indonesia Time)", 9 },
  105. { "AWST (Australian Western Time)", 8 },
  106. { "ACST (Australian Eastern Time)", 9.5 },
  107. { "ACDT (Australian Daylight Eastern Time)", 10.5 },
  108. { "AEST (Australian Eastern Time)", 10 },
  109. { "AEDT (Australian Daylight Eastern Time)", 11 },
  110. { "NZST (New Zealand Time)", 12 },
  111. { "NZDT (New Zealand Daylight Time)", 13 },
  112. { "UTC", 0 },
  113. { "UTC+1", 1 },
  114. { "UTC+2", 2 },
  115. { "UTC+3", 3 },
  116. { "UTC+4", 4 },
  117. { "UTC+5", 5 },
  118. { "UTC+6", 6 },
  119. { "UTC+7", 7 },
  120. { "UTC+8", 8 },
  121. { "UTC+9", 9 },
  122. { "UTC+10", 10 },
  123. { "UTC+11", 11 },
  124. { "UTC+12", 12 },
  125. { "UTC-1", -1 },
  126. { "UTC-2", -2 },
  127. { "UTC-3", -3 },
  128. { "UTC-4", -4 },
  129. { "UTC-5", -5 },
  130. { "UTC-6", -6 },
  131. { "UTC-7", -7 },
  132. { "UTC-8", -8 },
  133. { "UTC-9", -9 },
  134. { "UTC-10", -10 },
  135. { "UTC-11", -11 },
  136. { "UTC-12", -12 },
  137. { "UTC-13", -13 },
  138. { "UTC-14", -14 },
  139. { "GMT", 0 },
  140. { "GMT+1", -1 },
  141. { "GMT+2", -2 },
  142. { "GMT+3", -3 },
  143. { "GMT+4", -4 },
  144. { "GMT+5", -5 },
  145. { "GMT+6", -6 },
  146. { "GMT+7", -7 },
  147. { "GMT+8", -8 },
  148. { "GMT+9", -9 },
  149. { "GMT+10", -10 },
  150. { "GMT+11", -11 },
  151. { "GMT+12", -12 },
  152. { "GMT-1", 1 },
  153. { "GMT-2", 2 },
  154. { "GMT-3", 3 },
  155. { "GMT-4", 4 },
  156. { "GMT-5", 5 },
  157. { "GMT-6", 6 },
  158. { "GMT-7", 7 },
  159. { "GMT-8", 8 },
  160. { "GMT-9", 9 },
  161. { "GMT-10", 10 },
  162. { "GMT-11", 11 },
  163. { "GMT-12", 12 },
  164. { "GMT-13", 13 },
  165. { "GMT-14", 14 },
  166. { NULL, 0 }
  167. };
  168. xs_list *xs_tz_list(void)
  169. /* returns the list of supported timezones */
  170. {
  171. xs_list *l = xs_list_new();
  172. for (int n = 0; xs_tz[n].tz != NULL; n++)
  173. l = xs_list_append(l, xs_tz[n].tz);
  174. return l;
  175. }
  176. int xs_tz_offset(const char *tz)
  177. /* returns the offset in seconds from the specified Time Zone to UTC */
  178. {
  179. for (int n = 0; xs_tz[n].tz != NULL; n++) {
  180. if (strcmp(xs_tz[n].tz, tz) == 0)
  181. return xs_tz[n].h_offset * 3600;
  182. }
  183. return 0;
  184. }
  185. #endif /* XS_IMPLEMENTATION */
  186. #endif /* _XS_TIME_H */