home *** CD-ROM | disk | FTP | other *** search
/ InfoMagic Internet Tools 1993 July / Internet Tools.iso / RockRidge / mail / pp / pp-6.0 / Lib / format / UTC2rfc.c < prev    next >
Encoding:
C/C++ Source or Header  |  1991-12-18  |  2.5 KB  |  133 lines

  1. /* UTC2rfc.c - Converts a UTC struct into an RFC string */
  2.  
  3. # ifndef lint
  4. static char Rcsid[] = "@(#)$Header: /xtel/pp/pp-beta/Lib/format/RCS/UTC2rfc.c,v 6.0 1991/12/18 20:22:06 jpo Rel $";
  5. # endif
  6.  
  7. /*
  8.  * $Header: /xtel/pp/pp-beta/Lib/format/RCS/UTC2rfc.c,v 6.0 1991/12/18 20:22:06 jpo Rel $
  9.  *
  10.  * $Log: UTC2rfc.c,v $
  11.  * Revision 6.0  1991/12/18  20:22:06  jpo
  12.  * Release 6.0
  13.  *
  14.  */
  15.  
  16.  
  17.  
  18. #include "util.h"
  19. #include <isode/cmd_srch.h>
  20. #include <sys/time.h>
  21.  
  22.  
  23. #define UYEAR(x)        ((x) >= 100 ? (x) - 1900 : (x))
  24. #define YEAR(x)         ((x) >= 100 ? (x) : (x) + 1900)
  25. #define dysize(x)       (((x) % 4) ? 365 : (((x) % 100) ? 366 : \
  26.                         (((x) % 400) ? 365 : 366)))
  27.  
  28. static char  *Day []  = {
  29.     "Sat",
  30.     "Sun",
  31.     "Mon",
  32.     "Tue",
  33.     "Wed",
  34.     "Thu",
  35.     "Fri"
  36.     };
  37.  
  38. extern int dmsize[];
  39. extern CMD_TABLE        tbl_month[];
  40. extern time_t time ();
  41. extern    int abs ();
  42. static int makewkday ();
  43.  
  44.  
  45. /* ---------------------  Begin  Routines  -------------------------------- */
  46.  
  47.  
  48.  
  49. /*
  50. This routine produces a RFC 822 standard time string like:
  51.     "Tue, 16 Aug 88 10:23:21 BST"
  52. from a UTC structure.
  53. */
  54.  
  55.  
  56. int    UTC2rfc (utc, buffer)
  57. UTC     utc;
  58. char    *buffer;
  59. {
  60.     char    zone[10];
  61.     char secs[10];
  62.     int     wday;
  63.  
  64.     *buffer = '\0';
  65.  
  66.     if (utc == NULLUTC) {
  67.         PP_LOG (LLOG_EXCEPTIONS, ("NULL UTC time"));
  68.         return NOTOK;
  69.     }
  70.     
  71.     if (utc -> ut_mon <= 0 || utc -> ut_mon > 12) {
  72.         PP_LOG (LLOG_EXCEPTIONS, ("Lib/UTC2rfc zero UTC specified"));
  73.         return NOTOK;
  74.     }
  75.  
  76.     if (utc -> ut_flags & UT_ZONE)
  77.         (void) sprintf (zone, "%c%02d%02d",
  78.                 utc -> ut_zone < 0 ? '-' : '+',
  79.                 abs(utc -> ut_zone / 60),
  80.                 abs(utc -> ut_zone % 60));
  81.     else
  82.         (void) strcpy (zone, "+0000");
  83.  
  84.     if (utc -> ut_flags & UT_SEC)
  85.         (void) sprintf (secs, ":%02d", utc -> ut_sec);
  86.     else    secs[0] = 0;
  87.  
  88.     wday = makewkday (utc);
  89.     if (wday < 0 || wday > 6) {
  90.         PP_LOG (LLOG_EXCEPTIONS, ("Bad weekday calculated %d", wday));
  91.         return NOTOK;
  92.     }
  93.  
  94.     (void) sprintf (buffer, "%s, %d %s %d %02d:%02d%s %s",
  95.             Day [wday],
  96.             utc -> ut_mday,
  97.             rcmd_srch (utc -> ut_mon - 1, tbl_month),
  98.             YEAR(utc -> ut_year),
  99.             utc -> ut_hour,
  100.             utc -> ut_min,
  101.             secs,
  102.             zone);
  103.  
  104.     PP_DBG (("Lib/UTC2rfc returns (%s)", buffer));
  105.  
  106.     return OK;
  107. }
  108.  
  109. static int makewkday (ut)
  110. UTC     ut;
  111. {
  112.     int     year;
  113.     int     mon;
  114.     int     d;
  115.  
  116.     mon = ut -> ut_mon;
  117.     year = YEAR (ut -> ut_year);
  118.     d = 4 + year + (year+3)/4;
  119.  
  120.     if (year > 1800) {
  121.         d -= (year - 1701)/100;
  122.         d += (year - 1601)/400;
  123.     }
  124.     if (year > 1752)
  125.         d += 3;
  126.     if (dysize (year) == 366 && ut -> ut_mon >= 3)
  127.         d ++;
  128.     while (--mon)
  129.         d += dmsize[mon - 1];
  130.     d += ut -> ut_mday;
  131.     return (d % 7);
  132. }
  133.