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 modtime INTO :filetime FROM tblstats
42 WHERE table_name = :tbl_name;
43 if (sqlca.sqlcode != 0) {
44 if (sqlca.sqlcode == -37000 ||
45 sqlca.sqlcode == 17700)
47 fprintf(stderr, "Moddiff Query failed: %d\n", sqlca.sqlcode);
51 printf("about to call Date2Sec, flag = 0x%x\n", flag);
53 if (Date2Sec (filetime, &filetimeno)) {
54 fprintf(stderr,"Unable to parse mod. date for file %s\n", tbl);
58 printf("got time, flag = 0x%x\n", flag);
60 *flag = (int) (filetimeno - ModTime);
65 /***************************************************/
66 /** convert Unix-time (# of seconds since 1970) **/
67 /** from a date-time string **/
68 /** Return 1 if failure, 0 if success **/
69 /***************************************************/
75 static int defzone = -1;
76 int Day, Month, Year, Hour, Minute, Sec, Err, TotalDays, Iter;
81 if (gettimeofday(&tv, &tz))
84 defzone = tz.tz_minuteswest;
88 printf("Date2Sec(\"%s\", %d)\n", DS, *UTime);
90 Err = ParseDateString (DS, &Day, &Month, &Year, &Hour, &Minute,&Sec);
92 printf("date=%d/%d/%d %d:%d:%d, err=%d\n", Day, Month, Year,
93 Hour, Minute, Sec, Err);
95 if (Err) return (Err);
98 for (TotalDays = 0, Iter = 1969; Iter > Year; Iter--) {
99 TotalDays += NumdaysY(Iter);}
101 printf("Days in years : %d\n", TotalDays);
103 TotalDays += (NumdaysY(Year) - NumdaysM(Month, Year) - Day);
105 printf("Days after months & days: %d\n", TotalDays);
107 *UTime = -((((24 * TotalDays) + 24 - Hour) * 60 - Minute) * 60 -
111 for (TotalDays = 0, Iter = 1970; Iter < Year; Iter++) {
112 TotalDays += NumdaysY(Iter);}
114 printf("Days in years : %d\n", TotalDays);
116 TotalDays += NumdaysM(Month, Year);
118 printf("Days after months: %d\n", TotalDays);
120 TotalDays += (Day - 1);
122 printf("Days after Days : %d\n", TotalDays);
123 printf("Calculating seconds...(UTime = 0x%x)\n", *UTime);
125 *UTime = (((24 * TotalDays) + Hour) * 60 + Minute) * 60 +
128 printf("Calculated value of %d\n", *UTime);
135 /*****************************************/
136 /** Return the # of days in the Year **/
137 /*****************************************/
139 static int NumdaysY(Year)
142 return (365 + Leapyear(Year));
146 /*****************************************/
147 /** Return the # of days in the Month **/
148 /*****************************************/
150 static int NumdaysM(Month, Year)
153 if ((Month > 2) && (Leapyear (Year)))
154 return (MONTHDAYS[Month] + 1);
156 return (MONTHDAYS[Month]);
160 /*****************************************/
161 /** Return 1 if a leapyear, else 0 **/
162 /*****************************************/
164 static int Leapyear (Year)
167 if ((Year % 4) && (!(Year % 100) || (Year % 1000)))
174 /************************************************/
175 /** Compute numeric breakdown of date string **/
176 /** Return 0 if success, 1 if failure **/
177 /************************************************/
179 static int ParseDateString (DS, Day, Month, Year, Hour, Minute, Sec)
181 int *Day, *Month, *Year, *Hour, *Minute, *Sec;
185 int Y=0, H=0, Min=0, S=0, DayNum=0, MonthNum=0;
187 M = (char *)malloc(strlen(DS) + 2);
188 D = (char *)malloc(strlen(DS) + 2);
189 if (!(M && D)) return (1);
190 Gotten = sscanf (DS, "%[^-]-%[^-]-%d %d:%d:%d", D, M, &Y, &H, &Min, &S);
192 Gotten = sscanf (DS, "%[^/]/%[^/]/%d %d:%d:%d", D, M, &Y, &H, &Min, &S);
194 Gotten = sscanf (DS, "%s %[^,], %d %d:%d:%d", M, D, &Y, &H, &Min, &S);
196 printf ("Month = %s\nDay = %s\nYear = %d\n", M, D, Y);
197 printf ("Hour = %d\nMin= %d\nSec = %d\n", H, Min, S);
199 if ((Gotten < 3) || !(D && M && Y)) {
202 return (1);} /* Couldn't scan in a date */
203 if (atoi(M)) { /* Month not text, so M/D/Y not D/M/Y */
208 MonthNum = MonthNo(M);
212 printf ("Month = %d\nDay = %d\nYear = %d\n", MonthNum, DayNum, Y);
214 if ((DayNum < 1) || (DayNum > 31))
215 return (1); /* Bad Day */
217 return (1); /* Bad Month */
218 if ((Y < 1) || (Y > 10000))
219 return (1); /* Bad Year */
221 return(1); /* Bad Time (Hour only) */
222 if ((Gotten > 4) && (H < 0) || (H > 24))
223 return(1); /* Bad Hour */
224 if ((Gotten > 4) && ((Min < 0) || (Min > 59)))
225 return(1); /* Bad Minute */
226 if ((Gotten > 5) && ((S < 0) || (S > 59)))
227 return(1); /* Bad Second */
230 if (Y < 100) /* For abreviations like 90 for 1990 */
231 Y += 1900; /* (Yes, it will be wrong in 2000) */
248 /***********************************************************/
249 /** Return the Month number of a Month number or string **/
250 /***********************************************************/
252 static int MonthNo (M)
257 if ((atoi(M) > 0) && (atoi(M) < 13))
262 for (Count = 0; Count < 12; Count++) {
263 if (!strcasecmp (M, MONTHNAMES[Count])
264 || !strncasecmp(M, MONTHNAMES[Count], 3))