home *** CD-ROM | disk | FTP | other *** search
/ Liren Large Software Subsidy 7 / 07.iso / c / c222 / 1.ddi / SOURCE / CLIB / UT_DTSTR.C < prev    next >
Encoding:
C/C++ Source or Header  |  1990-07-10  |  2.7 KB  |  145 lines

  1. /*********************
  2.  *
  3.  *  ut_dstr.c - date and time string conversions.
  4.  *
  5.  *  Purpose: This file contains the functions to convert date and
  6.  *           time strings to structures and longs.
  7.  *
  8.  *  Blackstar C Function Library
  9.  *  (c) Copyright 1985,1989 Sterling Castle Software
  10.  *
  11.  *******/
  12.  
  13. #include <stdlib.h>
  14. #include <string.h>
  15. #include <ctype.h>
  16. #include "blackstr.h"
  17. #include "ut_head.h"
  18.  
  19. extern int mo_days_[];
  20.  
  21.  
  22. /********
  23.  *
  24.  *   ut_stdatel(sdate,str) - convert string to date structure
  25.  *
  26.  **/
  27.  
  28. int ut_stdates(struct DATE *sdate, char *str)
  29. {
  30.     char *p;
  31.  
  32.     if ((p = strtok(str,"/,- ")) != 0)
  33.     sdate->mo = atoi(p);
  34.     if ((p = strtok(NULL,"/,-")) != 0)
  35.     sdate->dy = atoi(p);
  36.     if ((p = strtok(NULL,"/,-")) != 0)
  37.     sdate->yr = atoi(p);
  38.     if(sdate->yr<50)
  39.     sdate->yr += 100;
  40.     if(sdate->mo<1 || sdate->mo>12)
  41.     return(ERROR);
  42.     else if (sdate->dy<1 || (sdate->dy > mo_days_[sdate->mo]))
  43.     if(sdate->mo!=2 || !(ut_isleap(sdate->yr)))
  44.         return(ERROR);
  45.     return(NOERROR);
  46. }
  47.  
  48.  
  49. /********
  50.  *
  51.  *   ut_sttimes(stime,str) - convert string to time structure
  52.  *
  53.  **/
  54.  
  55. int ut_sttimes(struct TIME *stime, char *str)
  56. {
  57.     int hr,mn;
  58.     char *ptr,am,pm;
  59.  
  60.     am=pm=FALSE;
  61.     stime->sec = stime->hsec = 0;           /* no secs,etc for now */
  62.     ptr = str;
  63.     while(*ptr++)                           /* see if am or pm input */
  64.     if(toupper(*str)=='P')
  65.         pm = TRUE;
  66.     else if(toupper(*str)=='A')
  67.         am = TRUE;
  68.     ptr = strtok(str,":apAP");
  69.     hr = atoi(ptr);
  70.     if(hr>99) {
  71.     mn = hr%100;
  72.     hr /= 100;              /* military time */
  73.     } else {
  74.     ptr = strtok(NULL,":apAP");
  75.     mn = atoi(ptr);
  76.     }
  77.  
  78.     /*  Assume a default value for am/mp */
  79.  
  80.     if(!am && !pm)
  81.     if(hr<6 || hr==12)
  82.         pm = TRUE;
  83.     else
  84.         am = TRUE;
  85.     
  86.     /* Now range test values */
  87.  
  88.     if(pm && hr!=12)
  89.     hr += 12;
  90.     if(am && hr==12)
  91.     hr -=12;
  92.     if(hr<0 || hr>23)
  93.     return(ERROR);
  94.     else if(mn<0||mn>59)
  95.     return(ERROR);
  96.     stime->hr = (char)hr;
  97.     stime->mn = (char)mn;
  98.     return(NOERROR);
  99. }
  100.  
  101.  
  102. /********
  103.  *
  104.  *   ut_stdatel(str) - convert string date to long
  105.  *
  106.  **/
  107.  
  108. long ut_stdatel(char *str)
  109. {
  110.     struct DATE sdate;
  111.  
  112.     ut_stdates(&sdate,str);         /*convert to structure */
  113.     return((sdate.yr%1900)*10000L + sdate.mo*100L + sdate.dy);
  114. }
  115.  
  116.  
  117. /********
  118.  *
  119.  *   ut_sttimel(str) - convert string time to long
  120.  *
  121.  **/
  122.  
  123. long ut_sttimel(char *str)
  124. {
  125.     struct TIME stime;
  126.  
  127.     ut_sttimes(&stime,str);         /*convert to structure */
  128.     return(stime.hr*10000L +stime.mn*100L + (long)stime.sec);
  129. }
  130.  
  131.  
  132. /********
  133.  *
  134.  *   ut_isleap(yr) - see if year is leap year
  135.  *
  136.  **/
  137.  
  138. int ut_isleap(int yr)
  139. {
  140.     if(!(yr%4) && !(yr%100))
  141.     return(TRUE);
  142.     else
  143.     return(FALSE);
  144. }
  145.