473,500 Members | 1,668 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Calender

2 New Member
Is it possible to program for an calender? if it is possible,give me an idea.
Jul 19 '07 #1
29 3024
ravenspoint
111 New Member
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

Expand|Select|Wrap|Line Numbers
  1.     __time64_t long_time;
  2.         struct tm myTime
  3.  
  4.     _time64( &long_time );                /* Get time as long integer. */
  5.     myTime = *_localtime64( &long_time ); /* Convert to local time. */
  6.  
Jul 19 '07 #2
weaknessforcats
9,208 Recognized Expert Moderator Expert
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.
Jul 19 '07 #3
ravenspoint
111 New Member
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.
Jul 19 '07 #4
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
Jul 19 '07 #5
ravenspoint
111 New Member
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.
Jul 19 '07 #6
weaknessforcats
9,208 Recognized Expert Moderator Expert
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.
Jul 19 '07 #7
ravenspoint
111 New Member
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.
Jul 19 '07 #8
weaknessforcats
9,208 Recognized Expert Moderator Expert
It is not required that C be supported by C++. In several areas C code just doesn't compile in C++.
Jul 19 '07 #9
Darryl
86 New Member
if (year % 4) == 0
nextday = feb 29
else
nextday = mar 1
What's the day after Febuary 28, 1900?
Jul 19 '07 #10
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

Expand|Select|Wrap|Line Numbers
  1. bool LeapYear()
  2. {
  3. return currentYear % 4 == 0 && currentYear % 100 != 0;
  4. }
  5.  
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.
Jul 19 '07 #11
weaknessforcats
9,208 Recognized Expert Moderator Expert
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.
Jul 19 '07 #12
JosAH
11,448 Recognized Expert MVP
For the Gregorian calendar, Zeller's congruence method comes in extremely
handy; google for it.

kind regards,

Jos
Jul 19 '07 #13
weaknessforcats
9,208 Recognized Expert Moderator Expert
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? :)
Jul 19 '07 #14
weaknessforcats
9,208 Recognized Expert Moderator Expert
2,620 hits on Google for Zeller's Congruence.

What else have I failed to learn?
Jul 19 '07 #15
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...
Jul 19 '07 #16
Darryl
86 New Member
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.
Jul 19 '07 #17
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>
Jul 19 '07 #18
ravenspoint
111 New Member
<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!"
Jul 19 '07 #19
TRScheel
638 Recognized Expert Contributor
NOTE TO SELF:

Let someone else deal with dates, focus on just getting one

=P
Jul 19 '07 #20
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 ;-)
Jul 19 '07 #21
JosAH
11,448 Recognized Expert MVP
Date calculations are notoriously difficult,
That simply is not true; see my previous replies.

kind regards,

Jos
Jul 19 '07 #22
ravenspoint
111 New Member
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 :-)
Jul 19 '07 #23
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?
Jul 19 '07 #24
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
Jul 19 '07 #25
ravenspoint
111 New Member
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?
Jul 19 '07 #26
ravenspoint
111 New Member
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
Jul 19 '07 #27
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
Jul 19 '07 #28
archonmagnus
113 New Member
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:

Expand|Select|Wrap|Line Numbers
  1. int dayOfWeek(int month, int day, int year)
  2. {
  3.     // This function determines what day of the week a specific date falls on
  4.  
  5.     int DOW;
  6.  
  7.     if (month < 3)
  8.     {
  9.         month += 12;
  10.         year -= 1;
  11.     }
  12.  
  13.     DOW = (day + (2 * month) + (int)((float)(6 * (month + 1)) / 10.0)
  14.             + year + (int)((float)year / 4.0) - (int)((float)year / 100.0)
  15.             + (int)((float)year / 400.0) + 1) % 7;
  16.  
  17.     return DOW;     // 0 = Sunday
  18.                     // 1 = Monday
  19.                     // 2 = Tuesday
  20.                     // 3 = Wednesday
  21.                     // 4 = Thursday
  22.                     // 5 = Friday
  23.                     // 6 = Saturday
  24. }
  25.  
Jul 19 '07 #29
ravenspoint
111 New Member
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:
Expand|Select|Wrap|Line Numbers
  1. #include "stdafx.h"
  2. #include <time.h>
  3.  
  4. int dayOfWeek(int month, int day, int year)
  5. {
  6.     // This function determines what day of the week a specific date falls on
  7.     struct tm Time;
  8.     memset( &Time, '\0', sizeof( Time ) );
  9.     // the tm structure codes years counting from 1900
  10.     Time.tm_year = year - 1900;
  11.     //The tm structure numbers months from 0 - 11
  12.     Time.tm_mon =month - 1;
  13.     Time.tm_mday = day;
  14.     Time.tm_hour = 12;
  15.     Time.tm_isdst = -1;
  16.     _mktime64( &Time );
  17.  
  18.     return Time.tm_wday;
  19.  
  20. }
  21.  
  22. int _tmain(int argc, _TCHAR* argv[])
  23. {
  24.     int m, d, y;
  25.     printf("Enter month day year\n");
  26.     scanf("%d %d %d",&m, &d, &y );
  27.  
  28.     int DOW = dayOfWeek( m, d, y );
  29.  
  30.     printf("Day of Week %d\n",DOW );
  31.     return 0;
  32. }
  33.  
  34.  
Expand|Select|Wrap|Line Numbers
  1. Enter month day year
  2. 7 19 2007
  3. Day of Week 4
  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.
Jul 20 '07 #30

Sign in to post your reply or Sign up for a free account.

Similar topics

0
1412
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...
8
2356
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...
0
1261
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...
4
2665
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...
0
1120
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...
0
2724
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. ...
0
1391
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...
7
2339
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...
21
18563
Vkas
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...
1
3817
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...
0
7134
marktang
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,...
0
7180
Oralloy
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,...
0
7229
jinu1996
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...
0
7395
tracyyun
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...
0
5485
agi2029
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,...
0
3108
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...
0
3103
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
0
1429
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 ...
1
667
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.