GIDForums  

Go Back   GIDForums > Computer Programming Forums > C Programming Language
User Name
Password
Register FAQ Members List Calendar Search Today's Posts Mark Forums Read

 
 
Thread Tools Search this Thread Rate Thread
  #1  
Old 20-Nov-2003, 15:40
mike3340 mike3340 is offline
New Member
 
Join Date: Oct 2003
Posts: 6
mike3340 is an unknown quantity at this point

1st of the Month


i'm writing a program to allow the user to input a year, and from that i need to print out each month of that year. does anybody know a formula or a way to find out what day the first of each month is?
thanks for you help!
  #2  
Old 27-Jan-2004, 08:39
dsmith's Avatar
dsmith dsmith is offline
Senior Member
 
Join Date: Jan 2004
Location: Utah, USA
Posts: 1,351
dsmith is a glorious beacon of lightdsmith is a glorious beacon of lightdsmith is a glorious beacon of lightdsmith is a glorious beacon of lightdsmith is a glorious beacon of light
I just had to answer this one. I was playing around with dates a while ago and stumbled across the time library. It does all the work for you. Basically, the time library is constructed around a structure called tm defined as (copied from the man page for mktime):
Code:
struct tm { int tm_sec; /* seconds */ int tm_min; /* minutes */ int tm_hour; /* hours */ int tm_mday; /* day of the month */ int tm_mon; /* month */ int tm_year; /* year */ int tm_wday; /* day of the week */ int tm_yday; /* day in the year */ int tm_isdst; /* daylight saving time */ };

Now, the coolest thing about this structure, is that when you make a processing call to it, it will update the incorrect values properly. So if I put in for the year: 2004, the month: January (0) and the day (1027) and then call a process on it, it will automatically update all of the fields to give you the correct Month, Year, Date, Day of the week, etc for the day that is 1000 days from today (Jan. 27, 2004).

Downside: The year is tracked as the number of years after 1900. So this structure is invalid for dates before 1900. (I am sure that there is a work around for this, I am just not smart enough to figure it out.)

Anyway, here is a small program that will print a calendar for any year that is input:
CPP / C++ / C Code:
#include <time.h>
#include <malloc.h>
#include <stdio.h>

char* full_month[] =
{	"January", "February", "March", "April", "May", "June", "July", "August",
	"September", "October", "November", "December"
};

void print_header(FILE* fp,int month,int year)
{
	int index;

	fprintf(fp,"\n");
	fprintf(fp,full_month[month]);
	for(index = 35 - strlen(full_month[month]); index>0; index--)
		fprintf(fp," ");
	fprintf(fp,"%d\n",year);
	for(index = 39; index>0; index--)
		fprintf(fp,"-");


	fprintf(fp,"\nSun   Mon   Tue   Wed   Thu   Fri   Sat");
}

void do_calendar(FILE* fp, int year)
{
	struct tm*		date = (struct tm*) malloc (sizeof(struct tm));
	time_t	seconds;
	int 	month;
	int 	day;
	int		index;

	date->tm_year = year - 1900;
	//Year in tm struct is defined as years since 1900.
	for(month=0; month<12; month++){			//Loop through months 0-11
		date->tm_mon = month;					//Set month in tm struct.
		print_header(fp,date->tm_mon,year);		//Call print header to print month heading
		for(day=1; day<32; day++){				//Loop through days 1-31
			date->tm_mday = day;				//Set day in tm struct

			/*This call is the workhorse.  Once you set the values of the
			  other variables, this call will hash the date to its proper
			  value and fill in the tm struct properly.  The variable seconds
			  is not used for anything but mktime returns a time_t variable
			  and it is proper to capture it.
			*/
			seconds = mktime(date);


			if(day == 1){						//It it is the first day, set proper spacing
				for(index=date->tm_wday;index>0;index--){
					if(index == date->tm_wday)
						fprintf(fp,"\n   ");
					else
						fprintf(fp,"      ");
				}
			}
			if(date->tm_mon != month)			//If tm_mon was changed, that means the day of the month is out of range
				break;							//Drops out of for loop.
			if(date->tm_wday == 0)				//If this is the first day of the week
				fprintf(fp,"\n ");
			else
				fprintf(fp,"    ");
			fprintf(fp,"%2d",day);       		//This left pads single digits
		}
		fprintf(fp,"\n");
	}

}


