Statistics
| Branch: | Tag: | Revision:

amiro-os / core / src / aos_time.c @ 8376530c

History | View | Annotate | Download (1.826 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
/**
20
 * @file    aos_time.c
21
 * @brief   Time related function implementations.
22
 *
23
 * @addtogroup aos_time
24
 * @{
25
 */
26

    
27
#include <aos_time.h>
28

    
29
#include <aos_debug.h>
30
#include <aos_system.h>
31

    
32
/**
33
 * @brief   Calculates the day of week from a specified date.
34
 *
35
 * @param[in] day     The day of the date.
36
 * @param[in] month   The month of the daste.
37
 * @param[in] year    The year of the date.
38
 *
39
 * @return    The day of week depending on the date represented in ISO format (1 = Monday to 7 = Sunday).
40
 */
41
uint8_t aosTimeDayOfWeekFromDate(const uint16_t day, const uint8_t month, const uint16_t year)
42
{
43
  aosDbgCheck(day >= 1 && day <= 31);
44
  aosDbgCheck(month >= 1 && month <= 12);
45

    
46
  /*
47
   * Implementation of Zeller's congruence for Gregorian calender.
48
   * See https://en.wikipedia.org/wiki/Zeller%27s_congruence for details.
49
   */
50
  const uint32_t q = day;
51
  const uint32_t m = (month >= 3) ? month : month + 12;
52
  const uint32_t K = year % 100;
53
  const uint32_t J = year / 100;
54

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

    
57
  return ((h + 5) % 7) + 1;
58
}
59

    
60
/** @} */