3 * (c) Copyright 1990 by the Massachusetts Institute of Technology.
4 * For copying and distribution information, please see the file
8 #include <mit-copyright.h>
10 #include <sys/types.h>
14 #include <moira_site.h>
15 EXEC SQL INCLUDE sqlca;
17 static char * MONTHNAMES[] = {"january", "february", "march", "april", "may",
18 "june", "july", "august", "september",
19 "october", "november", "december"};
21 static int MONTHDAYS[] = {0,0,31,59,90,120,151,181,212,243,273,304,334};
24 /****************************************************************/
25 /** Return the difference between the modtime & table modtime **/
26 /** in flag variable. Return 0 if success, else errno. **/
27 /****************************************************************/
29 int ModDiff (flag, tbl, ModTime)
35 EXEC SQL BEGIN DECLARE SECTION;
36 char filetime[48], *tbl_name;
37 EXEC SQL END DECLARE SECTION;
41 EXEC SQL SELECT TO_CHAR(modtime, 'DD-mon-YYYY HH24:MI:SS') INTO :filetime FROM tblstats
42 WHERE table_name = :tbl_name;
43 if (sqlca.sqlcode != 0) {
44 fprintf(stderr, "Moddiff Query failed: %d\n", sqlca.sqlcode);
48 printf("about to call Date2Sec, flag = 0x%x\n", flag);
50 if (Date2Sec (filetime, &filetimeno)) {
51 fprintf(stderr,"Unable to parse mod. date for file %s\n", tbl);
55 printf("got time, flag = 0x%x\n", flag);
57 *flag = (int) (filetimeno - ModTime);
62 /***************************************************/
63 /** convert Unix-time (# of seconds since 1970) **/
64 /** from a date-time string **/
65 /** Return 1 if failure, 0 if success **/
66 /***************************************************/
72 static int defzone = -1;
73 int Day, Month, Year, Hour, Minute, Sec, Err, TotalDays, Iter;
78 if (gettimeofday(&tv, &tz))
81 defzone = tz.tz_minuteswest;
85 printf("Date2Sec(\"%s\", %d)\n", DS, *UTime);
87 Err = ParseDateString (DS, &Day, &Month, &Year, &Hour, &Minute,&Sec);
89 printf("date=%d/%d/%d %d:%d:%d, err=%d\n", Day, Month, Year,
90 Hour, Minute, Sec, Err);
92 if (Err) return (Err);
95 for (TotalDays = 0, Iter = 1969; Iter > Year; Iter--) {
96 TotalDays += NumdaysY(Iter);}
98 printf("Days in years : %d\n", TotalDays);
100 TotalDays += (NumdaysY(Year) - NumdaysM(Month, Year) - Day);
102 printf("Days after months & days: %d\n", TotalDays);
104 *UTime = -((((24 * TotalDays) + 24 - Hour) * 60 - Minute) * 60 -
108 for (TotalDays = 0, Iter = 1970; Iter < Year; Iter++) {
109 TotalDays += NumdaysY(Iter);}
111 printf("Days in years : %d\n", TotalDays);
113 TotalDays += NumdaysM(Month, Year);
115 printf("Days after months: %d\n", TotalDays);
117 TotalDays += (Day - 1);
119 printf("Days after Days : %d\n", TotalDays);
120 printf("Calculating seconds...(UTime = 0x%x)\n", *UTime);
122 *UTime = (((24 * TotalDays) + Hour) * 60 + Minute) * 60 +
125 printf("Calculated value of %d\n", *UTime);
132 /*****************************************/
133 /** Return the # of days in the Year **/
134 /*****************************************/
136 static int NumdaysY(Year)
139 return (365 + Leapyear(Year));
143 /*****************************************/
144 /** Return the # of days in the Month **/
145 /*****************************************/
147 static int NumdaysM(Month, Year)
150 if ((Month > 2) && (Leapyear (Year)))
151 return (MONTHDAYS[Month] + 1);
153 return (MONTHDAYS[Month]);
157 /*****************************************/
158 /** Return 1 if a leapyear, else 0 **/
159 /*****************************************/
161 static int Leapyear (Year)
164 if ((Year % 4) && (!(Year % 100) || (Year % 1000)))
171 /************************************************/
172 /** Compute numeric breakdown of date string **/
173 /** Return 0 if success, 1 if failure **/
174 /************************************************/
176 static int ParseDateString (DS, Day, Month, Year, Hour, Minute, Sec)
178 int *Day, *Month, *Year, *Hour, *Minute, *Sec;
182 int Y=0, H=0, Min=0, S=0, DayNum=0, MonthNum=0;
184 M = (char *)malloc(strlen(DS) + 2);
185 D = (char *)malloc(strlen(DS) + 2);
186 if (!(M && D)) return (1);
187 Gotten = sscanf (DS, "%[^-]-%[^-]-%d %d:%d:%d", D, M, &Y, &H, &Min, &S);
189 Gotten = sscanf (DS, "%[^/]/%[^/]/%d %d:%d:%d", D, M, &Y, &H, &Min, &S);
191 Gotten = sscanf (DS, "%s %[^,], %d %d:%d:%d", M, D, &Y, &H, &Min, &S);
193 printf ("Month = %s\nDay = %s\nYear = %d\n", M, D, Y);
194 printf ("Hour = %d\nMin= %d\nSec = %d\n", H, Min, S);
196 if ((Gotten < 3) || !(D && M && Y)) {
199 return (1);} /* Couldn't scan in a date */
200 if (atoi(M)) { /* Month not text, so M/D/Y not D/M/Y */
205 MonthNum = MonthNo(M);
209 printf ("Month = %d\nDay = %d\nYear = %d\n", MonthNum, DayNum, Y);
211 if ((DayNum < 1) || (DayNum > 31))
212 return (1); /* Bad Day */
214 return (1); /* Bad Month */
215 if ((Y < 1) || (Y > 10000))
216 return (1); /* Bad Year */
218 return(1); /* Bad Time (Hour only) */
219 if ((Gotten > 4) && (H < 0) || (H > 24))
220 return(1); /* Bad Hour */
221 if ((Gotten > 4) && ((Min < 0) || (Min > 59)))
222 return(1); /* Bad Minute */
223 if ((Gotten > 5) && ((S < 0) || (S > 59)))
224 return(1); /* Bad Second */
227 if (Y < 100) /* For abreviations like 90 for 1990 */
228 Y += 1900; /* (Yes, it will be wrong in 2000) */
245 /***********************************************************/
246 /** Return the Month number of a Month number or string **/
247 /***********************************************************/
249 static int MonthNo (M)
254 if ((atoi(M) > 0) && (atoi(M) < 13))
259 for (Count = 0; Count < 12; Count++) {
260 if (!strcasecmp (M, MONTHNAMES[Count])
261 || !strncasecmp(M, MONTHNAMES[Count], 3))