home *** CD-ROM | disk | FTP | other *** search
/ OS/2 Shareware BBS: 10 Tools / 10-Tools.zip / OS2TD.ZIP / TD.C < prev    next >
C/C++ Source or Header  |  1989-03-15  |  6KB  |  208 lines

  1. /*
  2.     TD.C:  Time & Date Formatting Functions
  3.  
  4.     Ray Duncan, December 1987
  5.  
  6. */
  7.  
  8. #include <stdio.h>
  9. #include <stdlib.h>
  10. #include <dos.h>
  11.  
  12. union REGS regs;
  13.  
  14. char * systcvt(int);                /* function prototypes */
  15. char * sysdcvt(int);
  16. char * dirtcvt(int, unsigned);
  17. char * dirdcvt(int, unsigned);
  18. char * tcvt(int, int, int, int, int);
  19. char * dcvt(int, int, int, int);
  20. void   getctry();
  21.  
  22. static char cbuff[34];              /* receives country info */
  23.  
  24. static int cflag = 0;               /* true if country info */
  25.                                     /* present in cbuff */
  26.  
  27.  
  28.                                         
  29. /*
  30.         Convert current system time to ASCII string.
  31. */
  32.  
  33. char * systcvt(int length)
  34. {   
  35.     char *p;
  36.     int hour, min, sec, csec;
  37.  
  38.     regs.x.ax = 0x2c00;             /* get current time */
  39.     int86(0x21, ®s, ®s);
  40.  
  41.     hour = regs.h.ch;               /* extract hours, minutes, */
  42.     min = regs.h.cl;                /* seconds, and hundredths */
  43.     sec = regs.h.dh;                /* of seconds from registers */
  44.     csec = regs.h.dl;
  45.  
  46.                                     /* convert it to ASCII */
  47.     p=tcvt(length, hour, min, sec, csec);
  48.  
  49.     return(p);                      /* return pointer */
  50. }
  51.  
  52.  
  53. /*
  54.         Convert current system date to ASCII string.
  55. */
  56.  
  57. char * sysdcvt(int length)
  58. {   
  59.     char *p;
  60.     int month, day, year;
  61.  
  62.     regs.x.ax = 0x2a00;             /* get current date */
  63.     int86(0x21, ®s, ®s);
  64.  
  65.     month = regs.h.dh;              /* extract month, day, */
  66.     day = regs.h.dl;                /* year from registers */
  67.     year = regs.x.cx - 1900;
  68.  
  69.                                     /* convert it to ASCII */
  70.     p=dcvt(length, month, day, year);
  71.     return(p);                      /* return pointer */
  72. }
  73.  
  74.  
  75. /*
  76.         Convert time in directory format to ASCII string.
  77. */
  78.  
  79. char * dirtcvt(int length, unsigned dirtime)
  80. {   
  81.     char *p;
  82.     int hour, min, sec, csec;
  83.  
  84.     hour = (dirtime >> 11) & 0x1f;  /* isolate time fields */ 
  85.     min = (dirtime >> 5) & 0x3f;
  86.     sec = (dirtime & 0x1f) * 2;
  87.     csec = 0;
  88.  
  89.                                     /* convert it to ASCII */
  90.     p=tcvt(length, hour, min, sec, csec);
  91.  
  92.     return(p);                      /* return pointer */
  93. }
  94.  
  95.  
  96. /*
  97.         Convert date in directory format to ASCII string.
  98. */
  99.  
  100. char * dirdcvt(int length, unsigned dirdate)
  101. {
  102.     char *p;
  103.     int month, day, year;
  104.  
  105.     day = dirdate & 0x1f;           /* isolate date fields */
  106.     month = (dirdate >> 5) & 0x0f;
  107.     year = 80 + ((dirdate >> 9) & 0x3f);
  108.  
  109.                                     /* convert it to ASCII */
  110.     p=dcvt(length, month, day, year);
  111.     return(p);                      /* return pointer */
  112. }
  113.  
  114.  
  115.  
  116. /*
  117.         Convert hours, minutes, seconds, and hundredths
  118.         of seconds to ASCII string, truncating string to 
  119.         lesser of specified length or 11 characters.
  120. */
  121.  
  122. char * tcvt(int length, int hour, int min, int sec, int csec)
  123. {   
  124.     static char time[12];           /* receives formatted time */
  125.  
  126.     getctry();                      /* get country info */
  127.  
  128.     sprintf(time,"%02d%c%02d%c%02d%c%02d", 
  129.               hour, cbuff[13], min, cbuff[13], sec, cbuff[9], csec);
  130.  
  131.                                     /* truncate if necessary */
  132.     time[(int) min(length, 11)] = 0;
  133.  
  134.     return(time);                   /* return pointer */
  135. }
  136.  
  137.  
  138. /*
  139.         Convert month, day, and year to ASCII string, truncating
  140.         string to lesser of specified length or 8 characters.
  141. */
  142.  
  143. char * dcvt(int length, int month, int day, int year)
  144. {   
  145.     static char date[9];            /* receives formatted date */
  146.  
  147.     getctry();                      /* get country info */
  148.  
  149.     switch(cbuff[0])                /* format by date code */
  150.     {
  151.         case 0:                     /* USA: m d y */
  152.         sprintf(date,"%02d%c%02d%c%02d", 
  153.                   month, cbuff[11], day, cbuff[11], year);
  154.             break;
  155.  
  156.         case 1:                     /* Europe: d m y */
  157.         sprintf(date,"%02d%c%02d%c%02d", 
  158.                   day, cbuff[11], month, cbuff[11], year);
  159.             break;
  160.  
  161.         case 2:                     /* Japan: y m d */
  162.         sprintf(date,"%02d%c%02d%c%02d", 
  163.                   year, cbuff[11], month, cbuff[11], day);
  164.             break;
  165.     }
  166.  
  167.     date[(int) min(length, 8)] = 0; /* truncate string */
  168.  
  169.    return(date);                    /* return pointer */
  170. }
  171.  
  172.  
  173. /*
  174.         Get MS-DOS internationalization information into
  175.         'cbuff', or provide default information.
  176. */
  177.  
  178. void getctry()
  179. {   
  180.     int dosver;
  181.  
  182.     if(cflag) return;               /* exit if information */
  183.                                     /* already in buffer */
  184.  
  185.     memset(cbuff,0,34);             /* initialize buffer */
  186.  
  187.     regs.x.ax = 0x3000;             /* get MS-DOS version */
  188.     int86(0x21, ®s, ®s);
  189.     dosver = regs.h.al;
  190.  
  191.     if(dosver >= 2)                 /* if MS-DOS 2.x or 3.x */
  192.     {                               /* get country info */
  193.         (char *) regs.x.dx = cbuff;
  194.         regs.x.ax = 0x3800;
  195.         int86(0x21, ®s, ®s);
  196.     }
  197.  
  198.     if(dosver <= 2)                 /* if MS-DOS 1.x or 2.x */
  199.     {                               /* force delimiter info */
  200.         cbuff[9]  = '.';            /* decimal separator */
  201.         cbuff[11] = '/';            /* date separator */
  202.         cbuff[13] = ':';            /* time separator */
  203.     }
  204.  
  205.     cflag = -1;                     /* we've been here before */
  206. }
  207.  
  208.