OB.DAAC Logo
NASA Logo
Ocean Color Science Software

ocssw V2022
day2mday.c
Go to the documentation of this file.
1 #define IS_LEAP_YEAR(y) ( (!((y)%4) && (y)%100) || !((y)%400) )
2 
3 int day2mday(int year, int day_of_year, int *month, int *day_of_month)
4 /*------------------------------------------------------------------------------
5  Function: day2mday
6 
7  Returns type:
8 
9  Description: Given a year and a day of that year, determine what the month
10  and day of that month are.
11 
12  This function returns 1 upon successful completion and 0
13  otherwise.
14 
15  Parameters: (in calling order)
16  Type Name I/O Description
17  ---- ---- --- -----------
18  int year I all 4 digits (e.g. 1985 or 2001)
19  int day_of_year I 1 - 365 (or 366 for a leap year)
20  unsigned char ** month O 3 characters plus a terminator
21  int * day_of_month O # of day in month (1st day is 1)
22  Modification history:
23  Programmer Date Description of change
24  ---------- ---- ---------------------
25  Norman Kuring 10-Feb-1993 Original development
26  W. Robinson 25-Oct-1996 modify to output integer month
27 
28 ------------------------------------------------------------------------------*/ {
29 
30  static char days_per_month[] = {
31  31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31
32  };
33 
34  int date;
35  int month_index;
36 
37  if (IS_LEAP_YEAR(year))
38  days_per_month[1] = 29;
39  else
40  days_per_month[1] = 28;
41 
42  for (month_index = 0, date = day_of_year;
43  date > days_per_month[month_index];
44  date -= days_per_month[month_index++])
45  ;
46 
47  if (month_index > 11)
48  return ( -1);
49 
50  *month = month_index + 1;
51  *day_of_month = date;
52 
53  return ( 0);
54 
55 }
#define IS_LEAP_YEAR(y)
Definition: day2mday.c:1
int day2mday(int year, int day_of_year, int *month, int *day_of_month)
Definition: day2mday.c:3