home *** CD-ROM | disk | FTP | other *** search
/ Il CD di internet / CD.iso / SOURCE / N / TCPIP / NNTP-1.000 / NNTP-1 / nntp.1.5.11t / server / time.c < prev    next >
Encoding:
C/C++ Source or Header  |  1993-10-17  |  4.2 KB  |  221 lines

  1. #ifndef lint
  2. static char    *sccsid = "@(#)$Header: time.c,v 1.13 91/01/13 03:27:14 sob Exp $";
  3. #endif
  4.  
  5. /*
  6.  * Collection of routines for dealing with ASCII time strings.
  7.  * These may actually be useful in their own right.
  8.  */
  9.  
  10. #include "common.h"
  11. #ifdef USG
  12. #include <time.h>
  13. #else not USG
  14. #include <sys/time.h>
  15. #endif not USG
  16.  
  17. /*
  18.  * dtol -- convert date to long integer.  This is not implicitly
  19.  * local time, or any other kind of time, for that matter.  If you
  20.  * pass it a date you think is GMT, you wind up with that number of
  21.  * seconds...
  22.  *
  23.  *    Parameters:        "date_ascii" is in the form "yymmddhhmmss".
  24.  *
  25.  *    Returns:        Long integer containing number
  26.  *                of seconds since 000000 Jan 1, 1970,
  27.  *                and "date".  -1 on error.
  28.  *
  29.  *    Side effects:        None.
  30.  */
  31.  
  32. #define twodigtoi(x)    (((x[0]) - '0') + (x[1] - '0')*10)
  33. #define    dysize(y)    ((y % 4 ? 365 : 366))
  34.  
  35. static    int    dmsize[12] =
  36.     { 31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31 };
  37.  
  38. long
  39. dtol(date_ascii)
  40.     char    *date_ascii;
  41. {
  42.     char    date[32], *date_str;
  43.     char    *lhs, *rhs;
  44.     char    temp;
  45.     long    seconds;
  46.     int    year, month, day, hour, mins, secs;
  47.     int    len, i;
  48.  
  49.     len = strlen(date_ascii);
  50.     if (len != sizeof("yymmddhhmmss")-1)
  51.         return (-1);
  52.  
  53.     (void) strcpy(date, date_ascii);
  54.     date_str = date;
  55.  
  56. #ifdef DEBUG
  57.     if (debug > 1)
  58.         syslog(LOG_DEBUG, "(1) date_str = \"%s\"", date_str);
  59. #endif
  60.     rhs = date_str + len - 1;
  61.     lhs = date_str;
  62.  
  63.     for (; lhs < rhs; ++lhs, --rhs) {
  64.         temp = *lhs;
  65.         *lhs = *rhs;
  66.         *rhs = temp;
  67.         if (!isdigit(temp) || !isdigit(*lhs))
  68.             return (-1);
  69.     }
  70.  
  71.     lhs = date_str;
  72. #ifdef DEBUG
  73.     if (debug > 1)
  74.         syslog(LOG_DEBUG, "(2) date_str = \"%s\"", date_str);
  75. #endif
  76.  
  77.     secs = twodigtoi(lhs);
  78.     lhs += 2;
  79.     mins = twodigtoi(lhs);
  80.     lhs += 2;
  81.     hour = twodigtoi(lhs);
  82.     lhs += 2;
  83.     day = twodigtoi(lhs);
  84.     lhs += 2;
  85.     month = twodigtoi(lhs);
  86.     lhs += 2;
  87.     year = twodigtoi(lhs);
  88.  
  89.     if (month < 1 || month > 12 ||
  90.         day < 1 || day > 31 ||
  91.         mins < 0 || mins > 59 ||
  92.         secs < 0 || secs > 59)
  93.         return (-1);
  94.     if (hour == 24) {
  95.         hour = 0;
  96.         day++;
  97.     }
  98.     if (hour < 0 || hour > 23)
  99.         return (-1);
  100.     seconds = 0;
  101.     year += 1900;
  102.     for (i = 1970; i < year; i++)
  103.         seconds += dysize(i);
  104.     /* Leap year */
  105.     if (dysize(year) == 366 && month >= 3)
  106.         seconds++;
  107.     while (--month)
  108.         seconds += dmsize[month-1];
  109.     seconds += day-1;
  110.     seconds = 24 * seconds + hour;
  111.     seconds = 60 * seconds + mins;
  112.     seconds = 60 * seconds + secs;
  113.  
  114.     return (seconds);
  115. }
  116.  
  117.  
  118. /*
  119.  * ltod -- convert long integer to date string.
  120.  *
  121.  *    Parameters:        "date" is in the number of seconds
  122.  *                since the epoch.
  123.  *
  124.  *    Returns:        Pointer to static data in the form
  125.  *                yymmddhhmmss\0.
  126.  *
  127.  *    Side effects:        None.
  128.  */
  129.  
  130. char *
  131. ltod(date)
  132.     long        date;
  133. {
  134.     static char    timebuf[32];
  135.     struct tm    *tp;
  136.  
  137.     tp = gmtime(&date);
  138.  
  139.     (void) sprintf(timebuf, "%02d%02d%02d%02d%02d%02d",
  140.         tp->tm_year,
  141.         tp->tm_mon + 1,        /* 0 to 11??? How silly. */
  142.         tp->tm_mday,
  143.         tp->tm_hour,
  144.         tp->tm_min,
  145.         tp->tm_sec);
  146.  
  147.     return (timebuf);
  148. }
  149.  
  150.  
  151. /*
  152.  * local_to_gmt -- convert a local time (in form of number of
  153.  * seconds since you-know-when) to GMT.
  154.  *
  155.  *    Parameters:    "date" is date we want converted, in
  156.  *            seconds since 000000 1 Jan 1970.
  157.  *
  158.  *    Returns:    Number of seconds corrected for local
  159.  *            and dst.
  160.  */
  161.  
  162. long
  163. local_to_gmt(date)
  164.     long    date;
  165. {
  166. #ifdef USG
  167. #if !defined(dgux) && !defined(M_XENIX)
  168.     extern    long    timezone;
  169. #endif
  170.     tzset();
  171.     date += timezone;
  172. #else /* not USG */
  173.     struct    timeval    tv;
  174.     struct    timezone tz;
  175.  
  176.     (void) gettimeofday(&tv, &tz);
  177.     date += (long) tz.tz_minuteswest * 60;
  178. #endif /* not USG */
  179.  
  180.     /* now fix up local daylight time */
  181.     if (localtime((time_t *)&date)->tm_isdst)
  182.         date -= 60*60;
  183.  
  184.     return (date);
  185. }
  186.  
  187. /*
  188.  * gmt_to_local -- take a GMT time expressed in seconds since
  189.  * the epoch, and convert it to local time.
  190.  *
  191.  *    Parameters:    "date" is the number of seconds...
  192.  *
  193.  *    Returns:    Number of seconds corrected to reflect
  194.  *            local time and dst.
  195.  */
  196.  
  197. long
  198. gmt_to_local(date)
  199.     long    date;
  200. {
  201. #ifdef USG
  202. #if !defined(dgux) && !defined(M_XENIX)
  203.     extern    long    timezone;
  204. #endif
  205.     tzset();
  206.     date -= timezone;
  207. #else /* not USG */
  208.     struct    timeval    tv;
  209.     struct    timezone tz;
  210.  
  211.     (void) gettimeofday(&tv, &tz);
  212.     date -= (long) tz.tz_minuteswest * 60;
  213. #endif /* not USG */
  214.  
  215.     /* now fix up local daylight time */
  216.     if (localtime((time_t *)&date)->tm_isdst)
  217.         date += 60*60;
  218.  
  219.     return (date);
  220. }
  221.