home *** CD-ROM | disk | FTP | other *** search
/ AmigActive 6 / AACD06.ISO / AACD / Programming / ICU / src / icu / source / i18n / udat.cpp < prev    next >
Encoding:
C/C++ Source or Header  |  1999-10-19  |  12.5 KB  |  513 lines

  1. /*
  2. *******************************************************************************
  3. *                                                                             *
  4. * COPYRIGHT:                                                                  *
  5. *   (C) Copyright Taligent, Inc.,  1996                                       *
  6. *   (C) Copyright International Business Machines Corporation,  1998-1999     *
  7. *   Licensed Material - Program-Property of IBM - All Rights Reserved.        *
  8. *   US Government Users Restricted Rights - Use, duplication, or disclosure   *
  9. *   restricted by GSA ADP Schedule Contract with IBM Corp.                    *
  10. *                                                                             *
  11. *******************************************************************************
  12. */
  13.  
  14. #include "udat.h"
  15.  
  16. #include "uloc.h"
  17. #include "datefmt.h"
  18. #include "timezone.h"
  19. #include "smpdtfmt.h"
  20. #include "fieldpos.h"
  21. #include "cpputils.h"
  22. #include "parsepos.h"
  23. #include "calendar.h"
  24. #include "numfmt.h"
  25. #include "dtfmtsym.h"
  26. #include "ustring.h"
  27.  
  28. U_CAPI UDateFormat*
  29. udat_open(            UDateFormatStyle        timeStyle, 
  30.                 UDateFormatStyle        dateStyle,
  31.                 const   char*                locale,
  32.                 const   UChar                    *tzID,
  33.               int32_t tzIDLength,
  34.                 UErrorCode*             status)
  35. {
  36.   if(U_FAILURE(*status)) return 0;
  37.   
  38.   DateFormat *fmt;
  39.   if(locale == 0)
  40.     fmt = DateFormat::createDateTimeInstance((DateFormat::EStyle)dateStyle,
  41.                          (DateFormat::EStyle)timeStyle);
  42.   else
  43.     fmt = DateFormat::createDateTimeInstance((DateFormat::EStyle)dateStyle,
  44.                          (DateFormat::EStyle)timeStyle,
  45.                          Locale().init(locale));
  46.   
  47.   if(fmt == 0) {
  48.     *status = U_MEMORY_ALLOCATION_ERROR;
  49.     return 0;
  50.   }
  51.  
  52.   if(tzID != 0) {
  53.     TimeZone *zone = 0;
  54.     int32_t length = (tzIDLength == -1 ? u_strlen(tzID) : tzIDLength);
  55.     zone = TimeZone::createTimeZone(UnicodeString((UChar*)tzID,
  56.                           length, length));
  57.     if(zone == 0) {
  58.       *status = U_MEMORY_ALLOCATION_ERROR;
  59.       delete fmt;
  60.       return 0;
  61.     }
  62.     fmt->adoptTimeZone(zone);
  63.   }
  64.   
  65.   return (UDateFormat*)fmt;
  66. }
  67.  
  68. U_CAPI UDateFormat*
  69. udat_openPattern(    const   UChar           *pattern, 
  70.             int32_t         patternLength,
  71.             const   char         *locale,
  72.             UErrorCode      *status)
  73. {
  74.   if(U_FAILURE(*status)) return 0;
  75.  
  76.   int32_t len = (patternLength == -1 ? u_strlen(pattern) : patternLength);
  77.   UDateFormat *retVal = 0;
  78.  
  79.   if(locale == 0)
  80.     retVal = (UDateFormat*)new SimpleDateFormat(UnicodeString((UChar*)pattern,
  81.                                   len, len),
  82.                         *status);
  83.   else
  84.     retVal = (UDateFormat*)new SimpleDateFormat(UnicodeString((UChar*)pattern,
  85.                                   len, len),
  86.                         Locale().init(locale),
  87.                         *status);
  88.  
  89.   if(retVal == 0) {
  90.     *status = U_MEMORY_ALLOCATION_ERROR;
  91.     return 0;
  92.   }
  93.   return retVal;
  94. }
  95.  
  96. U_CAPI void
  97. udat_close(UDateFormat* format)
  98. {
  99.   delete (DateFormat*)format;
  100. }
  101.  
  102. U_CAPI UDateFormat*
  103. udat_clone(const UDateFormat *fmt,
  104.        UErrorCode *status)
  105. {
  106.   if(U_FAILURE(*status)) return 0;
  107.  
  108.   Format *res = ((SimpleDateFormat*)fmt)->clone();
  109.   
  110.   if(res == 0) {
  111.     *status = U_MEMORY_ALLOCATION_ERROR;
  112.     return 0;
  113.   }
  114.  
  115.   return (UDateFormat*) res;
  116. }
  117.  
  118. U_CAPI int32_t
  119. udat_format(    const    UDateFormat*    format,
  120.         UDate           dateToFormat,
  121.         UChar*          result,
  122.         int32_t         resultLength,
  123.         UFieldPosition* position,
  124.         UErrorCode*     status)
  125. {
  126.   if(U_FAILURE(*status)) return -1;
  127.  
  128.   int32_t actLen;
  129.  
  130.   UnicodeString res(result, 0, resultLength);
  131.   FieldPosition fp;
  132.   
  133.   if(position != 0)
  134.     fp.setField(position->field);
  135.   
  136.   ((DateFormat*)format)->format(dateToFormat, res, fp);
  137.   T_fillOutputParams(&res, result, resultLength, &actLen, status);
  138.   
  139.   if(position != 0) {
  140.     position->beginIndex = fp.getBeginIndex();
  141.     position->endIndex = fp.getEndIndex();
  142.   }
  143.   
  144.   return actLen;
  145. }
  146.  
  147. U_CAPI UDate
  148. udat_parse(    const    UDateFormat*        format,
  149.         const    UChar*          text,
  150.         int32_t         textLength,
  151.         int32_t         *parsePos,
  152.         UErrorCode      *status)
  153. {
  154.   if(U_FAILURE(*status)) return (UDate)0;
  155.  
  156.   int32_t len = (textLength == -1 ? u_strlen(text) : textLength);
  157.   const UnicodeString src((UChar*)text, len, len);
  158.   ParsePosition pp;
  159.   UDate res;
  160.  
  161.   if(parsePos != 0)
  162.     pp.setIndex(*parsePos);
  163.  
  164.   res = ((DateFormat*)format)->parse(src, pp);
  165.  
  166.   if(parsePos != 0) {
  167.     if(pp.getErrorIndex() == -1)
  168.       *parsePos = pp.getIndex();
  169.     else {
  170.       *parsePos = pp.getErrorIndex();
  171.       *status = U_PARSE_ERROR;
  172.     }
  173.   }
  174.   
  175.   return res;
  176. }
  177.  
  178. U_CAPI bool_t
  179. udat_isLenient(const UDateFormat* fmt)
  180. {
  181.   return ((DateFormat*)fmt)->isLenient();
  182. }
  183.  
  184. U_CAPI void
  185. udat_setLenient(    UDateFormat*    fmt,
  186.             bool_t          isLenient)
  187. {
  188.   ((DateFormat*)fmt)->setLenient(isLenient);
  189. }
  190.  
  191. U_CAPI const UCalendar*
  192. udat_getCalendar(const UDateFormat* fmt)
  193. {
  194.   return (const UCalendar*) ((DateFormat*)fmt)->getCalendar();
  195. }
  196.  
  197. U_CAPI void
  198. udat_setCalendar(            UDateFormat*    fmt,
  199.                     const   UCalendar*      calendarToSet)
  200. {
  201.   ((DateFormat*)fmt)->setCalendar(*((Calendar*)calendarToSet));
  202. }
  203.  
  204. U_CAPI const UNumberFormat*
  205. udat_getNumberFormat(const UDateFormat* fmt)
  206. {
  207.   return (const UNumberFormat*) ((DateFormat*)fmt)->getNumberFormat();
  208. }
  209.  
  210. U_CAPI void
  211. udat_setNumberFormat(            UDateFormat*    fmt,
  212.                     const   UNumberFormat*  numberFormatToSet)
  213. {
  214.   ((DateFormat*)fmt)->setNumberFormat(*((NumberFormat*)numberFormatToSet));
  215. }
  216.  
  217. U_CAPI const char*
  218. udat_getAvailable(int32_t index)
  219. {
  220.   return uloc_getAvailable(index);
  221. }
  222.  
  223. U_CAPI int32_t
  224. udat_countAvailable()
  225. {
  226.   return uloc_countAvailable();
  227. }
  228.  
  229. U_CAPI UDate
  230. udat_get2DigitYearStart(    const   UDateFormat     *fmt,
  231.                 UErrorCode      *status)
  232. {
  233.   if(U_FAILURE(*status)) return (UDate)0;
  234.   return ((SimpleDateFormat*)fmt)->get2DigitYearStart(*status);
  235. }
  236.  
  237. U_CAPI void
  238. udat_set2DigitYearStart(    UDateFormat     *fmt,
  239.                 UDate           d,
  240.                 UErrorCode      *status)
  241. {
  242.   if(U_FAILURE(*status)) return;
  243.   ((SimpleDateFormat*)fmt)->set2DigitYearStart(d, *status);
  244. }
  245.  
  246. U_CAPI int32_t
  247. udat_toPattern(    const   UDateFormat     *fmt,
  248.         bool_t          localized,
  249.         UChar           *result,
  250.         int32_t         resultLength,
  251.         UErrorCode      *status)
  252. {
  253.   if(U_FAILURE(*status)) return -1;
  254.  
  255.   int32_t actLen;
  256.  
  257.   UnicodeString res(result, 0, resultLength);
  258.  
  259.   if(localized)
  260.     ((SimpleDateFormat*)fmt)->toLocalizedPattern(res, *status);
  261.   else
  262.     ((SimpleDateFormat*)fmt)->toPattern(res);
  263.  
  264.   T_fillOutputParams(&res, result, resultLength, &actLen, status);
  265.   return actLen;
  266. }
  267.  
  268. // TBD: should this take an UErrorCode?
  269. U_CAPI void
  270. udat_applyPattern(            UDateFormat     *format,
  271.                     bool_t          localized,
  272.                     const   UChar           *pattern,
  273.                     int32_t         patternLength)
  274. {
  275.   int32_t len = (patternLength == -1 ? u_strlen(pattern) : patternLength);
  276.   const UnicodeString pat((UChar*)pattern, len, len);
  277.   UErrorCode status = U_ZERO_ERROR;
  278.  
  279.   if(localized)
  280.     ((SimpleDateFormat*)format)->applyLocalizedPattern(pat, status);
  281.   else
  282.     ((SimpleDateFormat*)format)->applyPattern(pat);
  283. }
  284.  
  285. U_CAPI int32_t
  286. udat_getSymbols(const   UDateFormat             *fmt,
  287.         UDateFormatSymbolType   type,
  288.         int32_t                 index,
  289.         UChar                   *result,
  290.         int32_t                 resultLength,
  291.         UErrorCode              *status)
  292. {
  293.   if(U_FAILURE(*status)) return -1;
  294.  
  295.   int32_t actLen;
  296.  
  297.   const DateFormatSymbols *syms = 
  298.     ((SimpleDateFormat*)fmt)->getDateFormatSymbols();
  299.   int32_t count;
  300.   const UnicodeString *res;
  301.   UnicodeString res1(result, 0, resultLength);
  302.  
  303.  
  304.   switch(type) {
  305.   case UDAT_ERAS:
  306.     res = syms->getEras(count);
  307.     if(index < count) {
  308.       T_fillOutputParams(&res[index], result, resultLength, 
  309.              &actLen, status);
  310.     }
  311.     break;
  312.  
  313.   case UDAT_MONTHS:
  314.     res = syms->getMonths(count);
  315.     if(index < count) {
  316.       T_fillOutputParams(&res[index], result, resultLength, 
  317.              &actLen, status);
  318.     }
  319.     break;
  320.  
  321.   case UDAT_SHORT_MONTHS:
  322.     res = syms->getShortMonths(count);
  323.     if(index < count) {
  324.       T_fillOutputParams(&res[index], result, resultLength, 
  325.              &actLen, status);
  326.     }
  327.     break;
  328.  
  329.   case UDAT_WEEKDAYS:
  330.     res = syms->getWeekdays(count);
  331.     if(index < count) {
  332.       T_fillOutputParams(&res[index], result, resultLength, 
  333.              &actLen, status);
  334.     }
  335.     break;
  336.  
  337.   case UDAT_SHORT_WEEKDAYS:
  338.     res = syms->getShortWeekdays(count);
  339.     if(index < count) {
  340.       T_fillOutputParams(&res[index], result, resultLength, 
  341.              &actLen, status);
  342.     }
  343.     break;
  344.  
  345.   case UDAT_AM_PMS:
  346.     res = syms->getAmPmStrings(count);
  347.     if(index < count) {
  348.       T_fillOutputParams(&res[index], result, resultLength, 
  349.              &actLen, status);
  350.     }
  351.     break;
  352.  
  353.   case UDAT_LOCALIZED_CHARS:
  354.     syms->getLocalPatternChars(res1);
  355.     T_fillOutputParams(&res1, result, resultLength, &actLen, status);
  356.     break;
  357.   }
  358.   
  359.   return actLen;
  360. }
  361.  
  362. U_CAPI int32_t
  363. udat_countSymbols(    const    UDateFormat                *fmt,
  364.             UDateFormatSymbolType    type)
  365. {
  366.   const DateFormatSymbols *syms = 
  367.     ((SimpleDateFormat*)fmt)->getDateFormatSymbols();
  368.   int32_t count;
  369.  
  370.   switch(type) {
  371.   case UDAT_ERAS:
  372.     syms->getEras(count);
  373.     break;
  374.  
  375.   case UDAT_MONTHS:
  376.     syms->getMonths(count);
  377.     break;
  378.  
  379.   case UDAT_SHORT_MONTHS:
  380.     syms->getShortMonths(count);
  381.     break;
  382.  
  383.   case UDAT_WEEKDAYS:
  384.     syms->getWeekdays(count);
  385.     break;
  386.  
  387.   case UDAT_SHORT_WEEKDAYS:
  388.     syms->getShortWeekdays(count);
  389.     break;
  390.  
  391.   case UDAT_AM_PMS:
  392.     syms->getAmPmStrings(count);
  393.     break;
  394.  
  395.   case UDAT_LOCALIZED_CHARS:
  396.     count = 1;
  397.     break;
  398.   }
  399.   
  400.   return count;
  401. }
  402.  
  403. U_CAPI void
  404. udat_setSymbols(    UDateFormat             *format,
  405.             UDateFormatSymbolType   type,
  406.             int32_t                 index,
  407.             UChar                   *value,
  408.             int32_t                 valueLength,
  409.             UErrorCode              *status)
  410. {
  411.   if(U_FAILURE(*status)) return;
  412.  
  413.   int32_t count;
  414.   int32_t len = (valueLength == -1 ? u_strlen(value) : valueLength);
  415.   const UnicodeString val((UChar*)value, len, len);
  416.   const UnicodeString *res;
  417.   DateFormatSymbols *syms = new DateFormatSymbols( 
  418.     * ((SimpleDateFormat*)format)->getDateFormatSymbols() );
  419.   UnicodeString *array = 0;
  420.  
  421.   if(syms == 0) {
  422.     *status = U_MEMORY_ALLOCATION_ERROR;
  423.     return;
  424.   }
  425.  
  426.   switch(type) {
  427.   case UDAT_ERAS:
  428.     res = syms->getEras(count);
  429.     array = new UnicodeString[count];
  430.     if(array == 0) {
  431.       *status = U_MEMORY_ALLOCATION_ERROR;
  432.       return;
  433.     }
  434.     icu_arrayCopy(res, array, count);
  435.     if(index < count)
  436.       array[index] = val;
  437.     syms->setEras(array, count);
  438.     break;
  439.  
  440.   case UDAT_MONTHS:
  441.     res = syms->getMonths(count);
  442.     array = new UnicodeString[count];
  443.     if(array == 0) {
  444.       *status = U_MEMORY_ALLOCATION_ERROR;
  445.       return;
  446.     }
  447.     icu_arrayCopy(res, array, count);
  448.     if(index < count)
  449.       array[index] = val;
  450.     syms->setMonths(array, count);
  451.     break;
  452.  
  453.   case UDAT_SHORT_MONTHS:
  454.     res = syms->getShortMonths(count);
  455.     array = new UnicodeString[count];
  456.     if(array == 0) {
  457.       *status = U_MEMORY_ALLOCATION_ERROR;
  458.       return;
  459.     }
  460.     icu_arrayCopy(res, array, count);
  461.     if(index < count)
  462.       array[index] = val;
  463.     syms->setShortMonths(array, count);
  464.     break;
  465.  
  466.   case UDAT_WEEKDAYS:
  467.     res = syms->getWeekdays(count);
  468.     array = new UnicodeString[count];
  469.     if(array == 0) {
  470.       *status = U_MEMORY_ALLOCATION_ERROR;
  471.       return;
  472.     }
  473.     icu_arrayCopy(res, array, count);
  474.     if(index < count)
  475.       array[index] = val;
  476.     syms->setWeekdays(array, count);
  477.     break;
  478.  
  479.   case UDAT_SHORT_WEEKDAYS:
  480.     res = syms->getShortWeekdays(count);
  481.     array = new UnicodeString[count];
  482.     if(array == 0) {
  483.       *status = U_MEMORY_ALLOCATION_ERROR;
  484.       return;
  485.     }
  486.     icu_arrayCopy(res, array, count);
  487.     if(index < count)
  488.       array[index] = val;
  489.     syms->setShortWeekdays(array, count);
  490.     break;
  491.  
  492.   case UDAT_AM_PMS:
  493.     res = syms->getAmPmStrings(count);
  494.     array = new UnicodeString[count];
  495.     if(array == 0) {
  496.       *status = U_MEMORY_ALLOCATION_ERROR;
  497.       return;
  498.     }
  499.     icu_arrayCopy(res, array, count);
  500.     if(index < count)
  501.       array[index] = val;
  502.     syms->setAmPmStrings(array, count);
  503.     break;
  504.  
  505.   case UDAT_LOCALIZED_CHARS:
  506.     syms->setLocalPatternChars(val);
  507.     break;
  508.   }
  509.   
  510.   ((SimpleDateFormat*)format)->adoptDateFormatSymbols(syms);
  511.   delete [] array;
  512. }
  513.