Is it possible to program for an calender? if it is possible,give me an idea.
Calender
Collapse
X
-
Date calculations are notoriously difficult, with many frustrating special cases that will catch you if you do not test your code to death with every possible date.
Do not undertake this lightly!
That said, a good place to start is
Code:__time64_t long_time; struct tm myTime _time64( &long_time ); /* Get time as long integer. */ myTime = *_localtime64( &long_time ); /* Convert to local time. */
-
Presumably, you would have 12 months where months were 28,29,30 or 31 days based on the year.
Can you see that a year is a container of months and a month is a container of days?
Try to write something on paper before attempting to code.Comment
-
Originally posted by weaknessforcatsPresumably, you would have 12 months where months were 28,29,30 or 31 days based on the year.
Can you see that a year is a container of months and a month is a container of days?
Try to write something on paper before attempting to code.
Who contains the years, I wonder? And how does poor old febuary know whether it is in a leap year or not?
I have found that it is better to create the days, one at a time, as they are needed - just like real life, really. The "struct tm" is a handy little thing for keeping track of all the pesky details.Comment
-
Originally posted by ravenspointGosh, weaknessforcats , you are so very modern with your containers of containers! Then you spoil it all by calling for paper and pencil!
Who contains the years, I wonder? And how does poor old febuary know whether it is in a leap year or not?
I have found that it is better to create the days, one at a time, as they are needed - just like real life, really. The "struct tm" is a handy little thing for keeping track of all the pesky details.
nextday = feb 29
else
nextday = mar 1Comment
-
Originally posted by TRScheelif (year % 4) == 0
nextday = feb 29
else
nextday = mar 1Comment
-
Originally posted by ravenspointGosh, weaknessforcats , you are so very modern with your containers of containers! Then you spoil it all by calling for paper and pencil!
Who contains the years, I wonder? And how does poor old febuary know whether it is in a leap year or not?
You need to desgin before coding. That iswhy things like UML are in existence in the first place.
A struct tm may not work in a particular application or operating system.Comment
-
Originally posted by weaknessforcatsA struct tm may not work in a particular application or operating system.Comment
-
It is not required that C be supported by C++. In several areas C code just doesn't compile in C++.Comment
-
Originally posted by DarrylWhat's the day after Febuary 28, 1900?
So that would be something like
[code=cpp]
bool LeapYear()
{
return currentYear % 4 == 0 && currentYear % 100 != 0;
}
[/code]
Make that accessible to the months, particularly February, and February checks that before going to the 29th. If all months inherit a base, you could override the next day function on February to just check that, while every other month is oblivious to the leap year. Sadly, this is a model where the month does have to be aware of what year it is in.Comment
-
I think the leap year check is that the leap year is divisible by 4 and not by 400.
1600 and 2000 were not leap years.
If your calendar doesn't need to go out that far, the 400 check isn't worth it.Comment
-
Originally posted by JOsAHZeller's congruence
Where did you learn all of this stuff? :)Comment
-
Comment