timelib.h 33 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955
  1. /*
  2. * The MIT License (MIT)
  3. *
  4. * Copyright (c) 2015-2019 Derick Rethans
  5. * Copyright (c) 2018 MongoDB, Inc.
  6. *
  7. * Permission is hereby granted, free of charge, to any person obtaining a copy
  8. * of this software and associated documentation files (the "Software"), to deal
  9. * in the Software without restriction, including without limitation the rights
  10. * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
  11. * copies of the Software, and to permit persons to whom the Software is
  12. * furnished to do so, subject to the following conditions:
  13. *
  14. * The above copyright notice and this permission notice shall be included in
  15. * all copies or substantial portions of the Software.
  16. *
  17. * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  18. * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  19. * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
  20. * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
  21. * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
  22. * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
  23. * THE SOFTWARE.
  24. */
  25. #ifndef __TIMELIB_H__
  26. #define __TIMELIB_H__
  27. #ifdef HAVE_TIMELIB_CONFIG_H
  28. # include "timelib_config.h"
  29. #endif
  30. #include <stdlib.h>
  31. #include <stdbool.h>
  32. #include <limits.h>
  33. #include <inttypes.h>
  34. # ifndef HAVE_INT32_T
  35. # if SIZEOF_INT == 4
  36. typedef int int32_t;
  37. # elif SIZEOF_LONG == 4
  38. typedef long int int32_t;
  39. # endif
  40. # endif
  41. # ifndef HAVE_UINT32_T
  42. # if SIZEOF_INT == 4
  43. typedef unsigned int uint32_t;
  44. # elif SIZEOF_LONG == 4
  45. typedef unsigned long int uint32_t;
  46. # endif
  47. # endif
  48. #ifdef _WIN32
  49. # if _MSC_VER >= 1600
  50. # include <stdint.h>
  51. # endif
  52. # ifndef SIZEOF_INT
  53. # define SIZEOF_INT 4
  54. # endif
  55. # ifndef SIZEOF_LONG
  56. # define SIZEOF_LONG 4
  57. # endif
  58. # ifndef PRId32
  59. # define PRId32 "I32d"
  60. # endif
  61. # ifndef PRIu32
  62. # define PRIu32 "I32u"
  63. # endif
  64. # ifndef PRId64
  65. # define PRId64 "I64d"
  66. # endif
  67. # ifndef PRIu64
  68. # define PRIu64 "I64u"
  69. # endif
  70. # ifndef INT32_MAX
  71. #define INT32_MAX _I32_MAX
  72. # endif
  73. # ifndef INT32_MIN
  74. #define INT32_MIN ((int32_t)_I32_MIN)
  75. # endif
  76. # ifndef UINT32_MAX
  77. #define UINT32_MAX _UI32_MAX
  78. # endif
  79. # ifndef INT64_MIN
  80. #define INT64_MIN ((int64_t)_I64_MIN)
  81. # endif
  82. # ifndef INT64_MAX
  83. #define INT64_MAX _I64_MAX
  84. # endif
  85. # ifndef UINT64_MAX
  86. #define UINT64_MAX _UI64_MAX
  87. # endif
  88. #endif
  89. #if (defined(__x86_64__) || defined(__LP64__) || defined(_LP64) || defined(_WIN64)) && !defined(TIMELIB_FORCE_LONG32)
  90. typedef int64_t timelib_long;
  91. typedef uint64_t timelib_ulong;
  92. # define TIMELIB_LONG_MAX INT64_MAX
  93. # define TIMELIB_LONG_MIN INT64_MIN
  94. # define TIMELIB_ULONG_MAX UINT64_MAX
  95. # define TIMELIB_LONG_FMT "%" PRId64
  96. # define TIMELIB_ULONG_FMT "%" PRIu64
  97. #else
  98. typedef int32_t timelib_long;
  99. typedef uint32_t timelib_ulong;
  100. # define TIMELIB_LONG_MAX INT32_MAX
  101. # define TIMELIB_LONG_MIN INT32_MIN
  102. # define TIMELIB_ULONG_MAX UINT32_MAX
  103. # define TIMELIB_LONG_FMT "%" PRId32
  104. # define TIMELIB_ULONG_FMT "%" PRIu32
  105. #endif
  106. #if defined(_MSC_VER)
  107. typedef uint64_t timelib_ull;
  108. typedef int64_t timelib_sll;
  109. # define TIMELIB_LL_CONST(n) n ## i64
  110. #else
  111. typedef unsigned long long timelib_ull;
  112. typedef signed long long timelib_sll;
  113. # define TIMELIB_LL_CONST(n) n ## ll
  114. #endif
  115. typedef struct _ttinfo ttinfo;
  116. typedef struct _tlinfo tlinfo;
  117. typedef struct _tlocinfo
  118. {
  119. char country_code[3];
  120. double latitude;
  121. double longitude;
  122. char *comments;
  123. } tlocinfo;
  124. typedef struct _timelib_tzinfo
  125. {
  126. char *name;
  127. struct {
  128. uint32_t ttisgmtcnt;
  129. uint32_t ttisstdcnt;
  130. uint32_t leapcnt;
  131. uint32_t timecnt;
  132. uint32_t typecnt;
  133. uint32_t charcnt;
  134. } _bit32;
  135. struct {
  136. uint64_t ttisgmtcnt;
  137. uint64_t ttisstdcnt;
  138. uint64_t leapcnt;
  139. uint64_t timecnt;
  140. uint64_t typecnt;
  141. uint64_t charcnt;
  142. } bit64;
  143. int64_t *trans;
  144. unsigned char *trans_idx;
  145. ttinfo *type;
  146. char *timezone_abbr;
  147. tlinfo *leap_times;
  148. unsigned char bc;
  149. tlocinfo location;
  150. } timelib_tzinfo;
  151. typedef struct _timelib_rel_time {
  152. timelib_sll y, m, d; /* Years, Months and Days */
  153. timelib_sll h, i, s; /* Hours, mInutes and Seconds */
  154. timelib_sll us; /* Microseconds */
  155. int weekday; /* Stores the day in 'next monday' */
  156. int weekday_behavior; /* 0: the current day should *not* be counted when advancing forwards; 1: the current day *should* be counted */
  157. int first_last_day_of;
  158. int invert; /* Whether the difference should be inverted */
  159. timelib_sll days; /* Contains the number of *days*, instead of Y-M-D differences */
  160. struct {
  161. unsigned int type;
  162. timelib_sll amount;
  163. } special;
  164. unsigned int have_weekday_relative, have_special_relative;
  165. } timelib_rel_time;
  166. typedef struct _timelib_time_offset {
  167. int32_t offset;
  168. unsigned int leap_secs;
  169. unsigned int is_dst;
  170. char *abbr;
  171. timelib_sll transition_time;
  172. } timelib_time_offset;
  173. typedef struct _timelib_time {
  174. timelib_sll y, m, d; /* Year, Month, Day */
  175. timelib_sll h, i, s; /* Hour, mInute, Second */
  176. timelib_sll us; /* Microseconds */
  177. int z; /* UTC offset in seconds */
  178. char *tz_abbr; /* Timezone abbreviation (display only) */
  179. timelib_tzinfo *tz_info; /* Timezone structure */
  180. signed int dst; /* Flag if we were parsing a DST zone */
  181. timelib_rel_time relative;
  182. timelib_sll sse; /* Seconds since epoch */
  183. unsigned int have_time, have_date, have_zone, have_relative, have_weeknr_day;
  184. unsigned int sse_uptodate; /* !0 if the sse member is up to date with the date/time members */
  185. unsigned int tim_uptodate; /* !0 if the date/time members are up to date with the sse member */
  186. unsigned int is_localtime; /* 1 if the current struct represents localtime, 0 if it is in GMT */
  187. unsigned int zone_type; /* 1 time offset,
  188. * 3 TimeZone identifier,
  189. * 2 TimeZone abbreviation */
  190. } timelib_time;
  191. typedef struct _timelib_abbr_info {
  192. timelib_sll utc_offset;
  193. char *abbr;
  194. int dst;
  195. } timelib_abbr_info;
  196. #define TIMELIB_WARN_MASK 0x1ff
  197. #define TIMELIB_ERR_MASK 0x2ff
  198. #define TIMELIB_WARN_DOUBLE_TZ 0x101
  199. #define TIMELIB_WARN_INVALID_TIME 0x102
  200. #define TIMELIB_WARN_INVALID_DATE 0x103
  201. #define TIMELIB_WARN_TRAILING_DATA 0x11a
  202. #define TIMELIB_ERR_DOUBLE_TZ 0x201
  203. #define TIMELIB_ERR_TZID_NOT_FOUND 0x202
  204. #define TIMELIB_ERR_DOUBLE_TIME 0x203
  205. #define TIMELIB_ERR_DOUBLE_DATE 0x204
  206. #define TIMELIB_ERR_UNEXPECTED_CHARACTER 0x205
  207. #define TIMELIB_ERR_EMPTY_STRING 0x206
  208. #define TIMELIB_ERR_UNEXPECTED_DATA 0x207
  209. #define TIMELIB_ERR_NO_TEXTUAL_DAY 0x208
  210. #define TIMELIB_ERR_NO_TWO_DIGIT_DAY 0x209
  211. #define TIMELIB_ERR_NO_THREE_DIGIT_DAY_OF_YEAR 0x20a
  212. #define TIMELIB_ERR_NO_TWO_DIGIT_MONTH 0x20b
  213. #define TIMELIB_ERR_NO_TEXTUAL_MONTH 0x20c
  214. #define TIMELIB_ERR_NO_TWO_DIGIT_YEAR 0x20d
  215. #define TIMELIB_ERR_NO_FOUR_DIGIT_YEAR 0x20e
  216. #define TIMELIB_ERR_NO_TWO_DIGIT_HOUR 0x20f
  217. #define TIMELIB_ERR_HOUR_LARGER_THAN_12 0x210
  218. #define TIMELIB_ERR_MERIDIAN_BEFORE_HOUR 0x211
  219. #define TIMELIB_ERR_NO_MERIDIAN 0x212
  220. #define TIMELIB_ERR_NO_TWO_DIGIT_MINUTE 0x213
  221. #define TIMELIB_ERR_NO_TWO_DIGIT_SECOND 0x214
  222. #define TIMELIB_ERR_NO_SIX_DIGIT_MICROSECOND 0x215
  223. #define TIMELIB_ERR_NO_SEP_SYMBOL 0x216
  224. #define TIMELIB_ERR_EXPECTED_ESCAPE_CHAR 0x217
  225. #define TIMELIB_ERR_NO_ESCAPED_CHAR 0x218
  226. #define TIMELIB_ERR_WRONG_FORMAT_SEP 0x219
  227. #define TIMELIB_ERR_TRAILING_DATA 0x21a
  228. #define TIMELIB_ERR_DATA_MISSING 0x21b
  229. #define TIMELIB_ERR_NO_THREE_DIGIT_MILLISECOND 0x21c
  230. #define TIMELIB_ERR_NO_FOUR_DIGIT_YEAR_ISO 0x21d
  231. #define TIMELIB_ERR_NO_TWO_DIGIT_WEEK 0x21e
  232. #define TIMELIB_ERR_INVALID_WEEK 0x21f
  233. #define TIMELIB_ERR_NO_DAY_OF_WEEK 0x220
  234. #define TIMELIB_ERR_INVALID_DAY_OF_WEEK 0x221
  235. #define TIMELIB_ERR_INVALID_SPECIFIER 0x222
  236. #define TIMELIB_ERR_INVALID_TZ_OFFSET 0x223
  237. #define TIMELIB_ERR_FORMAT_LITERAL_MISMATCH 0x224
  238. #define TIMELIB_ERR_MIX_ISO_WITH_NATURAL 0x225
  239. #define TIMELIB_ZONETYPE_OFFSET 1
  240. #define TIMELIB_ZONETYPE_ABBR 2
  241. #define TIMELIB_ZONETYPE_ID 3
  242. typedef struct _timelib_error_message {
  243. int error_code;
  244. int position;
  245. char character;
  246. char *message;
  247. } timelib_error_message;
  248. typedef struct _timelib_error_container {
  249. timelib_error_message *error_messages;
  250. timelib_error_message *warning_messages;
  251. int error_count;
  252. int warning_count;
  253. } timelib_error_container;
  254. typedef struct _timelib_tz_lookup_table {
  255. char *name;
  256. int type;
  257. float gmtoffset;
  258. char *full_tz_name;
  259. } timelib_tz_lookup_table;
  260. typedef struct _timelib_tzdb_index_entry {
  261. char *id;
  262. unsigned int pos;
  263. } timelib_tzdb_index_entry;
  264. typedef struct _timelib_tzdb {
  265. char *version;
  266. int index_size;
  267. const timelib_tzdb_index_entry *index;
  268. const unsigned char *data;
  269. } timelib_tzdb;
  270. #ifndef timelib_malloc
  271. # define timelib_malloc malloc
  272. # define timelib_realloc realloc
  273. # define timelib_calloc calloc
  274. # define timelib_strdup strdup
  275. # define timelib_free free
  276. #endif
  277. #define TIMELIB_VERSION 201802
  278. #define TIMELIB_EXTENDED_VERSION 20180201
  279. #define TIMELIB_ASCII_VERSION "2018.02"
  280. #define TIMELIB_NONE 0x00
  281. #define TIMELIB_OVERRIDE_TIME 0x01
  282. #define TIMELIB_NO_CLONE 0x02
  283. #define TIMELIB_UNSET -99999
  284. /* An entry for each of these error codes is also in the
  285. * timelib_error_messages array in timelib.c */
  286. #define TIMELIB_ERROR_NO_ERROR 0x00
  287. #define TIMELIB_ERROR_CANNOT_ALLOCATE 0x01
  288. #define TIMELIB_ERROR_CORRUPT_TRANSITIONS_DONT_INCREASE 0x02
  289. #define TIMELIB_ERROR_CORRUPT_NO_64BIT_PREAMBLE 0x03
  290. #define TIMELIB_ERROR_CORRUPT_NO_ABBREVIATION 0x04
  291. #define TIMELIB_ERROR_UNSUPPORTED_VERSION 0x05
  292. #define TIMELIB_ERROR_NO_SUCH_TIMEZONE 0x06
  293. #ifdef __cplusplus
  294. extern "C" {
  295. #endif
  296. typedef enum _timelib_format_specifier_code {
  297. TIMELIB_FORMAT_ALLOW_EXTRA_CHARACTERS = 0,
  298. TIMELIB_FORMAT_ANY_SEPARATOR,
  299. TIMELIB_FORMAT_DAY_TWO_DIGIT,
  300. TIMELIB_FORMAT_DAY_TWO_DIGIT_PADDED,
  301. TIMELIB_FORMAT_DAY_OF_WEEK_ISO,
  302. TIMELIB_FORMAT_DAY_OF_WEEK,
  303. TIMELIB_FORMAT_DAY_OF_YEAR,
  304. TIMELIB_FORMAT_DAY_SUFFIX,
  305. TIMELIB_FORMAT_END,
  306. TIMELIB_FORMAT_EPOCH_SECONDS,
  307. TIMELIB_FORMAT_ESCAPE,
  308. TIMELIB_FORMAT_HOUR_TWO_DIGIT_12_MAX,
  309. TIMELIB_FORMAT_HOUR_TWO_DIGIT_12_MAX_PADDED,
  310. TIMELIB_FORMAT_HOUR_TWO_DIGIT_24_MAX,
  311. TIMELIB_FORMAT_HOUR_TWO_DIGIT_24_MAX_PADDED,
  312. TIMELIB_FORMAT_LITERAL,
  313. TIMELIB_FORMAT_MERIDIAN,
  314. TIMELIB_FORMAT_MICROSECOND_SIX_DIGIT,
  315. TIMELIB_FORMAT_MILLISECOND_THREE_DIGIT,
  316. TIMELIB_FORMAT_MINUTE_TWO_DIGIT,
  317. TIMELIB_FORMAT_MONTH_TWO_DIGIT,
  318. TIMELIB_FORMAT_MONTH_TWO_DIGIT_PADDED,
  319. TIMELIB_FORMAT_RANDOM_CHAR,
  320. TIMELIB_FORMAT_RESET_ALL,
  321. TIMELIB_FORMAT_RESET_ALL_WHEN_NOT_SET,
  322. TIMELIB_FORMAT_SECOND_TWO_DIGIT,
  323. TIMELIB_FORMAT_SEPARATOR,
  324. TIMELIB_FORMAT_SKIP_TO_SEPARATOR,
  325. TIMELIB_FORMAT_TEXTUAL_DAY_3_LETTER,
  326. TIMELIB_FORMAT_TEXTUAL_DAY_FULL,
  327. TIMELIB_FORMAT_TEXTUAL_MONTH_3_LETTER,
  328. TIMELIB_FORMAT_TEXTUAL_MONTH_FULL,
  329. TIMELIB_FORMAT_TIMEZONE_OFFSET,
  330. TIMELIB_FORMAT_TIMEZONE_OFFSET_MINUTES,
  331. TIMELIB_FORMAT_WEEK_OF_YEAR_ISO,
  332. TIMELIB_FORMAT_WEEK_OF_YEAR,
  333. TIMELIB_FORMAT_WHITESPACE,
  334. TIMELIB_FORMAT_YEAR_TWO_DIGIT,
  335. TIMELIB_FORMAT_YEAR_FOUR_DIGIT,
  336. TIMELIB_FORMAT_YEAR_ISO
  337. } timelib_format_specifier_code;
  338. typedef struct _timelib_format_specifier {
  339. char specifier;
  340. timelib_format_specifier_code code;
  341. } timelib_format_specifier;
  342. typedef struct _timelib_format_config {
  343. const timelib_format_specifier *format_map;
  344. /* Format speciifiers must be preceded by 'prefix_char' if not '\0'. */
  345. char prefix_char;
  346. } timelib_format_config;
  347. /* Function pointers */
  348. typedef timelib_tzinfo* (*timelib_tz_get_wrapper)(char *tzname, const timelib_tzdb *tzdb, int *error_code);
  349. /* From dow.c */
  350. /* Calculates the day of the week from y, m, and d. 0=Sunday..6=Saturday */
  351. timelib_sll timelib_day_of_week(timelib_sll y, timelib_sll m, timelib_sll d);
  352. /* Calculates the day of the ISO week from y, m, and d. 1=Monday, 7=Sunday */
  353. timelib_sll timelib_iso_day_of_week(timelib_sll y, timelib_sll m, timelib_sll d);
  354. /* Calculates the day of the year according to y-m-d. 0=Jan 1st..364/365=Dec
  355. * 31st */
  356. timelib_sll timelib_day_of_year(timelib_sll y, timelib_sll m, timelib_sll d);
  357. /* Calculates the day of the year according to y-w-dow. 0..364/365 */
  358. timelib_sll timelib_daynr_from_weeknr(timelib_sll iy, timelib_sll iw, timelib_sll id);
  359. /* Calculates the number of days in month m for year y. 28..31 */
  360. timelib_sll timelib_days_in_month(timelib_sll y, timelib_sll m);
  361. /* Calculates the ISO year and week from y, m, and d, into iw and iy */
  362. void timelib_isoweek_from_date(timelib_sll y, timelib_sll m, timelib_sll d, timelib_sll *iw, timelib_sll *iy);
  363. /* Calculates the ISO year, week, and day of week from y, m, and d, into iy,
  364. * iw, and id */
  365. void timelib_isodate_from_date(timelib_sll y, timelib_sll m, timelib_sll d, timelib_sll *iy, timelib_sll *iw, timelib_sll *id);
  366. /* Calculates the year, month, and day from iy, iw, and iw, into y, m, and d */
  367. void timelib_date_from_isodate(timelib_sll iy, timelib_sll iw, timelib_sll id, timelib_sll *y, timelib_sll *m, timelib_sll *d);
  368. /* Returns true if h, i and s fit in the range 00:00:00..23:59:59, false
  369. * otherwise */
  370. int timelib_valid_time(timelib_sll h, timelib_sll i, timelib_sll s);
  371. /* Returns true if m fits in the range 1..12, and d fits in the range
  372. * 1..<days-in-month> for year y */
  373. int timelib_valid_date(timelib_sll y, timelib_sll m, timelib_sll d);
  374. /* From parse_date.re */
  375. /* Parses the date/time string in 's' with length 'len' into the constituent
  376. * parts of timelib_time*.
  377. *
  378. * Depending on the contents of the string 's', not all elements might be
  379. * filled. You can check whether a specific element has been parsed by
  380. * comparing with the TIMELIB_UNSET define.
  381. *
  382. * If errors occur, this function keeps already parsed elements in the
  383. * returned timelib_time* value.
  384. *
  385. * If the **errors points to a timelib_error_container variable, warnings
  386. * and errors will be recorded. You are responsible for freeing the stored
  387. * information with timelib_error_container_dtor(). To see whether errors have
  388. * occurred, inspect errors->errors_count. To see whether warnings have occurred,
  389. * inspect errors->warnings_count.
  390. *
  391. * The returned timelib_time* value is dynamically allocated and should be
  392. * freed with timelib_time_dtor().
  393. */
  394. timelib_time *timelib_strtotime(char *s, size_t len, timelib_error_container **errors, const timelib_tzdb *tzdb, timelib_tz_get_wrapper tz_get_wrapper);
  395. /* Parses the date/time string in 's' with length 'len' into the constituent
  396. * parts of timelib_time* according to the format in 'format'.
  397. *
  398. * Depending on the contents of the string 's', not all elements might be
  399. * filled. You can check whether a specific element has been parsed by
  400. * comparing with the TIMELIB_UNSET define.
  401. *
  402. * If errors occur, this function keeps already parsed elements in the
  403. * returned timelib_time* value.
  404. *
  405. * If the **errors points to a timelib_error_container variable, warnings
  406. * and errors will be recorded. You are responsible for freeing the stored
  407. * information with timelib_error_container_dtor(). To see whether errors have
  408. * occurred, inspect errors->errors_count. To see whether warnings have occurred,
  409. * inspect errors->warnings_count.
  410. *
  411. * The returned timelib_time* value is dynamically allocated and should be
  412. * freed with timelib_time_dtor().
  413. */
  414. timelib_time *timelib_parse_from_format(char *format, char *s, size_t len, timelib_error_container **errors, const timelib_tzdb *tzdb, timelib_tz_get_wrapper tz_get_wrapper);
  415. /* Parses the date/time string in 's' with length 'len' into the constituent
  416. * parts of timelib_time* according to the format in 'format' with format
  417. * specifier configuration 'format_config'.
  418. *
  419. * 'format_map' is an array of pairs, with the first element being the format
  420. * specifier as a character and the second element corresponds to the
  421. * representation of the specifier from the enum list
  422. * 'timelib_format_specifier_code'.
  423. *
  424. * Note: 'format_map' must be terminated with specifier '\0' to indicate to the
  425. * parser that there are no more format specifiers in the list.
  426. */
  427. timelib_time *timelib_parse_from_format_with_map(char *format, char *s, size_t len, timelib_error_container **errors, const timelib_tzdb *tzdb, timelib_tz_get_wrapper tz_get_wrapper, const timelib_format_config* format_config);
  428. /* Fills the gaps in the parsed timelib_time with information from the reference date/time in 'now'
  429. *
  430. * If any of the 'parsed' y, m, d, h, i or s parameters is unset (TIMELIB_UNSET):
  431. * - if microtime (us) is unset, then the us of the parsed time is set to 0.
  432. * - else if microtime (us) is unset and 'now'->'us' is set, use it, otherwise use 0.
  433. *
  434. * For either of the 'parsed' y, m, d, h, i, s, z (utc offset in seconds) or
  435. * dst is unset, set it to the corresponding value in 'now' if set, otherwise
  436. * set it to 0.
  437. *
  438. * It duplicates tz_abbr if unset in 'parsed' but set in 'now'.
  439. *
  440. * It duplicates tz_info if unset in 'parsed', but set in 'now' unless
  441. * TIMELIB_NO_CLONE is passed, in which case only the pointer in 'parsed' is
  442. * set to 'now'.
  443. *
  444. * If the option TIMELIB_OVERRIDE_TIME is passed and the parsed date/time has
  445. * no time portion, the function will ignore the time aspect in 'now' and
  446. * instead fill it with zeros.
  447. */
  448. void timelib_fill_holes(timelib_time *parsed, timelib_time *now, int options);
  449. /* Tries to convert a time zone abbreviation, gmtoffset and/or isdst flag
  450. * combination to a time zone identifier.
  451. *
  452. * If 'abbr' is either 'utc' or 'gmt' (case insensitve) then "UTC" is
  453. * returned.
  454. *
  455. * It first uses the data in the timezonemap.h file to find a matching
  456. * abbreviation/GMT offset combination. If not found, it uses the data in
  457. * fallbackmap.h to match only the GMT offset/isdst flag to try to find a
  458. * match. If nothing is found, NULL is returned.
  459. *
  460. * The returned char* is not duplicated, and should not be freed.
  461. */
  462. char *timelib_timezone_id_from_abbr(const char *abbr, timelib_long gmtoffset, int isdst);
  463. /* Returns an array of known time zone abbreviations
  464. *
  465. * This file is generated from the time zone database through the
  466. * gettzmapping.php scripts, which requires that an up-to-date time zone
  467. * database is used with the PHP binary that runs the script.
  468. *
  469. * Each item in the returned list contains the abbreviation, a flag whether
  470. * it's an abbreviation used with DST, the UTC offset in seconds, and the name
  471. * of the time zone identifier that this abbreviation belongs to.
  472. *
  473. * The order for each specific abbreviation is controlled through the
  474. * preference list in the gettzmapping.php script. Time zones that match the
  475. * pattern ±\d{2,4} are excluded
  476. */
  477. const timelib_tz_lookup_table *timelib_timezone_abbreviations_list(void);
  478. /**
  479. * DEPRECATED, but still used by PHP.
  480. */
  481. timelib_long timelib_parse_zone(char **ptr, int *dst, timelib_time *t, int *tz_not_found, const timelib_tzdb *tzdb, timelib_tz_get_wrapper tz_wrapper);
  482. /* From parse_iso_intervals.re */
  483. /**
  484. * Parses a subset of an ISO 8601 intervals specification string into its
  485. * constituent parts.
  486. *
  487. * If the **errors points to a timelib_error_container variable, warnings
  488. * and errors will be recorded. You are responsible for freeing the stored
  489. * information with timelib_error_container_dtor(). To see whether errors have
  490. * occurred, inspect errors->errors_count. To see whether warnings have occurred,
  491. * inspect errors->warnings_count.
  492. */
  493. void timelib_strtointerval(char *s, size_t len,
  494. timelib_time **begin, timelib_time **end,
  495. timelib_rel_time **period, int *recurrences,
  496. timelib_error_container **errors);
  497. /* From tm2unixtime.c */
  498. /**
  499. * Uses the y/m/d/h/i/s fields to calculate and store the equivalent timestamp
  500. * in the sse field.
  501. *
  502. * It uses the time zone information associated with 'time' to account for the
  503. * right UTC offset and/or DST rules. You can associate time zone information
  504. * with the timelib_set_timezone_* functions (see below).
  505. *
  506. * If the type is 'TIMELIB_ZONETYPE_ID' and there is no associated tzinfo, it
  507. * will use the second argument 'tzi' to provide the rules necessary to
  508. * calculate the right timestamp.
  509. */
  510. void timelib_update_ts(timelib_time* time, timelib_tzinfo* tzi);
  511. /**
  512. * Takes the information from the y/m/d/h/i/s fields and makes sure their
  513. * values are in the right range.
  514. *
  515. * If a value under- or overflows it will adjust the larger measure up (or
  516. * down). It also takes into account leap days.
  517. */
  518. void timelib_do_normalize(timelib_time *base);
  519. /**
  520. * Takes the information from the y/m/d/h/i/s fields of 'rt' and makes sure
  521. * their values are in the right range.
  522. *
  523. * If a value under- or overflows it will adjust the larger measure up (or
  524. * down). As this function operates on a *relative date/time*, it also takes
  525. * into account leap days and correctly accounts for the difference depending
  526. * on the base date/time in 'base'.
  527. */
  528. void timelib_do_rel_normalize(timelib_time *base, timelib_rel_time *rt);
  529. /* From unixtime2tm.c */
  530. /**
  531. * Takes the unix timestamp in seconds from 'ts' and populates the y/m/d/h/i/s
  532. * fields of 'tm' without taking time zones into account
  533. */
  534. void timelib_unixtime2gmt(timelib_time* tm, timelib_sll ts);
  535. /**
  536. * Takes the Unix timestamp from 'ts', and calculates the y/m/d/h/i/s fields
  537. * according to the time zone information attached to 'tm'.
  538. */
  539. void timelib_unixtime2local(timelib_time *tm, timelib_sll ts);
  540. /**
  541. * Takes the Unix timestamp stored in 'tm', and calculates the y/m/d/h/i/s
  542. * fields according to the time zone information attached to 'tm'.
  543. */
  544. void timelib_update_from_sse(timelib_time *tm);
  545. /**
  546. * Attaches the UTC offset as time zone information to 't'.
  547. *
  548. * 'utc_offset' is in seconds East of UTC.
  549. */
  550. void timelib_set_timezone_from_offset(timelib_time *t, timelib_sll utc_offset);
  551. /**
  552. * Attaches the information from 'abbr_info' as time zone information to 't'.
  553. *
  554. * The timelib_abbr_info struct contains an abbreviation ('abbr') which string
  555. * value is duplicated, as well as a 'utc_offset' and 'dst' flag. It only
  556. * supports a 'dst' change over of 1 hour.
  557. */
  558. void timelib_set_timezone_from_abbr(timelib_time *t, timelib_abbr_info abbr_info);
  559. /**
  560. * Attaches the time zone information in 'tz' to to 't'.
  561. *
  562. * It fetches the right UTC offset that is currently stored in the time
  563. * stamp field in 't' ('sse'), and assigns that to the 'z' field and 'dst'
  564. * field (whether DST is in effect at the time). It also sets the current
  565. * abbreviation to the 'tz_addr' field, making sure that if a value was already
  566. * set it was freed.
  567. *
  568. * The time zone information in 'tz' is *not* duplicated into the 't' field so
  569. * it should not be freed until all timelib_time* variables have been freed as
  570. * well.
  571. */
  572. void timelib_set_timezone(timelib_time *t, timelib_tzinfo *tz);
  573. /* From parse_tz.c */
  574. /**
  575. * Returns whether the time zone ID 'timezone' is available in the time zone
  576. * database as pointed to be 'tzdb'.
  577. */
  578. int timelib_timezone_id_is_valid(char *timezone, const timelib_tzdb *tzdb);
  579. /**
  580. * Converts the binary stored time zone information from 'tzdb' for the time
  581. * zone 'timeozne' into a structure the library can use for calculations.
  582. *
  583. * The function can be used on both timelib_builtin_db as well as a time zone
  584. * db as opened by timelib_zoneinfo.
  585. * The function will return null upon failure, and also set an error code
  586. * through 'error_code'. 'error_code' must not be a null pointer. The error
  587. * code is one of the TIMELIB_ERROR_* constants as listed above. These error
  588. * constants can be converted into a string by timelib_get_error_message.
  589. *
  590. * This function allocates memory for the new time zone structure, which must
  591. * be freed after use. Although it is recommended that a cache of each used
  592. * time zone is kept.
  593. */
  594. timelib_tzinfo *timelib_parse_tzfile(char *timezone, const timelib_tzdb *tzdb, int *error_code);
  595. /**
  596. * Frees up the resources allocated by 'timelib_parse_tzfile'.
  597. */
  598. void timelib_tzinfo_dtor(timelib_tzinfo *tz);
  599. /**
  600. * Deep-clones a timelib_tzinfo structure.
  601. *
  602. * This allocates resources that need to be freed with 'timelib_tzinfo_dtor'
  603. */
  604. timelib_tzinfo* timelib_tzinfo_clone(timelib_tzinfo *tz);
  605. /**
  606. * Returns whether DST is active with time zone 'tz' for the time stamp 'ts'.
  607. *
  608. * Returns 0 if DST is not active, 1 if DST is active, or -1 if no transitions
  609. * were available through 'tz'.
  610. */
  611. int timelib_timestamp_is_in_dst(timelib_sll ts, timelib_tzinfo *tz);
  612. /**
  613. * Returns offset information with time zone 'tz' for the time stamp 'ts'.
  614. *
  615. * The returned information contains: the offset in seconds East of UTC (in
  616. * 'offset'), whether DST is active ('is_dst'), what the current time zone
  617. * abbreviation is ('abbr') and the transition time that got to this state (in
  618. * 'transition_time');
  619. */
  620. timelib_time_offset *timelib_get_time_zone_info(timelib_sll ts, timelib_tzinfo *tz);
  621. /**
  622. * Returns the UTC offset currently applicable for the information stored in 't'.
  623. *
  624. * The value returned is the UTC offset in seconds East.
  625. */
  626. timelib_sll timelib_get_current_offset(timelib_time *t);
  627. /**
  628. * Displays debugging information about the time zone information in 'tz'.
  629. */
  630. void timelib_dump_tzinfo(timelib_tzinfo *tz);
  631. /**
  632. * Returns a pointer to the built-in time zone database.
  633. *
  634. * You must *not* free the returned pointer as it is part of the text segment.
  635. */
  636. const timelib_tzdb *timelib_builtin_db(void);
  637. /**
  638. * Returns a pointer to the start of an array containing a list of timezone identifiers.
  639. *
  640. * The amount of entries in the array is returned through the 'count' OUT parameter.
  641. *
  642. * Each entry contains the time zone ID ('id' field), and the position within the time zone
  643. * information ('pos' field). The pos field should not be used.
  644. */
  645. const timelib_tzdb_index_entry *timelib_timezone_identifiers_list(const timelib_tzdb *tzdb, int *count);
  646. /* From parse_zoneinfo.c */
  647. /**
  648. * Scans the directory and subdirectories of 'directory' for valid time zone files and builds
  649. * a time zone database out of these files.
  650. *
  651. * Typically, the directory should point to '/usr/share/zoneinfo'.
  652. *
  653. * Unlike 'timelib_builtin_db', the return value of this function must be freed
  654. * with the 'timelib_zoneinfo_dtor' function.
  655. */
  656. timelib_tzdb *timelib_zoneinfo(char *directory);
  657. /**
  658. * Frees up the resources as created through 'timelib_zoneinfo'.
  659. *
  660. * This function must be used to free up all the resources that have been
  661. * allocated while calling 'timelib_zoneinfo'.
  662. */
  663. void timelib_zoneinfo_dtor(timelib_tzdb *tzdb);
  664. /* From timelib.c */
  665. /**
  666. * Returns a static string containing an error message belonging to a specific
  667. * error code.
  668. */
  669. const char *timelib_get_error_message(int error_code);
  670. /**
  671. * Allocates resources for the relative time structure.
  672. *
  673. * Must be freed with 'timelib_rel_time_dtor'.
  674. */
  675. timelib_rel_time* timelib_rel_time_ctor(void);
  676. /**
  677. * Frees up the resources as allocated through 'timelib_rel_time_ctor'.
  678. */
  679. void timelib_rel_time_dtor(timelib_rel_time* t);
  680. /**
  681. * Creates a new timelib_rel_time resource and copies over the information
  682. * from 'tz'.
  683. *
  684. * Must be freed with 'timelib_rel_time_dtor'.
  685. */
  686. timelib_rel_time* timelib_rel_time_clone(timelib_rel_time *tz);
  687. /**
  688. * Allocates resources for the time structure.
  689. *
  690. * Must be freed with 'timelib_time_dtor'.
  691. */
  692. timelib_time* timelib_time_ctor(void);
  693. /**
  694. * Frees up the resources as allocated through 'timelib_time_ctor'.
  695. */
  696. void timelib_time_dtor(timelib_time* t);
  697. /**
  698. * Creates a new timelib_time resource and copies over the information
  699. * from 'orig'.
  700. *
  701. * Must be freed with 'timelib_time_dtor'.
  702. */
  703. timelib_time* timelib_time_clone(timelib_time* orig);
  704. /**
  705. * Compares two timelib_time structures and returns which one is earlier in
  706. * time.
  707. *
  708. * To decide which comes earlier it uses the 'sse' (Seconds Since Epoch) and
  709. * 'us' (microseconds) fields.
  710. *
  711. * Returns -1 if t1 < t2, 0 if t1 == t2, and -1 if t1 > t2.
  712. */
  713. int timelib_time_compare(timelib_time *t1, timelib_time *t2);
  714. /**
  715. * Allocates resources for the time offset structure.
  716. *
  717. * Must be freed with 'timelib_time_offset_dtor'.
  718. */
  719. timelib_time_offset* timelib_time_offset_ctor(void);
  720. /**
  721. * Frees up the resources as allocated through 'timelib_time_offset_ctor'.
  722. */
  723. void timelib_time_offset_dtor(timelib_time_offset* t);
  724. /**
  725. * Frees up the resources allocated while converting strings to timelib_time
  726. * structures with the timelib_strtotime and timelib_strtointerval functions.
  727. */
  728. void timelib_error_container_dtor(timelib_error_container *errors);
  729. /**
  730. * Converts the 'sse' value of 'd' to a timelib_long type.
  731. *
  732. * If the value fits in the TIMELIB_LONG_MIN and TIMELIB_LONG_MAX range, the
  733. * value is cast to (timelib_long) and returned. If *error is not a NULL
  734. * pointer, it will be set to 0.
  735. *
  736. * If the value does *not* fit in the range, the function returns 0 and if
  737. * *error is not a NULL pointer, it will be set to 1.
  738. *
  739. * timelib_long is a 32 bit signed long integer on 32 bit platforms, and a 64
  740. * bit signed long long integer on 64 bit platforms. In other words, it makes
  741. * sure that the value in 'sse' (which is always a signed long long 64 bit
  742. * integer) can be used safely outside of the library.
  743. */
  744. timelib_long timelib_date_to_int(timelib_time *d, int *error);
  745. /**
  746. * Displays debugging information about the date/time information stored in 'd'.
  747. *
  748. * 'options' is a bit field, where:
  749. * - 1 controls whether the relative time portion is shown.
  750. * - 2 controls whether the zone type is shown.
  751. */
  752. void timelib_dump_date(timelib_time *d, int options);
  753. /**
  754. * Displays debugging information about the relative time information stored
  755. * in 'd'.
  756. */
  757. void timelib_dump_rel_time(timelib_rel_time *d);
  758. /**
  759. * Converts a decimal hour into hour/min/sec components
  760. */
  761. void timelib_decimal_hour_to_hms(double h, int *hour, int *min, int *sec);
  762. /**
  763. * Converts hour/min/sec values into a decimal hour
  764. */
  765. void timelib_hms_to_decimal_hour(int hour, int min, int sec, double *h);
  766. /* from astro.c */
  767. /**
  768. * Converts the Unix Epoch time stamp 'ts' to a Julian Day
  769. *
  770. * The value returned is the number of whole days since -4714-11-24T12:00:00 UTC
  771. * (in the proleptic Gregorian calendar):
  772. * https://en.wikipedia.org/wiki/Julian_day
  773. */
  774. double timelib_ts_to_julianday(timelib_sll ts);
  775. /**
  776. * Converts the Unix Epoch time stamp 'ts' to the J2000 epoch
  777. *
  778. * The value returned is the number of whole days since 2000-01-01T12:00:00
  779. * UTC: https://en.wikipedia.org/wiki/Epoch_(astronomy)#Julian_years_and_J2000
  780. */
  781. double timelib_ts_to_j2000(timelib_sll ts);
  782. /**
  783. * Calculates when the Sun is above a certain latitude.
  784. *
  785. * Parameters:
  786. * - time: A timelib_time time describing that needs to specific midnight for a
  787. * specific day.
  788. * - lon: The longitude of the observer (East positive, West negative).
  789. * - lat: The latitude of the observer (North positive, South negative).
  790. * - altit: The altitude. Set to -35/60 for rise/set, -6 for civil twilight,
  791. * -12 for nautical, and -18 for astronomical twilight.
  792. * - upper_limb: set to non-zero for rise/set calculations, and 0 for twilight
  793. * calculations.
  794. *
  795. * Out Parameters:
  796. * - h_rise: The decimal hour when the Sun rises
  797. * - h_set: The decimal hour when the Sun sets
  798. * - ts_rise: The Unix timestamp of the Sun rising
  799. * - ts_set: The Unix timestamp of the Sun setting
  800. * - ts_transit: The Unix timestmap of the Sun transitting through South
  801. *
  802. * Return Values:
  803. * - 0: The Sun rises and sets.
  804. * - +1: The Sun is always above the horizon. (ts_rise is set to ts_transit -
  805. * (12 * 3600); ts_set is set to ts_transit + (12 * 3600).
  806. * - -1: The Sun is awlays below the horizon. (ts_rise and ts_set are set
  807. * to ts_transit)
  808. */
  809. int timelib_astro_rise_set_altitude(timelib_time *time, double lon, double lat, double altit, int upper_limb, double *h_rise, double *h_set, timelib_sll *ts_rise, timelib_sll *ts_set, timelib_sll *ts_transit);
  810. /* from interval.c */
  811. /**
  812. * Calculates the difference between two times
  813. *
  814. * The result is a timelib_rel_time structure that describes how you can
  815. * convert from 'one' to 'two' with 'timelib_add'. This does *not* necessarily
  816. * mean that you can go from 'two' to 'one' by using 'timelib_sub' due to the
  817. * way months and days are calculated.
  818. */
  819. timelib_rel_time *timelib_diff(timelib_time *one, timelib_time *two);
  820. /**
  821. * Adds the relative time information 'interval' to the base time 't'.
  822. *
  823. * This can be a relative time as created by 'timelib_diff', but also by more
  824. * complex statements such as "next workday".
  825. */
  826. timelib_time *timelib_add(timelib_time *t, timelib_rel_time *interval);
  827. /**
  828. * Subtracts the relative time information 'interval' to the base time 't'.
  829. *
  830. * This can be a relative time as created by 'timelib_diff'. Unlike with
  831. * 'timelib_add', this does not support more complex statements such as "next
  832. * workday".
  833. */
  834. timelib_time *timelib_sub(timelib_time *t, timelib_rel_time *interval);
  835. #ifdef __cplusplus
  836. } /* extern "C" */
  837. #endif
  838. #endif