Skip to content

Instantly share code, notes, and snippets.

@esobchenko
Created September 25, 2018 17:19
Show Gist options
  • Save esobchenko/7e11c05e29f27bcd52f82def0168e687 to your computer and use it in GitHub Desktop.
Save esobchenko/7e11c05e29f27bcd52f82def0168e687 to your computer and use it in GitHub Desktop.
day of the year
#include <stdio.h>
int leap(int year)
{
return (0 == year % 4 && 0 != year % 100) || (0 == year % 400);
}
int day_number(int d, int m, int y)
{
int days_in_mo[12] = {31, 28 + leap(y), 31, 30, 31, 30, 31, 31, 30, 31, 30, 31};
int day_num = 0;
for ( int i = 0; i < (m-1); i++ )
day_num += days_in_mo[i];
day_num += d;
return day_num;
}
int main( int argc, char *argv[] )
{
int day, month, year;
sscanf( argv[1], "%d", &day );
sscanf( argv[2], "%d", &month );
sscanf( argv[3], "%d", &year );
printf( "Day number is: %d\n", day_number(day, month, year) );
return 0;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment