home *** CD-ROM | disk | FTP | other *** search
/ ProfitPress Mega CDROM2 …eeware (MSDOS)(1992)(Eng) / ProfitPress-MegaCDROM2.B6I / BBS / NETMAIL / MSGD2SRC.ZIP / DATE.C < prev    next >
Encoding:
C/C++ Source or Header  |  1990-07-30  |  4.5 KB  |  202 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 (!valid_date((timestamp)))
  101.         return("invalid date");
  102.  
  103.     sprintf(atime_buffer, "%s %s %02d 19%02d  %02d:%02d:%02d",
  104.         day[timestamp->tm_wday], month[timestamp->tm_mon],
  105.         timestamp->tm_mday, timestamp->tm_year,
  106.         timestamp->tm_hour, timestamp->tm_min, timestamp->tm_sec);
  107.     return(atime_buffer);
  108. }
  109.  
  110. char * pascal mtime(time_t now)
  111.  
  112. {
  113.     static char mtime_buffer[21];
  114.     struct tm *timestamp;
  115.  
  116.     timestamp = localtime(&now);
  117.  
  118.     if (!valid_date((timestamp)))
  119.         return("invalid date");
  120.  
  121.     sprintf(mtime_buffer, "%02d %s %02d  %02d:%02d:%02d",
  122.         timestamp->tm_mday, month[timestamp->tm_mon],
  123.         timestamp->tm_year, timestamp->tm_hour,
  124.         timestamp->tm_min, timestamp->tm_sec);
  125.     return(mtime_buffer);
  126. }
  127.  
  128. char * pascal qtime(time_t now)
  129.  
  130. {
  131.     static char qtime_buffer[20];
  132.     struct tm *timestamp;
  133.  
  134.     timestamp = localtime(&now);
  135.  
  136.     if (!valid_date((timestamp)))
  137.         return("invalid date");
  138.  
  139.     sprintf(qtime_buffer, "%s %02d %02d:%02d",
  140.         month[timestamp->tm_mon], timestamp->tm_mday,
  141.         timestamp->tm_hour, timestamp->tm_min);
  142.     return(qtime_buffer);
  143. }
  144.  
  145. char * pascal attrib_line(MSG *m, char *format)
  146.  
  147. {
  148.     char work[128];
  149.     char *t = work;
  150.     struct tm *timestamp;
  151.  
  152.     if (format == NULL)
  153.         return(NULL);
  154.  
  155.     memset(work,0,sizeof work);
  156.     timestamp = localtime(&(m->timestamp));
  157.     while (*format) {
  158.         if (*format == '%') {
  159.             switch (tolower(*++format)) {
  160.                 case '%' :
  161.                     *t = *format;
  162.                 default  :
  163.                     break;
  164.                 case 't' :
  165.                     strcpy(t,m->isto);
  166.                     break;
  167.                 case 'f' :
  168.                     strcpy(t,m->isfrom);
  169.                     break;
  170.                 case 'a' :
  171.                     strcpy(t,show_address(m->from));
  172.                     break;
  173.                 case 'w' :
  174.                     strcpy(t,day[timestamp->tm_wday]);
  175.                     break;
  176.                 case 'd' :
  177.                     sprintf(t,"%02d",timestamp->tm_mday);
  178.                     break;
  179.                 case 'y' :
  180.                     sprintf(t,"%02d",timestamp->tm_year);
  181.                     break;
  182.                 case 'm' :
  183.                     strcpy(t,month[timestamp->tm_mon]);
  184.                     break;
  185.                 case 'h' :
  186.                     sprintf(t,"%02d:%02d",timestamp->tm_hour,timestamp->tm_min);
  187.                     break;
  188.             }
  189.             t = work + strlen(work);
  190.             format++;
  191.         }
  192.         else if (*format == '\\') {
  193.             if (*++format == 'n')
  194.                 *t++ = '\n';
  195.             format++;
  196.         }
  197.         else
  198.             *t++ = *format++;
  199.     }
  200.     return strdup(work);
  201. }
  202.