Introduction
This article will guide you through a simple algorithm to find a certain day in the next week.
For example, you want to find next Sunday:
Today's date: 27/11/2009
The program will return: 29/11/2009 (Or corresponding UNIX Time)
Using the Code
Well, you can use the algorithm function by calling the function:
struct tm * nextday (int day=0)
This function will return a struct tm *
variable type that contains all information about day
you selected. To find more information about tm struct
, click here.
time_t nextday_UNIX (int day=0)
This function will return a time_t
variable type that contains ONLY Unix time format. To find more information about time_t
, click here.
In both functions, you must enter a parameter (default is 0). This parameter is the number of the day in the week, starting from Sunday, so:
Sunday -> 0
Monday -> 1
Tuesday -> 2
[...]
Saturday -> 6
If you try to pass a value that is not in this range, both functions will return NULL
.
Ok, stop talk, this is the first function, self-explained in the code:
struct tm * nextday (int day=0)
{
time_t rawtime;
struct tm * timeinfo;
time ( &rawtime );
timeinfo = localtime ( &rawtime );
mktime ( timeinfo );
time_t nextday;
struct tm* nextdaydate;
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;
nextdaydate = localtime(&nextday);
return nextdaydate; }
And that's the other one (UNIX time):
time_t nextday_UNIX (int day=0)
{
time_t rawtime;
struct tm * timeinfo;
time ( &rawtime );
timeinfo = localtime ( &rawtime );
mktime ( timeinfo );
time_t nextday;
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; }
Example
Ok guys, this is a little example that find next Sunday:
#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);
}
}
I hope you enjoyed and understood my article, write comments and have fun!
History
- 1.0 - 29/11/2009: Initial release