int main()
{
	int		year;

	printf("Calendar Program\n");
	printf("----------------\n");
	printf("\nEnter year for which to generate calendar: ");
	scanf("%d",&year);
	if(year>=1900)
		printf("Processing for year: %d\n",year);
	else
		printf("Year must be greater than 1900.  Exiting.\n");
		/*Code could be added here to adjust a year that is earlier
		  than 1900 to a like year after 1900.  I don't know how to calc
		  that
		*/
	/*I called do_calendar with the ability to pass a file pointer to file
	  or a port or whatever if desired.
	*/
	do_calendar(stdout,year);

	return 0;
}

Here is the first few lines from running this program:
Code:
Calendar Program ---------------- Enter year for which to generate calendar: 2004 Processing for year: 2004 January 2004 --------------------------------------- Sun Mon Tue Wed Thu Fri Sat 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 February 2004 --------------------------------------- Sun Mon Tue Wed Thu Fri Sat 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29

Anyway, the time library is very helpful in a lot of things like this.

PLEASE NOTE: This was compiled using gcc under linux. I think that the time library is a standard ansi library and this should compile *anywhere*. If anyone compiles it on something different, let me know.
  #3  
Old 04-Feb-2004, 14:44
tay's Avatar
tay tay is offline
Junior Member
 
Join Date: Jan 2004
Posts: 77
tay will become famous soon enough
i compile this
it keep looping din print out anything
is it got anythings wrong above ur program
  #4  
Old 04-Feb-2004, 18:44
dsmith's Avatar
dsmith dsmith is offline
Senior Member
 
Join Date: Jan 2004
Location: Utah, USA
Posts: 1,351
dsmith is a glorious beacon of lightdsmith is a glorious beacon of lightdsmith is a glorious beacon of lightdsmith is a glorious beacon of lightdsmith is a glorious beacon of light
Tay, I compiled this under linux using gcc. For curiosity sake, I tried to compile under Visual C++ 6.0 in Windows XP. First of all, it wouldn't compile without adding:
CPP / C++ / C Code:
#include <string.h>

Second, Visual C++ was more sensitive to the contents of the tm structure. While it is frustrating, it is my own fault for not being more careful about making assumptions. When I called mktime with the original program, I never set the other variables of the tm structures, so they were loaded with garbage. These were mostly the time containers. gcc still ran mktime, under Visual C++, it returns an error. To avoid this, I changed the first part of my do_calendar function to:

