home *** CD-ROM | disk | FTP | other *** search
- /*********************
- *
- * ut_dstr.c - date and time string conversions.
- *
- * Purpose: This file contains the functions to convert date and
- * time strings to structures and longs.
- *
- * Blackstar C Function Library
- * (c) Copyright 1985,1989 Sterling Castle Software
- *
- *******/
-
- #include <stdlib.h>
- #include <string.h>
- #include <ctype.h>
- #include "blackstr.h"
- #include "ut_head.h"
-
- extern int mo_days_[];
-
-
- /********
- *
- * ut_stdatel(sdate,str) - convert string to date structure
- *
- **/
-
- int ut_stdates(struct DATE *sdate, char *str)
- {
- char *p;
-
- if ((p = strtok(str,"/,- ")) != 0)
- sdate->mo = atoi(p);
- if ((p = strtok(NULL,"/,-")) != 0)
- sdate->dy = atoi(p);
- if ((p = strtok(NULL,"/,-")) != 0)
- sdate->yr = atoi(p);
- if(sdate->yr<50)
- sdate->yr += 100;
- if(sdate->mo<1 || sdate->mo>12)
- return(ERROR);
- else if (sdate->dy<1 || (sdate->dy > mo_days_[sdate->mo]))
- if(sdate->mo!=2 || !(ut_isleap(sdate->yr)))
- return(ERROR);
- return(NOERROR);
- }
-
-
- /********
- *
- * ut_sttimes(stime,str) - convert string to time structure
- *
- **/
-
- int ut_sttimes(struct TIME *stime, char *str)
- {
- int hr,mn;
- char *ptr,am,pm;
-
- am=pm=FALSE;
- stime->sec = stime->hsec = 0; /* no secs,etc for now */
- ptr = str;
- while(*ptr++) /* see if am or pm input */
- if(toupper(*str)=='P')
- pm = TRUE;
- else if(toupper(*str)=='A')
- am = TRUE;
- ptr = strtok(str,":apAP");
- hr = atoi(ptr);
- if(hr>99) {
- mn = hr%100;
- hr /= 100; /* military time */
- } else {
- ptr = strtok(NULL,":apAP");
- mn = atoi(ptr);
- }
-
- /* Assume a default value for am/mp */
-
- if(!am && !pm)
- if(hr<6 || hr==12)
- pm = TRUE;
- else
- am = TRUE;
-
- /* Now range test values */
-
- if(pm && hr!=12)
- hr += 12;
- if(am && hr==12)
- hr -=12;
- if(hr<0 || hr>23)
- return(ERROR);
- else if(mn<0||mn>59)
- return(ERROR);
- stime->hr = (char)hr;
- stime->mn = (char)mn;
- return(NOERROR);
- }
-
-
- /********
- *
- * ut_stdatel(str) - convert string date to long
- *
- **/
-
- long ut_stdatel(char *str)
- {
- struct DATE sdate;
-
- ut_stdates(&sdate,str); /*convert to structure */
- return((sdate.yr%1900)*10000L + sdate.mo*100L + sdate.dy);
- }
-
-
- /********
- *
- * ut_sttimel(str) - convert string time to long
- *
- **/
-
- long ut_sttimel(char *str)
- {
- struct TIME stime;
-
- ut_sttimes(&stime,str); /*convert to structure */
- return(stime.hr*10000L +stime.mn*100L + (long)stime.sec);
- }
-
-
- /********
- *
- * ut_isleap(yr) - see if year is leap year
- *
- **/
-
- int ut_isleap(int yr)
- {
- if(!(yr%4) && !(yr%100))
- return(TRUE);
- else
- return(FALSE);
- }
-