home *** CD-ROM | disk | FTP | other *** search
/ OS/2 Shareware BBS: 10 Tools / 10-Tools.zip / PP0802.ZIP / TD.C < prev    next >
C/C++ Source or Header  |  1988-10-11  |  5KB  |  143 lines

  1. /*  TD.C:  Time & Date Formatting Functions, OS/2 version
  2.     Copyright (c) 1989 Ziff Communications Co.
  3.     PC Magazine * Ray Duncan
  4.  */
  5.  
  6. #include <stdio.h>
  7. #include <stdlib.h>
  8.  
  9. char * systcvt(int);                /* local function prototypes */
  10. char * sysdcvt(int);
  11. char * dirtcvt(int, unsigned);
  12. char * dirdcvt(int, unsigned);
  13. char * tcvt(int, int, int, int, int);
  14. char * dcvt(int, int, int, int);
  15. static void getctry(void);
  16.  
  17. #define API unsigned extern far pascal
  18.  
  19. API DosGetDateTime(void far *);     /* API function prototypes */
  20.  
  21. API DosGetCtryInfo(int, int far *, void far *, int far *);
  22.  
  23. static char cbuff[38];              /* receives country info */
  24.  
  25. static int cflag = 0;               /* true if country info */
  26.                                     /* present in cbuff */
  27.  
  28. static struct _tdinfo {             /* used by DosGetDateTime */
  29.     char hour;
  30.     char min;
  31.     char sec;
  32.     char csec;
  33.     char day;
  34.     char month;
  35.     int  year;
  36.     int  zone;
  37.     char dow; } tdinfo;              
  38.  
  39. /* Convert current system time to ASCII string. */
  40.  
  41. char * systcvt(int length) {   
  42.     DosGetDateTime(&tdinfo);        /* get current time */
  43.                                     /* convert it to ASCII */
  44.     return(tcvt(length, tdinfo.hour, tdinfo.min, tdinfo.sec, tdinfo.csec));
  45. }
  46.  
  47. /* Convert current system date to ASCII string. */
  48.  
  49. char * sysdcvt(int length) {   
  50.     DosGetDateTime(&tdinfo);        /* get current date */
  51.                                     /* convert it to ASCII */
  52.     return(dcvt(length, tdinfo.month, tdinfo.day, tdinfo.year-1900));
  53. }
  54.  
  55. /* Convert time in directory format to ASCII string. */
  56.  
  57. char * dirtcvt(int length, unsigned dirtime) {   
  58.     int hour, min, sec, csec;
  59.  
  60.     hour = (dirtime >> 11) & 0x1f;  /* isolate time fields */ 
  61.     min = (dirtime >> 5) & 0x3f;
  62.     sec = (dirtime & 0x1f) * 2;
  63.     csec = 0;
  64.     return(tcvt(length, hour, min, sec, csec)); /* convert it to ASCII */
  65. }
  66.  
  67. /* Convert date in directory format to ASCII string. */
  68.  
  69. char * dirdcvt(int length, unsigned dirdate) {
  70.     int month, day, year;
  71.  
  72.     day = dirdate & 0x1f;           /* isolate date fields */
  73.     month = (dirdate >> 5) & 0x0f;
  74.     year = 80 + ((dirdate >> 9) & 0x3f);
  75.  
  76.     return(dcvt(length, month, day, year)); /* convert it to ASCII */
  77. }
  78.  
  79. /* Convert hours, minutes, seconds, and hundredths of seconds to ASCII string,
  80.    truncating string to lesser of specified length or 11 characters. */
  81.  
  82. char * tcvt(int length, int hour, int min, int sec, int csec) {   
  83.     static char time[12];           /* receives formatted time */
  84.  
  85.     getctry();                      /* get country info */
  86.     sprintf(time,"%02d%c%02d%c%02d%c%02d", 
  87.             hour, cbuff[17], min, cbuff[17], sec, cbuff[13], csec);
  88.  
  89.     time[(int) min(length, 11)] = 0; /* truncate if necessary */
  90.     return(time);                   /* return pointer */
  91. }
  92.  
  93. /* Convert month, day, and year to ASCII string, truncating
  94.    string to lesser of specified length or 8 characters. */
  95.  
  96. char * dcvt(int length, int month, int day, int year) {   
  97.     static char date[9];            /* receives formatted date */
  98.  
  99.     getctry();                      /* get country info */
  100.     switch(cbuff[4])                /* format by date code */
  101.     {
  102.         case 0:                     /* USA: m d y */
  103.         sprintf(date,"%02d%c%02d%c%02d", 
  104.                 month, cbuff[15], day, cbuff[15], year);
  105.             break;
  106.  
  107.         case 1:                     /* Europe: d m y */
  108.         sprintf(date,"%02d%c%02d%c%02d", 
  109.                 day, cbuff[15], month, cbuff[15], year);
  110.             break;
  111.  
  112.         case 2:                     /* Japan: y m d */
  113.         sprintf(date,"%02d%c%02d%c%02d", 
  114.                 year, cbuff[15], month, cbuff[15], day);
  115.             break;
  116.     }
  117.  
  118.     date[(int) min(length, 8)] = 0; /* truncate string */
  119.     return(date);                   /* return pointer */
  120. }
  121.  
  122. /* Get internationalization information into 'cbuff', or provide
  123.    default information. */
  124.  
  125. static void getctry(void) {   
  126.     int ccode[2];                   /* country & code page */
  127.     int cbytes;                     /* receives length of info */
  128.     unsigned status;                /* scratch variable */
  129.  
  130.     if(cflag) return;               /* exit if information already in buffer */
  131.     
  132.     memset(cbuff,0,sizeof(cbuff));  /* initialize buffer */
  133.     ccode[0] = 0;                   /* country = default */
  134.     ccode[1] = 0;                   /* code page = default */
  135.  
  136.                             /* get country info or supply USA defaults */
  137.     if(DosGetCtryInfo(sizeof(cbuff), ccode, cbuff, &cbytes)) {
  138.         cbuff[13] = '.';            /* decimal separator */
  139.         cbuff[15] = '-';            /* date separator */
  140.         cbuff[17] = ':';            /* time separator */
  141.     }
  142. }
  143.