]> andersk Git - moira.git/blob - gen/moddiff.pc
Code style cleanup. (No functional changes)
[moira.git] / gen / moddiff.pc
1 /* $Header$
2  *
3  *  (c) Copyright 1990 by the Massachusetts Institute of Technology.
4  *  For copying and distribution information, please see the file
5  *  <mit-copyright.h>.
6  */
7
8 #include <mit-copyright.h>
9 #include <stdio.h>
10 #include <stdlib.h>
11 #include <sys/types.h>
12 #include <sys/stat.h>
13 #include <sys/time.h>
14 #include <moira.h>
15 #include <moira_site.h>
16 EXEC SQL INCLUDE sqlca;
17
18 static char *MONTHNAMES[] = {"january", "february", "march", "april", "may",
19                              "june", "july", "august", "september",
20                              "october", "november", "december"};
21
22 static int MONTHDAYS[] = {0,0,31,59,90,120,151,181,212,243,273,304,334};
23
24
25 /****************************************************************/
26 /**  Return the difference between the modtime & table modtime **/
27 /**  in flag variable.  Return 0 if success, else errno.       **/
28 /****************************************************************/
29
30 int ModDiff(int *flag, char *tbl, time_t ModTime)
31 {
32   time_t filetimeno;
33   EXEC SQL BEGIN DECLARE SECTION;
34   char filetime[48], *tbl_name;
35   EXEC SQL END DECLARE SECTION;
36
37   *flag = 0;
38   tbl_name = tbl;
39   EXEC SQL SELECT TO_CHAR(modtime, 'DD-mon-YYYY HH24:MI:SS')
40     INTO :filetime FROM tblstats
41     WHERE table_name = :tbl_name;
42   if (sqlca.sqlcode)
43     {
44       fprintf(stderr, "Moddiff Query failed: %d\n", sqlca.sqlcode);
45       return MR_DATE;
46     }
47   if (Date2Sec (filetime, &filetimeno))
48     {
49       fprintf(stderr, "Unable to parse mod. date for file %s\n", tbl);
50       return MR_DATE;
51     }
52   *flag = (int) (filetimeno - ModTime);
53   return 0;
54 }
55
56
57 /***************************************************/
58 /**  convert Unix-time (# of seconds since 1970)  **/
59 /**  from a date-time string                      **/
60 /**  Return 1 if failure, 0 if success            **/
61 /***************************************************/
62
63 int Date2Sec(char *DS, time_t *UTime)
64 {
65   static int defzone = -1;
66   int Day, Month, Year, Hour, Minute, Sec, Err, TotalDays, Iter;
67   struct timeval tv;
68   struct timezone tz;
69
70   if (defzone == -1)
71     {
72       if (gettimeofday(&tv, &tz))
73         defzone = 0;
74       else
75         defzone = tz.tz_minuteswest;
76     }
77
78   Err = ParseDateString (DS, &Day, &Month, &Year, &Hour, &Minute, &Sec);
79   if (Err)
80     return Err;
81
82   if (Year < 1970)
83     {
84       for (TotalDays = 0, Iter = 1969; Iter > Year; Iter--)
85         TotalDays += NumdaysY(Iter);
86       TotalDays += (NumdaysY(Year) - NumdaysM(Month, Year) - Day);
87       *UTime = -((((24 * TotalDays) + 24 - Hour) * 60 - Minute) * 60 -
88                  Sec) + defzone;
89       return 0;
90     }
91   else
92     {
93       for (TotalDays = 0, Iter = 1970; Iter < Year; Iter++)
94         TotalDays += NumdaysY(Iter);
95       TotalDays += NumdaysM(Month, Year);
96       TotalDays += (Day - 1);
97       *UTime = (((24 * TotalDays) + Hour) * 60 + Minute) * 60 +
98         Sec + defzone;
99       return 0;
100     }
101 }
102
103
104 /*****************************************/
105 /**  Return the # of days in the Year   **/
106 /*****************************************/
107
108 int NumdaysY(int Year)
109 {
110   return 365 + Leapyear(Year);
111 }
112
113
114 /*****************************************/
115 /**  Return the # of days in the Month  **/
116 /*****************************************/
117
118 int NumdaysM(int Month, int Year)
119 {
120   if ((Month > 2) && (Leapyear (Year)))
121     return MONTHDAYS[Month] + 1;
122   else
123     return MONTHDAYS[Month];
124 }
125
126
127 /*****************************************/
128 /**  Return 1 if a leapyear, else 0     **/
129 /*****************************************/
130
131 int Leapyear(int Year)
132 {
133   if ((Year % 4) && (!(Year % 100) || (Year % 1000)))
134     return 0;
135   else
136     return 1;
137 }
138
139
140 /************************************************/
141 /**  Compute numeric breakdown of date string  **/
142 /**  Return 0 if success, 1 if failure         **/
143 /************************************************/
144
145 int ParseDateString(char *DS, int *Day, int *Month, int *Year, int *Hour,
146                     int *Minute, int *Sec)
147 {
148   int Gotten;
149   char *M, *D, *Temp;
150   int Y = 0, H = 0, Min = 0, S = 0, DayNum = 0, MonthNum = 0;
151
152   M = malloc(strlen(DS) + 2);
153   D = malloc(strlen(DS) + 2);
154   if (!(M && D))
155     return 1;
156   Gotten = sscanf (DS, "%[^-]-%[^-]-%d %d:%d:%d", D, M, &Y, &H, &Min, &S);
157   if (Gotten < 3)
158     Gotten = sscanf (DS, "%[^/]/%[^/]/%d %d:%d:%d", D, M, &Y, &H, &Min, &S);
159   if (Gotten < 3)
160     Gotten = sscanf (DS, "%s %[^,], %d %d:%d:%d", M, D, &Y, &H, &Min, &S);
161   if ((Gotten < 3) || !(D && M && Y))
162     {
163       free(M);
164       free(D);
165       return 1;               /* Couldn't scan in a date */
166     }
167   if (atoi(M))
168     {               /* Month not text, so M/D/Y not D/M/Y */
169       Temp = M;
170       M = D;
171       D = Temp;
172     }
173   DayNum = atoi(D);
174   MonthNum = MonthNo(M);
175   free(M);
176   free(D);
177   if ((DayNum < 1) || (DayNum > 31))
178     return 1;                /* Bad Day */
179   if (!MonthNum)
180     return 1;                /* Bad Month */
181   if ((Y < 1) || (Y > 10000))
182     return 1;                /* Bad Year */
183   if (Gotten == 4)
184     return 1;                 /* Bad Time (Hour only) */
185   if ((Gotten > 4) && (H < 0) || (H > 24))
186     return 1;                 /* Bad Hour */
187   if ((Gotten > 4) && ((Min < 0) || (Min > 59)))
188     return 1;                 /* Bad Minute */
189   if ((Gotten > 5) && ((S < 0) || (S > 59)))
190     return 1;                 /* Bad Second */
191   *Day = DayNum;
192   *Month = MonthNum;
193   if (Y < 100)                   /* For abreviations like 90 for 1990    */
194     Y += 1900;                   /* (Yes, it will be wrong in 2000) */
195   *Year = Y;
196   if (Gotten > 4)
197     {
198       *Hour = H;
199       *Minute = Min;
200     }
201   else
202     {
203       *Hour = 0;
204       *Minute = 0;
205     }
206   if (Gotten > 5)
207     *Sec = S;
208   else
209     *Sec = 0;
210   return 0;
211 }
212
213
214 /***********************************************************/
215 /**  Return the Month number of a Month number or string  **/
216 /***********************************************************/
217
218 int MonthNo(char *M)
219 {
220   int Count;
221   if (atoi(M))
222     {
223       if ((atoi(M) > 0) && (atoi(M) < 13))
224         return atoi(M);
225       else
226         return 0;
227     }
228   for (Count = 0; Count < 12; Count++)
229     {
230       if (!strcasecmp (M, MONTHNAMES[Count])
231           || !strncasecmp(M, MONTHNAMES[Count], 3))
232         return Count + 1;
233     }
234   return 0;
235 }
This page took 1.256079 seconds and 5 git commands to generate.