calendar.h 6.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205
  1. /**
  2. * \file
  3. *
  4. * \brief Calendar.
  5. *
  6. * Copyright (c) 2011-2015 Atmel Corporation. All rights reserved.
  7. *
  8. * \asf_license_start
  9. *
  10. * \page License
  11. *
  12. * Redistribution and use in source and binary forms, with or without
  13. * modification, are permitted provided that the following conditions are met:
  14. *
  15. * 1. Redistributions of source code must retain the above copyright notice,
  16. * this list of conditions and the following disclaimer.
  17. *
  18. * 2. Redistributions in binary form must reproduce the above copyright notice,
  19. * this list of conditions and the following disclaimer in the documentation
  20. * and/or other materials provided with the distribution.
  21. *
  22. * 3. The name of Atmel may not be used to endorse or promote products derived
  23. * from this software without specific prior written permission.
  24. *
  25. * 4. This software may only be redistributed and used in connection with an
  26. * Atmel microcontroller product.
  27. *
  28. * THIS SOFTWARE IS PROVIDED BY ATMEL "AS IS" AND ANY EXPRESS OR IMPLIED
  29. * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
  30. * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NON-INFRINGEMENT ARE
  31. * EXPRESSLY AND SPECIFICALLY DISCLAIMED. IN NO EVENT SHALL ATMEL BE LIABLE FOR
  32. * ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
  33. * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
  34. * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
  35. * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
  36. * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN
  37. * ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
  38. * POSSIBILITY OF SUCH DAMAGE.
  39. *
  40. * \asf_license_stop
  41. *
  42. */
  43. /*
  44. * Support and FAQ: visit <a href="http://www.atmel.com/design-support/">Atmel Support</a>
  45. */
  46. #ifndef _CALENDAR_H_INCLUDED_
  47. #define _CALENDAR_H_INCLUDED_
  48. #ifdef __cplusplus
  49. extern "C" {
  50. #endif
  51. #include <stdint.h>
  52. #include <stdbool.h>
  53. /**
  54. *
  55. * \defgroup calendar_group Calendar service
  56. *
  57. * See \ref calendar_quickstart.
  58. *
  59. * This is the common API for a calendar service.
  60. * It provides functionality to convert UNIX timestamps to dates and back. It
  61. * also provides functionality for calculating the difference between dates and
  62. * converting timestamps to dates with different time zones and back.
  63. * \note
  64. * This module is a pure software module, and does not interface
  65. * hardware calendar peripherals (e.g. the AST on UC3L)
  66. *
  67. * @{
  68. */
  69. //! Calendar structure to hold a date
  70. struct calendar_date {
  71. uint8_t second; //!< 0-59
  72. uint8_t minute; //!< 0-59
  73. uint8_t hour; //!< 0-23
  74. uint8_t date; //!< 0-30 \note First day of month is 0, not 1.
  75. uint8_t month; //!< 0 January - 11 December
  76. uint16_t year; //!< 1970-2105
  77. uint8_t dayofweek; //!< 0 Sunday - 6 Saturday
  78. };
  79. bool calendar_is_date_valid(struct calendar_date *date);
  80. void calendar_timestamp_to_date(uint32_t timestamp, struct calendar_date
  81. *date_out);
  82. void calendar_timestamp_to_date_tz(uint32_t timestamp, int8_t hour,
  83. uint8_t min, struct calendar_date *date_out);
  84. uint32_t calendar_date_to_timestamp(struct calendar_date *date);
  85. uint32_t calendar_date_to_timestamp_tz(struct calendar_date *date, int8_t hour,
  86. uint8_t min);
  87. void calendar_time_between_dates(struct calendar_date *date_end,
  88. struct calendar_date *date_start, struct calendar_date *date_out);
  89. void calendar_add_second_to_date(struct calendar_date *date);
  90. /** @} */
  91. #ifdef __cplusplus
  92. }
  93. #endif
  94. /**
  95. * \page calendar_quickstart Quick start guide for Calendar service
  96. *
  97. * This is the quick start guide for the \ref calendar_group, with
  98. * step-by-step instructions on how to configure and use the driver in a
  99. * selection of use cases.
  100. *
  101. * The use cases contain several code fragments. The code fragments in the
  102. * steps for setup can be copied into a custom initialization function, while
  103. * the steps for usage can be copied into, e.g., the main application function.
  104. *
  105. * \section calendar_basic_use_case Basic use case
  106. * \section calendar_use_cases Calendar use cases
  107. * - \ref calendar_basic_use_case
  108. * - \subpage calendar_use_case_1
  109. *
  110. * \section calendar_basic_use_case Basic use case - Calculate timestamp
  111. *
  112. * The use case will let the user calculate the corresponding timestamp to a
  113. * date
  114. *
  115. * \section calendar_basic_use_case_setup Setup steps
  116. *
  117. * \subsection calendar_basic_use_case_setup_prereq Prerequisites
  118. * For the code of this use case to work, the following must
  119. * be added to the project:
  120. * -# A date struct with a date:
  121. * \code
  122. struct calendar_date date = {
  123. .second = 12,
  124. .minute = 1,
  125. .hour = 22,
  126. .date = 8,
  127. .month = 2,
  128. .year = 1985
  129. };
  130. \endcode
  131. *
  132. * \subsection calendar_basic_use_case_setup_code Example code
  133. * No setup code is needed, the service is ready for use as-is.
  134. *
  135. * \section calendar_basic_use_case_usage Usage steps
  136. *
  137. * \subsection calendar_basic_use_case_usage_code Example code
  138. * Add to, e.g. the main loop in the application C-file:
  139. * \code uint32_t timestamp = calendar_date_to_timestamp(&date); \endcode
  140. *
  141. * \subsection calendar_basic_use_case_usage_flow Workflow
  142. * -# Convert date to timestamp:
  143. * - \code uint32_t timestamp = calendar_date_to_timestamp(&date); \endcode
  144. */
  145. /**
  146. * \page calendar_use_case_1 Calculate time between dates
  147. *
  148. * The use case will let the user calculate the time between two dates, by
  149. * first calculating the dates from two timestamps.
  150. *
  151. * \section calendar_use_case_1_setup Setup steps
  152. *
  153. * \subsection calendar_use_case_1_setup_prereq Prerequisites
  154. * For the code of this use case to work, the following must
  155. * be added to the project:
  156. * -# Three date structs:
  157. * \code
  158. struct calendar_date result;
  159. struct calendar_date end_date;
  160. struct calendar_date start_date;
  161. \endcode
  162. * -# Two timestamps:
  163. * \code
  164. uint32_t end_timestamp = 1309174659;
  165. uint32_t start_timestamp = 123456789;
  166. \endcode
  167. *
  168. * \subsection calendar_use_case_1_setup_code Example code
  169. * No setup code is needed, the service is ready for use as-is.
  170. *
  171. * \section calendar_use_case_1_usage Usage steps
  172. *
  173. * \subsection calendar_use_case_1_usage_code Example code
  174. * Add to, e.g. the main loop in the application C-file:
  175. * \code
  176. calendar_timestamp_to_date(end_timestamp, &end_date);
  177. calendar_timestamp_to_date(start_timestamp, &start_date);
  178. calendar_time_between_dates(&end_date, &start_date, &result);
  179. \endcode
  180. *
  181. * \subsection calendar_use_case_1_usage_flow Workflow
  182. * -# Convert the end timestamp to date:
  183. * - \code calendar_timestamp_to_date(end_timestamp, &end_date); \endcode
  184. * -# Convert the start timestamp to date:
  185. * - \code calendar_timestamp_to_date(start_timestamp, &start_date); \endcode
  186. * -# Calculate the time between the two dates:
  187. * - \code calendar_time_between_dates(&end_date, &start_date, &result);
  188. \endcode
  189. */
  190. #endif /* _CALENDAR_H_INCLUDED_ */