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

  1. /**
  2. *
  3. * Name        stpfdate -- Format date in national style
  4. *
  5. * Synopsis    presult = stpfdate(ptarget,year,month,day,pinfo);
  6. *
  7. *        char   *presult  The resulting string, or NIL if error.
  8. *        char   *ptarget  Buffer in which to put the resulting
  9. *                   string.  It must be long enough to
  10. *                   accommodate the result:  allow 11 bytes
  11. *                   if year exceeds 99, 9 bytes if not.
  12. *        int    year     Year (0-99 or 1000-2099)
  13. *        int    month     Month (1-12)
  14. *        int    day     Day (1-31)
  15. *        COUNTRY_INFO *pinfo
  16. *                 Pointer to structure with country info
  17. *
  18. * Description    This function formats a calendar date according to DOS's
  19. *        information about national style.  pinfo must point to a
  20. *        COUNTRY_INFO structure in the format returned by
  21. *        QYGCOUN:  in particular, the dfmt and datsep members of
  22. *        the structure must be valid.  (This may not be true if
  23. *        the structure was filled by DOS 2.x, or if QYGCOUN
  24. *        reported an error.)
  25. *
  26. *        If the year value exceeds 99, then four characters will
  27. *        be used for the year; otherwise two characters.  For
  28. *        example, in the United States,
  29. *
  30. *            year =   85, month = 9, day = 26 gives "09-26-85"
  31. *
  32. *        but
  33. *
  34. *            year = 1985, month = 9, day = 26 gives "09-26-1985".
  35. *
  36. *        An illegal date or an unknown value of (pinfo->dfmt) may
  37. *        result in an error.  (However, not all illegal dates are
  38. *        detected.)  In that case *ptarget will be set to the null
  39. *        string (no characters) and NIL will be returned as the
  40. *        value of the function.
  41. *
  42. * Returns    presult     Pointer to the resulting string, or NIL
  43. *                if error.
  44. *        *ptarget    The resulting string, which is the null
  45. *                string if there is an error.
  46. *
  47. * Version    3.0 (C)Copyright Blaise Computing Inc.    1986
  48. *
  49. **/
  50.  
  51. #include <stdio.h>
  52.  
  53. #include <bstring.h>
  54.  
  55. char *stpfdate(ptarget,year,month,day,pinfo)
  56. register char          *ptarget;
  57. int              year,month,day;
  58. register COUNTRY_INFO *pinfo;
  59. {
  60.     char *fmt,date_sep;
  61.  
  62.     *ptarget = '\0';          /* Produce null string in case of error */
  63.  
  64.     if (utrange(year,0,2099) || utrange(month,1,12) || utrange(day,1,31))
  65.     return NIL;          /* Error: illegal date              */
  66.  
  67.     fmt      = "%02d%c%02d%c%02d";
  68.     date_sep = (pinfo->datsep)[0];
  69.     switch (pinfo->dfmt)
  70.     {
  71.     case 0:           /* USA format                  */
  72.         sprintf(ptarget,fmt,month,date_sep,
  73.                 day,date_sep,
  74.                 year);
  75.         break;
  76.     case 1:           /* Europe format                  */
  77.         sprintf(ptarget,fmt,day,date_sep,
  78.                 month,date_sep,
  79.                 year);
  80.         break;
  81.     case 2:           /* Japan format                  */
  82.         sprintf(ptarget,fmt,year,date_sep,
  83.                 month,date_sep,
  84.                 day);
  85.         break;
  86.     default:          /* Error:  unknown date format          */
  87.         return NIL;
  88.     }
  89.  
  90.     return ptarget;
  91. }
  92.