I need to write a function in C that will be passed the current julian date for the current year for e.g
333 is today 28/11/2008 format required 20081128
60 is the 29/02/2008 format required 20080229
I have a function but it is very cumbersome and I am sure it could be far simpler than the way it is written.
Code:
char* getpaydate(char *date)
{
time_t currtime;
char charJuli[8] = {0};
char charYear[6] = {0};
char caldate[8] = {0};
int Yr,Dy,JDD;
int q = 0;
int Mt[] = {31,28,31,30,31,30,31,31,30,31,30,31};
time(&currtime);
strftime(charJuli,sizeof(charJuli)-1,"%j",localtime(&currtime));
strftime(charYear,sizeof(charYear)-1,"%Y",localtime(&currtime));
Yr = atoi(charYear);
JDD = atoi(charJuli);
Dy = atoi(date);
if (Dy > 345 && JDD < 20)
{
Yr--;
}
if ((Yr/4)*4==Yr)
{
Mt[1] = 29;
}
if (((Yr/100)*100==Yr)&&((Yr/400)*400!=Yr))
{
Mt[1] = 28;
}
while ( Dy > Mt[q] )
{
Dy = Dy - Mt[q];
q++;
}
q++;
sprintf(caldate,"%i%02i%02i",Yr,q,Dy);
printf("Start of the debug:%s\n", date );
printf("Value of date equals :%s\n", date );
printf("Value of charJuli equals :%s\n", charJuli );
printf("Value of charYear equals :%s\n", charYear );
printf("Value of caldate equals :%s\n", caldate );
printf("Value of Yr equals :%d\n", Yr );
printf("Value of JDD equals :%d\n", JDD );
printf("Value of Dy equals :%d\n", Dy );
printf("End of Initial debug:%s\n", date );
return caldate;
}
This if passed in a day of the year will get the current year and return the gregorian date 2008mmdd - so if it were returning today's date you would pass in the number 333 to get 20081128 and say 60 to get 20080229 (leap year).
Does this make sense ?
There was a super post of a function that presented the current julian date see below.
Code:
#include <stdio.h>
#include <time.h>
int main(void)
{
time_t now;
if ( time(&now) != (time_t)(-1) )
{
struct tm *mytime = localtime(&now);
if ( mytime )
{
char jday [ 4 ];
if ( strftime(jday, sizeof jday, "%j", mytime) )
{
printf("mytime->tm_yday = %d, jday = \"%s\"\n", mytime->tm_yda
y, jday);
}
}
}
return 0;
}
Thanks in advance.