home *** CD-ROM | disk | FTP | other *** search
/ The Pier Shareware 6 / The_Pier_Shareware_Number_6_(The_Pier_Exchange)_(1995).iso / 035 / kwclass.zip / KVALDATE.CPP < prev    next >
C/C++ Source or Header  |  1994-05-12  |  2KB  |  83 lines

  1. #include <kvaldate.hpp>
  2. #include <ikeyevt.hpp>
  3. #include <kentryfd.hpp>
  4. #include <istring.hpp>
  5. #include <istrtest.hpp>
  6. #include <ctype.h>
  7. #include <iexcept.hpp>
  8. #include <iprofile.hpp>
  9. #include <idate.hpp>
  10.  
  11. KDateValidator::Settings KDateValidator::settings;
  12.  
  13. KDateValidator::Settings::Settings()
  14. {
  15.    // get the date separator character from the profile
  16.    // default to "-"
  17.    try
  18.    {
  19.       separator = IProfile::userProfile().elementWithKey("sDate", 
  20.                                                       "PM_National");
  21.    }
  22.    catch(...)
  23.    {
  24.       separator = "-";
  25.    }
  26.    mask = "99" + separator + "99" + separator + "99";
  27.  
  28.    // get the date format from the profile
  29.    // default to 0
  30.    unsigned dateFormat;
  31.    try
  32.    {
  33.       IProfile::userProfile().elementWithKey("iDate", 
  34.                                              "PM_National").asUnsigned();
  35.    }
  36.    catch(...)
  37.    {
  38.      dateFormat = 0;
  39.    }
  40.  
  41.    // set up the position indexes
  42.    switch(dateFormat)
  43.    {
  44.       case 0:      // MM/DD/YY
  45.          dayPos = 3;
  46.          monthPos = 0;
  47.          yearPos = 6;
  48.          break;
  49.  
  50.       case 1:     // DD/MM/YY
  51.          dayPos = 0;
  52.          monthPos = 3;
  53.          yearPos = 6;
  54.          break;
  55.  
  56.       case 2:     // YY/MM/DD
  57.          dayPos = 6;
  58.          monthPos = 3;
  59.          yearPos = 0;
  60.          break;
  61.  
  62.       default:
  63.          break;
  64.    }
  65. }
  66.  
  67. KDateValidator::KDateValidator()
  68.   : KMaskValidator(settings.mask)
  69. {
  70. }
  71.  
  72. Boolean KDateValidator::isValid(const char *text, Boolean fill) const
  73. {
  74.    if (!Inherited::isValid(text, fill))
  75.       return false;
  76.  
  77.    IString strText(text);
  78.    int nDay = strText.subString(settings.dayPos, 2).asUnsigned();
  79.    IDate::Month month = (IDate::Month)strText.subString(settings.monthPos, 2).asUnsigned();
  80.    int nYear = strText.subString(settings.yearPos, 2).asUnsigned();
  81.    return IDate::isValid(month, nDay, nYear+1900);
  82. }
  83.