home *** CD-ROM | disk | FTP | other *** search
/ The C Users' Group Library 1994 August / wc-cdrom-cusersgrouplibrary-1994-08.iso / vol_300 / 328_02 / wtime.c < prev    next >
C/C++ Source or Header  |  1991-03-17  |  3KB  |  97 lines

  1. /* wtime.c
  2.  *    time handling routines for getting and displaying time as time_t
  3.  *
  4.  *     wt2showt() - converts time_t to strings mm/dd/yyyy and hh:mm:ss
  5.  *    wshowt2t() - converts strings as above to time_t
  6.  *            if yyyy is a 2 digit number, 1900 is assumed
  7.  *            allows for overflows (ie: 25 hours, 36 days,etc)
  8.  *
  9.  *
  10.  *    PARAMETERS are same for both routines:
  11.  *        time_t *t = UNIX_style time counter, seconds since 1/1/1970
  12.  *              char   *date = ptr to a buffer that must be AT LEAST 11 bytes
  13.  *                holds, or returns mm/dd/yyyy
  14.  *        char   *time = ptr to a buffer that must be AT LEAST  9 bytes
  15.  *                holds, or returns hh:mm:ss
  16.  *
  17.  *  BEWARE!!!
  18.  *        ANSI struct tm, year= yrs since 1900 (0 and up)
  19.  *                      mon = mons since Jan (0-11)
  20.  *                        day = day of the month (1-31) 
  21.  *                        h.m.s all start at 0.
  22.  *        LOCALTIME:      use tzset() to pick up environment var TZ=PST8, etc...
  23.  *        ANSI time_t     value is # seconds since 1/1/1970
  24.  *
  25.  *        DOS  struct time - h, m, s all start at 0.
  26.  *        DOS  struct date - da_day runs 1-31, da_mon 1-12, da_year 1970=1970
  27.  *
  28.  */
  29. #include "wsys.h"
  30. #include <dos.h>
  31. #include <time.h>
  32.  
  33. #define BUFFSZ    11
  34.  
  35.  
  36. void wshowt2t (char *date, char *time, time_t  *t)
  37.     {
  38.     struct tm  tms;
  39.     char   buff[BUFFSZ];
  40.     char   *ptr;
  41.  
  42.  
  43.     memset (&tms, 0, sizeof (tms) );
  44.  
  45.  
  46.     /* break date from mm/dd/yyyy
  47.      */
  48.     memcpy (buff, date, BUFFSZ);
  49.     buff[BUFFSZ-1] =0;
  50.     ptr = strtok (buff, "/-");
  51.     tms.tm_mon = atoi (ptr)    -1;            /* month goes 0-11 */
  52.     ptr = strtok (NULL, "/-");
  53.     tms.tm_mday = atoi (ptr);                /* day runs 1-31 */
  54.     ptr = strtok (NULL, "/-");
  55.     tms.tm_year = atoi (ptr);
  56.  
  57.     if ( tms.tm_year >1900 )
  58.         {
  59.         tms.tm_year -= 1900;            /* ANSI struct tm.tm_year=0 == 1900 */
  60.         }
  61.  
  62.     /* break time from hh:mm:ss
  63.      */
  64.     memcpy (buff, time, BUFFSZ);
  65.     buff[BUFFSZ-1] =0;
  66.     ptr = strtok (buff, ".:-");
  67.     tms.tm_hour = atoi (ptr);
  68.     ptr = strtok (NULL, ".:-");
  69.     tms.tm_min = atoi (ptr);
  70.     ptr = strtok (NULL, ".:-");
  71.     tms.tm_sec = atoi (ptr);
  72.  
  73.  
  74.     *t = mktime ( &tms );
  75.  
  76.     return;        /* wt2showt */
  77.     }
  78.  
  79.  
  80.  
  81. void wt2showt (time_t  *t, char  *date, char *time)
  82.     {
  83.     struct tm tms;
  84.  
  85.     tms = *localtime (t);    /* convert time_t to struct tm */
  86.  
  87.     tms.tm_year += 1900;    /* 0 = 1900 */
  88.     tms.tm_mon  += 1;        /* 0 = JAN  */
  89.  
  90.     sprintf(date,"%2.2i/%2.2i/%2.4i", tms.tm_mon, tms.tm_mday,tms.tm_year );
  91.     sprintf(time,"%2.2i:%2.2i:%2.2i", tms.tm_hour,tms.tm_min, tms.tm_sec  );
  92.  
  93.     return;        /* wt2showt */
  94.     }
  95.  
  96.  
  97.     /*---------------- end of WTIME.C -----------------*/