1 /*:ts=8*/ 2 /***************************************************************************** 3 * FIDOGATE --- Gateway UNIX Mail/News <-> FIDO NetMail/EchoMail 4 * 5 * $Id: gettime.c,v 4.1 1998/01/18 09:47:48 mj Exp $ 6 * 7 * Get system date/time. Taken from ifmail 1.7 / inn 1.4 and adopted 8 * for FIDOGATE . 9 * 10 *****************************************************************************/ 11 /* Original %Revision: 1.4 % 12 ** 13 */ 14 15 #include "fidogate.h" 16 17 #include <sys/time.h> 18 19 20 21 int GetTimeInfo(TIMEINFO * Now)22GetTimeInfo(TIMEINFO *Now) 23 { 24 static time_t LastTime; 25 static long LastTzone; 26 struct tm *tm; 27 #ifdef HAS_GETTIMEOFDAY 28 struct timeval tv; 29 #endif 30 #ifndef HAS_TM_GMTOFF 31 struct tm local; 32 struct tm gmt; 33 #endif 34 35 /* Get the basic time. */ 36 #ifdef HAS_GETTIMEOFDAY 37 if (gettimeofday(&tv, (struct timezone *)NULL) == -1) 38 return -1; 39 Now->time = tv.tv_sec; 40 Now->usec = tv.tv_usec; 41 #else 42 /* Can't check for -1 since that might be a time, I guess. */ 43 (void)time(&Now->time); 44 Now->usec = 0; 45 #endif 46 47 /* Now get the timezone if it's been an hour since the last time. */ 48 if (Now->time - LastTime > 60 * 60) { 49 LastTime = Now->time; 50 if ((tm = localtime(&Now->time)) == NULL) 51 return -1; 52 #ifndef HAS_TM_GMTOFF 53 /* To get the timezone, compare localtime with GMT. */ 54 local = *tm; 55 if ((tm = gmtime(&Now->time)) == NULL) 56 return -1; 57 gmt = *tm; 58 59 /* Assume we are never more than 24 hours away. */ 60 LastTzone = gmt.tm_yday - local.tm_yday; 61 if (LastTzone > 1) 62 LastTzone = -24; 63 else if (LastTzone < -1) 64 LastTzone = 24; 65 else 66 LastTzone *= 24; 67 68 /* Scale in the hours and minutes; ignore seconds. */ 69 LastTzone += gmt.tm_hour - local.tm_hour; 70 LastTzone *= 60; 71 LastTzone += gmt.tm_min - local.tm_min; 72 #else 73 LastTzone = (0 - tm->tm_gmtoff) / 60; 74 #endif 75 } 76 Now->tzone = LastTzone; 77 return 0; 78 } 79