home *** CD-ROM | disk | FTP | other *** search
/ AmigActive 6 / AACD06.ISO / AACD / Programming / ICU / src / icu / source / i18n / ucal.cpp < prev    next >
Encoding:
C/C++ Source or Header  |  1999-10-22  |  9.3 KB  |  379 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 "ucal.h"
  15.  
  16. #include "uloc.h"
  17. #include "calendar.h"
  18. #include "timezone.h"
  19. #include "ustring.h"
  20. #include "cpputils.h"
  21.  
  22. U_CAPI const UChar*
  23. ucal_getAvailableTZIDs(        int32_t         rawOffset,
  24.                 int32_t         index,
  25.                 UErrorCode*     status)
  26. {
  27.   if(U_FAILURE(*status)) return 0;
  28.   
  29.   int32_t count = 0;
  30.   const UChar *retVal = 0;
  31.   
  32.   const UnicodeString** const tzs = TimeZone::createAvailableIDs(rawOffset, 
  33.                                  count);
  34.  
  35.   if(tzs == 0) {
  36.     *status = U_MEMORY_ALLOCATION_ERROR;
  37.     return 0;
  38.   }
  39.  
  40.   if(index < count)
  41.     retVal = tzs[index]->getUChars();
  42.   else
  43.     *status = U_INDEX_OUTOFBOUNDS_ERROR;
  44.   
  45.   delete [] tzs;
  46.   return retVal;
  47. }
  48.  
  49. U_CAPI int32_t
  50. ucal_countAvailableTZIDs(int32_t rawOffset)
  51. {  
  52.   int32_t count = 0;
  53.   
  54.   const UnicodeString** const tzs  = TimeZone::createAvailableIDs(rawOffset, 
  55.                                   count);
  56.  
  57.   if(tzs == 0) {
  58.     // TBD: U_MEMORY_ALLOCATION_ERROR
  59.     return 0;
  60.   }
  61.  
  62.   delete [] tzs;
  63.   return count;
  64. }
  65.  
  66. U_CAPI UDate 
  67. ucal_getNow()
  68. {
  69.   return Calendar::getNow();
  70. }
  71.  
  72. // ignore type until we add more subclasses
  73. U_CAPI UCalendar* 
  74. ucal_open(    const    UChar*          zoneID,
  75.             int32_t        len,
  76.         const    char*       locale,
  77.             UCalendarType     type,
  78.             UErrorCode*    status)
  79. {
  80.   if(U_FAILURE(*status)) return 0;
  81.   
  82.   TimeZone *zone = 0;
  83.   if(zoneID == 0) {
  84.     zone = TimeZone::createDefault();
  85.   }
  86.   else {
  87.     int32_t length = (len == -1 ? u_strlen(zoneID) : len);
  88.  
  89.     zone = TimeZone::createTimeZone(UnicodeString((UChar*)zoneID,
  90.                           length, length));
  91.   }
  92.   if(zone == 0) {
  93.     *status = U_MEMORY_ALLOCATION_ERROR;
  94.     return 0;
  95.   }
  96.   
  97.   return (UCalendar*)Calendar::createInstance(zone, Locale().init(locale), *status);
  98. }
  99.  
  100. U_CAPI void
  101. ucal_close(UCalendar *cal)
  102. {
  103.   delete (Calendar*) cal;
  104. }
  105.  
  106. U_CAPI void 
  107. ucal_setTimeZone(    UCalendar*      cal,
  108.             const    UChar*            zoneID,
  109.             int32_t        len,
  110.             UErrorCode *status)
  111. {
  112.   if(U_FAILURE(*status)) return;
  113.  
  114.   TimeZone *zone = 0;
  115.   if(zone == 0) {
  116.     zone = TimeZone::createDefault();
  117.   }
  118.   else {
  119.     int32_t length = (len == -1 ? u_strlen(zoneID) : len);
  120.     zone = TimeZone::createTimeZone(UnicodeString((UChar*)zoneID, 
  121.                           length, length));
  122.   }
  123.   if(zone == 0) {
  124.     *status = U_MEMORY_ALLOCATION_ERROR;
  125.     return;
  126.   }
  127.  
  128.   ((Calendar*)cal)->adoptTimeZone(zone);
  129. }
  130.  
  131. U_CAPI int32_t
  132. ucal_getTimeZoneDisplayName(    const     UCalendar*                 cal,
  133.                     UCalendarDisplayNameType     type,
  134.                 const      char                     *locale,
  135.                     UChar*                  result,
  136.                     int32_t                 resultLength,
  137.                     UErrorCode*             status)
  138. {
  139.   if(U_FAILURE(*status)) return -1;
  140.  
  141.   int32_t actLen;
  142.  
  143.   const TimeZone& tz = ((Calendar*)cal)->getTimeZone();
  144.   UnicodeString id(result, 0, resultLength);
  145.  
  146.   switch(type) {
  147.   case UCAL_STANDARD:
  148.     tz.getDisplayName(FALSE, TimeZone::LONG, Locale().init(locale), id);
  149.     break;
  150.  
  151.   case UCAL_SHORT_STANDARD:
  152.     tz.getDisplayName(FALSE, TimeZone::SHORT, Locale().init(locale), id);
  153.     break;
  154.  
  155.   case UCAL_DST:
  156.     tz.getDisplayName(TRUE, TimeZone::LONG, Locale().init(locale), id);
  157.     break;
  158.  
  159.   case UCAL_SHORT_DST:
  160.     tz.getDisplayName(TRUE, TimeZone::SHORT, Locale().init(locale), id);
  161.     break;
  162.   }
  163.  
  164.   T_fillOutputParams(&id, result, resultLength, &actLen, status);
  165.   return actLen;
  166. }
  167.  
  168. U_CAPI bool_t 
  169. ucal_inDaylightTime(    const    UCalendar*      cal, 
  170.             UErrorCode*     status )
  171. {
  172.   if(U_FAILURE(*status)) return (bool_t) -1;
  173.   return ((Calendar*)cal)->inDaylightTime(*status);
  174. }
  175.  
  176. U_CAPI int32_t
  177. ucal_getAttribute(    const    UCalendar*              cal,
  178.             UCalendarAttribute      attr)
  179. {
  180.   switch(attr) {
  181.   case UCAL_LENIENT:
  182.     return ((Calendar*)cal)->isLenient();
  183.     
  184.   case UCAL_FIRST_DAY_OF_WEEK:
  185.     return ((Calendar*)cal)->getFirstDayOfWeek();
  186.       
  187.   case UCAL_MINIMAL_DAYS_IN_FIRST_WEEK:
  188.     return ((Calendar*)cal)->getMinimalDaysInFirstWeek();
  189.  
  190.   default:
  191.     break;
  192.   }
  193.   return -1;
  194. }
  195.  
  196. U_CAPI void
  197. ucal_setAttribute(      UCalendar*              cal,
  198.             UCalendarAttribute      attr,
  199.             int32_t                 newValue)
  200. {
  201.   switch(attr) {
  202.   case UCAL_LENIENT:
  203.     ((Calendar*)cal)->setLenient((bool_t)newValue);
  204.     break;
  205.     
  206.   case UCAL_FIRST_DAY_OF_WEEK:
  207.     ((Calendar*)cal)->setFirstDayOfWeek((Calendar::EDaysOfWeek)newValue);
  208.     break;
  209.       
  210.   case UCAL_MINIMAL_DAYS_IN_FIRST_WEEK:
  211.     ((Calendar*)cal)->setMinimalDaysInFirstWeek((uint8_t)newValue);
  212.     break;
  213.   }
  214. }
  215.  
  216. U_CAPI const char*
  217. ucal_getAvailable(int32_t index)
  218. {
  219.   return uloc_getAvailable(index);
  220. }
  221.  
  222. U_CAPI int32_t
  223. ucal_countAvailable()
  224. {
  225.   return uloc_countAvailable();
  226. }
  227.  
  228. U_CAPI UDate 
  229. ucal_getMillis(    const    UCalendar*      cal,
  230.         UErrorCode*     status)
  231. {
  232.   if(U_FAILURE(*status)) return (UDate) 0;
  233.  
  234.   return ((Calendar*)cal)->getTime(*status);
  235. }
  236.  
  237. U_CAPI void 
  238. ucal_setMillis(        UCalendar*      cal,
  239.             UDate           dateTime,
  240.             UErrorCode*     status )
  241. {
  242.   if(U_FAILURE(*status)) return;
  243.  
  244.   ((Calendar*)cal)->setTime(dateTime, *status);
  245. }
  246.  
  247. // TBD: why does this take an UErrorCode?
  248. U_CAPI void 
  249. ucal_setDate(        UCalendar*        cal,
  250.             int32_t            year,
  251.             int32_t            month,
  252.             int32_t            date,
  253.             UErrorCode        *status)
  254. {
  255.   if(U_FAILURE(*status)) return;
  256.  
  257.   ((Calendar*)cal)->set(year, month, date);
  258. }
  259.  
  260. // TBD: why does this take an UErrorCode?
  261. U_CAPI void 
  262. ucal_setDateTime(    UCalendar*        cal,
  263.             int32_t            year,
  264.             int32_t            month,
  265.             int32_t            date,
  266.             int32_t            hour,
  267.             int32_t            minute,
  268.             int32_t            second,
  269.             UErrorCode        *status)
  270. {
  271.   if(U_FAILURE(*status)) return;
  272.  
  273.   ((Calendar*)cal)->set(year, month, date, hour, minute, second);
  274. }
  275.  
  276. U_CAPI bool_t 
  277. ucal_equivalentTo(    const UCalendar*      cal1,
  278.             const UCalendar*      cal2)
  279. {
  280.   return ((Calendar*)cal1)->equivalentTo(*((Calendar*)cal2));
  281. }
  282.  
  283. U_CAPI void 
  284. ucal_add(    UCalendar*                cal,
  285.         UCalendarDateFields        field,
  286.         int32_t                    amount,
  287.         UErrorCode*                status)
  288. {
  289.   if(U_FAILURE(*status)) return;
  290.  
  291.   ((Calendar*)cal)->add((Calendar::EDateFields)field, amount, *status);
  292. }
  293.  
  294. U_CAPI void 
  295. ucal_roll(        UCalendar*            cal,
  296.             UCalendarDateFields field,
  297.             int32_t                amount,
  298.             UErrorCode*            status)
  299. {
  300.   if(U_FAILURE(*status)) return;
  301.  
  302.   ((Calendar*)cal)->roll((Calendar::EDateFields)field, amount, *status);
  303. }
  304.  
  305. U_CAPI int32_t 
  306. ucal_get(    const    UCalendar*                cal,
  307.         UCalendarDateFields        field,
  308.         UErrorCode*                status )
  309. {
  310.   if(U_FAILURE(*status)) return -1;
  311.  
  312.   return ((Calendar*)cal)->get((Calendar::EDateFields)field, *status);
  313. }
  314.  
  315. U_CAPI void 
  316. ucal_set(    UCalendar*                cal,
  317.         UCalendarDateFields        field,
  318.         int32_t                    value)
  319. {
  320.   ((Calendar*)cal)->set((Calendar::EDateFields)field, value);
  321. }
  322.  
  323. U_CAPI bool_t 
  324. ucal_isSet(    const    UCalendar*                cal,
  325.         UCalendarDateFields        field)
  326. {
  327.   return ((Calendar*)cal)->isSet((Calendar::EDateFields)field);
  328. }
  329.  
  330. U_CAPI void 
  331. ucal_clearField(    UCalendar*            cal,
  332.             UCalendarDateFields field)
  333. {
  334.   ((Calendar*)cal)->clear((Calendar::EDateFields)field);
  335. }
  336.  
  337. U_CAPI void 
  338. ucal_clear(UCalendar* calendar)
  339. {
  340.   ((Calendar*)calendar)->clear();
  341. }
  342.  
  343. U_CAPI int32_t 
  344. ucal_getLimit(    const    UCalendar*              cal,
  345.             UCalendarDateFields     field,
  346.             UCalendarLimitType      type,
  347.             UErrorCode        *status)
  348. {
  349.   if(status==0 || U_FAILURE(*status)) {
  350.     return -1;
  351.   }
  352.   
  353.   switch(type) {
  354.   case UCAL_MINIMUM:
  355.     return ((Calendar*)cal)->getMinimum((Calendar::EDateFields)field);
  356.  
  357.   case UCAL_MAXIMUM:
  358.     return ((Calendar*)cal)->getMaximum((Calendar::EDateFields)field);
  359.  
  360.   case UCAL_GREATEST_MINIMUM:
  361.     return ((Calendar*)cal)->getGreatestMinimum((Calendar::EDateFields)field);
  362.  
  363.   case UCAL_LEAST_MAXIMUM:
  364.     return ((Calendar*)cal)->getLeastMaximum((Calendar::EDateFields)field);
  365.  
  366.   case UCAL_ACTUAL_MINIMUM:
  367.     return ((Calendar*)cal)->getActualMinimum((Calendar::EDateFields)field,
  368.                           *status);
  369.  
  370.   case UCAL_ACTUAL_MAXIMUM:
  371.     return ((Calendar*)cal)->getActualMaximum((Calendar::EDateFields)field,
  372.                           *status);
  373.  
  374.   default:
  375.     break;
  376.   }
  377.   return -1;
  378. }
  379.