CPP / C++ / C Code:
int do_calendar(FILE* fp, int year)
{
	struct tm*	date;
	time_t		seconds;
	int 		month;
	int 		day;
	int			index;

	time(&seconds);						//Get current time as a long
	date = localtime(&seconds);			//Load tm structure with valid values


First off, the function returns an int instead of nothing. This will allow me to capture errors when mktime is called. Second, I preload the tm structure with the current date & time, so that all of the variables of the structure are valid values.

Then in the function, where I call mktime, I changed it to:
CPP / C++ / C Code:
	if( (seconds = mktime(date) ) == -1)
				return -1;
This way, if mktime produces an error, it will drop out of the function immediately and return an error code.

Finally, to call the do_calendar function should now be something like the following:
CPP / C++ / C Code:
	if(do_calendar(stdout,year))
		printf("There was a problem with the mktime function");

One other disheartening thing that I learned about mktime is that it is only valid for the years 1970-2036.

I have attached the complete revised source code below. If any one gets it running on something else, please let me know. This now works with both gcc/linux and Visual C++/win32. Remember this is a console program as well.
Attached Files
File Type: zip calander.zip (1.3 KB, 32 views)
  #5  
Old 04-Feb-2004, 23:50
tay's Avatar
tay tay is offline
Junior Member
 
Join Date: Jan 2004
Posts: 77
tay will become famous soon enough
it can compile de.
it is a very good programm :-o
thx you share this programing
  #6  
Old 05-Feb-2004, 06:54
dsmith's Avatar
dsmith dsmith is offline
Senior Member
 
Join Date: Jan 2004
Location: Utah, USA
Posts: 1,351
dsmith is a glorious beacon of lightdsmith is a glorious beacon of lightdsmith is a glorious beacon of lightdsmith is a glorious beacon of lightdsmith is a glorious beacon of light
Tay.

I am glad to hear that it compiled for you. Portability, even on a small program, can be a pain! I am curious if you don't mind, what compiler/OS are you using?
  #7  
Old 05-Feb-2004, 07:19
tay's Avatar
tay tay is offline
Junior Member
 
Join Date: Jan 2004
Posts: 77
tay will become famous soon enough
i use win XP Home edition
the compiler is dev c++
it is a very good compiler u can try to use it
u can get it at this web:
http://www.bloodshed.net/devcpp.html

it is free software and support many library file
like microsoft c++ did not support conio
but dev c++ support it
so u can play text and background color



Quote:
Originally Posted by dsmith
Tay.

I am glad to hear that it compiled for you. Portability, even on a small program, can be a pain! I am curious if you don't mind, what compiler/OS are you using?
  #8  
Old 05-Feb-2004, 17:07
dsmith's Avatar
dsmith dsmith is offline
Senior Member
 
Join Date: Jan 2004
Location: Utah, USA
Posts: 1,351
dsmith is a glorious beacon of lightdsmith is a glorious beacon of lightdsmith is a glorious beacon of lightdsmith is a glorious beacon of lightdsmith is a glorious beacon of light
Tay.

Thanks for the link. I downloaded this and I am very impressed with it. It is based upon the gcc compiler which I am very used to.

On a side note, I really try to avoid conio. It is not a standard library and so it makes portability a pain. The same is true with the *nix library called ncurses. Actually, anytime I need an interface, I am using a gui toolkit called FLTK. This stands for fast light tool kit it is extremely portable, compiles into small code and runs very fast. If you do much interface programming you may want to take a look at it.
  #9  
Old 05-Feb-2004, 19:50
tay's Avatar
tay tay is offline
Junior Member
 
Join Date: Jan 2004
Posts: 77
tay will become famous soon enough
dsmith

u know how to use fltk rite??
can u teach me how to use it with c++ code together

and which compiler u use together with fltk?

do u know how to include fltk inside the dev-c++
can u teach me
thx
  #10  
Old 05-Feb-2004, 20:19
dsmith's Avatar
dsmith dsmith is offline
Senior Member
 
Join Date: Jan 2004
Location: Utah, USA
Posts: 1,351
dsmith is a glorious beacon of lightdsmith is a glorious beacon of lightdsmith is a glorious beacon of lightdsmith is a glorious beacon of lightdsmith is a glorious beacon of light
Hi Tay. Have you seen fltk before? I have actually only used it to date in linux under gcc. It is really pretty simple to use. I use the fluid dialog editor to get a basic setup that I like and then write out the code.

I will definitely give it a try in dev-c++ and let you know if it is as simple.

If you would like to see any code before then, let me know and I will upload some for you to take a look at.
 
 

Recent GIDBlogProblems with the Navy (Enlisted) by crystalattice

Thread Tools Search this Thread
Search this Thread:

Advanced Search
Rate This Thread
Rate This Thread:

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Forum Jump

Similar Threads
Thread Thread Starter Forum Replies Last Post
How do I get 'last month' in YYYYmm format? JdS MySQL / PHP Forum 6 25-Mar-2009 03:26
150 mb storage only $3.30 a month! WebServeHosting Web Hosting Advertisements & Offers 0 25-Nov-2003 01:28
Free 1st month / Free setup / No credit card needed...Plans start at 4.95 LarryIsaac Web Hosting Advertisements & Offers 0 11-Oct-2003 14:03

Network Sites: GIDNetwork · GIDWebHosts · GIDSearch · Learning Journal by J de Silva, The

All times are GMT -6. The time now is 18:53.


vBulletin, Copyright © 2000 - 2010, Jelsoft Enterprises Ltd.