home *** CD-ROM | disk | FTP | other *** search
/ OS/2 Shareware BBS: 2 BBS / 02-BBS.zip / MSGDP206.SZH / DATE.C < prev    next >
Text File  |  1990-08-12  |  5KB  |  213 lines

  1. /**
  2.  *
  3.  * date.c
  4.  *
  5.  * parse various string date formats into a unix style timestamp
  6.  *
  7.  * jim nutt
  8.  * 31 aug 1989
  9.  * released into the PUBLIC DOMAIN 30 Jul 1990 by jim nutt
  10.  *
  11. **/
  12.  
  13. #include <stdio.h>
  14. #include <stdlib.h>
  15. #include <string.h>
  16. #include <ctype.h>
  17. #include "msged.h"
  18. #include "date.h"
  19.  
  20. #define valid_date(timestamp) \
  21.        !((timestamp->tm_wday > 6)  || (timestamp->tm_wday < 0) || \
  22.      (timestamp->tm_mon > 11)  || (timestamp->tm_mon < 0)  || \
  23.      (timestamp->tm_mday > 31) || (timestamp->tm_mday < 0) || \
  24.      (timestamp->tm_year > 99) || (timestamp->tm_year < 0) || \
  25.      (timestamp->tm_hour > 23) || (timestamp->tm_hour < 0) || \
  26.      (timestamp->tm_min > 59)  || (timestamp->tm_min < 0)  || \
  27.      (timestamp->tm_sec > 59)  || (timestamp->tm_sec < 0))
  28.  
  29. /**OS2**/    /* Removed redundant #define of strcmpl */
  30.  
  31. char * pascal attrib_line(MSG *m, char *format);
  32.  
  33. static char *month[] = {
  34.     "Jan", "Feb", "Mar", "Apr", "May", "Jun",
  35.     "Jul", "Aug", "Sep", "Oct", "Nov", "Dec"
  36. };
  37.  
  38. static char *day[] = {
  39.     "Sun", "Mon", "Tue", "Wed", "Thu", "Fri", "Sat"
  40. };
  41.  
  42. time_t pascal parsedate(char *ds)
  43.  
  44. {
  45.     char work[80];
  46.     char *s;
  47.     int t;
  48.     struct tm timestamp;
  49.  
  50.     memset(×tamp,0, sizeof timestamp);
  51.     strcpy(work,ds);
  52.  
  53.     if (strchr(ds,'-') != NULL) {   /* quickbbs style date */
  54.         s = strtok(work,"-");
  55.         timestamp.tm_mon = atoi(s) - 1;
  56.         s = strtok(NULL,"-");
  57.         timestamp.tm_mday = atoi(s);
  58.         s = strtok(NULL," ");
  59.         timestamp.tm_year = atoi(s);
  60.         s = strtok(NULL,":");
  61.         while (isspace(*s)) s++;
  62.         timestamp.tm_hour = atoi(s);
  63.         s = strtok(NULL," ");
  64.         timestamp.tm_min = atoi(s);
  65.     }
  66.     else {                /* fido style date */
  67.         s = strtok(work, " ");
  68.         if ((t = atoi(s)) == 0) { /* a usenet date */
  69.             s = strtok(NULL," ");
  70.             t = atoi(s);
  71.         }
  72.         timestamp.tm_mday = t;
  73.         s = strtok(NULL, " ");
  74.         for (t = 0; t < 12; t++)
  75.             if (strcmpl(s, month[t]) == 0) break;
  76.         if (t==12) t=1;                                         /*WRA*/
  77.         timestamp.tm_mon = t;
  78.         s = strtok(NULL, " ");
  79.         timestamp.tm_year = atoi(s);
  80.         s = strtok(NULL,":");
  81.         while (isspace(*s)) s++;
  82.         timestamp.tm_hour = atoi(s);
  83.         s = strtok(NULL,": \0");
  84.         timestamp.tm_min = atoi(s);
  85.         s = strtok(NULL," ");
  86.         if (s != NULL)
  87.             timestamp.tm_sec = atoi(s);
  88.     }
  89.     return mktime(×tamp);
  90. }
  91.  
  92. char * pascal atime(time_t now)
  93.  
  94. {
  95.     static char atime_buffer[40];
  96.     struct tm *timestamp;
  97.  
  98.     timestamp = localtime(&now);
  99.  
  100.     if (timestamp == NULL)
  101.         return("invalid date");
  102.  
  103.     if (!valid_date((timestamp)))
  104.         return("invalid date");
  105.  
  106.     sprintf(atime_buffer, "%s %s %02d 19%02d  %02d:%02d:%02d",
  107.         day[timestamp->tm_wday], month[timestamp->tm_mon],
  108.         timestamp->tm_mday, timestamp->tm_year,
  109.         timestamp->tm_hour, timestamp->tm_min, timestamp->tm_sec);
  110.     return(atime_buffer);
  111. }
  112.  
  113. char * pascal mtime(time_t now)
  114.  
  115. {
  116.     static char mtime_buffer[21];
  117.     struct tm *timestamp;
  118.  
  119.     timestamp = localtime(&now);
  120.  
  121.     if (timestamp == NULL)
  122.         return("invalid date");
  123.  
  124.     if (!valid_date((timestamp)))
  125.         return("invalid date");
  126.  
  127.     sprintf(mtime_buffer, "%02d %s %02d  %02d:%02d:%02d",
  128.         timestamp->tm_mday, month[timestamp->tm_mon],
  129.         timestamp->tm_year, timestamp->tm_hour,
  130.         timestamp->tm_min, timestamp->tm_sec);
  131.     return(mtime_buffer);
  132. }
  133.  
  134. char * pascal qtime(time_t now)
  135.  
  136. {
  137.     static char qtime_buffer[20];
  138.     struct tm *timestamp;
  139.  
  140.     timestamp = localtime(&now);
  141.  
  142.     if (timestamp == NULL)
  143.         return("invalid date");
  144.  
  145.     if (!valid_date((timestamp)))
  146.         return("invalid date");
  147.  
  148.     sprintf(qtime_buffer, "%s %02d %02d:%02d",
  149.         month[timestamp->tm_mon], timestamp->tm_mday,
  150.         timestamp->tm_hour, timestamp->tm_min);
  151.     return(qtime_buffer);
  152. }
  153.  
  154. char * pascal attrib_line(MSG *m, char *format)
  155.  
  156. {
  157.     char work[128];
  158.     char *t = work;
  159.     struct tm *timestamp;
  160.  
  161.     if (format == NULL)
  162.         return(NULL);
  163.  
  164.     memset(work,0,sizeof work);
  165.     timestamp = localtime(&(m->timestamp));
  166.     if (timestamp == NULL)
  167.         return(NULL);
  168.     while (*format) {
  169.         if (*format == '%') {
  170.             switch (tolower(*++format)) {
  171.                 case '%' :
  172.                     *t = *format;
  173.                 default  :
  174.                     break;
  175.                 case 't' :
  176.                     strcpy(t,m->isto);
  177.                     break;
  178.                 case 'f' :
  179.                     strcpy(t,m->isfrom);
  180.                     break;
  181.                 case 'a' :
  182.                     strcpy(t,show_address(m->from));
  183.                     break;
  184.                 case 'w' :
  185.                     strcpy(t,day[timestamp->tm_wday]);
  186.                     break;
  187.                 case 'd' :
  188.                     sprintf(t,"%02d",timestamp->tm_mday);
  189.                     break;
  190.                 case 'y' :
  191.                     sprintf(t,"%02d",timestamp->tm_year);
  192.                     break;
  193.                 case 'm' :
  194.                     strcpy(t,month[timestamp->tm_mon]);
  195.                     break;
  196.                 case 'h' :
  197.                     sprintf(t,"%02d:%02d",timestamp->tm_hour,timestamp->tm_min);
  198.                     break;
  199.             }
  200.             t = work + strlen(work);
  201.             format++;
  202.         }
  203.         else if (*format == '\\') {
  204.             if (*++format == 'n')
  205.                 *t++ = '\n';
  206.             format++;
  207.         }
  208.         else
  209.             *t++ = *format++;
  210.     }
  211.     return strdup(work);
  212. }
  213.