📄 datetime.c
字号:
/*
*
* Copyright (C) 2003 Xiangbin Lee <honeycombs@sina.com> <honeycombs@263.net>
*
* This program is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation; either version 2 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program; if not, write to the Free Software Foundation.
*/
#include "datetime.h"
#ifndef EQ
#define EQ ==
#endif
void DT_CopyDateTime(DT_DATE_TIME *thisdat, DT_DATE_TIME nextdat)
{
thisdat->year=nextdat.year;
thisdat->month=nextdat.month;
thisdat->day=nextdat.day;
thisdat->hour=nextdat.hour;
thisdat->minute=nextdat.minute;
thisdat->second=nextdat.second;
}
////////////////////////////////////////
// check day
unsigned char DT_GetMaxDay(unsigned short year, unsigned char month)
{
unsigned char ifrun;
unsigned char reday;
ifrun= 0;
reday =0;
if(year<=0||month<=0||month>12)
return reday;
if( ((year%4 EQ 0)&&(year%100!=0))||(year%400 EQ 0))
ifrun=1;
if(month EQ 2)
{
if(ifrun)
{
reday = 29;
}
else
{
reday = 28;
}
}
else
{
switch(month)
{
case 1:
case 3:
case 5:
case 7:
case 8:
case 10:
case 12:
{
reday = 31;
}break;
case 4:
case 6:
case 9:
case 11:
{
reday = 30;
}break;
}
}
return reday;
}
unsigned char DT_CheckDateTime(DT_DATE_TIME thisdat)
{
unsigned char tday;
if(thisdat.month<=0||thisdat.month>12)
return 0;
tday = DT_GetMaxDay(thisdat.year, thisdat.month);
if(thisdat.day<=0||thisdat.day>tday)
return 0;
if(thisdat.hour>23)
return 0;
if(thisdat.minute>59)
return 0;
if(thisdat.second>59)
return 0;
return 1;
}
unsigned char DT_GetNextDay(DT_DATE_TIME *nextdat, DT_DATE_TIME thisdat)
{
unsigned char day ;
day = DT_GetMaxDay(thisdat.year,thisdat.month);
if(day<=0)
return 0;
if(thisdat.day>day&&thisdat.day<=0)
return 0;
else if(thisdat.day<day)
{
nextdat->year=thisdat.year;
nextdat->month=thisdat.month;
nextdat->day=thisdat.day+1;
return 1;
}
else if(thisdat.day>=day)
{
if(thisdat.month>=12)
{
nextdat->year=thisdat.year+1;
nextdat->month=1;
nextdat->day=1;
}
else
{
nextdat->year=thisdat.year;
nextdat->month=thisdat.month+1;
nextdat->day=1;
}
return 1;
}
return 0;
}
unsigned char DT_GetLastDay(DT_DATE_TIME *nextdat, DT_DATE_TIME thisdat)
{
unsigned char day ;
day = DT_GetMaxDay(thisdat.year,thisdat.month);
if(day<=0)
return 0;
if(thisdat.day>day&&thisdat.day<=0)
return 0;
else if(thisdat.day>1)
{
nextdat->year=thisdat.year;
nextdat->month=thisdat.month;
nextdat->day=thisdat.day-1;
if(nextdat->day<=0)
return 0;
else return 1;
}
else if(thisdat.day<=1)
{
if(thisdat.month>1)
{
nextdat->year=thisdat.year;
nextdat->month=thisdat.month-1;
nextdat->day=(unsigned char)DT_GetMaxDay(nextdat->year,nextdat->month);
}
else
{
nextdat->year=thisdat.year-1;
nextdat->month=12;
nextdat->day=(unsigned char)DT_GetMaxDay(nextdat->year,nextdat->month);
}
if(nextdat->day<=0)
return 0;
else return 1;
}
return 0;
}
unsigned short DT_NextYear(DT_DATE_TIME *nowDateTime)
{
unsigned char tmpday;
nowDateTime->year++;
tmpday=(unsigned char)DT_GetMaxDay(nowDateTime->year, nowDateTime->month);
if(nowDateTime->day>tmpday)
nowDateTime->day=tmpday;
return nowDateTime->year;
}
unsigned char DT_NextMonth(DT_DATE_TIME *nowDateTime)
{
unsigned char tmpday;
nowDateTime->month++;
if(nowDateTime->month>12)
{
nowDateTime->month=1;
nowDateTime->year++;
}
tmpday=(unsigned char)DT_GetMaxDay(nowDateTime->year, nowDateTime->month);
if(nowDateTime->day>tmpday)
nowDateTime->day=tmpday;
return nowDateTime->month;
}
unsigned char DT_NextDay(DT_DATE_TIME *nowDateTime)
{
DT_DATE_TIME tmpTimer;
DT_CopyDateTime(&tmpTimer, *nowDateTime);
DT_GetNextDay(nowDateTime, tmpTimer);
return nowDateTime->day;
}
unsigned char DT_NextHour(DT_DATE_TIME *nowDateTime)
{
if(nowDateTime->hour<23)
nowDateTime->hour++;
else
{
nowDateTime->hour=0;
DT_NextDay(nowDateTime);
}
return nowDateTime->hour;
}
unsigned char DT_NextMinute(DT_DATE_TIME *nowDateTime)
{
if(nowDateTime->minute<59)
nowDateTime->minute++;
else
{
nowDateTime->minute=0;
DT_NextHour(nowDateTime);
}
return nowDateTime->minute;
}
unsigned char DT_NextSecond(DT_DATE_TIME *nowDateTime)
{
if(nowDateTime->second<59)
nowDateTime->second++;
else
{
nowDateTime->second=0;
DT_NextMinute(nowDateTime);
}
return nowDateTime->second;
}
unsigned short DT_LastYear(DT_DATE_TIME *nowDateTime)
{
unsigned char tmpday;
if(nowDateTime->year>1)
nowDateTime->year--;
tmpday=(unsigned char)DT_GetMaxDay(nowDateTime->year, nowDateTime->month);
if(nowDateTime->day>tmpday)
nowDateTime->day=tmpday;
return nowDateTime->year;
}
unsigned char DT_LastMonth(DT_DATE_TIME *nowDateTime)
{
unsigned char tmpday;
if(nowDateTime->month>1)
nowDateTime->month--;
else
{
if(nowDateTime->year>1)
{
nowDateTime->month=12;
nowDateTime->year--;
}
}
tmpday=(unsigned char)DT_GetMaxDay(nowDateTime->year, nowDateTime->month);
if(nowDateTime->day>tmpday)
nowDateTime->day=tmpday;
return nowDateTime->month;
}
unsigned char DT_LastDay(DT_DATE_TIME *nowDateTime)
{
DT_DATE_TIME tmpTimer;
DT_CopyDateTime(&tmpTimer, *nowDateTime);
DT_GetLastDay(nowDateTime, tmpTimer);
return nowDateTime->day;
}
unsigned char DT_LastHour(DT_DATE_TIME *nowDateTime)
{
if(nowDateTime->hour>0)
nowDateTime->hour--;
else
{
nowDateTime->hour=23;
DT_LastDay(nowDateTime);
}
return nowDateTime->hour;
}
unsigned char DT_LastMinute(DT_DATE_TIME *nowDateTime)
{
if(nowDateTime->minute>0)
nowDateTime->minute--;
else
{
nowDateTime->minute=59;
DT_LastHour(nowDateTime);
}
return nowDateTime->minute;
}
unsigned char DT_LastSecond(DT_DATE_TIME *nowDateTime)
{
if(nowDateTime->second>0)
nowDateTime->second--;
else
{
nowDateTime->minute=59;
DT_LastMinute(nowDateTime);
}
return nowDateTime->second;
}
void DT_MaxDateTime(DT_DATE_TIME *nowDateTime, DT_DATE_TIME maxDateTimer)
{
unsigned char ifCover=0;
if(nowDateTime->year>maxDateTimer.year)
ifCover=1;
else if(nowDateTime->year EQ maxDateTimer.year)
{
if(nowDateTime->month>maxDateTimer.month)
ifCover=1;
else if(nowDateTime->month EQ maxDateTimer.month)
{
if(nowDateTime->day>maxDateTimer.day)
ifCover=1;
else if(nowDateTime->day EQ maxDateTimer.day)
{
if(nowDateTime->hour>maxDateTimer.hour)
ifCover=1;
else if(nowDateTime->hour EQ maxDateTimer.hour)
{
if(nowDateTime->minute>maxDateTimer.minute)
ifCover=1;
else if(nowDateTime->minute EQ maxDateTimer.minute)
{
if(nowDateTime->second>maxDateTimer.second)
ifCover=1;
}
}
}
}
}
if(ifCover)
DT_CopyDateTime(nowDateTime,maxDateTimer);
}
void DT_MinDateTime(DT_DATE_TIME *nowDateTime, DT_DATE_TIME minDateTimer)
{
unsigned char ifCover=0;
if(nowDateTime->year<minDateTimer.year)
ifCover=1;
else if(nowDateTime->year EQ minDateTimer.year)
{
if(nowDateTime->month<minDateTimer.month)
ifCover=1;
else if(nowDateTime->month EQ minDateTimer.month)
{
if(nowDateTime->day<minDateTimer.day)
ifCover=1;
else if(nowDateTime->day EQ minDateTimer.day)
{
if(nowDateTime->hour<minDateTimer.hour)
ifCover=1;
else if(nowDateTime->hour EQ minDateTimer.hour)
{
if(nowDateTime->minute<minDateTimer.minute)
ifCover=1;
else if(nowDateTime->minute EQ minDateTimer.minute)
{
if(nowDateTime->second<minDateTimer.second)
ifCover=1;
}
}
}
}
}
if(ifCover)
DT_CopyDateTime(nowDateTime,minDateTimer);
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -