home *** CD-ROM | disk | FTP | other *** search
/ InfoMagic Internet Tools 1993 July / Internet Tools.iso / RockRidge / mail / elm / elm2.4 / src / date.c < prev    next >
Encoding:
C/C++ Source or Header  |  1992-12-10  |  6.2 KB  |  236 lines

  1.  
  2. static char rcsid[] = "@(#)$Id: date.c,v 5.4 1992/12/11 01:45:04 syd Exp $";
  3.  
  4. /*******************************************************************************
  5.  *  The Elm Mail System  -  $Revision: 5.4 $   $State: Exp $
  6.  *
  7.  *             Copyright (c) 1988-1992 USENET Community Trust
  8.  *             Copyright (c) 1986,1987 Dave Taylor
  9.  *******************************************************************************
  10.  * Bug reports, patches, comments, suggestions should be sent to:
  11.  *
  12.  *    Syd Weinstein, Elm Coordinator
  13.  *    elm@DSI.COM            dsinc!elm
  14.  *
  15.  *******************************************************************************
  16.  * $Log: date.c,v $
  17.  * Revision 5.4  1992/12/11  01:45:04  syd
  18.  * remove sys/types.h include, it is now included by defs.h
  19.  * and this routine includes defs.h or indirectly includes defs.h
  20.  * From: Syd
  21.  *
  22.  * Revision 5.3  1992/12/07  02:57:09  syd
  23.  * convert long to time_t where relevant
  24.  * From: Syd via prompting from Jim Brown
  25.  *
  26.  * Revision 5.2  1992/11/15  02:10:11  syd
  27.  * remove no longer used tzname
  28.  * From: Syd
  29.  *
  30.  * Revision 5.1  1992/10/03  22:58:40  syd
  31.  * Initial checkin as of 2.4 Release at PL0
  32.  *
  33.  *
  34.  ******************************************************************************/
  35.  
  36. /** return the current date and time in a readable format! **/
  37. /** also returns an ARPA RFC-822 format date...            **/
  38.  
  39.  
  40. #include "headers.h"
  41.  
  42. #ifdef I_TIME
  43. #  include <time.h>
  44. #endif
  45. #ifdef I_SYSTIME
  46. #  include <sys/time.h>
  47. #endif
  48. #ifdef BSD
  49. #  include <sys/timeb.h>
  50. #endif
  51.  
  52. #include <ctype.h>
  53.  
  54. #ifndef    _POSIX_SOURCE
  55. extern struct tm *localtime();
  56. extern struct tm *gmtime();
  57. extern time_t      time();
  58. #endif
  59.  
  60. #ifdef BSD
  61. #undef tolower
  62. #endif
  63.  
  64. #define MONTHS_IN_YEAR    11    /* 0-11 equals 12 months! */
  65. #define FEB         1    /* 0 = January           */
  66. #define DAYS_IN_LEAP_FEB 29    /* leap year only       */
  67.  
  68. #define ampm(n)        (n > 12? n - 12 : n)
  69. #define am_or_pm(n)    (n > 11? (n > 23? "am" : "pm") : "am")
  70. #define leapyear(year)    ((year % 4 == 0) && ((year % 100 != 0) || (year % 400 == 0)))
  71.  
  72. char *arpa_dayname[] = { "Sun", "Mon", "Tue", "Wed", "Thu",
  73.           "Fri", "Sat", "" };
  74.  
  75. char *arpa_monname[] = { "Jan", "Feb", "Mar", "Apr", "May", "Jun",
  76.           "Jul", "Aug", "Sep", "Oct", "Nov", "Dec", ""};
  77.  
  78. int  days_in_month[] = { 31,    28,    31,    30,    31,     30, 
  79.           31,     31,    30,   31,    30,     31,  -1};
  80.  
  81. days_ahead(days, buffer)
  82. int days;
  83. char *buffer;
  84. {
  85.     /** return in buffer the date (Day, Mon Day, Year) of the date
  86.         'days' days after today.  
  87.     **/
  88.  
  89.     struct tm *the_time;        /* Time structure, see CTIME(3C) */
  90.     time_t       junk;        /* time in seconds....         */
  91.  
  92.     junk = time((time_t *) 0);    /* this must be here for it to work! */
  93.     the_time = localtime(&junk);
  94.  
  95.     /* increment the day of the week */
  96.  
  97.     the_time->tm_wday = (the_time->tm_wday + days) % 7;
  98.  
  99.     /* the day of the month... */
  100.     the_time->tm_mday += days;
  101.     
  102.         while (the_time->tm_mday > days_in_month[the_time->tm_mon]) {
  103.           if (the_time->tm_mon == FEB && leapyear(the_time->tm_year)) {
  104.             if (the_time->tm_mday > DAYS_IN_LEAP_FEB) {
  105.               the_time->tm_mday -= DAYS_IN_LEAP_FEB;
  106.               the_time->tm_mon += 1;
  107.             }
  108.             else
  109.               break;            /* Is Feb 29, so leave */
  110.           }
  111.           else {
  112.             the_time->tm_mday -= days_in_month[the_time->tm_mon];
  113.             the_time->tm_mon += 1;
  114.           }
  115.  
  116.           /* check the month of the year */
  117.           if (the_time->tm_mon > MONTHS_IN_YEAR) {
  118.             the_time->tm_mon -= (MONTHS_IN_YEAR + 1);
  119.             the_time->tm_year += 1;
  120.           }
  121.         }
  122.   
  123.         /* now, finally, build the actual date string */
  124.  
  125.     sprintf(buffer, "%s, %d %s %d",
  126.       arpa_dayname[the_time->tm_wday],
  127.       the_time->tm_mday % 32,
  128.       arpa_monname[the_time->tm_mon],
  129.       the_time->tm_year % 100);
  130. }
  131.  
  132. int
  133. month_number(name)
  134. char *name;
  135. {
  136.     /** return the month number given the month name... **/
  137.  
  138.     char ch;
  139.  
  140.     switch (tolower(name[0])) {
  141.      case 'a' : if ((ch = tolower(name[1])) == 'p')    return(APRIL);
  142.             else if (ch == 'u') return(AUGUST);
  143.             else return(-1);    /* error! */
  144.     
  145.      case 'd' : return(DECEMBER);
  146.      case 'f' : return(FEBRUARY);
  147.      case 'j' : if ((ch = tolower(name[1])) == 'a') return(JANUARY);
  148.             else if (ch == 'u') {
  149.                   if ((ch = tolower(name[2])) == 'n') return(JUNE);
  150.               else if (ch == 'l') return(JULY);
  151.               else return(-1);        /* error! */
  152.                 }
  153.             else return(-1);        /* error */
  154.      case 'm' : if ((ch = tolower(name[2])) == 'r') return(MARCH);
  155.             else if (ch == 'y') return(MAY);
  156.             else return(-1);        /* error! */
  157.      case 'n' : return(NOVEMBER);
  158.      case 'o' : return(OCTOBER);
  159.      case 's' : return(SEPTEMBER);
  160.      default  : return(-1);
  161.     }
  162. }
  163.  
  164. #ifdef SITE_HIDING
  165.  
  166. char *get_ctime_date()
  167. {
  168.     /** returns a ctime() format date, but a few minutes in the 
  169.         past...(more cunningness to implement hidden sites) **/
  170.  
  171.     static char buffer[SLEN];    /* static character buffer       */
  172.     struct tm *the_time;        /* Time structure, see CTIME(3C) */
  173.  
  174. #ifdef BSD
  175.     struct  timeval  time_val;        
  176.     struct  timezone time_zone;
  177.     long       junk;        /* time in seconds....         */
  178. #else
  179.     time_t       junk;        /* time in seconds....         */
  180. #endif
  181.  
  182. #ifdef BSD
  183.     gettimeofday(&time_val, &time_zone);
  184.     junk = time_val.tv_sec;
  185. #else
  186.     junk = time((time_t *) 0);    /* this must be here for it to work! */
  187. #endif
  188.     the_time = localtime(&junk);
  189.  
  190.     sprintf(buffer, "%s %s %d %02d:%02d:%02d %d",
  191.       arpa_dayname[the_time->tm_wday],
  192.       arpa_monname[the_time->tm_mon],
  193.       the_time->tm_mday % 32,
  194.       min(the_time->tm_hour % 24, (rand() % 24)),
  195.       min(abs(the_time->tm_min  % 61 - (rand() % 60)), (rand() % 60)),
  196.       min(abs(the_time->tm_sec  % 61 - (rand() % 60)), (rand() % 60)),
  197.       the_time->tm_year % 100 + 1900);
  198.     
  199.     return( (char *) buffer);
  200. }
  201.  
  202. #endif
  203.  
  204. char *
  205. elm_date_str(buf, seconds)
  206. char *buf;
  207. time_t seconds;
  208. {
  209.     struct tm *tmbuf;
  210.  
  211.     tmbuf = gmtime(&seconds);
  212.  
  213.     sprintf(buf, "%s %d, %d %2.2d:%2.2d:%2.2d %s",
  214.         arpa_monname[tmbuf->tm_mon],
  215.         tmbuf->tm_mday,
  216.         tmbuf->tm_year % 100,
  217.         ampm(tmbuf->tm_hour),
  218.         tmbuf->tm_min,
  219.         tmbuf->tm_sec,
  220.         am_or_pm(tmbuf->tm_hour));
  221.  
  222.     return(buf);
  223. }
  224.  
  225. make_menu_date(entry)
  226. struct header_rec *entry;
  227. {
  228.     struct tm *tmbuf;
  229.     time_t seconds;
  230.  
  231.     seconds = entry->time_sent + entry->tz_offset;
  232.     tmbuf = gmtime(&seconds);
  233.     sprintf(entry->time_menu, "%3.3s %-2d",
  234.         arpa_monname[tmbuf->tm_mon], tmbuf->tm_mday);
  235. }
  236.