如何查找下一个用户定义的日间时间
如何查找下一个用户定义的日间时间
引言
本文将指导您完成一个简单的算法,以查找下周的特定日期。
例如,您想找到下个星期日
今天的日期:2009/11/27
程序将返回:2009/11/29 (或相应的UNIX时间戳)
Using the Code
好的,您可以通过调用该函数来使用该算法函数
struct tm * nextday (int day=0)
此函数将返回一个struct tm *
变量类型,其中包含有关您选择的day
的所有信息。要了解更多关于tm struct
的信息,请点击这里。
time_t nextday_UNIX (int day=0)
此函数将返回一个time_t
变量类型,其中仅包含UNIX时间戳格式。要了解更多关于time_t
的信息,请点击这里。
在两个函数中,您必须输入一个参数(默认值为0)。此参数是星期几的数字,从星期日开始,因此
星期日 -> 0
星期一 -> 1
星期二 -> 2
[...]
星期六 -> 6
如果您尝试传递不在此范围内的值,则两个函数都将返回NULL
。
/*
=================================
HOW TO FIND NEXT DAY TIME (struct)
=================================
by ItalianSoul
The function returns a struct type tm.
To extract real date you can use the following codes:
punt->tm_mday //Day
punt->tm_mon+1 //Month
punt->tm_year+1900 //Year
*/
struct tm * nextday (int day=0)
{
/* Initialize variables */
time_t rawtime;
struct tm * timeinfo;
/* get current timeinfo*/
time ( &rawtime );
timeinfo = localtime ( &rawtime );
/* call mktime: timeinfo->tm_wday will be set */
mktime ( timeinfo );
/*Now let's find next day time*/
/*Initialize temporarily variables */
time_t nextday;
struct tm* nextdaydate;
/*Now I'll search which day is it and I'll add 86400 seconds(a day) *days remaining */
if(timeinfo->tm_wday == day)
nextday = rawtime + (86400*7);
else if (timeinfo->tm_wday == day-6)
nextday = rawtime + (86400*6);
else if (timeinfo->tm_wday == day-5)
nextday = rawtime + (86400*5);
else if (timeinfo->tm_wday == day-4)
nextday = rawtime + (86400*4);
else if (timeinfo->tm_wday == day-3)
nextday = rawtime + (86400*3);
else if (timeinfo->tm_wday == day-2)
nextday = rawtime + (86400*2);
else if (timeinfo->tm_wday == day-1)
nextday = rawtime + (86400);
else
return NULL;
/*Convert him to localtime*/
nextdaydate = localtime(&nextday);
return nextdaydate; //Return struct
}
这是另一个(UNIX时间戳)
/*
=================================
HOW TO FIND NEXT DAY TIME (UNIX)
=================================
by ItalianSoul
The function returns an time_t format variable,
that contains UNIX date format of next Sunday (or day that you selected)
*/
time_t nextday_UNIX (int day=0)
{
/* Initialize variables */
time_t rawtime;
struct tm * timeinfo;
/* get current timeinfo*/
time ( &rawtime );
timeinfo = localtime ( &rawtime );
/* call mktime: timeinfo->tm_wday will be set */
mktime ( timeinfo );
/*Now let's find next day time*/
/*Intialize temporarily variables */
time_t nextday;
/*Now I'll search which day is it and I'll add 86400 seconds(a day) *days remaining */
if(timeinfo->tm_wday == day)
nextday = rawtime + (86400*7);
else if (timeinfo->tm_wday == day-6)
nextday = rawtime + (86400*6);
else if (timeinfo->tm_wday == day-5)
nextday = rawtime + (86400*5);
else if (timeinfo->tm_wday == day-4)
nextday = rawtime + (86400*4);
else if (timeinfo->tm_wday == day-3)
nextday = rawtime + (86400*3);
else if (timeinfo->tm_wday == day-2)
nextday = rawtime + (86400*2);
else if (timeinfo->tm_wday == day-1)
nextday = rawtime + (86400);
else
return (time_t) NULL;
return nextday; //Return the result as time_t
}
示例
好的,伙计们,这是一个查找下个星期日的示例
#include <stdio.h>
#include <time.h> //You must include time.h library to compile that functions
int main()
{
time_t sunday_unix;
struct tm *sunday_date;
sunday_date=nextday(0);
sunday_unix=nextday_UNIX(7);
if(sunday_date != NULL)
{
printf("Next sunday will come on: %d\\%d\\%d",
sunday_date->tm_mday,sunday_date->tm_mon+1,sunday_date->tm_year+1900);
}
if(sunday_unix != (time_t)NULL)
{
printf("\nNext sunday will come on: %d",sunday_unix);
}
}
我希望您喜欢并理解我的文章,请发表评论并玩得开心!
历史
- 1.0 - 2009/11/29:初始发布