Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Mike Lewis on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

C/UNIX date program for my homework ;-((

Status
Not open for further replies.

Guest_imported

New member
Jan 1, 1970
0
0
0
kara-t (Visitor) Jul 31, 2001
Hello;
Please help me
I have an assignment due to this thursday. I need to write a program that takes the year and number of days of the year and returns exact day in that year:
Lets say:
year: 2000
days: 56
OUTPUT: Sunday, Feb 25-th

Would you please help me?
Thanks
 
Well... i would make an array of size 12 and in it put the max days the months have. Enumerate the month names. To determine a leap year

if Year%4 == 0 // leap year

loop

// if it is a leap year set months[1] = 29 else = 28
int total = 0;
int i;
for(i=0;total < days,total+=months[i++];)
{
} // does nothing except determine the month

int current_month = i-1;
int difference = total - days;

the day is month[current_month]-difference;

switch on current month to determine the month

Matt




total
 
Here's one way of achieving what you need ...

[ignore]#include<stdio.h>
#include<time.h> [/ignore]/* not needed if weekday is not required */[ignore]

char *month[] = {&quot;January&quot;, &quot;February&quot;, &quot;March&quot;, &quot;April&quot;, &quot;May&quot;, &quot;June&quot;, &quot;July&quot;, &quot;August&quot;, &quot;September&quot;, &quot;October&quot;, &quot;November&quot;, &quot;December&quot;};
int days[2][12] = {{31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31},
{31, 29, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31}};
char *weekdays[] = {&quot;Sunday&quot;, &quot;Monday&quot;, &quot;Tuesday&quot;, &quot;Wednesday&quot;, &quot;Thursday&quot;, &quot;Friday&quot;, &quot;Saturday&quot;};

int main()
{
int leap, date, year, i = 0, day;
int getday(int year, int month, int date);

printf(&quot;Enter year and number of days ... &quot;);
scanf(&quot;%d %d&quot;, &year, &date);

leap = ( !(year % 4) && (year % 100) || !(year % 400) )? 1 : 0;
[/ignore]/* a year is leap if it divisible by 4 and not divisible by 100
OR if it is divisible by 400 */
[ignore]

while(date > days[leap])
date -= days[leap][i++];

if(i > 11)
printf(&quot;Too many days!!\n&quot;);
else {
printf(&quot;%s, %d&quot;, month, date);
[/ignore]/* the following is not needed if weekday is not required */[ignore]
if ((day = getday(year, i, date)) != -1)
printf(&quot;, %s&quot;, weekdays[day]);
}
printf(&quot;\n&quot;);

return 0;
}

int getday(int year, int month, int date) [/ignore]/* not needed if weekday is not required */[ignore]
{
struct tm ToFindDay;

ToFindDay.tm_year = year - 1900;
ToFindDay.tm_mon = month;
ToFindDay.tm_mday = date;
ToFindDay.tm_min = 0;
ToFindDay.tm_hour = 12;
ToFindDay.tm_sec = 0;

if(mktime(&ToFindDay) == -1)
return -1;
else
return ToFindDay.tm_wday;
}[/ignore]

By the way, mktime function may not work in unix. I'm not sure.:)

Hope this helps. Bye.
Ankan.

Please do correct me if I am wrong. s-)
 
I wrote and tested this under SCO Unix. Enjoy.
/*
A Time Example by C Williams
c.williams@swflug.org
for educational purposes only
built on SCO Unix (OpenServer 5.01) Aug 1, 2001
*/

/* our handy include files */
#include <stdio.h>
#include <stdlib.h>
#include <sys/types.h>
#include <time.h>


/* some useful constants */
const static char *months[] = { &quot;Jan&quot;,
&quot;Feb&quot;,
&quot;Mar&quot;,
&quot;Apr&quot;,
&quot;May&quot;,
&quot;Jun&quot;,
&quot;Jul&quot;,
&quot;Aug&quot;,
&quot;Sep&quot;,
&quot;Oct&quot;,
&quot;Nov&quot;,
&quot;Dec&quot;
};

const static char *days[] = { &quot;Sunday&quot;,
&quot;Monday&quot;,
&quot;Tuesday&quot;,
&quot;Wednesday&quot;,
&quot;Thursday&quot;,
&quot;Friday&quot;,
&quot;Saturday&quot;
};

const static char *suffix[] = { &quot;st&quot;,
&quot;nd&quot;,
&quot;rd&quot;,
&quot;th&quot;
};

/* prototype for local fucntion */
char *timeFunc(int yr, int dy);

int main(int argc, char **argv)
{
char kbBuffer[255]; // keyboard buffer
int year; // year
int days; // days

// get input from console and format it to an integer for year
printf(&quot;Please enter a year (Range = 1900+): &quot;);
fgets(kbBuffer,255,stdin);
year = atoi(kbBuffer);
// handle errors
if (year < 1900)
{
printf(&quot;Year out of range.\n&quot;);
return 1;
}

// get input from console and format it to an integer for days
printf(&quot;Please the number of days: &quot;);
fgets(kbBuffer,255,stdin);
days = atoi(kbBuffer);
// handle errors
if (days < 0)
{
printf(&quot;Day out of range.\n&quot;);
return 1;
}

// do output, using our timeFunc
printf(&quot;Output: %s\n&quot;, timeFunc(year,days));

return 0;


}

char *timeFunc(int yr, int dy)
{
char timestr[255]; // output string buffer
struct tm tmvar; // a tm structure we'll use
time_t tempvar, timevar; // some time_t variables we'll need
int weekday; // day of week
int month; // number of month
int day; // number of day
int lastdigit; // last digit of day
int sufxnum; // number of suffix to append

time(&tempvar); // get time & store in tempvar
tmvar = *localtime(&tempvar); // convert tempvar to a tm struct

tmvar.tm_mday = dy; // set the day
tmvar.tm_mon = 0; // reset month
tmvar.tm_year = yr - 1900; // set the year
tmvar.tm_yday = 0; // rest the day of year

timevar = mktime(&tmvar); // use mktime to update timevar
// handle errors
if (timevar == (time_t)(-1))
{
printf(&quot;Mktime() failed.\n&quot;);
exit (1);
}

tmvar = *localtime(&timevar); // convert timevar to a tm struct

// set our local integer variables
weekday = tmvar.tm_wday;
month = tmvar.tm_mon;
day = tmvar.tm_mday;

lastdigit = day - (10*(int)(day/10)); // get last digit
// we'll use do decide
// suffix &quot;st&quot;, &quot;nd&quot;, &quot;rd&quot;, &quot;st&quot;
/* conditional to pick suffix for day of month */
switch (lastdigit)
{
case 1:
sufxnum = 0;
break;
case 2:
sufxnum = 1;
break;
case 3:
sufxnum = 2;
break;
default:
sufxnum = 3;
break;
}

/* now we'll format our output string with the data */
sprintf(timestr,&quot;%s, %s %i-%s\0&quot;, days[weekday], months[month], day, suffix[sufxnum]);


return timestr;
}
 
By the way,
Feb 25, 2000 is Friday not Sunday.
Feb 25, 2001 is Sunday.:)

and since mktime seems to be working in unix/linux, there's nothing to worry.
Ankan.

Please do correct me if I am wrong. s-)
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top