-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathcaldate_fmjd.c
48 lines (39 loc) · 992 Bytes
/
caldate_fmjd.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
#include "caldate.h"
void caldate_frommjd(cd,day,pwday,pyday)
struct caldate *cd;
long day;
int *pwday;
int *pyday;
{
long year;
long month;
int yday;
year = day / 146097L;
day %= 146097L;
day += 678881L;
while (day >= 146097L) { day -= 146097L; ++year; }
/* year * 146097 + day - 678881 is MJD; 0 <= day < 146097 */
/* 2000-03-01, MJD 51604, is year 5, day 0 */
if (pwday) *pwday = (day + 3) % 7;
year *= 4;
if (day == 146096L) { year += 3; day = 36524L; }
else { year += day / 36524L; day %= 36524L; }
year *= 25;
year += day / 1461;
day %= 1461;
year *= 4;
yday = (day < 306);
if (day == 1460) { year += 3; day = 365; }
else { year += day / 365; day %= 365; }
yday += day;
day *= 10;
month = (day + 5) / 306;
day = (day + 5) % 306;
day /= 10;
if (month >= 10) { yday -= 306; ++year; month -= 10; }
else { yday += 59; month += 2; }
cd->year = year;
cd->month = month + 1;
cd->day = day + 1;
if (pyday) *pyday = yday;
}