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 (Date2Sec (filetime, &filetimeno)) {
43 fprintf(stderr,"Unable to parse mod. date for file %s\n", tbl);
46 *flag = (filetimeno - ModTime);
51 /***************************************************/
52 /** convert Unix-time (# of seconds since 1970) **/
53 /** from a date-time string **/
54 /** Return 1 if failure, 0 if success **/
55 /***************************************************/
59 struct timeval *UTime;
61 int Day, Month, Year, Hour, Minute, Sec, Err, TotalDays, Iter;
63 Err = ParseDateString (DS, &Day, &Month, &Year, &Hour, &Minute,&Sec);
64 if (Err) return (Err);
66 for (TotalDays = 0, Iter = 1969; Iter > Year; Iter--) {
67 TotalDays += NumdaysY(Iter);}
69 printf("Days in years : %d\n", TotalDays);
71 TotalDays += (NumdaysY(Year) - NumdaysM(Month, Year) - Day);
73 printf("Days after months & days: %d\n", TotalDays);
75 UTime->tv_sec = -((((24 * TotalDays) + 24 - Hour) * 60 - Minute) * 60 -
80 for (TotalDays = 0, Iter = 1970; Iter < Year; Iter++) {
81 TotalDays += NumdaysY(Iter);}
83 printf("Days in years : %d\n", TotalDays);
85 TotalDays += NumdaysM(Month, Year);
87 printf("Days after months: %d\n", TotalDays);
89 TotalDays += (Day - 1);
91 printf("Days after Days : %d\n", TotalDays);
93 UTime->tv_sec = (((24 * TotalDays) + Hour) * 60 + Minute) * 60 + Sec;
100 /*****************************************/
101 /** Return the # of days in the Year **/
102 /*****************************************/
104 static int NumdaysY(Year)
107 return (365 + Leapyear(Year));
111 /*****************************************/
112 /** Return the # of days in the Month **/
113 /*****************************************/
115 static int NumdaysM(Month, Year)
118 if ((Month > 2) && (Leapyear (Year)))
119 return (MONTHDAYS[Month] + 1);
121 return (MONTHDAYS[Month]);
125 /*****************************************/
126 /** Return 1 if a leapyear, else 0 **/
127 /*****************************************/
129 static int Leapyear (Year)
132 if ((Year % 4) && (!(Year % 100) || (Year % 1000)))
139 /************************************************/
140 /** Compute numeric breakdown of date string **/
141 /** Return 0 if success, 1 if failure **/
142 /************************************************/
144 static int ParseDateString (DS, Day, Month, Year, Hour, Minute, Sec)
146 int *Day, *Month, *Year, *Hour, *Minute, *Sec;
150 int Y=0, H=0, Min=0, S=0, DayNum=0, MonthNum=0;
152 M = (char *)malloc(strlen(DS));
153 D = (char *)malloc(strlen(DS));
154 if (!(M && D)) return (0);
155 Gotten = sscanf (DS, "%[^-]-%[^-]-%d %d:%d:%d", D, M, &Y, &H, &Min, &S);
157 Gotten = sscanf (DS, "%[^/]/%[^/]/%d %d:%d:%d", D, M, &Y, &H, &Min, &S);
159 Gotten = sscanf (DS, "%s %[^,], %d %d:%d:%d", M, D, &Y, &H, &Min, &S);
161 printf ("Month = %s\nDay = %s\nYear = %d\n", M, D, Y);
162 printf ("Hour = %d\nMin= %d\nSec = %d\n", H, Min, S);
164 if ((Gotten < 3) || !(D && M && Y)) {
167 return (1);} /* Couldn't scan in a date */
168 if (atoi(M)) { /* Month not text, so M/D/Y not D/M/Y */
173 MonthNum = MonthNo(M);
177 printf ("Month = %d\nDay = %d\nYear = %d\n", MonthNum, DayNum, Y);
179 if ((DayNum < 1) || (DayNum > 31))
180 return (1); /* Bad Day */
182 return (1); /* Bad Month */
183 if ((Y < 1) || (Y > 10000))
184 return (1); /* Bad Year */
186 return(1); /* Bad Time (Hour only) */
187 if ((Gotten > 4) && (H < 0) || (H > 24))
188 return(1); /* Bad Hour */
189 if ((Gotten > 4) && ((Min < 0) || (Min > 59)))
190 return(1); /* Bad Minute */
191 if ((Gotten > 5) && ((S < 0) || (S > 59)))
192 return(1); /* Bad Second */
195 if (Y < 100) /* For abreviations like 90 for 1990 */
196 Y += 1900; /* (Yes, it will be wrong in 2000) */
213 /***********************************************************/
214 /** Return the Month number of a Month number or string **/
215 /***********************************************************/
217 static int MonthNo (M)
222 if ((atoi(M) > 0) && (atoi(M) < 13))
227 for (Count = 0; Count < 12; Count++) {
228 if (!strcasecmp (M, MONTHNAMES[Count])
229 || !strncasecmp(M, MONTHNAMES[Count], 3))