定义函数给出任意一天是星期几
来源:学生作业帮助网 编辑:六六作业网 时间:2024/12/18 22:29:44
定义函数给出任意一天是星期几
定义函数给出任意一天是星期几
定义函数给出任意一天是星期几
/*-----------------------------------------------------------------*\
author: wanfustudio
mail : [email protected]
Rights: 任意拷贝,修改,传播包括商业使用
Q & A :
如何疑问,以及想法,不满足的需求
欢迎来函!
\*-----------------------------------------------------------------*/
#include
#include
#include
#include
/*这段是注释,放在此处用以说明各个成员的含义,在time.h中定义*/
#if 0
struct tm {
int tm_sec; /* seconds after the minute - [0,59] */
int tm_min; /* minutes after the hour - [0,59] */
int tm_hour; /* hours since midnight - [0,23] */
int tm_mday; /* day of the month - [1,31] */
int tm_mon; /* months since January - [0,11] */
int tm_year; /* years since 1900 */
int tm_wday; /* days since Sunday - [0,6] */
int tm_yday; /* days since January 1 - [0,365] */
int tm_isdst; /* daylight savings time flag */
};
#endif
/*-----------------------------------------------------------------*\
全局变量
\*-----------------------------------------------------------------*/
/* 数组与下面tm结构中定义的tm_wday取值范围相对应 */
const char *Week[]=
{
"星期天",
"星期一",
"星期二",
"星期三",
"星期四",
"星期五",
"星期六"
};
/*-----------------------------------------------------------------*\
函数声明
\*-----------------------------------------------------------------*/
/* 一年中第X天是星期几 */
void GetWeekDay(int year,int num);
/* X年X月X日是一年中的第X天,星期几 */
void GetSomeDay(int year, int mon, int day);
/* X年X月X日再过X天是一年中的第X天,星期几 */
void GetNextTime(int year, int mon, int day,int num);
/* X年某月末日到X年X月某日有多少天 */
int GetDistBetweenTwoDays(int year, int mon, int day, int year2, int mon2, int day2);
/*-----------------------------------------------------------------*\
函数实现
\*-----------------------------------------------------------------*/
void GetSomeDay(int year, int mon, int day)
{
time_t t;//存储需要计算时间距1900的秒数
struct tm test ;//填充这个结构
struct tm *now = NULL;
memset(&test, 0, sizeof(tm));//先清空下
test.tm_year = year - 1900;
test.tm_mon = mon -1;
test.tm_mday = day ;
t = mktime(&test);// 转换成秒
now = localtime(&t);//再转回tm
printf("%d\n", now->tm_yday);//第几天
printf("%s\n", Week[now->tm_wday]);//星期几
}
void GetNextTime(int year, int mon, int day,int num)
{
struct tm *pNext= NULL;
struct tm now, tmp;
time_t t;
memset(&now, 0 , sizeof(struct tm));
memset(&tmp, 0 , sizeof(struct tm));
now.tm_year = year-1900; //指定年
now.tm_mon = mon -1;//指定月份
now.tm_mday = day + num; //指定是哪一天
t = mktime(&now);//转换成秒
pNext = localtime(&t);
printf("%s\n", asctime(pNext));//哪一天
printf("%s\n", Week[pNext->tm_wday]);//星期几
}
void GetWeekDay(int year,int num)
{
time_t t;//存储需要计算时间距1900的秒数
struct tm test ;//填充这个结构
struct tm *now= NULL;
memset(&test, 0, sizeof(tm));//先清空下
test.tm_year = year - 1900;
test.tm_mday = num +1;
t = mktime(&test);// 转换成秒
now = localtime(&t);//再转回tm
printf("%d\n", now->tm_yday);//第几天
printf("%s\n", Week[now->tm_wday]);//星期几
}
int GetDistBetweenTwoDays(int year, int mon, int day, int year2, int mon2, int day2)
{
int distance = 0;
struct tm now, tmp;
time_t t1,t2;
long diff = 0;
memset(&now, 0 , sizeof(struct tm));
memset(&tmp, 0 , sizeof(struct tm));
now.tm_year = year-1900; //指定年
now.tm_mon = mon -1;//指定月份
now.tm_mday = day; //指定是哪一天
tmp.tm_year = year2-1900;
tmp.tm_mon = mon2 -1;
tmp.tm_mday = day2;
t1 = mktime(&now);// 转换成秒
t2 = mktime(&tmp);
diff = (long)fabs(difftime(t1, t2)); /* 算绝对值(秒) */
distance = diff / 3600 / 24; /* 转成天 */
printf("相隔 %d 天\n", distance);
return distance;
}
/*-----------------------------------------------------------------*\
函数测试
\*-----------------------------------------------------------------*/
int main()
{
GetWeekDay(2007,337);
GetSomeDay(2007,12,4);
GetNextTime(2005,12,4,100);
GetDistBetweenTwoDays(2011, 4, 30, 2011, 5, 2);
return 0;
}
/*-----------------------------------------------------------------*\
author: wanfustudio
mail : [email protected]
如何疑问,以及想法,不满足你的需求
欢迎来函!
\*-----------------------------------------------------------------*/