Statistics
| Branch: | Tag: | Revision:

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

History | View | Annotate | Download (3.25 KB)

1
/*
2
AMiRo-OS is an operating system designed for the Autonomous Mini Robot (AMiRo) platform.
3
Copyright (C) 2016..2019  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
/******************************************************************************/
30
/* LOCAL DEFINITIONS                                                          */
31
/******************************************************************************/
32

    
33
/******************************************************************************/
34
/* EXPORTED VARIABLES                                                         */
35
/******************************************************************************/
36

    
37
/******************************************************************************/
38
/* LOCAL TYPES                                                                */
39
/******************************************************************************/
40

    
41
/******************************************************************************/
42
/* LOCAL VARIABLES                                                            */
43
/******************************************************************************/
44

    
45
/******************************************************************************/
46
/* LOCAL FUNCTIONS                                                            */
47
/******************************************************************************/
48

    
49
/******************************************************************************/
50
/* EXPORTED FUNCTIONS                                                         */
51
/******************************************************************************/
52
#include <aos_debug.h>
53
#include <aos_system.h>
54

    
55
/**
56
 * @brief   Calculates the day of week from a specified date.
57
 *
58
 * @param[in] day     The day of the date.
59
 * @param[in] month   The month of the daste.
60
 * @param[in] year    The year of the date.
61
 *
62
 * @return    The day of week depending on the date represented in ISO format (1 = Monday to 7 = Sunday).
63
 */
64
uint8_t aosTimeDayOfWeekFromDate(const uint16_t day, const uint8_t month, const uint16_t year)
65
{
66
  aosDbgCheck(day >= 1 && day <= 31);
67
  aosDbgCheck(month >= 1 && month <= 12);
68

    
69
  /*
70
   * Implementation of Zeller's congruence for Gregorian calender.
71
   * See https://en.wikipedia.org/wiki/Zeller%27s_congruence for details.
72
   */
73
  const uint32_t q = day;
74
  const uint32_t m = (month >= 3) ? month : month + 12;
75
  const uint32_t K = year % 100;
76
  const uint32_t J = year / 100;
77

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

    
80
  return ((h + 5) % 7) + 1;
81
}
82

    
83
/** @} */