home *** CD-ROM | disk | FTP | other *** search
/ ftp.uv.es / 2014.11.ftp.uv.es.tar / ftp.uv.es / pub / unix / aix-rs6000 / elm2.3.11.AIX3.1.5.Z / elm2.3.11.AIX3.1.5 / src / date.c < prev    next >
C/C++ Source or Header  |  1990-04-28  |  8KB  |  310 lines

  1.  
  2. static char rcsid[] = "@(#)$Id: date.c,v 4.1 90/04/28 22:42:41 syd Exp $";
  3.  
  4. /*******************************************************************************
  5.  *  The Elm Mail System  -  $Revision: 4.1 $   $State: Exp $
  6.  *
  7.  *             Copyright (c) 1986, 1987 Dave Taylor
  8.  *             Copyright (c) 1988, 1989, 1990 USENET Community Trust
  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 4.1  90/04/28  22:42:41  syd
  18.  * checkin of Elm 2.3 as of Release PL0
  19.  * 
  20.  *
  21.  ******************************************************************************/
  22.  
  23. /** return the current date and time in a readable format! **/
  24. /** also returns an ARPA RFC-822 format date...            **/
  25.  
  26.  
  27. #include "headers.h"
  28.  
  29. #ifdef I_TIME
  30. #  include <time.h>
  31. #endif
  32. #ifdef I_SYSTIME
  33. #  include <sys/time.h>
  34. #endif
  35. #ifdef BSD
  36. #  include <sys/types.h>
  37. #  include <sys/timeb.h>
  38. #endif
  39.  
  40. #include <ctype.h>
  41.  
  42. #ifndef    _POSIX_SOURCE
  43. extern struct tm *localtime();
  44. extern long      time();
  45. #endif
  46.  
  47. #ifdef BSD
  48. #undef toupper
  49. #undef tolower
  50. #endif
  51.  
  52. #define MONTHS_IN_YEAR    11    /* 0-11 equals 12 months! */
  53. #define FEB         1    /* 0 = January           */
  54. #define DAYS_IN_LEAP_FEB 29    /* leap year only       */
  55.  
  56. #define ampm(n)        (n > 12? n - 12 : n)
  57. #define am_or_pm(n)    (n > 11? (n > 23? "am" : "pm") : "am")
  58. #define leapyear(year)    ((year % 4 == 0) && (year % 100 != 0))
  59.  
  60. char *arpa_dayname[] = { "Sun", "Mon", "Tue", "Wed", "Thu",
  61.           "Fri", "Sat", "" };
  62.  
  63. char *arpa_monname[] = { "Jan", "Feb", "Mar", "Apr", "May", "Jun",
  64.           "Jul", "Aug", "Sep", "Oct", "Nov", "Dec", ""};
  65.  
  66. int  days_in_month[] = { 31,    28,    31,    30,    31,     30, 
  67.           31,     31,    30,   31,    30,     31,  -1};
  68.  
  69. #ifdef BSD
  70.   char *timezone();
  71. #else
  72.   extern char *tzname[];
  73. #endif
  74.  
  75. char *get_arpa_date()
  76. {
  77.     /** returns an ARPA standard date.  The format for the date
  78.         according to DARPA document RFC-822 is exemplified by;
  79.  
  80.                      Mon, 12 Aug 85 6:29:08 MST
  81.  
  82.     **/
  83.  
  84.     static char buffer[SLEN];    /* static character buffer       */
  85.     struct tm *the_time;        /* Time structure, see CTIME(3C) */
  86.     long       junk;        /* time in seconds....         */
  87.  
  88. #ifdef BSD
  89. # ifndef TZ_MINUTESWEST
  90.     struct timeb    loc_time;
  91.  
  92.     junk = time((long *) 0);
  93.     ftime(&loc_time);
  94. # else
  95.     struct  timeval  time_val;        
  96.     struct  timezone time_zone;
  97.  
  98.     gettimeofday(&time_val, &time_zone);
  99.     junk = time_val.tv_sec;
  100. # endif
  101.  
  102. #else
  103.     junk = time((long *) 0);    /* this must be here for it to work! */
  104. #endif
  105.  
  106.     the_time = localtime(&junk);
  107.  
  108.     sprintf(buffer, "%s, %d %s %d %d:%02d:%02d %s",
  109.       arpa_dayname[the_time->tm_wday],
  110.       the_time->tm_mday % 32,
  111.       arpa_monname[the_time->tm_mon],
  112.       the_time->tm_year % 100,
  113.       the_time->tm_hour % 24,
  114.       the_time->tm_min  % 61,
  115.       the_time->tm_sec  % 61,
  116. #ifdef BSD
  117. #ifdef TZ_MINUTESWEST
  118. # ifdef GOULD_NP1
  119.       the_time->tm_zone);
  120. # else
  121.       timezone(time_zone.tz_minuteswest, the_time->tm_isdst));
  122. # endif
  123. #else
  124.       timezone(loc_time.timezone, the_time->tm_isdst));
  125. #endif
  126. #else
  127.       tzname[the_time->tm_isdst]);
  128. #endif
  129.     
  130.     return( (char *) buffer);
  131. }
  132.  
  133. days_ahead(days, buffer)
  134. int days;
  135. char *buffer;
  136. {
  137.     /** return in buffer the date (Day, Mon Day, Year) of the date
  138.         'days' days after today.  
  139.     **/
  140.  
  141.     struct tm *the_time;        /* Time structure, see CTIME(3C) */
  142.     long       junk;        /* time in seconds....         */
  143.  
  144.     junk = time((long *) 0);    /* this must be here for it to work! */
  145.     the_time = localtime(&junk);
  146.  
  147.     /* increment the day of the week */
  148.  
  149.     the_time->tm_wday = (the_time->tm_wday + days) % 7;
  150.  
  151.     /* the day of the month... */
  152.     the_time->tm_mday += days;
  153.     
  154.         while (the_time->tm_mday > days_in_month[the_time->tm_mon]) {
  155.           if (the_time->tm_mon == FEB && leapyear(the_time->tm_year)) {
  156.             if (the_time->tm_mday > DAYS_IN_LEAP_FEB) {
  157.               the_time->tm_mday -= DAYS_IN_LEAP_FEB;
  158.               the_time->tm_mon += 1;
  159.             }
  160.             else
  161.               break;            /* Is Feb 29, so leave */
  162.           }
  163.           else {
  164.             the_time->tm_mday -= days_in_month[the_time->tm_mon];
  165.             the_time->tm_mon += 1;
  166.           }
  167.  
  168.           /* check the month of the year */
  169.           if (the_time->tm_mon > MONTHS_IN_YEAR) {
  170.             the_time->tm_mon -= (MONTHS_IN_YEAR + 1);
  171.             the_time->tm_year += 1;
  172.           }
  173.         }
  174.   
  175.         /* now, finally, build the actual date string */
  176.  
  177.     sprintf(buffer, "%s, %d %s %d",
  178.       arpa_dayname[the_time->tm_wday],
  179.       the_time->tm_mday % 32,
  180.       arpa_monname[the_time->tm_mon],
  181.       the_time->tm_year % 100);
  182. }
  183.  
  184. fix_date(entry)
  185. struct header_rec *entry;
  186. {
  187.     /** This routine will 'fix' the date entry for the specified
  188.         message.  This consists of 1) adjusting the year to 0-99
  189.         and 2) altering time from HH:MM:SS to HH:MM am|pm **/ 
  190.  
  191.     if (atoi(entry->year) > 99)     
  192.       sprintf(entry->year,"%d", atoi(entry->year) - 1900);
  193.  
  194.     fix_time(entry->time);
  195. }
  196.  
  197. fix_time(timestring)
  198. char *timestring;
  199. {
  200.     /** Timestring in format HH:MM:SS (24 hour time).  This routine
  201.         will fix it to display as: HH:MM [am|pm] **/
  202.  
  203.     int hour, minute;
  204.  
  205.     sscanf(timestring, "%d:%d", &hour, &minute);
  206.  
  207.     if (hour < 1 || hour == 24) 
  208.       sprintf(timestring, "12:%02d am", minute);
  209.     else if (hour < 12)
  210.       sprintf(timestring, "%d:%02d am", hour, minute);
  211.     else if (hour == 12)
  212.       sprintf(timestring, "12:%02d pm", minute);
  213.     else if (hour < 24)
  214.       sprintf(timestring, "%d:%02d pm", hour-12, minute);
  215. }
  216.  
  217. int
  218. compare_parsed_dates(rec1, rec2)
  219. struct date_rec rec1, rec2;
  220. {
  221.     /** This function is very similar to the compare_dates
  222.         function but assumes that the two record structures
  223.         are already parsed and stored in "date_rec" format.
  224.     **/
  225.  
  226.     if (rec1.year != rec2.year)
  227.       return( rec1.year - rec2.year );
  228.     
  229.     if (rec1.month != rec2.month)
  230.       return( rec1.month - rec2.month );
  231.  
  232.     if (rec1.day != rec2.day)
  233.       return( rec1.day - rec2.day );
  234.  
  235.     if (rec1.hour != rec2.hour)
  236.       return( rec1.hour - rec2.hour );
  237.  
  238.     return( rec1.minute - rec2.minute );        /* ignore seconds... */
  239. }
  240.  
  241. int
  242. month_number(name)
  243. char *name;
  244. {
  245.     /** return the month number given the month name... **/
  246.  
  247.     char ch;
  248.  
  249.     switch (tolower(name[0])) {
  250.      case 'a' : if ((ch = tolower(name[1])) == 'p')    return(APRIL);
  251.             else if (ch == 'u') return(AUGUST);
  252.             else return(-1);    /* error! */
  253.     
  254.      case 'd' : return(DECEMBER);
  255.      case 'f' : return(FEBRUARY);
  256.      case 'j' : if ((ch = tolower(name[1])) == 'a') return(JANUARY);
  257.             else if (ch == 'u') {
  258.                   if ((ch = tolower(name[2])) == 'n') return(JUNE);
  259.               else if (ch == 'l') return(JULY);
  260.               else return(-1);        /* error! */
  261.                 }
  262.             else return(-1);        /* error */
  263.      case 'm' : if ((ch = tolower(name[2])) == 'r') return(MARCH);
  264.             else if (ch == 'y') return(MAY);
  265.             else return(-1);        /* error! */
  266.      case 'n' : return(NOVEMBER);
  267.      case 'o' : return(OCTOBER);
  268.      case 's' : return(SEPTEMBER);
  269.      default  : return(-1);
  270.     }
  271. }
  272.  
  273. #ifdef SITE_HIDING
  274.  
  275. char *get_ctime_date()
  276. {
  277.     /** returns a ctime() format date, but a few minutes in the 
  278.         past...(more cunningness to implement hidden sites) **/
  279.  
  280.     static char buffer[SLEN];    /* static character buffer       */
  281.     struct tm *the_time;        /* Time structure, see CTIME(3C) */
  282.     long       junk;        /* time in seconds....         */
  283.  
  284. #ifdef BSD
  285.     struct  timeval  time_val;        
  286.     struct  timezone time_zone;
  287. #endif
  288.  
  289. #ifdef BSD
  290.     gettimeofday(&time_val, &time_zone);
  291.     junk = time_val.tv_sec;
  292. #else
  293.     junk = time((long *) 0);    /* this must be here for it to work! */
  294. #endif
  295.     the_time = localtime(&junk);
  296.  
  297.     sprintf(buffer, "%s %s %d %02d:%02d:%02d %d",
  298.       arpa_dayname[the_time->tm_wday],
  299.       arpa_monname[the_time->tm_mon],
  300.       the_time->tm_mday % 32,
  301.       min(the_time->tm_hour % 24, (rand() % 24)),
  302.       min(abs(the_time->tm_min  % 61 - (rand() % 60)), (rand() % 60)),
  303.       min(abs(the_time->tm_sec  % 61 - (rand() % 60)), (rand() % 60)),
  304.       the_time->tm_year % 100 + 1900);
  305.     
  306.     return( (char *) buffer);
  307. }
  308.  
  309. #endif
  310.