Is it possible to program for an calender? if it is possible,give me an idea.
29 3024
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 -
__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.
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.
Gosh, 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.
TRScheel 638
Recognized Expert Contributor
Gosh, 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.
if (year % 4) == 0
nextday = feb 29
else
nextday = mar 1
if (year % 4) == 0
nextday = feb 29
else
nextday = mar 1
Yes, this works if the month knows what year it is contained by. Which is why I asked about just that.
Gosh, 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?
Yes, indeed. Who does contain the years? Are there years?? What kind calendar it is it?? Gregorian? Tibetan? Astronomical? Julian?
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.
A struct tm may not work in a particular application or operating system.
Really? As I understand it, it is part of the C run time library. I believe that a compliant C compiler toolset should provide it.
It is not required that C be supported by C++. In several areas C code just doesn't compile in C++.
if (year % 4) == 0
nextday = feb 29
else
nextday = mar 1
What's the day after Febuary 28, 1900?
TRScheel 638
Recognized Expert Contributor
What's the day after Febuary 28, 1900?
Throw in % 100 to check those years. Or make a method that returns leap year or not.
So that would be something like -
bool LeapYear()
-
{
-
return currentYear % 4 == 0 && currentYear % 100 != 0;
-
}
-
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.
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.
JosAH 11,448
Recognized Expert MVP
For the Gregorian calendar, Zeller's congruence method comes in extremely
handy; google for it.
kind regards,
Jos
Zeller's congruence
What is that!! I have seen you pull all sorts of rabbits like this out of your hat.
Where did you learn all of this stuff? :)
2,620 hits on Google for Zeller's Congruence.
What else have I failed to learn?
TRScheel 638
Recognized Expert Contributor
This must be the way that kid from a few years back used. The one who was all the rage for being able to figure out the day of the week for any day past or future in his head.
I always knew that kid had a secret...
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.
1600 and 2000 Were leap years.
The rule is:divisible by 4 is a leap year unless divisible 100, then not leap year unless also divisible by 400, then it is.
TRScheel 638
Recognized Expert Contributor
1600 and 2000 Were leap years.
The rule is:divisible by 4 is a leap year unless divisible 100, then not leap year unless also divisible by 400, then it is.
<cry>Oh god, the rules just keep piling on... </cry>
<cry>Oh god, the rules just keep piling on... </cry>
As I said in my first post:
"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!"
TRScheel 638
Recognized Expert Contributor
NOTE TO SELF:
Let someone else deal with dates, focus on just getting one
=P
JosAH 11,448
Recognized Expert MVP
What is that!! I have seen you pull all sorts of rabbits like this out of your hat.
Where did you learn all of this stuff? :)
Zeller's congruence rule and the [4, !100, 400] leap year rule is all you need to
produce a calendar for any month of any year you like. There's nothing complicated
about it.
Zeller's congruence rule calculates the Julian day number for a date; the difference
between the Julian day number for two dates is the difference in days for those
two days. The Julian day number mod 7 gives you the day of the week for that
date.
Of course all assuming a Gregorian calendar of course.
kind regards,
Jos (old bag of old tricks ;-)
JosAH 11,448
Recognized Expert MVP
Date calculations are notoriously difficult,
That simply is not true; see my previous replies.
kind regards,
Jos
That simply is not true; see my previous replies.
kind regards,
Jos
Really? Ever hear of a little thing called Y2K?
Perhaps 25% of my contract revenue comes from writing calendar code. I usually get this work after someone has given up in frustration. They start out optimistically, with a happy-go-lucky spirit akin to yours, perhaps, and are flummoxed by the ever mounting special cases. Or they hire it out cheap, to someone who has never coded a serious date class before and so who bids way less than I do - but I get the job the second time around :-)
TRScheel 638
Recognized Expert Contributor
This brings this to mind (to be more kosher, I moved the joke to the jokes thread, look for my last post): Y2K Joke
.... On topic, after reading through this thread, I am curious as to what special cases would not work with that formula?
JosAH 11,448
Recognized Expert MVP
Really? Ever hear of a little thing called Y2K?
That was a hype and a lot of crappy software. Zeller's congruence rule, the
[4, !100, 400] leap year rule and enough resolution for the time stamps
(8 bytes mayhap?) take care of the rest. Not much to be proud of to get it right.
To get it wrong takes quite a bit of stupidity though and having to fix it is very
boring IMHO. I don't do that type of 'work' thank you.
kind regards,
Jos
You want special cases?
here's one:
When is Easter Bank Holiday in the year X in country Y?
Well, Easter Sunday is the first sunday after the first full moon after the vernal equinox which is the first day of spring when the sun stands at the zenith on the equator at noon. The Bank holiday is either the Friday before, or the Monday after, depending on the country.
Now any calendar coder worth his salt can deal with that. If that seems easy, try the day the Japanese banks close to celebrate the emperor's birthday?
That was a hype and a lot of crappy software. Zeller's congruence rule, the
[4, !100, 400] leap year rule and enough resolution for the time stamps
(8 bytes mayhap?) take care of the rest. Not much to be proud of to get it right.
To get it wrong takes quite a bit of stupidity though and having to fix it is very
boring IMHO. I don't do that type of 'work' thank you.
kind regards,
Jos
IMHO, your post is downright rude. 'Kind regards', indeed.
The OP wanted to know if it was reasonable to undertake a calendar coding task. I believe I gave him fair warning. If he is still reading this long thread, then I expect my warning will have been reinforced - plus the knowledge that even if he succeeds he won't get any respect from the likes of JosAH
JosAH 11,448
Recognized Expert MVP
You want special cases?
Nope not me thank you; I feed them to my rule based 'database' (mind the
quotes). Rule based programming is quite an old and well known little discipline;
I use it for those silly irregularities all the time. Astronomic (Solar) rules are just
calculus and I can't consider them 'special cases' either.
kind regards,
Jos
Technically, the OP just wanted a calendar. Not a listing of all the holidays, festivals, celebrations, and other arbitrary (said, tongue-in-cheek) data that's printed on one. I took that to mean a standard grid of 28/29/30/31 boxes with only numbers in them correlating to the days of the week. In doing so, I've always just used the abridged code below to determine the first day of the month, and then another bit of code to space the numbers correctly in my calendar programs: -
int dayOfWeek(int month, int day, int year)
-
{
-
// This function determines what day of the week a specific date falls on
-
-
int DOW;
-
-
if (month < 3)
-
{
-
month += 12;
-
year -= 1;
-
}
-
-
DOW = (day + (2 * month) + (int)((float)(6 * (month + 1)) / 10.0)
-
+ year + (int)((float)year / 4.0) - (int)((float)year / 100.0)
-
+ (int)((float)year / 400.0) + 1) % 7;
-
-
return DOW; // 0 = Sunday
-
// 1 = Monday
-
// 2 = Tuesday
-
// 3 = Wednesday
-
// 4 = Thursday
-
// 5 = Friday
-
// 6 = Saturday
-
}
-
Well, archonmagnus, if that is all you want from your calendar, you can let the C runtime library do the work for you. As I explained in my first post to this thread, the best place to start a calendar project is with struct tm.
This does all you need: -
#include "stdafx.h"
-
#include <time.h>
-
-
int dayOfWeek(int month, int day, int year)
-
{
-
// This function determines what day of the week a specific date falls on
-
struct tm Time;
-
memset( &Time, '\0', sizeof( Time ) );
-
// the tm structure codes years counting from 1900
-
Time.tm_year = year - 1900;
-
//The tm structure numbers months from 0 - 11
-
Time.tm_mon =month - 1;
-
Time.tm_mday = day;
-
Time.tm_hour = 12;
-
Time.tm_isdst = -1;
-
_mktime64( &Time );
-
-
return Time.tm_wday;
-
-
}
-
-
int _tmain(int argc, _TCHAR* argv[])
-
{
-
int m, d, y;
-
printf("Enter month day year\n");
-
scanf("%d %d %d",&m, &d, &y );
-
-
int DOW = dayOfWeek( m, d, y );
-
-
printf("Day of Week %d\n",DOW );
-
return 0;
-
}
-
-
-
Enter month day year
-
7 19 2007
-
Day of Week 4
-
I find most of the posts to this thread very strange. I cannot believe they are intended to help the OP. They address problems that are already solved by the C runtime, struct tm and _mktime64(). They dismiss problems which the OP will likely have to face as if they were somehow beneath the dignity of the posters.
archonmagnus is the only poster who has shown some code and made a claim to have actually tackled calendar problems. The rest appear to be using this thread to demonstrate their arrogance, contempt for ordinary coders , and a total ignorance of the calendar support offered by the C runtime library.
Let me make a suggestion. If the Original Poster still wants to tackle his calendar project ( something I can barely believe he would after reading this thread ) then he may perhaps wish to send me a private message and I will endeavor to offer him useful, practical advice without having to endure sniping from the academic types who have hijacked this thread.
Sign in to post your reply or Sign up for a free account.
Similar topics |
by: jason |
last post by:
Hi Everyone,
I would really appreciate some thoughts on how best to tackle an
availability calender in ASP for our yacht fleet.
I need to be able to show the booking STATUS of a particular...
|
by: PerryC |
last post by:
I want to be able to accomplish this, please help:
When click on the DOB field, a ActiveX Calender popup and the user
choose a date, the value is automatically refreshed in the DOB field
in the...
|
by: Steve Peterson |
last post by:
Hi
I have a web app running on WinXP Pro which has the OS's operating system's
regional settings set to "Spanish - Spain". I have a web app running on this
computer that one page has a calender...
|
by: Chris |
last post by:
Hi,
I am trying to create a popup calender so a user can click on a button on
the main form, a calender will then popup, the user will select a date and
then click ok on the popup. The date will...
|
by: Vishnu |
last post by:
In a Windows Application How to display a monthly , weekly calender ( Just
like the calender in Outlook or the calender display airline websites)
.....Also how will the code know if some month and...
| |
by: Steven Blair |
last post by:
Hi,
I would like to create a Calender effect like this website:
http://www.expedia.co.uk/default.aspx
This site uses ASP.NET, so I guess we should be able to use VS 2005 to
recreate this.
...
|
by: kalichakradhar |
last post by:
hi all,
hi,
I am developing a application which would open the shared calender of outlook and read the meeting notices and also modifies the meeting notice from Vb.I am successful in opening the...
|
by: gubbachchi |
last post by:
Hi all,
In my application I need to display the data fetched from mysql database after the user selects date from javascript calender. I have written the code in which after the user selects the...
|
by: Vkas |
last post by:
i had created my own web calender control!!!!
it consist
1} text box (it display's the selected date)
2} button (it hide and show the calender control)
3} calender control (use for the selection...
|
by: xtremebass |
last post by:
Hello Bytes,
i have a calender program which is created by using Javascript. when i execute that program using Internet Explorer,it works properly but when i tried in Mozilla firefox it didnt...
|
by: marktang |
last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
| |
by: Oralloy |
last post by:
Hello folks,
I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>".
The problem is that using the GNU compilers,...
|
by: jinu1996 |
last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
|
by: tracyyun |
last post by:
Dear forum friends,
With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
|
by: agi2029 |
last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
|
by: TSSRALBI |
last post by:
Hello
I'm a network technician in training and I need your help.
I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs.
The...
|
by: adsilva |
last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
| |
by: 6302768590 |
last post by:
Hai team
i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated ...
|
by: muto222 |
last post by:
How can i add a mobile payment intergratation into php mysql website.
| |