home *** CD-ROM | disk | FTP | other *** search
/ OS/2 Shareware BBS: 10 Tools / 10-Tools.zip / mavcl130.zip / mavconv.cpp < prev    next >
C/C++ Source or Header  |  1995-12-17  |  6KB  |  227 lines

  1. /*  File: mavconv.cpp      Updated: Sun Dec 17 19:21:04 1995
  2. Copyright (c) Fabrizio Aversa
  3. ===========================================================*/
  4. #define INCL_DOSNLS        /* National Language Support values */
  5. #include <os2.h>
  6.  
  7. #include <istring.hpp>
  8. #include <idate.hpp>
  9.  
  10. #include <mavconv.hpp>
  11.  
  12. void getCountryInfo(COUNTRYINFO * CountryInfo)
  13. {
  14. COUNTRYCODE  Country = {0};   /* Country code info (0 = current country) */
  15.    ULONG        ulInfoLen = 0;
  16.    APIRET       rc = 0;
  17.  
  18.    rc = DosQueryCtryInfo(sizeof(COUNTRYINFO), &Country,
  19.    CountryInfo, &ulInfoLen);
  20.  
  21.    if (rc) {
  22.       memset(CountryInfo, '\0', sizeof(COUNTRYINFO));
  23.    }
  24.  
  25. }
  26.  
  27. /* ===================================================
  28. Conversion function for idate
  29. ===================================================  */
  30. IDate StringToDate(const IString & temps1)
  31. /* Converts from IString to IDate
  32. searching for separators and parsing supplied string */
  33. {
  34.    int SepPos[2];
  35.    int i, j= 0, Da, Mo, Ye;
  36.    IString temps= temps1;
  37.    IDate dat1;
  38.    COUNTRYINFO  CountryInfo; /* Buffer for country-specific information */
  39.  
  40.    getCountryInfo(&CountryInfo);
  41.  
  42.    temps.strip();
  43.    if(temps.length()<6) {
  44.       dat1= 0;
  45.       return dat1;
  46.    }
  47.    /* try scanning date with separators as 12/10/90 or 12/10/1990 */
  48.  
  49.    /* search separators */
  50.    for (i= 0 ; (i < temps.length()) && (j < 2); i++ )
  51.    /* check for possible separators */
  52.    if ( strchr(" ,/-.=*_:", temps[i]) ) SepPos[j++]= i;
  53.  
  54.    if (j == 2 && SepPos[0] > 0 && SepPos[1] - SepPos[0] > 1) { // 2 separ. found
  55.       Da= atoi(temps.subString(1, SepPos[0]-1));
  56.       Mo= atoi(temps.subString(SepPos[0]+1,SepPos[1]-SepPos[0]-1));
  57.       Ye= atoi(temps.subString(SepPos[1]+1,temps.length()-SepPos[1]));
  58.    } else {
  59.       /* try scanning date without separators as 121090 or 12101990 */
  60.       Da= atoi(temps.subString(1, 2));
  61.       Mo= atoi(temps.subString(3, 2));
  62.       Ye= atoi(temps.subString(5, temps.length()-4));
  63.    }
  64.  
  65.    switch (CountryInfo.fsDateFmt) {
  66.  
  67.       case(0):
  68.       /* it is to be interpreted as mm/dd/yy or mm/dd */
  69.       /* swap mm with dd */
  70.       i = Mo;
  71.       Mo = Da;
  72.       Da = i;
  73.  
  74.       break;
  75.  
  76.       case (1):
  77.       break;
  78.  
  79.       case (2):
  80.       /* it is to be interpreted as yy/mm/dd, DO NOT ADMIT noYear */
  81.  
  82.       /* swap yy with dd */
  83.       i = Ye;
  84.       Ye = Da;
  85.       Da = i;
  86.  
  87.       break;
  88.  
  89.    }
  90.  
  91.    /* year offset is now taken into account if year between 1970 and 2069 */
  92.    /* if year < 70 it's beyond 2000 !!! */
  93.    if(Ye < 70) Ye += 2000;
  94.    else
  95.    /* if year is > 100 user entered in format ccyy */
  96.    if(Ye < 100) Ye += 1900;
  97.  
  98.    if(IDate::isValid((IDate::Month)Mo,Da,Ye)) {
  99.       dat1= IDate((IDate::Month)Mo,Da,Ye);
  100.    } else {
  101.       dat1= 0;
  102.    }
  103.  
  104.    return dat1;
  105.  
  106. }
  107.  
  108. IString DateToString(const IDate & d, const Boolean booNoYear, const Boolean booFullYear)
  109. {
  110.    COUNTRYINFO  CountryInfo; /* Buffer for country-specific information */
  111.    getCountryInfo(&CountryInfo);
  112.  
  113.    IString strSep= IString(CountryInfo.szDateSeparator);
  114.    IString strYearFormat, strNoYearFormat;
  115.    IString strYear4;
  116.    if(booFullYear) {
  117.       strYear4 = "%Y";
  118.    } else {
  119.       strYear4 = "%y";
  120.    }
  121.  
  122.    if(d.julianDate()) {
  123.  
  124.       switch (CountryInfo.fsDateFmt) {
  125.  
  126.          case(0):
  127.          strYearFormat= IString("%m") + strSep + IString("%d") + strSep + strYear4;
  128.          strNoYearFormat= IString("%m") + strSep + IString("%d");
  129.          break;
  130.  
  131.          case(1):
  132.          strYearFormat= IString("%d") + strSep + IString("%m") + strSep + strYear4;
  133.          strNoYearFormat= IString("%d") + strSep + IString("%m");
  134.          break;
  135.  
  136.          case(2):
  137.          strYearFormat= strYear4 + strSep + IString("%m") + strSep + IString("%d");
  138.          strNoYearFormat= IString("%m") + strSep + IString("%d");
  139.          break;
  140.  
  141.       } /* endswitch */
  142.  
  143.       if(booNoYear) {
  144.          return d.asString(strNoYearFormat);
  145.       } else {
  146.          return d.asString(strYearFormat);
  147.       }
  148.  
  149.    }
  150.  
  151.    return IString("");
  152.  
  153. }
  154.  
  155. double StringToDouble(IString & temps)
  156. /* converts from a string representing a number in national
  157. format to a double */
  158. {
  159.    USHORT i, dotfound= 0 ;
  160.  
  161.    COUNTRYINFO  CountryInfo; /* Buffer for country-specific information */
  162.    getCountryInfo(&CountryInfo);
  163.  
  164.    temps.lowerCase().strip();
  165.  
  166.    if ( temps.length() == 0 ) return 0.0 ;
  167.  
  168.    /* remove thousands separators */
  169.    for (i= 1 ; i<= temps.length() ; i++ ) {
  170.  
  171.       if ( temps[i] == CountryInfo.szThousandsSeparator[0] )
  172.       temps= temps.subString(1, i-1)+temps.subString(i+1, temps.length()-i);
  173.  
  174.    } /* end of for */
  175.  
  176.    /* replace country specific decimal separator with '.' */
  177.    for (i= 1 ; i<= temps.length() ; i++ ) {
  178.  
  179.       if ( temps[i] == CountryInfo.szDecimal[0] ) temps[i] = '.';
  180.  
  181.    } /* end of for */
  182.  
  183.    return temps.asDouble();
  184.  
  185. }
  186.  
  187. IString DoubleToString(const double d, const Boolean booTh)
  188. /* formats a number in national format eventually with thousands separator */
  189. {
  190.    INT i, iStart;
  191.  
  192.    COUNTRYINFO  CountryInfo; /* Buffer for country-specific information */
  193.    getCountryInfo(&CountryInfo);
  194.  
  195.    IString strRet(d);
  196.  
  197.    // do not show 0
  198.    if (!d) return (strRet= "");
  199.  
  200.    /* replace '.' with country specific decimal separator */
  201.    for (i= strRet.length() ; i; i-- ) {
  202.       if ( strRet[i] == '.' ) strRet[i] =  CountryInfo.szDecimal[0];
  203.    }
  204.  
  205.    if (booTh) {
  206.  
  207.       iStart= strRet.length()+1;
  208.  
  209.       /* find where integer part begins */
  210.       for (i= strRet.length() ; i; i-- ) {
  211.          if ( strRet[i] == CountryInfo.szDecimal[0] || strRet[i] == 'e') iStart= i ;
  212.       }
  213.  
  214.       for (i= iStart-4 ; i >= (strRet[1] == '-' ? 2:1) ; i -= 3 ) {
  215.          strRet= strRet.subString(1, i)+
  216.          IString(CountryInfo.szThousandsSeparator)+
  217.          strRet.subString(i+1, strRet.length()-i);
  218.       }
  219.    }
  220.  
  221.    return strRet;
  222. }
  223.  
  224.  
  225.  
  226.  
  227.