📄 datetimelib.c
字号:
#include "Kernel.h"
const UINT8 DaysOfMonth[12] = {31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31};
const UINT16 monthDate[] = {31,59,90,120,151,181,212,243,273,304,334,365};
/****************************************************************************/
/* FUNCTION: TimCheckLeapYear */
/* DESCRIPTION:judge leap year */
/* INPUTS: 年份 */
/* OUTPUTS: NONE */
/* RETURN: 判断结果 */
/****************************************************************************/
/* NAME DATE REMARKS */
/* ========== ============ ==============================================*/
/* 谢永良 2002-09-26 创建 */
/****************************************************************************/
BOOL TimCheckLeapYear(UINT16 year)
{
if(year%4 != 0)
{
return FALSE;
}
else
{
if(year%100 == 0)
{
if(year%400 != 0)
{
return FALSE;
}
}
}
return TRUE;
}
/****************************************************************************/
/* FUNCTION: TimDaysInMonth */
/* DESCRIPTION:get total days of the month and the year */
/* INPUTS: month, day */
/* OUTPUTS: NONE */
/* RETURN: total days */
/****************************************************************************/
/* NAME DATE REMARKS */
/* ========== ============ ==============================================*/
/* 谢永良 2002-09-26 创建 */
/****************************************************************************/
UINT8 TimDaysInMonth(UINT8 month, UINT16 year)
{
UINT8 days;
if(month == 2 && TimCheckLeapYear(year) == TRUE){
days = DaysOfMonth[month - 1] + 1;
}else{
days = DaysOfMonth[month - 1];
}
return days;
}
/****************************************************************************/
/* FUNCTION: _TimTimeCompare */
/* DESCRIPTION:比较两时间的大小 */
/* INPUTS: pTime1, pTime2 */
/* OUTPUTS: NONE */
/* RETURN: pTime1 > pTime2 ---- 1 */
/* pTime1 = pTime2 ---- 0 */
/* pTime1 < pTime2 ---- -1 */
/****************************************************************************/
/* NAME DATE REMARKS */
/* ========== ============ ==============================================*/
/* 谢永良 2002-09-26 创建 */
/****************************************************************************/
INT8 TimTimeCompare(TimeType *pTime1, TimeType *pTime2)
{
TimeType time1;
TimeType time2;
memcpy((UINT8*)&time1, (UINT8*)pTime1, sizeof(TimeType));
memcpy((UINT8*)&time2, (UINT8*)pTime2, sizeof(TimeType));
if(time1.byHour > time2.byHour){ /* Year compare */
return 1;
}
else if(time1.byHour < time2.byHour){
return -1;
}else{
if(time1.byMinute > time2.byMinute){ /* Month compare */
return 1;
}
else if(time1.byMinute < time2.byMinute){
return -1;
}else{
if(time1.bySecond > time2.bySecond){ /* Day compare */
return 1;
}
else if(time1.bySecond < time2.bySecond){
return -1;
}else{
return 0;
}
}
}
}
/****************************************************************************/
/* FUNCTION: TimDateCompare */
/* DESCRIPTION:比较两日期的大小 */
/* INPUTS: pDate1, pDate2 */
/* OUTPUTS: NONE */
/* RETURN: pDate1 > pDate2 ---- 1 */
/* pDate1 = pDate2 ---- 0 */
/* pDate1 < pDate2 ---- -1 */
/****************************************************************************/
/* NAME DATE REMARKS */
/* ========== ============ ==============================================*/
/* 谢永良 2002-09-26 创建 */
/****************************************************************************/
INT8 TimDateCompare(DateType *pDate1, DateType *pDate2)
{
DateType date1;
DateType date2;
memcpy((UINT8*)&date1, (UINT8*)pDate1, sizeof(DateType));
memcpy((UINT8*)&date2, (UINT8*)pDate2, sizeof(DateType));
if(date1.wYear > date2.wYear){ /* Year compare */
return 1;
}
else if(date1.wYear < date2.wYear){
return -1;
}else{
if(date1.byMonth > date2.byMonth){ /* Month compare */
return 1;
}
else if(date1.byMonth < date2.byMonth){
return -1;
}else{
if(date1.byDay > date2.byDay){ /* Day compare */
return 1;
}
else if(date1.byDay < date2.byDay){
return -1;
}else{
return 0;
}
}
}
}
/****************************************************************************/
/* FUNCTION: _TimDateTimeCompare */
/* DESCRIPTION:比较两日期时间的大小 */
/* INPUTS: pDate1,pTime1, pDate2,pTime2 */
/* OUTPUTS: NONE */
/* RETURN: (pDate1>pDate2) || (pDate1 == pDate2 && pTime1 > pTime2)-- 1*/
/* (pTime1 == pTime2) && (pDate1 == pDate2) -- 0*/
/* else -1*/
/****************************************************************************/
/* NAME DATE REMARKS */
/* ========== ============ ==============================================*/
/* gaolinhui 2006-02-07 创建 */
/****************************************************************************/
INT8 TimDateTimeCompare(DateType *pDate1, TimeType *pTime1, DateType *pDate2, TimeType *pTime2)
{
INT8 byCmp;
byCmp = TimDateCompare(pDate1,pDate2);
if(byCmp > 0 )
{
return 1;
}
else if(byCmp < 0)
{
return -1;
}
else
{
byCmp = TimTimeCompare(pTime1,pTime2);
if(byCmp > 0 )
{
return 1;
}
else if(byCmp < 0)
{
return -1;
}
else
{
return 0;
}
}
}
/****************************************************************************/
/* FUNCTION: TimDateToDays */
/* DESCRIPTION:get total days from 1904.01.01 to DateType */
/* INPUTS: pDate */
/* OUTPUTS: NONE */
/* RETURN: total days */
/****************************************************************************/
/* NAME DATE REMARKS */
/* ========== ============ ==============================================*/
/* 谢永良 2002-09-26 创建 */
/****************************************************************************/
UINT16 TimDateToDays(DateType *pDate)
{
DateType date;
UINT16 dateYear,recyear,yuyear;
UINT8 dateMonth;
UINT8 dateDay;
UINT16 days;
memcpy((UINT8*)&date, (UINT8*)pDate, sizeof(DateType));
dateDay = date.byDay;
dateMonth = date.byMonth;
dateYear = date.wYear;
recyear=dateYear-STARTYEAR;
days=recyear*365+recyear/4;
yuyear=recyear%4;
if (yuyear>2)
days++;
if(dateMonth > 1){
days += monthDate[dateMonth - 2];
if(dateMonth > 2 && TimCheckLeapYear(dateYear) == TRUE){
days ++;
}
}
days += (dateDay - STARTDAY);
return days;
}
/****************************************************************************/
/* FUNCTION: TimDaysToDate */
/* DESCRIPTION:days invert into from 1904.01.01 to DateType */
/* INPUTS: days */
/* OUTPUTS: DateType *pDate */
/* RETURN: NONE */
/****************************************************************************/
/* NAME DATE REMARKS */
/* ========== ============ ==============================================*/
/* 谢永良 2002-09-26 创建 */
/****************************************************************************/
VOID TimDaysToDate(UINT16 days, DateType *pDate)
{
DateType date;
UINT16 recday,year;
UINT8 counter;
recday=days%1461;
year=STARTYEAR+days/1461*4;
if (recday>=1096){
year+=3;
recday-=1096;
}
else if(recday>=730){
year+=2;
recday-=730;
}
else{
year+=(recday/365);
recday=recday%365;
}
for(counter = 0; recday >= DaysOfMonth[counter]; counter++){
if(counter == 1 && TimCheckLeapYear(year) == TRUE){
if(recday >= DaysOfMonth[counter] + 1){
recday -= (DaysOfMonth[counter] + 1);
}else{
break;
}
}else{
recday -= DaysOfMonth[counter];
}
}
date.wYear=year;
date.byMonth = STARTMONTH + counter;
date.byDay = STARTDAY + recday;
memcpy((UINT8*)pDate, (UINT8*)&date, sizeof(DateType));
}
/****************************************************************************/
/* FUNCTION: TimDateAdjust */
/* DESCRIPTION:根据给出的日期和调节值来调节日期 */
/* INPUTS: pDate,调节值 */
/* OUTPUTS: pDate */
/* RETURN: NONE */
/****************************************************************************/
/* NAME DATE REMARKS */
/* ========== ============ ==============================================*/
/* 谢永良 2002-09-26 创建 */
/****************************************************************************/
VOID TimDateAdjust(DateType *pDate, INT16 adjustment)
{
UINT16 days;
days = TimDateToDays(pDate) + adjustment; /* DateType ---> days */
TimDaysToDate(days, pDate); /* days ---> DateType */
}
/****************************************************************************/
/* FUNCTION: TimDayOfWeek */
/* DESCRIPTION:get week information from 1904.01.01 to DateTimeType */
/* INPUTS: month, day, year */
/* OUTPUTS: NONE */
/* RETURN: 星期几 */
/****************************************************************************/
/* NAME DATE REMARKS */
/* ========== ============ ==============================================*/
/* 谢永良 2002-09-26 创建 */
/****************************************************************************/
UINT8 TimDayOfWeek(UINT8 month, UINT8 day, UINT16 year)
{
DateType date;
UINT16 days;
UINT8 week;
date.wYear = year;
date.byMonth = month;
date.byDay = day;
days = TimDateToDays(&date);
week = (days + STARTWEEKDAY) % 7;
return week;
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -