-/* $Header$
+/* $Id$
*
- * (c) Copyright 1990 by the Massachusetts Institute of Technology.
- * For copying and distribution information, please see the file
- * <mit-copyright.h>.
+ * Copyright (C) 1990-1998 by the Massachusetts Institute of Technology.
+ * For copying and distribution information, please see the file
+ * <mit-copyright.h>.
*/
#include <mit-copyright.h>
+#include <moira.h>
+
+#include <sys/types.h>
+
#include <stdio.h>
#include <stdlib.h>
-#include <sys/types.h>
-#include <sys/stat.h>
-#include <sys/time.h>
-#include <moira.h>
-#include <moira_site.h>
-EXEC SQL INCLUDE sqlca;
-static char *MONTHNAMES[] = {"january", "february", "march", "april", "may",
- "june", "july", "august", "september",
- "october", "november", "december"};
+#include "util.h"
-static int MONTHDAYS[] = {0,0,31,59,90,120,151,181,212,243,273,304,334};
+EXEC SQL INCLUDE sqlca;
+/* Julian day of the UNIX epoch (January 1, 1970) */
+#define UNIX_EPOCH 2440588
/****************************************************************/
/** Return the difference between the modtime & table modtime **/
{
time_t filetimeno;
EXEC SQL BEGIN DECLARE SECTION;
- char filetime[48], *tbl_name;
+ char filetime[48], *ft = filetime, *tbl_name;
EXEC SQL END DECLARE SECTION;
*flag = 0;
tbl_name = tbl;
- EXEC SQL SELECT TO_CHAR(modtime, 'DD-mon-YYYY HH24:MI:SS')
+ /* Get modtime as seconds, minutes, hours, and Julian day */
+ EXEC SQL SELECT TO_CHAR(modtime, 'SS MI HH24 J')
INTO :filetime FROM tblstats
WHERE table_name = :tbl_name;
if (sqlca.sqlcode)
{
- fprintf(stderr, "Moddiff Query failed: %d\n", sqlca.sqlcode);
+ fprintf(stderr, "Moddiff Query failed: %ld\n", sqlca.sqlcode);
return MR_DATE;
}
- if (Date2Sec (filetime, &filetimeno))
- {
- fprintf(stderr, "Unable to parse mod. date for file %s\n", tbl);
- return MR_DATE;
- }
- *flag = (int) (filetimeno - ModTime);
- return 0;
-}
-
-
-/***************************************************/
-/** convert Unix-time (# of seconds since 1970) **/
-/** from a date-time string **/
-/** Return 1 if failure, 0 if success **/
-/***************************************************/
-
-int Date2Sec(char *DS, time_t *UTime)
-{
- static int defzone = -1;
- int Day, Month, Year, Hour, Minute, Sec, Err, TotalDays, Iter;
- struct timeval tv;
- struct timezone tz;
-
- if (defzone == -1)
- {
- if (gettimeofday(&tv, &tz))
- defzone = 0;
- else
- defzone = tz.tz_minuteswest;
- }
-
- Err = ParseDateString (DS, &Day, &Month, &Year, &Hour, &Minute, &Sec);
- if (Err)
- return Err;
-
- if (Year < 1970)
- {
- for (TotalDays = 0, Iter = 1969; Iter > Year; Iter--)
- TotalDays += NumdaysY(Iter);
- TotalDays += (NumdaysY(Year) - NumdaysM(Month, Year) - Day);
- *UTime = -((((24 * TotalDays) + 24 - Hour) * 60 - Minute) * 60 -
- Sec) + defzone;
- return 0;
- }
- else
- {
- for (TotalDays = 0, Iter = 1970; Iter < Year; Iter++)
- TotalDays += NumdaysY(Iter);
- TotalDays += NumdaysM(Month, Year);
- TotalDays += (Day - 1);
- *UTime = (((24 * TotalDays) + Hour) * 60 + Minute) * 60 +
- Sec + defzone;
- return 0;
- }
-}
-
-
-/*****************************************/
-/** Return the # of days in the Year **/
-/*****************************************/
-
-int NumdaysY(int Year)
-{
- return 365 + Leapyear(Year);
-}
-
-
-/*****************************************/
-/** Return the # of days in the Month **/
-/*****************************************/
-
-int NumdaysM(int Month, int Year)
-{
- if ((Month > 2) && (Leapyear (Year)))
- return MONTHDAYS[Month] + 1;
- else
- return MONTHDAYS[Month];
-}
-
-
-/*****************************************/
-/** Return 1 if a leapyear, else 0 **/
-/*****************************************/
-
-int Leapyear(int Year)
-{
- if ((Year % 4) && (!(Year % 100) || (Year % 1000)))
- return 0;
- else
- return 1;
-}
-
-
-/************************************************/
-/** Compute numeric breakdown of date string **/
-/** Return 0 if success, 1 if failure **/
-/************************************************/
-
-int ParseDateString(char *DS, int *Day, int *Month, int *Year, int *Hour,
- int *Minute, int *Sec)
-{
- int Gotten;
- char *M, *D, *Temp;
- int Y = 0, H = 0, Min = 0, S = 0, DayNum = 0, MonthNum = 0;
-
- M = malloc(strlen(DS) + 2);
- D = malloc(strlen(DS) + 2);
- if (!(M && D))
- return 1;
- Gotten = sscanf (DS, "%[^-]-%[^-]-%d %d:%d:%d", D, M, &Y, &H, &Min, &S);
- if (Gotten < 3)
- Gotten = sscanf (DS, "%[^/]/%[^/]/%d %d:%d:%d", D, M, &Y, &H, &Min, &S);
- if (Gotten < 3)
- Gotten = sscanf (DS, "%s %[^,], %d %d:%d:%d", M, D, &Y, &H, &Min, &S);
- if ((Gotten < 3) || !(D && M && Y))
- {
- free(M);
- free(D);
- return 1; /* Couldn't scan in a date */
- }
- if (atoi(M))
- { /* Month not text, so M/D/Y not D/M/Y */
- Temp = M;
- M = D;
- D = Temp;
- }
- DayNum = atoi(D);
- MonthNum = MonthNo(M);
- free(M);
- free(D);
- if ((DayNum < 1) || (DayNum > 31))
- return 1; /* Bad Day */
- if (!MonthNum)
- return 1; /* Bad Month */
- if ((Y < 1) || (Y > 10000))
- return 1; /* Bad Year */
- if (Gotten == 4)
- return 1; /* Bad Time (Hour only) */
- if ((Gotten > 4) && (H < 0) || (H > 24))
- return 1; /* Bad Hour */
- if ((Gotten > 4) && ((Min < 0) || (Min > 59)))
- return 1; /* Bad Minute */
- if ((Gotten > 5) && ((S < 0) || (S > 59)))
- return 1; /* Bad Second */
- *Day = DayNum;
- *Month = MonthNum;
- if (Y < 100) /* For abreviations like 90 for 1990 */
- Y += 1900; /* (Yes, it will be wrong in 2000) */
- *Year = Y;
- if (Gotten > 4)
- {
- *Hour = H;
- *Minute = Min;
- }
- else
- {
- *Hour = 0;
- *Minute = 0;
- }
- if (Gotten > 5)
- *Sec = S;
- else
- *Sec = 0;
- return 0;
-}
-
-/***********************************************************/
-/** Return the Month number of a Month number or string **/
-/***********************************************************/
+ filetimeno = strtol(ft, &ft, 10);
+ filetimeno = filetimeno * 60 + strtol(ft, &ft, 10);
+ filetimeno = filetimeno * 60 + strtol(ft, &ft, 10);
+ filetimeno = filetimeno * 24 + (strtol(ft, NULL, 10) - UNIX_EPOCH);
-int MonthNo(char *M)
-{
- int Count;
- if (atoi(M))
- {
- if ((atoi(M) > 0) && (atoi(M) < 13))
- return atoi(M);
- else
- return 0;
- }
- for (Count = 0; Count < 12; Count++)
- {
- if (!strcasecmp (M, MONTHNAMES[Count])
- || !strncasecmp(M, MONTHNAMES[Count], 3))
- return Count + 1;
- }
+ *flag = (int) (filetimeno - ModTime);
return 0;
}