1 #include <exec/types.h>
7 /* Date functions for MM2 */
9 char *Months
= "JANFEBMARAPRMAYJUNJULAUGSEPOCTNOVDEC";
12 int Month2Num(char *MonthTxt
);
13 BOOL
Month2Txt(int MonthNum
, char *MonthTxt
);
14 BOOL
CheckMemoDate(char *dbuf
);
15 BOOL
CheckDate(int dd
, int mm
, int yy
);
16 void Today(char *Today_buf
);
19 /*-----------------------------------------------------------------------
20 * Convert month as MMM (eg JUL) to month number (eg 2 for FEB).
21 * Does not assume case or that text is NULL terminated
23 int Month2Num(char *MonthTxt
)
30 m
[x
] = toupper(*(MonthTxt
+x
));
34 month
=(int) (strstr(Months
, m
) - Months
);
35 if (month
%3 != 0) return 0;
36 month
= (month
/ 3) + 1;
37 if ((month
< 1) || (month
> 12))
43 /*-----------------------------------------------------------------------
44 * Convert month number (eg 3 for March) to three char string in caller
45 * supplied buffer. Return TRUE of FALSE.
47 BOOL
Month2Txt(int MonthNum
, char *MonthTxt
)
49 if ((MonthNum
< 1) || (MonthNum
> 12))
54 MonthNum
= (MonthNum
- 1) * 3;
55 *MonthTxt
= *(Months
+MonthNum
);
56 *(MonthTxt
+1) = *(Months
+MonthNum
+1);
57 *(MonthTxt
+2) = *(Months
+MonthNum
+2);
62 /*===========================================================================*/
63 BOOL
CheckMemoDate(char *dbuf
)
74 return (CheckDate(atoi(DDay
), Month2Num((char*)(dbuf
+3)), atoi(DYear
)));
76 /*==========================================================================*/
77 BOOL
CheckDate(int dd
, int mm
, int yy
)
80 int cd_md
[]={0,31,28,31,30,31,30,31,31,30,31,30,31};
82 if ((mm
<1)||(mm
>12)) return FALSE
; /* Validate month */
84 if (yy
<20) yy
+=100; /* Cope with years from 1920 to 2019 ??? */
85 if ((mm
==2)&&((yy
-68)%4==0))
86 md
++; /* If Feb in a leap year, allow 29th */
87 if (md
<dd
) return FALSE
; /* Validate day */
88 return TRUE
; /* Date passed checks so OK */
92 /*------------------------------------------------------------------------
93 * void Today(char *Today_buf)
94 * Sets up string containing todays date as dd-mmm-yy (eg 04-jan-56) in
95 * buffer supplied by caller. Buffer must be at least 10 bytes long.
97 void Today(char *Today_buf
)
102 GMTsecs
= time(NULL
);
103 time_str
= ctime(&GMTsecs
);
104 Today_buf
[0] = *(time_str
+8);
105 Today_buf
[1] = *(time_str
+9);
107 Today_buf
[3] = *(time_str
+4);
108 Today_buf
[4] = *(time_str
+5);
109 Today_buf
[5] = *(time_str
+6);
111 Today_buf
[7] = *(time_str
+22);
112 Today_buf
[8] = *(time_str
+23);
116 /*==========================================================================*/
117 int date2days(dd
,mm
,yy
)
119 /* Returns number of days since start of 1970, ie 1 Jan 1970 is day 1 */
121 int wy
,wyd
,lyd
,wm
,wmd
;
122 int ad
[]={0,3,3,6,8,11,13,16,19,21,24,26};
124 wy
=yy
-70; /* whole years elapsed */
125 wyd
=wy
*365; /* days in whole years ignoring leap years */
127 lyd
=((wy
-3)/4)+1; /* work out additions due to leap years */
130 wm
=mm
-1; /* whole months in last year */
132 if (((wy
-2)%4==0)&&(wm
>=2)) /**/
133 wmd
++; /* days in whole months in latest year */
135 return wyd
+lyd
+wmd
+dd
; /* return total */
138 /*==========================================================================*/