Various Datatypes.
[AROS-Contrib.git] / fish / memomaster / datefunc.c
blob46eeb902b2e6da08262a0a4a6b31f28a8fe89c4e
1 #include <exec/types.h>
2 #include <stdlib.h>
3 #include <string.h>
4 #include <ctype.h>
5 #include <time.h>
7 /* Date functions for MM2 */
9 char *Months = "JANFEBMARAPRMAYJUNJULAUGSEPOCTNOVDEC";
10 char Today_buf[10];
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);
17 char *ctime();
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)
25 int x, month;
26 char m[4];
28 for (x=0 ; x<3 ; x++)
30 m[x] = toupper(*(MonthTxt+x));
33 m[3]='\0';
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))
38 return 0;
39 else
40 return month;
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))
51 *MonthTxt = '\0';
52 return FALSE;
54 MonthNum = (MonthNum - 1) * 3;
55 *MonthTxt = *(Months+MonthNum);
56 *(MonthTxt+1) = *(Months+MonthNum+1);
57 *(MonthTxt+2) = *(Months+MonthNum+2);
58 *(MonthTxt+3) = '\0';
59 return TRUE;
62 /*===========================================================================*/
63 BOOL CheckMemoDate(char *dbuf)
65 char DDay[3];
66 char DYear[3];
68 DDay[0]=dbuf[0];
69 DDay[1]=dbuf[1];
70 DDay[2]='\0';
71 DYear[0]=dbuf[7];
72 DYear[1]=dbuf[8];
73 DYear[2]='\0';
74 return (CheckDate(atoi(DDay), Month2Num((char*)(dbuf+3)), atoi(DYear)));
76 /*==========================================================================*/
77 BOOL CheckDate(int dd, int mm, int yy)
79 int md;
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 */
83 md=cd_md[mm];
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)
99 time_t GMTsecs;
100 char *time_str;
102 GMTsecs = time(NULL);
103 time_str = ctime(&GMTsecs);
104 Today_buf[0] = *(time_str+8);
105 Today_buf[1] = *(time_str+9);
106 Today_buf[2] = '-';
107 Today_buf[3] = *(time_str+4);
108 Today_buf[4] = *(time_str+5);
109 Today_buf[5] = *(time_str+6);
110 Today_buf[6] = '-';
111 Today_buf[7] = *(time_str+22);
112 Today_buf[8] = *(time_str+23);
113 Today_buf[9] = '\0';
116 /*==========================================================================*/
117 int date2days(dd,mm,yy)
118 int 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 */
126 if (wy>=3) /**/
127 lyd=((wy-3)/4)+1; /* work out additions due to leap years */
128 else /**/
129 lyd=0; /**/
130 wm=mm-1; /* whole months in last year */
131 wmd=wm*28; /**/
132 if (((wy-2)%4==0)&&(wm>=2)) /**/
133 wmd++; /* days in whole months in latest year */
134 wmd+=ad[wm]; /**/
135 return wyd+lyd+wmd+dd; /* return total */
138 /*==========================================================================*/