2012-08-05 00:00:18 +02:00
|
|
|
/****************************************************************************
|
2014-08-08 22:43:02 +02:00
|
|
|
* sched/clock/clock_dow.c
|
2012-08-05 00:00:18 +02:00
|
|
|
*
|
2024-09-11 13:45:11 +02:00
|
|
|
* SPDX-License-Identifier: Apache-2.0
|
|
|
|
*
|
2021-02-08 16:33:58 +01:00
|
|
|
* Licensed to the Apache Software Foundation (ASF) under one or more
|
|
|
|
* contributor license agreements. See the NOTICE file distributed with
|
|
|
|
* this work for additional information regarding copyright ownership. The
|
|
|
|
* ASF licenses this file to you under the Apache License, Version 2.0 (the
|
|
|
|
* "License"); you may not use this file except in compliance with the
|
|
|
|
* License. You may obtain a copy of the License at
|
2012-08-05 00:00:18 +02:00
|
|
|
*
|
2021-02-08 16:33:58 +01:00
|
|
|
* http://www.apache.org/licenses/LICENSE-2.0
|
2012-08-05 00:00:18 +02:00
|
|
|
*
|
2021-02-08 16:33:58 +01:00
|
|
|
* Unless required by applicable law or agreed to in writing, software
|
|
|
|
* distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
|
|
|
|
* WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
|
|
|
|
* License for the specific language governing permissions and limitations
|
|
|
|
* under the License.
|
2012-08-05 00:00:18 +02:00
|
|
|
*
|
|
|
|
****************************************************************************/
|
|
|
|
|
|
|
|
/****************************************************************************
|
|
|
|
* Included Files
|
|
|
|
****************************************************************************/
|
|
|
|
|
|
|
|
#include <nuttx/config.h>
|
|
|
|
|
|
|
|
#include <stdint.h>
|
|
|
|
|
|
|
|
#include <nuttx/clock.h>
|
|
|
|
|
2014-08-08 22:43:02 +02:00
|
|
|
#include "clock/clock.h"
|
2012-08-05 00:00:18 +02:00
|
|
|
|
|
|
|
/****************************************************************************
|
|
|
|
* Private Data
|
|
|
|
****************************************************************************/
|
|
|
|
|
|
|
|
/* 23 * (month + 1) / 9, month = 0..11 */
|
|
|
|
|
2019-03-01 17:50:02 +01:00
|
|
|
static const uint8_t g_lookup[12] =
|
|
|
|
{
|
|
|
|
2, 5, 7, 10, 12, 15, 17, 20, 23, 25, 28, 30
|
|
|
|
};
|
2012-08-05 00:00:18 +02:00
|
|
|
|
|
|
|
/****************************************************************************
|
|
|
|
* Public Functions
|
|
|
|
****************************************************************************/
|
|
|
|
|
|
|
|
/****************************************************************************
|
|
|
|
* Name: clock_dow
|
|
|
|
*
|
|
|
|
* Description:
|
|
|
|
* Calculate the day of week (DOW) from they year month and day. Based on
|
2021-02-25 13:48:46 +01:00
|
|
|
* an algorithm published in 1990 by Michael Keith and Tom Craver with some
|
2012-08-05 00:00:18 +02:00
|
|
|
* tweaks to handle months in the range 0-11.
|
|
|
|
*
|
2018-03-13 16:52:27 +01:00
|
|
|
* Input Parameters:
|
2012-08-05 00:00:18 +02:00
|
|
|
* year - year (e.g., 1988)
|
|
|
|
* month - 0 through 11
|
|
|
|
* day - 1 through 31
|
|
|
|
*
|
2018-02-01 17:00:02 +01:00
|
|
|
* Returned Value:
|
2012-08-05 00:00:18 +02:00
|
|
|
* The day of the week as days since Sunday: 0 = Sunday, 1 = Monday, etc.
|
|
|
|
*
|
|
|
|
* Assumptions:
|
|
|
|
*
|
|
|
|
****************************************************************************/
|
|
|
|
|
|
|
|
int clock_dow(int year, int month, int day)
|
|
|
|
{
|
|
|
|
day += month < 2 ? year-- : year - 2;
|
2021-02-08 18:46:31 +01:00
|
|
|
return ((int)g_lookup[month] + day + 4 + year / 4 -
|
|
|
|
year / 100 + year / 400) % 7;
|
2012-08-05 00:00:18 +02:00
|
|
|
}
|