📄 time.c
字号:
#ifndef _TIME_
#define _TIME_
long calculateTime(struct Item *p);
long calculateTime2(struct Item *p);
long calculateTime3(struct Item *p);
long calculateTime4(struct Item *p);
long Date_second_s(struct Item *p);
long Date_second_e(struct Item *p);
void timemonitor();
void systemtime();
void countDown();
void nextPro();
void updateCurrent();
void updateNext();
void seek(int s);
/************************************************/
int LeapYear(int year)/*判断是否是闰年*/
{
if((year%4==0&&year%100!=0)||year%400==0) return(1);
else return(0);
}
/****************************************************/
int cal_days(int temp,int year)
{
int mon[]={31,28,31,30,31,30,31,31,30,31,30,31};
int i,total=0;
if (LeapYear(year)==1)mon[1]=29;
for(i=0;i<temp-1;i++)total+=mon[i];
return total;
}
/***************************************************/
long calculateTime(struct Item *p)
{
const long SECPERDAY=24L*60*60;
const long SECPERYEAR=365L*24*60*60;
int i;
long num=0;
int year,month,day,hour,minute,second;
year=p->startdate.year;/*从1900 年算起至今的年数*/
month=p->startdate.month; /*代表目前月份,从一月算起,范围从0-11*/
day=p->startdate.day; /*目前月份的日数,范围01-31*/
hour=p->starttime.hour; /*从午夜算起的时数,范围为0-23*/
minute=p->starttime.minute;/*代表目前分数,范围0-59 */
second=0;/*代表目前秒数,正常范围为0-59,但允许至61秒*/
for(i=1970;i<year;i++){if(LeapYear(i)) num+=SECPERDAY;}
num+=(year-1970)*SECPERYEAR;
num+=(((cal_days(month,year)+day-1)*24L+hour)*60L+minute)*60L+(long)second;
return num;
}
/************************************************/
long calseconds(struct CLOCK booktime)
{
const long SECPERDAY=24L*60*60;
const long SECPERYEAR=365L*24*60*60;
int i;
long num=0;
int year,month,day,hour,minute,second;
year=booktime.Year;
month=booktime.Month;
day=booktime.Day;
hour=booktime.Hour;
minute=booktime.Minute;
second=booktime.Second;
for(i=1970;i<year;i++){if(LeapYear(i)) num+=SECPERDAY;}
num+=(year-1970)*SECPERYEAR;
num+=(((cal_days(month,year)+day-1)*24L+hour)*60L+minute)*60L+(long)second;
return num;
}
/*******************************************************************************
No: 1
Name: calculateTime
Function: calculate the time
计算时间,并进行转换为秒
Input: Item *p
Output: sum
*******************************************************************************/
/*******************************************************************************
No: 2
Name: calculateTime
Function: calculate the time
计算时间,并进行转换为秒
Input: Item *p
Output: sum
*******************************************************************************/
long calculateTime2(struct Item *p)
{
const long SECPERDAY=24L*60*60;
const long SECPERYEAR=365L*24*60*60;
int i=0;
long sum=0;
int year,month,day,hour,minute,second;
if(item[i].scflag==0)
{
year=p->enddate.year;/*从1900 年算起至今的年数*/
month=p->enddate.month; /*代表目前月份,从一月算起,范围从0-11*/
day=p->enddate.day; /*目前月份的日数,范围01-31*/
hour=p->endtime.hour; /*从午夜算起的时数,范围为0-23*/
minute=p->endtime.minute;/*代表目前分数,范围0-59 */
second=0;/*代表目前秒数,正常范围为0-59,但允许至61秒*/
for(i=1970;i<year;i++){if(LeapYear(i))sum+=SECPERDAY;}
sum+=SECPERYEAR*(year-1970);
sum+=(((cal_days(month,year)+day-1)*24+hour)*60L+minute)*60L+(long)second;
return(sum);
}
else
{
year=p->startdate.year;/*从1900 年算起至今的年数*/
month=p->startdate.month; /*代表目前月份,从一月算起,范围从0-11*/
day=p->startdate.day; /*目前月份的日数,范围01-31*/
hour=p->endtime.hour; /*从午夜算起的时数,范围为0-23*/
minute=p->endtime.minute;/*代表目前分数,范围0-59 */
second=0;/*代表目前秒数,正常范围为0-59,但允许至61秒*/
for(i=1970;i<year;i++){if(LeapYear(i))sum+=SECPERDAY;}
sum+=SECPERYEAR*(year-1970);
sum+=(((cal_days(month,year)+day-1)*24+hour)*60L+minute)*60L+(long)second;
return(sum);
}
}
long calculateTime3(struct Item *p)
{
const long SECPERDAY=24L*60*60;
const long SECPERYEAR=365L*24*60*60;
int i=0;
long sum=0;
int year,month,day,hour,minute,second;
year=p->startdate.year;/*从1900 年算起至今的年数*/
month=p->startdate.month; /*代表目前月份,从一月算起,范围从0-11*/
day=p->startdate.day; /*目前月份的日数,范围01-31*/
hour=p->endtime.hour; /*从午夜算起的时数,范围为0-23*/
minute=p->endtime.minute;/*代表目前分数,范围0-59 */
second=0;/*代表目前秒数,正常范围为0-59,但允许至61秒*/
for(i=1970;i<year;i++){if(LeapYear(i))sum+=SECPERDAY;}
sum+=SECPERYEAR*(year-1970);
sum+=(((cal_days(month,year)+day-1)*24+hour)*60L+minute)*60L+(long)second;
return(sum);
}
long calculateTime4(struct Item *p)
{
const long SECPERDAY=24L*60*60;
const long SECPERYEAR=365L*24*60*60;
int i=0;
long sum=0;
int year,month,day,hour,minute,second;
year=p->enddate.year;/*从1900 年算起至今的年数*/
month=p->enddate.month; /*代表目前月份,从一月算起,范围从0-11*/
day=p->enddate.day; /*目前月份的日数,范围01-31*/
hour=p->endtime.hour; /*从午夜算起的时数,范围为0-23*/
minute=p->endtime.minute;/*代表目前分数,范围0-59 */
second=0;/*代表目前秒数,正常范围为0-59,但允许至61秒*/
for(i=1970;i<year;i++){if(LeapYear(i))sum+=SECPERDAY;}
sum+=SECPERYEAR*(year-1970);
sum+=(((cal_days(month,year)+day-1)*24+hour)*60L+minute)*60L+(long)second;
return(sum);
}
/************************************************/
void drawButton1(int x,int y,char *string,char arr)
{
gotoxy(x,y);
textattr(LIGHTGRAY+(BLACK<<4));
cprintf(string);
textattr(YELLOW+(BLACK<<4));
}
/*******************************************************************************
No: 3
Name: getTime
Function: Get system time
获取系统时间
Input: None
Output: None
*******************************************************************************/
void systemtime(int x,int y)
{
static time_t t_old;
struct tm *p;
time_t t_now;
time(&t_now);
p=localtime(&t_now);
Systemtime.Year=p->tm_year+1900;
Systemtime.Month=p->tm_mon+1;
Systemtime.Day=p->tm_mday;
Systemtime.Week=p->tm_wday;
Systemtime.Hour=p->tm_hour;
Systemtime.Minute=p->tm_min;
Systemtime.Second=p->tm_sec;
while(1){
if(t_now==t_old)break;
drawButton1(x,y,ctime(&t_now),0);
t_old=t_now;
timemonitor();
}
}
/*******************************************************************************
No:4
Name: timemonitor
Function: judge time events per second
每秒进行时间事件判断
Input: None
Output: None
*******************************************************************************/
void timemonitor()
{
seek(1);
countDown();
nextPro();
}
/*******************************************************************************
No: 5
Name: seek
Function: seek the nearest info
查找最近的节目
Input: None
Output: None
*******************************************************************************/
/*******************************************************************************
No: 5
Name: seek
Function: seek the nearest info
查找最近的节目
Input: None
Output: None
*******************************************************************************/
void seek(int s)
{
int i,j,m;
static int n;
long k[SIZE]={0},f[SIZE]={0};
for(i=0;i<length;i++)
{
if(item[i].mark!=1)
{
m=caldays(item[i].startdate,item[i].enddate); /*计算预约的天数 可以为0,是单预约*/
if((item[i].scflag==0)&&(calculateTime(&item[i])<calseconds(Systemtime)))
continue;
k[i]=calculateTime(&item[i]); /*计算开始时间并存储在k[i]中*/
f[i]=calculateTime2(&item[i]);/*计算结束的时间存储在f[i]中 */
for(j=1;j<m;j++)
{ /*当为连续预约的时候要进行现行时间的转换*/
if((t-k[i])>0)
{
k[i]+=24L*60*60;
f[i]+=24L*60*60;
}
else
break;
}
}
}
n=0;
for(i=0;i<length;i++)/*查找存储秒数最小的那条预约*/
{
if(item[i].mark!=1){
if(k[n]>k[i]&&k[i]>1190000000)
{
n=i;
}
p_info=&item[n];
if(s==0)
{
start_time=k[n];
finish_time=f[n];
}}
}
}
/*******************************************************************************
No:6
Name: countDown
Function: display the countdown second
Input: None
Output: None
*******************************************************************************/
void countDown()
{
int i=0,j;
int countdown=start_time-calseconds(Systemtime);
if((countdown<=30)&&(countdown>=1))
{
gotoxy(50,8);
cprintf("%d seconds later",countdown);
}
else if((calseconds(Systemtime)-start_time)==0)
{
gotoxy(50,8);
cprintf(" ");
}
for(i=0;i<length;i++)
{
if(calculateTime(&item[i])<calseconds(Systemtime)&&calculateTime4(&item[i])>calseconds(Systemtime))
item[i].mark=1;
else if(calculateTime(&item[i])<calseconds(Systemtime))
item[i].mark=2;
}
}
/*******************************************************************************
No: 7
Name: nextPro
Function: show the next program
显示下一节目
Input: None
Output: None
*******************************************************************************/
void nextPro()
{
int i;
if(p_info->name==NULL)
return 0;
else if(Menu==2)
{gotoxy(20,8);
for(i=0;i<8;i++)
printf("%c",p_info->name[i]);
gotoxy(1,6);
}
}
/*******************************************************************************
No: 8
Name: currentPro
Function: show the current program
显示当前节目
Input: None
Output: None
*******************************************************************************/
/*******************************************************************************
No: 10
Name: caldays
Function: calculation the program days
计算节目持续的天数
Input: None
Output: None
*******************************************************************************/
int caldays(Date info1,Date info2)
{ int i;long n;
long sumdays1=0,sumdays2=0;
if(item[x].scflag==0)
return 1;
else
{for(i=1970;i<info1.year;i++) {if(LeapYear(i))sumdays1+=1;}
sumdays1+=365*(info1.year-1970);
sumdays1+=cal_days(info1.month,LeapYear(info1.year))+info1.day-1;
for(i=1970;i<info2.year;i++) {if(LeapYear(i))sumdays2+=1;}
sumdays2+=365*(info2.year-1970);
sumdays2+=cal_days(info2.month,LeapYear(info2.year))+info2.day-1;
n=sumdays2-sumdays1+1;
return n;}
}
/*****************************************************/
#endif
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -