Click here to Skip to main content
65,938 articles
CodeProject is changing. Read more.
Articles / Languages / C

How To Find the Next User-defined Day Time

3.86/5 (3 votes)
29 Nov 2009GPL31 min read 21.9K   84  
How to find the next user-defined day time

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:

C++
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.

C++
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:

C++
/*
=================================
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
}

And that's the other one (UNIX time):

C++
 /*
=================================
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
}

Example

Ok guys, this is a little example that find next Sunday:

C++
#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

License

This article, along with any associated source code and files, is licensed under The GNU General Public License (GPLv3)