ccronexpr.h 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123
  1. /*
  2. * Copyright 2015, alex at staticlibs.net
  3. *
  4. * Licensed under the Apache License, Version 2.0 (the "License");
  5. * you may not use this file except in compliance with the License.
  6. * You may obtain a copy of the License at
  7. *
  8. * http://www.apache.org/licenses/LICENSE-2.0
  9. *
  10. * Unless required by applicable law or agreed to in writing, software
  11. * distributed under the License is distributed on an "AS IS" BASIS,
  12. * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  13. * See the License for the specific language governing permissions and
  14. * limitations under the License.
  15. */
  16. /*
  17. * File: ccronexpr.h
  18. * Author: alex
  19. *
  20. * Created on February 24, 2015, 9:35 AM
  21. */
  22. #ifndef CCRONEXPR_H
  23. #define CCRONEXPR_H
  24. #include "config.h"
  25. #if defined(__cplusplus) && !defined(CRON_COMPILE_AS_CXX)
  26. extern "C" {
  27. #endif
  28. #include <time.h>
  29. #include <stdint.h> /*added for use if uint*_t data types*/
  30. /**
  31. * Parsed cron expression
  32. */
  33. typedef struct {
  34. uint8_t seconds[8];
  35. uint8_t minutes[8];
  36. uint8_t hours[3];
  37. uint8_t days_of_week[1];
  38. uint8_t days_of_month[4];
  39. uint8_t months[2];
  40. } cron_expr;
  41. /**
  42. * uint8_t* replace char* for storing hit dates, set_bit and get_bit are used as handlers
  43. */
  44. static void cron_set_bit(uint8_t* rbyte, int idx)
  45. {
  46. uint8_t j = (uint8_t)(idx / 8);
  47. uint8_t k = (uint8_t)(idx % 8);
  48. rbyte[j] |= (1 << k);
  49. }
  50. static void cron_del_bit(uint8_t* rbyte, int idx)
  51. {
  52. uint8_t j = (uint8_t)(idx / 8);
  53. uint8_t k = (uint8_t)(idx % 8);
  54. rbyte[j] &= ~(1 << k);
  55. }
  56. static uint8_t cron_get_bit(uint8_t* rbyte, int idx)
  57. {
  58. uint8_t j = (uint8_t)(idx / 8);
  59. uint8_t k = (uint8_t)(idx % 8);
  60. if (rbyte[j] & (1 << k)) {
  61. return 1;
  62. } else {
  63. return 0;
  64. }
  65. }
  66. /**
  67. * Parses specified cron expression.
  68. *
  69. * @param expression cron expression as nul-terminated string,
  70. * should be no longer that 256 bytes
  71. * @param pointer to cron expression structure, it's client code responsibility
  72. * to free/destroy it afterwards
  73. * @param error output error message, will be set to string literal
  74. * error message in case of error. Will be set to NULL on success.
  75. * The error message should NOT be freed by client.
  76. */
  77. TOOLKIT_API void cron_parse_expr(const char* expression, cron_expr* target, const char** error);
  78. /**
  79. * Uses the specified expression to calculate the next 'fire' date after
  80. * the specified date. All dates are processed as UTC (GMT) dates
  81. * without timezones information. To use local dates (current system timezone)
  82. * instead of GMT compile with '-DCRON_USE_LOCAL_TIME'
  83. *
  84. * @param expr parsed cron expression to use in next date calculation
  85. * @param date start date to start calculation from
  86. * @return next 'fire' date in case of success, '((time_t) -1)' in case of error.
  87. */
  88. TOOLKIT_API time_t cron_next(cron_expr* expr, time_t date);
  89. /**
  90. * Uses the specified expression to calculate the previous 'fire' date after
  91. * the specified date. All dates are processed as UTC (GMT) dates
  92. * without timezones information. To use local dates (current system timezone)
  93. * instead of GMT compile with '-DCRON_USE_LOCAL_TIME'
  94. *
  95. * @param expr parsed cron expression to use in previous date calculation
  96. * @param date start date to start calculation from
  97. * @return previous 'fire' date in case of success, '((time_t) -1)' in case of error.
  98. */
  99. TOOLKIT_API time_t cron_prev(cron_expr* expr, time_t date);
  100. #if defined(__cplusplus) && !defined(CRON_COMPILE_AS_CXX)
  101. } /* extern "C"*/
  102. #endif
  103. #endif /* CCRONEXPR_H */