timelib_structs.h 7.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315
  1. /*
  2. * The MIT License (MIT)
  3. *
  4. * Copyright (c) 2015 Derick Rethans
  5. *
  6. * Permission is hereby granted, free of charge, to any person obtaining a copy
  7. * of this software and associated documentation files (the "Software"), to deal
  8. * in the Software without restriction, including without limitation the rights
  9. * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
  10. * copies of the Software, and to permit persons to whom the Software is
  11. * furnished to do so, subject to the following conditions:
  12. *
  13. * The above copyright notice and this permission notice shall be included in
  14. * all copies or substantial portions of the Software.
  15. *
  16. * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  17. * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  18. * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
  19. * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
  20. * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
  21. * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
  22. * THE SOFTWARE.
  23. */
  24. #ifndef __TIMELIB_STRUCTS_H__
  25. #define __TIMELIB_STRUCTS_H__
  26. #ifdef HAVE_TIMELIB_CONFIG_H
  27. # include "timelib_config.h"
  28. #endif
  29. #ifndef TIMELIB_OMIT_STDINT
  30. #ifdef HAVE_SYS_TYPES_H
  31. #include <sys/types.h>
  32. #endif
  33. #if defined(HAVE_INTTYPES_H)
  34. #include <inttypes.h>
  35. #elif defined(HAVE_STDINT_H)
  36. #include <stdint.h>
  37. #endif
  38. # ifndef HAVE_INT32_T
  39. # if SIZEOF_INT == 4
  40. typedef int int32_t;
  41. # elif SIZEOF_LONG == 4
  42. typedef long int int32_t;
  43. # endif
  44. # endif
  45. # ifndef HAVE_UINT32_T
  46. # if SIZEOF_INT == 4
  47. typedef unsigned int uint32_t;
  48. # elif SIZEOF_LONG == 4
  49. typedef unsigned long int uint32_t;
  50. # endif
  51. # endif
  52. #ifdef _WIN32
  53. # if _MSC_VER >= 1600
  54. # include <stdint.h>
  55. # endif
  56. # ifndef SIZEOF_INT
  57. # define SIZEOF_INT 4
  58. # endif
  59. # ifndef SIZEOF_LONG
  60. # define SIZEOF_LONG 4
  61. # endif
  62. # ifndef int32_t
  63. typedef __int32 int32_t;
  64. # endif
  65. # ifndef uint32_t
  66. typedef unsigned __int32 uint32_t;
  67. # endif
  68. # ifndef int64_t
  69. typedef __int64 int64_t;
  70. # endif
  71. # ifndef uint64_t
  72. typedef unsigned __int64 uint64_t;
  73. # endif
  74. # ifndef PRId32
  75. # define PRId32 "I32d"
  76. # endif
  77. # ifndef PRIu32
  78. # define PRIu32 "I32u"
  79. # endif
  80. # ifndef PRId64
  81. # define PRId64 "I64d"
  82. # endif
  83. # ifndef PRIu64
  84. # define PRIu64 "I64u"
  85. # endif
  86. # ifndef INT32_MAX
  87. #define INT32_MAX _I32_MAX
  88. # endif
  89. # ifndef INT32_MIN
  90. #define INT32_MIN ((int32_t)_I32_MIN)
  91. # endif
  92. # ifndef UINT32_MAX
  93. #define UINT32_MAX _UI32_MAX
  94. # endif
  95. # ifndef INT64_MIN
  96. #define INT64_MIN ((int64_t)_I64_MIN)
  97. # endif
  98. # ifndef INT64_MAX
  99. #define INT64_MAX _I64_MAX
  100. # endif
  101. # ifndef UINT64_MAX
  102. #define UINT64_MAX _UI64_MAX
  103. # endif
  104. #endif
  105. #endif /* TIMELIB_OMIT_STDINT */
  106. #include <stdio.h>
  107. #ifdef HAVE_STDLIB_H
  108. #include <stdlib.h>
  109. #endif
  110. #ifdef HAVE_STRING_H
  111. #include <string.h>
  112. #else
  113. #include <strings.h>
  114. #endif
  115. #if (defined(__x86_64__) || defined(__LP64__) || defined(_LP64) || defined(_WIN64)) && !defined(TIMELIB_FORCE_LONG32)
  116. typedef int64_t timelib_long;
  117. typedef uint64_t timelib_ulong;
  118. # define TIMELIB_LONG_MAX INT64_MAX
  119. # define TIMELIB_LONG_MIN INT64_MIN
  120. # define TIMELIB_ULONG_MAX UINT64_MAX
  121. # define TIMELIB_LONG_FMT "%" PRId64
  122. # define TIMELIB_ULONG_FMT "%" PRIu64
  123. #else
  124. typedef int32_t timelib_long;
  125. typedef uint32_t timelib_ulong;
  126. # define TIMELIB_LONG_MAX INT32_MAX
  127. # define TIMELIB_LONG_MIN INT32_MIN
  128. # define TIMELIB_ULONG_MAX UINT32_MAX
  129. # define TIMELIB_LONG_FMT "%" PRId32
  130. # define TIMELIB_ULONG_FMT "%" PRIu32
  131. #endif
  132. #if defined(_MSC_VER)
  133. typedef uint64_t timelib_ull;
  134. typedef int64_t timelib_sll;
  135. # define TIMELIB_LL_CONST(n) n ## i64
  136. #else
  137. typedef unsigned long long timelib_ull;
  138. typedef signed long long timelib_sll;
  139. # define TIMELIB_LL_CONST(n) n ## ll
  140. #endif
  141. typedef struct ttinfo
  142. {
  143. int32_t offset;
  144. int isdst;
  145. unsigned int abbr_idx;
  146. unsigned int isstdcnt;
  147. unsigned int isgmtcnt;
  148. } ttinfo;
  149. typedef struct tlinfo
  150. {
  151. int32_t trans;
  152. int32_t offset;
  153. } tlinfo;
  154. typedef struct tlocinfo
  155. {
  156. char country_code[3];
  157. double latitude;
  158. double longitude;
  159. char *comments;
  160. } tlocinfo;
  161. typedef struct timelib_tzinfo
  162. {
  163. char *name;
  164. struct {
  165. uint32_t ttisgmtcnt;
  166. uint32_t ttisstdcnt;
  167. uint32_t leapcnt;
  168. uint32_t timecnt;
  169. uint32_t typecnt;
  170. uint32_t charcnt;
  171. } bit32;
  172. struct {
  173. uint64_t ttisgmtcnt;
  174. uint64_t ttisstdcnt;
  175. uint64_t leapcnt;
  176. uint64_t timecnt;
  177. uint64_t typecnt;
  178. uint64_t charcnt;
  179. } bit64;
  180. int32_t *trans;
  181. unsigned char *trans_idx;
  182. ttinfo *type;
  183. char *timezone_abbr;
  184. tlinfo *leap_times;
  185. unsigned char bc;
  186. tlocinfo location;
  187. } timelib_tzinfo;
  188. typedef struct timelib_special {
  189. unsigned int type;
  190. timelib_sll amount;
  191. } timelib_special;
  192. typedef struct timelib_rel_time {
  193. timelib_sll y, m, d; /* Years, Months and Days */
  194. timelib_sll h, i, s; /* Hours, mInutes and Seconds */
  195. int weekday; /* Stores the day in 'next monday' */
  196. int weekday_behavior; /* 0: the current day should *not* be counted when advancing forwards; 1: the current day *should* be counted */
  197. int first_last_day_of;
  198. int invert; /* Whether the difference should be inverted */
  199. timelib_sll days; /* Contains the number of *days*, instead of Y-M-D differences */
  200. timelib_special special;
  201. unsigned int have_weekday_relative, have_special_relative;
  202. } timelib_rel_time;
  203. typedef struct timelib_time_offset {
  204. int32_t offset;
  205. unsigned int leap_secs;
  206. unsigned int is_dst;
  207. char *abbr;
  208. timelib_sll transistion_time;
  209. } timelib_time_offset;
  210. typedef struct timelib_time {
  211. timelib_sll y, m, d; /* Year, Month, Day */
  212. timelib_sll h, i, s; /* Hour, mInute, Second */
  213. double f; /* Fraction */
  214. int z; /* GMT offset in minutes */
  215. char *tz_abbr; /* Timezone abbreviation (display only) */
  216. timelib_tzinfo *tz_info; /* Timezone structure */
  217. signed int dst; /* Flag if we were parsing a DST zone */
  218. timelib_rel_time relative;
  219. timelib_sll sse; /* Seconds since epoch */
  220. unsigned int have_time, have_date, have_zone, have_relative, have_weeknr_day;
  221. unsigned int sse_uptodate; /* !0 if the sse member is up to date with the date/time members */
  222. unsigned int tim_uptodate; /* !0 if the date/time members are up to date with the sse member */
  223. unsigned int is_localtime; /* 1 if the current struct represents localtime, 0 if it is in GMT */
  224. unsigned int zone_type; /* 1 time offset,
  225. * 3 TimeZone identifier,
  226. * 2 TimeZone abbreviation */
  227. } timelib_time;
  228. typedef struct timelib_abbr_info {
  229. timelib_sll utc_offset;
  230. char *abbr;
  231. int dst;
  232. } timelib_abbr_info;
  233. typedef struct timelib_error_message {
  234. int position;
  235. char character;
  236. char *message;
  237. } timelib_error_message;
  238. typedef struct timelib_error_container {
  239. struct timelib_error_message *error_messages;
  240. struct timelib_error_message *warning_messages;
  241. int error_count;
  242. int warning_count;
  243. } timelib_error_container;
  244. typedef struct _timelib_tz_lookup_table {
  245. char *name;
  246. int type;
  247. float gmtoffset;
  248. char *full_tz_name;
  249. } timelib_tz_lookup_table;
  250. typedef struct _timelib_tzdb_index_entry {
  251. char *id;
  252. unsigned int pos;
  253. } timelib_tzdb_index_entry;
  254. typedef struct _timelib_tzdb {
  255. char *version;
  256. int index_size;
  257. const timelib_tzdb_index_entry *index;
  258. const unsigned char *data;
  259. } timelib_tzdb;
  260. #define TIMELIB_ZONETYPE_OFFSET 1
  261. #define TIMELIB_ZONETYPE_ABBR 2
  262. #define TIMELIB_ZONETYPE_ID 3
  263. #define SECS_PER_ERA TIMELIB_LL_CONST(12622780800)
  264. #define SECS_PER_DAY 86400
  265. #define DAYS_PER_YEAR 365
  266. #define DAYS_PER_LYEAR 366
  267. /* 400*365 days + 97 leap days */
  268. #define DAYS_PER_LYEAR_PERIOD 146097
  269. #define YEARS_PER_LYEAR_PERIOD 400
  270. #define timelib_is_leap(y) ((y) % 4 == 0 && ((y) % 100 != 0 || (y) % 400 == 0))
  271. #define TIMELIB_DEBUG(s) if (0) { s }
  272. #endif