calendar.c 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492
  1. /**
  2. * \file
  3. *
  4. * \brief Calendar service.
  5. *
  6. * Copyright (c) 2014-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. #include "compiler.h"
  47. #include "calendar.h"
  48. #include <stdio.h>
  49. //! Unix epoch year
  50. #define EPOCH_YEAR 1970
  51. //! Number of seconds in a day
  52. #define SECS_PER_DAY 86400UL
  53. //! Number of seconds in an hour
  54. #define SECS_PER_HOUR 3600UL
  55. //! Number of seconds in a minute
  56. #define SECS_PER_MINUTE 60UL
  57. //! Number of days in a specified month. Index 1 for leap year, else 0.
  58. const uint8_t month[2][12] = {
  59. { 31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31 },
  60. { 31, 29, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31 }
  61. };
  62. /**
  63. * \internal
  64. * \brief Check if a year is a leap year
  65. *
  66. * Returns true or false depending if the input year is a leap year or not.
  67. *
  68. * \param year the year in format YYYY to check for leap year or not
  69. *
  70. * \retval true if the year is a leap year
  71. * \retval false if the year is not a leap year
  72. */
  73. static bool calendar_leapyear(uint16_t year)
  74. {
  75. if(!((year) % 4) && (((year) % 100) || !((year) % 400))) {
  76. return true;
  77. } else {
  78. return false;
  79. }
  80. }
  81. /**
  82. * \internal
  83. * \brief Find number of days in a year
  84. *
  85. * Returns the number of days in a year, depending if the input is leap year
  86. * or not.
  87. *
  88. * \param year the year in format YYYY to check number of days
  89. *
  90. * \retval 366 if the year is a leap year
  91. * \retval 365 if the year is not a leap year
  92. */
  93. static uint16_t calendar_yearsize(uint16_t year)
  94. {
  95. if (calendar_leapyear(year)) {
  96. return 366;
  97. } else {
  98. return 365;
  99. }
  100. }
  101. /**
  102. * \internal
  103. * \brief Add a year to a date
  104. *
  105. * Adds one year to specified date as long as the current year is before 2105.
  106. *
  107. * \param *date the date to add a year to
  108. *
  109. */
  110. static void calendar_add_year_to_date(struct calendar_date *date)
  111. {
  112. if (date->year < 2105) {
  113. date->year++;
  114. }
  115. }
  116. /**
  117. * \internal
  118. * \brief Add a month to a date
  119. *
  120. * Adds one month to specified date. If month is december, increment year.
  121. *
  122. * \param *date the date to add a month to
  123. *
  124. */
  125. static void calendar_add_month_to_date(struct calendar_date *date)
  126. {
  127. uint8_t months = date->month;
  128. months++;
  129. if (months == 12){
  130. months = 0;
  131. calendar_add_year_to_date(date);
  132. }
  133. date->month = months;
  134. }
  135. /**
  136. * \internal
  137. * \brief Add a day to a date
  138. *
  139. * Adds one day to specified date. If day is the last of the month, increment
  140. * month.
  141. *
  142. * \param *date the date to add a day to
  143. *
  144. */
  145. static void calendar_add_day_to_date(struct calendar_date *date)
  146. {
  147. uint8_t dates = date->date;
  148. uint8_t months = date->month;
  149. uint8_t year = date->year;
  150. dates++;
  151. if (dates == month[calendar_leapyear(year)][months]) {
  152. dates = 0;
  153. calendar_add_month_to_date(date);
  154. }
  155. date->dayofweek++;
  156. if (date->dayofweek == 7) {
  157. date->dayofweek = 0;
  158. }
  159. date->date = dates;
  160. }
  161. /**
  162. * \internal
  163. * \brief Add an hour to a date
  164. *
  165. * Adds one hour to specified date. If hour is 23, increment day.
  166. *
  167. * \param *date the date to add an hour to
  168. *
  169. */
  170. static void calendar_add_hour_to_date(struct calendar_date *date)
  171. {
  172. int8_t hour = date->hour;
  173. hour++;
  174. if (hour == 24){
  175. hour = 0;
  176. calendar_add_day_to_date(date);
  177. }
  178. date->hour = hour;
  179. }
  180. /**
  181. * \internal
  182. * \brief Add a minute to a date
  183. *
  184. * Adds one minute to specified date. If minute is 59, increment hour.
  185. *
  186. * \param *date the date to add a minute to
  187. *
  188. */
  189. static void calendar_add_minute_to_date(struct calendar_date *date)
  190. {
  191. uint8_t minute = date->minute;
  192. minute++;
  193. if (minute == 60){
  194. minute = 0;
  195. calendar_add_hour_to_date(date);
  196. }
  197. date->minute = minute;
  198. }
  199. /**
  200. * \brief Check if a date is valid
  201. *
  202. * Checks that number of seconds, minutes and hours is a valid value.
  203. * Checks that number of days does not exceed number of days in current month.
  204. * Checks that number of months is a valid value, and checks that year is
  205. * between 1970 (epoch year) and 2106 (overflow year).
  206. *
  207. * \param *date the date to check if valid
  208. *
  209. */
  210. bool calendar_is_date_valid(struct calendar_date *date)
  211. {
  212. // Make sure time is valid
  213. if ((date->second >= 60) || (date->minute >= 60) || (date->hour >= 24)) {
  214. return false;
  215. }
  216. // Make sure month and date is valid
  217. if ((date->month >= 12) || (date->date >=31)) {
  218. return false;
  219. }
  220. // Make sure days in month are not more than it should be
  221. if (date->date >= month[calendar_leapyear(date->year)][date->month]) {
  222. return false;
  223. }
  224. // Make sure year is not earlier than 1970 and before 2106
  225. if ((date->year < EPOCH_YEAR) || (date->year >= 2106)) {
  226. return false;
  227. } else {
  228. return true;
  229. }
  230. }
  231. /**
  232. * \brief Convert a UNIX timestamp to a date
  233. *
  234. * Finds the corresponding date and time for a UNIX timestamp.
  235. *
  236. * \param timestamp UNIX timestamp
  237. * \param date_out Date to store result
  238. *
  239. */
  240. void calendar_timestamp_to_date(uint32_t timestamp,
  241. struct calendar_date *date_out)
  242. {
  243. uint32_t day_number;
  244. uint32_t day_clock;
  245. date_out->year = EPOCH_YEAR;
  246. date_out->month = 0;
  247. day_clock = timestamp % SECS_PER_DAY;
  248. day_number = timestamp / SECS_PER_DAY;
  249. date_out->second = day_clock % SECS_PER_MINUTE;
  250. date_out->minute = (day_clock % SECS_PER_HOUR) / SECS_PER_MINUTE;
  251. date_out->hour = day_clock / SECS_PER_HOUR;
  252. date_out->dayofweek = (day_number + 4) % 7;
  253. while (day_number >= calendar_yearsize(date_out->year)) {
  254. day_number -= calendar_yearsize(date_out->year);
  255. date_out->year++;
  256. }
  257. while (day_number >=
  258. month[calendar_leapyear(date_out->year)][date_out->month]) {
  259. day_number -= month[calendar_leapyear(date_out->year)][date_out->month];
  260. date_out->month++;
  261. }
  262. date_out->date = day_number;
  263. }
  264. /**
  265. * \brief Convert a UNIX timestamp to a date in a given time zone.
  266. *
  267. * The provided UNIX timestamp is converted to the corresponding time in the
  268. * provided time zone.
  269. *
  270. * \param timestamp UNIX timestamp
  271. * \param hour Hour offset from UTC (UTC-12 to UTC+14)
  272. * \param min Minute offset from UTC (0, 15, 30, 45)
  273. * \param date_out Date to store result
  274. *
  275. */
  276. void calendar_timestamp_to_date_tz(uint32_t timestamp, int8_t hour,
  277. uint8_t min, struct calendar_date *date_out)
  278. {
  279. // Multiply timezone offset by seconds, and add to timestamp
  280. if (hour >= 0) {
  281. calendar_timestamp_to_date((timestamp + (SECS_PER_HOUR * hour) +
  282. (SECS_PER_MINUTE * min)), date_out);
  283. } else {
  284. calendar_timestamp_to_date((timestamp + (SECS_PER_HOUR * hour) -
  285. (SECS_PER_MINUTE * min)), date_out);
  286. }
  287. }
  288. /**
  289. * \brief Convert a date to a UNIX timestamp.
  290. *
  291. * \note
  292. * If date is invalid, timestamp 0 will be returned.
  293. *
  294. * \param date Date
  295. *
  296. * \return The corresponding UNIX timestamp
  297. * \retval 0 if date is not valid
  298. */
  299. uint32_t calendar_date_to_timestamp(struct calendar_date *date)
  300. {
  301. // Make sure date is valid
  302. if (!calendar_is_date_valid(date))
  303. return 0;
  304. uint32_t timestamp = 0;
  305. uint8_t date_month;
  306. uint16_t date_year;
  307. date_month = date->month;
  308. date_year = date->year;
  309. // Add number of seconds elapsed in current month
  310. timestamp += (date->date * SECS_PER_DAY) + (date->hour * SECS_PER_HOUR) +
  311. (date->minute * SECS_PER_MINUTE) + date->second;
  312. while (date_month != 0) {
  313. date_month--;
  314. // Add number of seconds in months of current year
  315. timestamp += month[calendar_leapyear(date_year)][date_month]
  316. * SECS_PER_DAY;
  317. }
  318. while (date_year > EPOCH_YEAR) {
  319. date_year--;
  320. // Add number of seconds in all years since epoch year
  321. timestamp += calendar_yearsize(date_year) * SECS_PER_DAY;
  322. }
  323. return timestamp;
  324. }
  325. /**
  326. * \brief This function converts a date in a given time zone to a UNIX
  327. * timestamp
  328. * \note
  329. * If date is invalid, timestamp 0 will be returned.
  330. *
  331. * \param date Date
  332. * \param hour Hour offset from UTC (UTC-12 to UTC+14)
  333. * \param min Minute offset from UTC (0, 15, 30, 45)
  334. *
  335. * \return The corresponding UNIX timestamp
  336. * \retval 0 if date is not valid
  337. */
  338. uint32_t calendar_date_to_timestamp_tz(struct calendar_date *date, int8_t hour,
  339. uint8_t min)
  340. {
  341. uint32_t timestamp = calendar_date_to_timestamp(date);
  342. if (timestamp == 0) {
  343. return 0;
  344. } else {
  345. // Subtract the seconds of offset in time zone offset from timestamp
  346. if (hour >= 0) {
  347. return (timestamp - (SECS_PER_HOUR * hour + SECS_PER_MINUTE *
  348. min));
  349. } else {
  350. return (timestamp - (SECS_PER_HOUR * hour - SECS_PER_MINUTE *
  351. min));
  352. }
  353. }
  354. }
  355. /**
  356. * \brief This function calculates the time difference between to dates.
  357. *
  358. * The time difference is provided as number of years, months, days, hours,
  359. * minutes and seconds between the dates. If end date is before start date,
  360. * the dates are switched.
  361. *
  362. * \param date_end The end date
  363. * \param date_start The start date
  364. * \param date_out The time between the dates
  365. *
  366. */
  367. void calendar_time_between_dates(struct calendar_date *date_end,
  368. struct calendar_date *date_start, struct calendar_date *date_out)
  369. {
  370. uint32_t timestamp_start;
  371. uint32_t timestamp_end;
  372. struct calendar_date *temp;
  373. timestamp_start = calendar_date_to_timestamp(date_start);
  374. timestamp_end = calendar_date_to_timestamp(date_end);
  375. // Switch dates if date_end is before date_start
  376. if (timestamp_end < timestamp_start) {
  377. temp = date_end;
  378. date_end = date_start;
  379. date_start = temp;
  380. }
  381. // Calculate number of years
  382. date_out->year = date_end->year - date_start->year;
  383. // Check if months wrap around new year
  384. if (date_end->month - date_start->month < 0 ) {
  385. date_end->month += 12;
  386. if (date_out->year != 0) {
  387. date_out->year--;
  388. }
  389. }
  390. // Calculate number of months
  391. date_out->month = date_end->month - date_start->month;
  392. // Check if dates wrap around month
  393. if(date_end->date - date_start->date < 0) {
  394. // Add number of days in last month to get number of days correct
  395. date_end->date +=
  396. month[calendar_leapyear(date_end->year)][date_end->month-1];
  397. if (date_out->month != 0) {
  398. date_out->month--;
  399. }
  400. }
  401. // Calculate number of days
  402. date_out->date = date_end->date - date_start->date;
  403. // Check if hours wrap around midnight
  404. if (date_end->hour - date_start->hour < 0) {
  405. date_end->hour += 24;
  406. if (date_out->date != 0) {
  407. date_out->date--;
  408. }
  409. }
  410. // Calculate number of hours
  411. date_out->hour = date_end->hour - date_start->hour;
  412. // Check if minutes wrap around hour
  413. if (date_end->minute - date_start->minute < 0) {
  414. date_end->minute += 60;
  415. if (date_out->hour != 0) {
  416. date_out->hour--;
  417. }
  418. }
  419. // Calculate number of minutes
  420. date_out->minute = date_end->minute - date_start->minute;
  421. // Check if seconds wrap around minute
  422. if (date_end->second - date_start->second < 0) {
  423. date_end->second += 60;
  424. if (date_out->minute != 0) {
  425. date_out->minute--;
  426. }
  427. }
  428. // Calculate number of seconds
  429. date_out->second = date_end->second - date_start->second;
  430. }
  431. /**
  432. * \brief Increments a date with one second.
  433. *
  434. * This function will add one second to specified date. If second is 59,
  435. * it will increment minute.
  436. *
  437. * \param date The date to add a second to
  438. *
  439. */
  440. void calendar_add_second_to_date(struct calendar_date *date)
  441. {
  442. // Check if input date is valid
  443. Assert(calendar_is_date_valid(date));
  444. if (++date->second == 60) {
  445. date->second = 0;
  446. calendar_add_minute_to_date(date);
  447. }
  448. }