home *** CD-ROM | disk | FTP | other *** search
/ Liren Large Software Subsidy 7 / 07.iso / c / c005 / 5.ddi / C / STPFTIME.C < prev    next >
Encoding:
C/C++ Source or Header  |  1986-08-05  |  4.1 KB  |  156 lines

  1. /**
  2. *
  3. * Name        stpftime -- Format time of day in national style
  4. *
  5. * Synopsis    presult = stpftime(ptarget,hour,minute,second,hund,fmt,
  6. *              pinfo);
  7. *
  8. *        char *presult    The resulting string, or NIL if the
  9. *                time is out of range.
  10. *        char *ptarget    Buffer in which to put the resulting
  11. *                  string.  It must be long enough to
  12. *                  accommodate the result, whose size
  13. *                  depends on fmt.
  14. *        int  hour    Hour (0-23)
  15. *        int  minute    Minute (0-59)
  16. *        int  second    Second (0-59); this may be ignored,
  17. *                  depending on the value of fmt.
  18. *        int  hund    Hundredths of a second (0-99); this may be
  19. *                  ignored, depending on the value of fmt.
  20. *        int  fmt    Format code (see below).
  21. *        COUNTRY_INFO *pinfo
  22. *                Pointer to structure with country info
  23. *
  24. * Description    This function formats a time of day according to DOS's
  25. *        information about national style.  pinfo must point to a
  26. *        COUNTRY_INFO structure in the format returned by
  27. *        QYGCOUN:  in particular, the timsep and tfmt members
  28. *        (and possibly the decsep member) of the structure must
  29. *        be valid.  (This may not be true if the structure was
  30. *        filled by DOS 2.x, or if QYGCOUN reported an error.)
  31. *
  32. *        The possible codes for fmt are
  33. *
  34. *        YES_AMPM        1     Include AM or PM for 12-hour clock.
  35. *
  36. *        YES_HUNDS        2     Include hundredths of seconds.
  37. *        YES_SECS        4     Include seconds.  (Ignore hundredths of
  38. *                    seconds if this is absent.)
  39. *
  40. *        LEAD_ZERO        0     If hour < 10, include leading zero.
  41. *        LEAD_BLANK        8     If hour < 10, include leading blank.
  42. *        LEAD_NOTHING   16     Include no leading character.
  43. *
  44. *        The format codes can be combined by adding them
  45. *        together or by the "|" (bitwise OR) operator.
  46. *
  47. *        If the time is illegal then the function returns the
  48. *        null string in *ptarget and NIL as the value of the
  49. *        function.  However, the values of second and hund are
  50. *        not checked if fmt does not use them.
  51. *
  52. * Examples    First example:    leading blank, all fields present.
  53. *
  54. *            hour   = 14
  55. *            minute = 30
  56. *            second = 12
  57. *            hund   = 23
  58. *            fmt    = LEAD_BLANK | YES_SECS | YES_HUNDS | YES_AMPM
  59. *            country = USA   -->  result = " 2:30:12.23 PM"
  60. *
  61. *        Second example:  default format
  62. *
  63. *            hour   =  9
  64. *            minute = 30
  65. *            second = 12
  66. *            hund   = 23
  67. *            fmt    =  0
  68. *            country = USA   -->  result = "09:30"
  69. *
  70. * Returns    presult     Pointer to the resulting string.
  71. *        *ptarget    The resulting string.
  72. *
  73. * Version    3.0 (C)Copyright Blaise Computing Inc.    1986
  74. *
  75. **/
  76.  
  77. #include <string.h>
  78. #include <stdio.h>
  79.  
  80. #include <bstring.h>
  81.  
  82. char *stpftime(ptarget,hour,minute,second,hund,fmt,pinfo)
  83. register char          *ptarget;
  84. int              hour,minute,second,hund,fmt;
  85. register COUNTRY_INFO *pinfo;
  86. {
  87.     int  am;
  88.     char time_sep,*save_target;
  89.  
  90.     if (utrange(hour,0,23) || utrange(minute,0,59))
  91.     {                  /* Error:  illegal time              */
  92.     *ptarget = '\0';
  93.     return NIL;
  94.     }
  95.  
  96.     save_target = ptarget;
  97.     time_sep    = (pinfo->timsep)[0];
  98.  
  99.     if (pinfo->tfmt == 0)
  100.     {                  /* 12-hour clock                  */
  101.     if (hour < 12)          /* Set the AM or PM flag              */
  102.     {
  103.        am = 1;
  104.        if (hour == 0)
  105.           hour = 12;      /* Midnight                  */
  106.     }
  107.     else
  108.     {
  109.        am = 0;
  110.        if (hour > 12)
  111.           hour -= 12;
  112.     }
  113.     }
  114.     if (hour < 10)
  115.     switch (fmt & (LEAD_BLANK + LEAD_NOTHING))
  116.     {
  117.         case LEAD_BLANK + LEAD_NOTHING:
  118.         case LEAD_BLANK:
  119.         *ptarget++ = ' ';
  120.         break;
  121.         case LEAD_ZERO:
  122.         *ptarget++ = '0';
  123.         break;
  124.         case LEAD_NOTHING:
  125.         break;
  126.     }
  127.     ptarget += sprintf(ptarget,"%d%c%02d",hour,time_sep,
  128.                       minute);
  129.     if (fmt & YES_SECS)
  130.     {
  131.     if (utrange(second,0,59))
  132.     {              /* Error:  illegal time              */
  133.         *save_target = '\0';
  134.         return NIL;
  135.     }
  136.     ptarget += sprintf(ptarget,"%c%02d",time_sep,
  137.                         second);
  138.     if (fmt & YES_HUNDS)
  139.     {
  140.         if (utrange(hund,0,99))
  141.         {              /* Error:  illegal time              */
  142.         *save_target = '\0';
  143.         return NIL;
  144.         }
  145.         ptarget += sprintf(ptarget,"%c%02d",(pinfo->decsep)[0],
  146.                         hund);
  147.     }
  148.     }
  149.  
  150.     if ((fmt & YES_AMPM) && (pinfo->tfmt == 0))
  151.     strcpy(ptarget,am ? " AM"
  152.               : " PM");
  153.  
  154.     return save_target;
  155. }
  156.