Solved

Need advanced date and day calculations in C++

Posted on 2006-06-19
7
1,599 Views
Last Modified: 2008-05-03
We are developing a proprietary task scheduler (just like windows scheduler) for a product.
I desperately need a ready-made source code or algorithms or anything that will help me.

I need something like: First/Second/Third/Fourth/Last  Monday/Tuesday/Wed/Thur...Sunday of a particular Month.

Task will be customised to run at any one of above combinations chosen by user.

Any help will be greatly appreciated.

Thanks in Advance.

0
Comment
Question by:chinawal
7 Comments
 
LVL 12

Assisted Solution

by:rajeev_devin
rajeev_devin earned 25 total points
ID: 16939385
0
 
LVL 5

Author Comment

by:chinawal
ID: 16943475
Thanks for the reply Rajeev.

Actually we have already developed the code for the schedular.What we are interested in dates calculation part of the schedular.we need to get the dates calculation in Terms of Monthly,weekly and Daily , those are aviable with the windows
schedular.

Thanks ,
Uma
0
 
LVL 39

Assisted Solution

by:itsmeandnobodyelse
itsmeandnobodyelse earned 25 total points
ID: 16950090
The following should do the job.

#include <windows.h>
#include <time.h>

enum Weekday { SUNDAY, MONDAY, TUESDAY, WEDNESDAY, THURSDAY, FRIDAY, SATURDAY };
enum DayInMonth { FIRSTDAY, SECONDDAY, THIRDDAY, FOURTHDAY, LASTDAY  };

int daysMonth[] = { 31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31 };

int getDayOfMonth(int year, int month, Weekday wd, DayInMonth dim)
{
     struct tm tm = { 0 };
     tm.tm_mday = 1;
     tm.tm_mon  = month-1;
     tm.tm_year = year - 1900;
     time_t  tt = mktime(&tm);
     struct tm* ptm = localtime(&tt); // get day of week to struct tm
     
     // calculate first weekday
     int day = 1 + (wd - ptm->tm_wday);
     if (dim != LASTDAY)
         return day + 7 * dim;

     day += 28;
     if (day <= daysMonth[month-1])
         return day;
     if (month == 2 && day == 29 && year%4 == 0)
         return 29;
     return day - 7;
}

Regards, Alex
0
Microsoft Certification Exam 74-409

Veeam® is happy to provide the Microsoft community with a study guide prepared by MVP and MCT, Orin Thomas. This guide will take you through each of the exam objectives, helping you to prepare for and pass the examination.

 
LVL 4

Accepted Solution

by:
e_tadeu earned 50 total points
ID: 16969080
0
 
LVL 4

Expert Comment

by:markscha
ID: 21494619
I was looking for code that does this very thing, and I came across this thread.
The code submitted by "itsmeandnobodyelse" does not work.  Perhaps there is a typo somewhere, but I don't see how it can possibly work.
As an example, I cut and pasted the code into a simple program, and I get peculiar results:
e.g. second Sunday in 3/2008 is the 2nd, an obviously invalid result:
from code:  Second Sun  in 3/2008 is: 2

If we go through the algorithm for the second Sunday in 3/2008:
desired day, wd, is Sunday=0.
First day of 3/2008 is a Saturday=6 should be in ptm->tm_wday.
day = 1 + (wd - ptm->tm_wday) = 1 + (0 - 6), so day = -5
dim (i.e. SECONDAY = 1) is not equal to LASTDAY (4), so we are returning day+7*dim = -5+7*1 = 2

The rest of the code is garbage, too, including the incorrectly handled leap year calculation (no leap  year in years ending with 00).

DOES ANYONE CHECK THIS STUFF?
#include <iostream>
#include <time.h>
#include <string>
 
enum Weekday { SUNDAY, MONDAY, TUESDAY, WEDNESDAY, THURSDAY, FRIDAY, SATURDAY };
enum DayInMonth { FIRSTDAY, SECONDDAY, THIRDDAY, FOURTHDAY, LASTDAY  };
 
int daysMonth[] = { 31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31 };
 
int getDayOfMonth(int year, int month, Weekday wd, DayInMonth dim)
{
     struct tm tm = { 0 };
     tm.tm_mday = 1;
     tm.tm_mon  = month-1;
     tm.tm_year = year - 1900;
     time_t  tt = mktime(&tm);
     struct tm* ptm = localtime(&tt); // get day of week to struct tm
     
     // calculate first weekday 
     int day = 1 + (wd - ptm->tm_wday);
     if (dim != LASTDAY)
         return day + 7 * dim;
 
     day += 28; 
     if (day <= daysMonth[month-1])
         return day;
     if (month == 2 && day == 29 && year%4 == 0)
         return 29;
     return day - 7;
}
 
 
int main()
{
	std::string weekDayName[] = { "Sun", "Mon", "Tue", "Wed", "Thu", "Fri", "Sat" };
	std::string whichDay[] = { "First", "Second", "Third", "Fourth", "Last" };
	
	int year = 2008;
	int month = 3;
	Weekday wday = SUNDAY;
	DayInMonth which = SECONDDAY;
 
	int dayOfMonth = getDayOfMonth(year, month, wday, which);
	
	std::cout << whichDay[which] << " " << weekDayName[wday] << "  " << "in " << month << "/" << year ;
	std::cout << " is: " << dayOfMonth << std::endl;
	return 0;

Open in new window

0
 
LVL 39

Expert Comment

by:itsmeandnobodyelse
ID: 21512882
>>>> int day = 1 + (wd - ptm->tm_wday);
   
The prog should work correct if changing the above line into

 int day = 1 + ((7+ wd - ptm->tm_wday)%7);


>>>> The rest of the code is garbage, too, including the incorrectly handled leap

2000 was a leap year because it can be divided by 400 without rest. So the above algorithm covers all years between 1901 and 2099 what seems to be a sufficient range for normal date calculations.

You could make it work for all dates by

     if (month == 2 && day == 29 && 
         (year%4 == 0 && (year%100 != 0 || year%400 == 0)))
         return 29;

I currently am on vacancies and can't compile and test the above.

Regards, Alex
0

Featured Post

Does Powershell have you tied up in knots?

Managing Active Directory does not always have to be complicated.  If you are spending more time trying instead of doing, then it's time to look at something else. For nearly 20 years, AD admins around the world have used one tool for day-to-day AD management: Hyena. Discover why

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Title # Comments Views Activity
How to programmatically differentiate between C and Java 10 196
Find Visual Studio Tools 2 103
sorting efficency of sorting algorithm 30 113
C++ mouse_event mouse look 7 83
Article by: SunnyDark
This article's goal is to present you with an easy to use XML wrapper for C++ and also present some interesting techniques that you might use with MS C++. The reason I built this class is to ease the pain of using XML files with C++, since there is…
Often, when implementing a feature, you won't know how certain events should be handled at the point where they occur and you'd rather defer to the user of your function or class. For example, a XML parser will extract a tag from the source code, wh…
The goal of the video will be to teach the user the concept of local variables and scope. An example of a locally defined variable will be given as well as an explanation of what scope is in C++. The local variable and concept of scope will be relat…
The viewer will learn how to user default arguments when defining functions. This method of defining functions will be contrasted with the non-default-argument of defining functions.

777 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question