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[29], *tbl_name;
37 EXEC SQL END DECLARE SECTION;
41 EXEC SQL SELECT modtime INTO :filetime FROM tblstats WHERE tbl = :tbl_name;
42 if (sqlca.sqlcode != 0) {
43 fprintf(stderr, "Query failed: %d\n", sqlca.sqlcode);
46 if (Date2Sec (filetime, &filetimeno)) {
47 fprintf(stderr,"Unable to parse mod. date for file %s\n", tbl);
50 *flag = (filetimeno - ModTime);
55 /***************************************************/
56 /** convert Unix-time (# of seconds since 1970) **/
57 /** from a date-time string **/
58 /** Return 1 if failure, 0 if success **/
59 /***************************************************/
63 struct timeval *UTime;
65 static int defzone = -1;
66 int Day, Month, Year, Hour, Minute, Sec, Err, TotalDays, Iter;
72 if (gettimeofday(&tv, &tz))
75 defzone = tz.tz_minuteswest;
79 printf("Date2Sec(\"%s\", %d.%d)\n", DS, UTime->tv_sec, UTime->tv_usec);
81 Err = ParseDateString (DS, &Day, &Month, &Year, &Hour, &Minute,&Sec);
83 printf("date=%d/%d/%d %d:%d:%d, err=%d\n", Day, Month, Year,
84 Hour, Minute, Sec, Err);
86 if (Err) return (Err);
89 for (TotalDays = 0, Iter = 1969; Iter > Year; Iter--) {
90 TotalDays += NumdaysY(Iter);}
92 printf("Days in years : %d\n", TotalDays);
94 TotalDays += (NumdaysY(Year) - NumdaysM(Month, Year) - Day);
96 printf("Days after months & days: %d\n", TotalDays);
98 UTime->tv_sec = -((((24 * TotalDays) + 24 - Hour) * 60 - Minute) * 60 -
103 for (TotalDays = 0, Iter = 1970; Iter < Year; Iter++) {
104 TotalDays += NumdaysY(Iter);}
106 printf("Days in years : %d\n", TotalDays);
108 TotalDays += NumdaysM(Month, Year);
110 printf("Days after months: %d\n", TotalDays);
112 TotalDays += (Day - 1);
114 printf("Days after Days : %d\n", TotalDays);
116 UTime->tv_sec = (((24 * TotalDays) + Hour) * 60 + Minute) * 60 +
124 /*****************************************/
125 /** Return the # of days in the Year **/
126 /*****************************************/
128 static int NumdaysY(Year)
131 return (365 + Leapyear(Year));
135 /*****************************************/
136 /** Return the # of days in the Month **/
137 /*****************************************/
139 static int NumdaysM(Month, Year)
142 if ((Month > 2) && (Leapyear (Year)))
143 return (MONTHDAYS[Month] + 1);
145 return (MONTHDAYS[Month]);
149 /*****************************************/
150 /** Return 1 if a leapyear, else 0 **/
151 /*****************************************/
153 static int Leapyear (Year)
156 if ((Year % 4) && (!(Year % 100) || (Year % 1000)))
163 /************************************************/
164 /** Compute numeric breakdown of date string **/
165 /** Return 0 if success, 1 if failure **/
166 /************************************************/
168 static int ParseDateString (DS, Day, Month, Year, Hour, Minute, Sec)
170 int *Day, *Month, *Year, *Hour, *Minute, *Sec;
174 int Y=0, H=0, Min=0, S=0, DayNum=0, MonthNum=0;
176 M = (char *)malloc(strlen(DS));
177 D = (char *)malloc(strlen(DS));
178 if (!(M && D)) return (1);
179 Gotten = sscanf (DS, "%[^-]-%[^-]-%d %d:%d:%d", D, M, &Y, &H, &Min, &S);
181 Gotten = sscanf (DS, "%[^/]/%[^/]/%d %d:%d:%d", D, M, &Y, &H, &Min, &S);
183 Gotten = sscanf (DS, "%s %[^,], %d %d:%d:%d", M, D, &Y, &H, &Min, &S);
185 printf ("Month = %s\nDay = %s\nYear = %d\n", M, D, Y);
186 printf ("Hour = %d\nMin= %d\nSec = %d\n", H, Min, S);
188 if ((Gotten < 3) || !(D && M && Y)) {
191 return (1);} /* Couldn't scan in a date */
192 if (atoi(M)) { /* Month not text, so M/D/Y not D/M/Y */
197 MonthNum = MonthNo(M);
201 printf ("Month = %d\nDay = %d\nYear = %d\n", MonthNum, DayNum, Y);
203 if ((DayNum < 1) || (DayNum > 31))
204 return (1); /* Bad Day */
206 return (1); /* Bad Month */
207 if ((Y < 1) || (Y > 10000))
208 return (1); /* Bad Year */
210 return(1); /* Bad Time (Hour only) */
211 if ((Gotten > 4) && (H < 0) || (H > 24))
212 return(1); /* Bad Hour */
213 if ((Gotten > 4) && ((Min < 0) || (Min > 59)))
214 return(1); /* Bad Minute */
215 if ((Gotten > 5) && ((S < 0) || (S > 59)))
216 return(1); /* Bad Second */
219 if (Y < 100) /* For abreviations like 90 for 1990 */
220 Y += 1900; /* (Yes, it will be wrong in 2000) */
237 /***********************************************************/
238 /** Return the Month number of a Month number or string **/
239 /***********************************************************/
241 static int MonthNo (M)
246 if ((atoi(M) > 0) && (atoi(M) < 13))
251 for (Count = 0; Count < 12; Count++) {
252 if (!strcasecmp (M, MONTHNAMES[Count])
253 || !strncasecmp(M, MONTHNAMES[Count], 3))