home *** CD-ROM | disk | FTP | other *** search
/ Usenet 1994 October / usenetsourcesnewsgroupsinfomagicoctober1994disk2.iso / unix / volume28 / yapp / part01 / dates.c < prev    next >
Encoding:
C/C++ Source or Header  |  1994-05-29  |  3.3 KB  |  143 lines

  1. /* DATES.C */
  2. static char *sccsid="@(#)dates.c 1.2 93/05/10 Copyright (c)1993 thalerd";
  3. #include <time.h>
  4. #include <ctype.h>
  5. #include <limits.h>
  6. #include <memory.h>
  7. #include "config.h"
  8.  
  9. static char *month[]={ 
  10.    "jan_uary","feb_ruary","mar_ch","apr_il","may","jun_e",
  11.    "jul_y","aug_ust","sep_tember","oct_ober","nov_ember","dec_ember"
  12. };              
  13.  
  14. void
  15. get_num(a,ptr)
  16. int   *a;
  17. char **ptr;
  18. {
  19.    while (isdigit(**ptr)) { *a = (*a)*10 + **ptr - '0'; (*ptr)++; }
  20. }
  21.  
  22. void
  23. get_str(m,ptr)
  24. int   *m;
  25. char **ptr;
  26. {
  27.    int i,l;
  28.    char buff[20],*p;
  29.  
  30.    p = *ptr;
  31.    for (l=0; isalpha(p[l]) && l<19; l++) buff[l]=p[l];
  32.    buff[l]=0;
  33.  
  34.    for (i=0; i<12; i++) {
  35.       if (match(buff,month[i])) {
  36.          (*m) = i+1;
  37.          (*ptr) += l;
  38.       }     
  39.    }
  40. }
  41.  
  42. void
  43. get_time(tm,ptr)
  44. struct tm *tm;
  45. char     **ptr;
  46. {
  47.    tm->tm_hour=0;
  48.    get_num(&(tm->tm_hour),ptr);
  49.    if (**ptr==':') { 
  50.       (*ptr)++;
  51.       tm->tm_min=0;
  52.       get_num(&(tm->tm_min),ptr);
  53.    } else tm->tm_min = 0;
  54.    if (**ptr==':') { 
  55.       (*ptr)++;
  56.       tm->tm_sec=0;
  57.       get_num(&(tm->tm_sec),ptr);
  58.    } else tm->tm_sec = 0;
  59.    while (**ptr==' ') (*ptr)++;
  60.    if (tolower(**ptr)=='a' || tolower(**ptr)=='m') tm->tm_hour %= 12;
  61.    if (tolower(**ptr)=='p' || tolower(**ptr)=='n') tm->tm_hour = (tm->tm_hour%12)+12;
  62. }
  63.  
  64. /* Take a string, return time_t value */
  65. char *
  66. getdate(tt,str)
  67. time_t *tt;
  68. char *str;
  69. {
  70.    struct tm tm;
  71.    time_t t;
  72.    int i,sgn;
  73.    char *ptr,*ptr2;
  74.    int   a=0,b=0,c=0,m=0;
  75.  
  76.    time(&t); /* get current time */
  77.    memcpy(&tm,localtime(&t),sizeof(tm));
  78.    tm.tm_sec = tm.tm_min = tm.tm_hour = 0; /* ok on grex */
  79.    
  80.    ptr=str;
  81.    while (*ptr==' ') ptr++; /* skip leading spaces */
  82.    if (*ptr=='+' || *ptr=='-') {
  83.       sgn = (*ptr=='+')? 1 : -1;
  84.       ptr++; i=0;
  85.       while (isdigit(*ptr)) { i = i*10 + (*ptr - '0'); ptr++; }
  86.       *tt = timelocal(&tm) + sgn*i*24*60*60;
  87.    } else {
  88.  
  89.       /* Leading (timestamp) */
  90.       if (*ptr=='(') { /* ) */
  91.          ptr++;
  92.          get_time(&tm,&ptr);
  93.          while (*ptr && *ptr!=')') ptr++;
  94.          if (*ptr==')') ptr++;
  95.          while (isspace(*ptr)) ptr++;
  96.       }
  97.  
  98.       /* Get date */
  99.       if (isdigit(*ptr)) get_num(&a,&ptr);
  100.       else               get_str(&m,&ptr);
  101.       while (*ptr==' ' || *ptr=='/' || *ptr=='-') ptr++;
  102.       if (isdigit(*ptr)) get_num(&b,&ptr);
  103.       else               get_str(&m,&ptr);
  104.       while (*ptr==' ' || *ptr=='/' || *ptr=='-' || *ptr==',') ptr++;
  105.       if (isdigit(*ptr)) get_num(&c,&ptr);
  106.       if (c>1900) c-=1900;
  107.       if (c)              tm.tm_year = c;
  108.  
  109.       /* Assign values to date structure */
  110.       if (m) {            
  111.          tm.tm_mon  = m-1;
  112.          if      (a) tm.tm_mday = a;
  113.          else if (b) tm.tm_mday = b;
  114.       } else if (a*b) {
  115.          tm.tm_mon  = a-1;
  116.          tm.tm_mday = b;
  117. /*
  118.       } else {
  119.          *tt = LONG_MAX;
  120.          printf("Bad date near \"%s\"\n",ptr);
  121.          return ptr; 
  122. */
  123.       }
  124.  
  125.       /* Trailing (timestamp) */
  126.       while (isspace(*ptr)) ptr++;
  127.       if (*ptr=='(') { /* ) */
  128.          ptr++;
  129.          get_time(&tm,&ptr);
  130.          while (*ptr && *ptr!=')') ptr++;
  131.          if (*ptr==')') ptr++;
  132.  
  133.       /* Trailing timestamp */
  134.       } else if (isdigit(*ptr))
  135.          get_time(&tm,&ptr);
  136.          /* do we need to advance ptr? */
  137.  
  138.       *tt = timelocal(&tm);
  139.       memcpy(&tm,localtime(tt),sizeof(tm));
  140.    }
  141.    return ptr;
  142. }
  143.