#include <stdio.h>
#include <string.h>
#include <time.h>
int daysOfYear( char *dateStr )
{
char year[4+1];
char mon[2+1];
char day[2+1];
char hour[2+1];
char min[2+1];
char sec[2+1];
struct tm *p;
time_t now;
time_t in;
memset( year, 0x00, sizeof(year) );
memset( mon, 0x00, sizeof(mon) );
memset( day, 0x00, sizeof(day) );
memset( hour, 0x00, sizeof(hour) );
memset( min, 0x00, sizeof(min) );
memset( sec, 0x00, sizeof(sec) );
if ( strlen(dateStr) != 8 )
{
printf( "dateStr:%s is invalid, must be in format: yyyymmdd\n", dateStr );
return -1;
}
time( &now);
p = localtime( &now );
strncpy( year, dateStr, 4 );
strncpy( mon, dateStr+4, 2 );
strncpy( day, dateStr+6, 2 );
strncpy( hour, "00", 2 );
strncpy( min, "00", 2 );
strncpy( sec, "01", 2 );
p->tm_year = atoi(year) - 1900;
p->tm_mon = atoi(mon) - 1;
p->tm_mday = atoi(day);
p->tm_hour = atoi(hour);
p->tm_min = atoi( min );
p->tm_sec = atoi( sec );
in = mktime( p );
p = localtime( &in );
return p->tm_yday+1;
}
void main()
{
char date[20];
printf( "input a date(yyyymmdd):" );
scanf( "%s", date );
printf( "%s 是一年的第 %d 天\n", date, daysOfYear(date) );
}