Statistics
| Branch: | Tag: | Revision:

amiro-os / os / core / src / aos_time.c @ 8399aeae

History | View | Annotate | Download (1.702 KB)

1
/*
2
AMiRo-OS is an operating system designed for the Autonomous Mini Robot (AMiRo) platform.
3
Copyright (C) 2016..2018  Thomas Schöpping et al.
4

5
This program is free software: you can redistribute it and/or modify
6
it under the terms of the GNU General Public License as published by
7
the Free Software Foundation, either version 3 of the License, or
8
(at your option) any later version.
9

10
This program is distributed in the hope that it will be useful,
11
but WITHOUT ANY WARRANTY; without even the implied warranty of
12
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
13
GNU General Public License for more details.
14

15
You should have received a copy of the GNU General Public License
16
along with this program.  If not, see <http://www.gnu.org/licenses/>.
17
*/
18

    
19
#include <aos_time.h>
20

    
21
#include <aos_debug.h>
22
#include <aos_system.h>
23

    
24
/**
25
 * @brief   Calculates the day of week from a specified date.
26
 *
27
 * @param[in] day     The day of the date.
28
 * @param[in] month   The month of the daste.
29
 * @param[in] year    The year of the date.
30
 *
31
 * @return    The day of week depending on the date represented in ISO format (1 = Monday to 7 = Sunday).
32
 */
33
uint8_t aosTimeDayOfWeekFromDate(const uint16_t day, const uint8_t month, const uint16_t year)
34
{
35
  aosDbgCheck(day >= 1 && day <= 31);
36
  aosDbgCheck(month >= 1 && month <= 12);
37

    
38
  /*
39
   * Implementation of Zeller's congruence for Gregorian calender.
40
   * See https://en.wikipedia.org/wiki/Zeller%27s_congruence for details.
41
   */
42
  const uint32_t q = day;
43
  const uint32_t m = (month >= 3) ? month : month + 12;
44
  const uint32_t K = year % 100;
45
  const uint32_t J = year / 100;
46

    
47
  const uint32_t h = (q + ((13 * (m + 1)) / 5) + K + (K / 4) + (J / 4) - (2 * J)) % 7;
48

    
49
  return ((h + 5) % 7) + 1;
50
}