Click here to Skip to main content
Click here to Skip to main content

How To Find the Next User-defined Day Time

By , 29 Nov 2009
Rate this:
Please Sign up or sign in to vote.

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:

/*
=================================
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):

 /*
=================================
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:

#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)

About the Author

SpaceSoft
Software Developer (Junior) Space Softwares
Italy Italy
We're born in the 2008. From the 2010, we're working for fun at an FPS opensource project.
This isn't a commercial company, is only a group created for fun by some teens

Comments and Discussions

 
GeneralNothing new here PinmemberRichard MacCutchan30-Nov-09 3:44 
GeneralRe: Nothing new here PinmemberItalianSoul30-Nov-09 4:08 
Thanks mate, I know that, but my code is for guys that don't want to study that library.
 
Thank you for your help too.
GeneralRe: Nothing new here PinmemberRichard MacCutchan30-Nov-09 5:46 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.

| Advertise | Privacy | Mobile
Web03 | 2.8.140415.2 | Last Updated 30 Nov 2009
Article Copyright 2009 by SpaceSoft
Everything else Copyright © CodeProject, 1999-2014
Terms of Use
Layout: fixed | fluid