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

  1. /*
  2. ********************************************************************************
  3. *                                                                              *
  4. * COPYRIGHT:                                                                   *
  5. *   (C) Copyright Taligent, Inc.,  1997                                        *
  6. *   (C) Copyright International Business Machines Corporation,  1997-1998           *
  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. * File CALTZTST.H
  14. *
  15. * Modification History:
  16. *
  17. *   Date        Name        Description
  18. *   08/06/97    aliu        Creation.
  19. ********************************************************************************
  20. */
  21.  
  22. #include "caltztst.h"
  23. #include "smpdtfmt.h"
  24. #include "mutex.h"
  25.  
  26. DateFormat*         CalendarTimeZoneTest::fgDateFormat = 0;
  27. Calendar*           CalendarTimeZoneTest::fgCalendar   = 0;
  28.  
  29. bool_t CalendarTimeZoneTest::failure(UErrorCode status, const char* msg)
  30. {
  31.     if (U_FAILURE(status))
  32.     {
  33.         errln(UnicodeString("FAIL: ") + msg + " failed, error " + errorName(status));
  34.         return TRUE;
  35.     }
  36.     return FALSE;
  37. }
  38.  
  39. DateFormat*   CalendarTimeZoneTest::getDateFormat()
  40. {
  41.     DateFormat *theFormat = 0;
  42.  
  43.     if (fgDateFormat != 0) // if there's something in the cache
  44.     {
  45.         Mutex lock;
  46.  
  47.         if (fgDateFormat != 0) // Someone might have grabbed it.
  48.         {
  49.             theFormat = fgDateFormat;
  50.             fgDateFormat = 0; // We have exclusive right to this formatter.
  51.         }
  52.     }
  53.  
  54.     if(theFormat == 0) // If we weren't able to pull it out of the cache, then we have to create it.
  55.     {
  56.         UErrorCode status = U_ZERO_ERROR;
  57.         theFormat = new SimpleDateFormat("EEE MMM dd HH:mm:ss zzz yyyy", status);
  58.         if (U_FAILURE(status))
  59.         {
  60.             delete theFormat;
  61.             theFormat = 0;
  62.             errln("FAIL: Could not create SimpleDateFormat");
  63.         }
  64.     }
  65.  
  66.     return theFormat;
  67. }
  68.  
  69. void CalendarTimeZoneTest::releaseDateFormat(DateFormat *adopt)
  70. {
  71.     if(fgDateFormat == 0) // If the cache is empty we must add it back.
  72.     {
  73.         Mutex lock;
  74.  
  75.         if(fgDateFormat == 0)
  76.         {
  77.             fgDateFormat = adopt;
  78.             adopt = 0;
  79.         }
  80.     }
  81.  
  82.     delete adopt;
  83. }
  84.  
  85. Calendar*  CalendarTimeZoneTest::getCalendar()
  86. {
  87.     Calendar *theCalendar = 0;
  88.  
  89.     if (fgCalendar != 0) // if there's something in the cache
  90.     {
  91.         Mutex lock;
  92.  
  93.         if (fgCalendar != 0) // Someone might have grabbed it.
  94.         {
  95.             theCalendar = fgCalendar;
  96.             fgCalendar = 0; // We have exclusive right to this calendar.
  97.         }
  98.     }
  99.  
  100.     if(theCalendar == 0) // If we weren't able to pull it out of the cache, then we have to create it.
  101.     {
  102.         UErrorCode status = U_ZERO_ERROR;
  103.         theCalendar = Calendar::createInstance(status);
  104.         if (U_FAILURE(status))
  105.         {
  106.             delete theCalendar;
  107.             theCalendar = 0;
  108.             errln("FAIL: Calendar::createInstance failed");
  109.         }
  110.     }
  111.     return theCalendar;
  112. }
  113.  
  114. void CalendarTimeZoneTest::releaseCalendar(Calendar* adopt)
  115. {
  116.     if(fgCalendar == 0) // If the cache is empty we must add it back.
  117.     {
  118.         Mutex lock;
  119.  
  120.         if(fgCalendar == 0)
  121.         {
  122.             fgCalendar = adopt;
  123.             adopt = 0;
  124.         }
  125.     }
  126.  
  127.     delete adopt;
  128. }
  129.  
  130. // Utility method for formatting dates for printing; useful for Java->C++ conversion.
  131. // Tries to mimic the Java Date.toString() format.
  132. UnicodeString
  133. CalendarTimeZoneTest::dateToString(UDate d)
  134. {
  135.     UnicodeString str;
  136.     return dateToString(d, str);
  137. }
  138.  
  139. UnicodeString&
  140. CalendarTimeZoneTest::dateToString(UDate d, UnicodeString& str)
  141. {
  142.     str.remove();
  143.     DateFormat* format = getDateFormat();
  144.     if (format == 0)
  145.     {
  146.         str += "DATE_FORMAT_FAILURE";
  147.         return str;
  148.     }
  149.     format->format(d, str);
  150.     releaseDateFormat(format);
  151.     return str;
  152. }
  153.  
  154. // Utility methods to create a date.  This is useful for converting Java constructs
  155. // which create a Date object.
  156. UDate
  157. CalendarTimeZoneTest::date(int32_t y, int32_t m, int32_t d, int32_t hr, int32_t min, int32_t sec)
  158. {
  159.     Calendar* cal = getCalendar();
  160.     if (cal == 0) return 0.0;
  161.     cal->clear();
  162.     cal->set(1900 + y, m, d, hr, min, sec); // Add 1900 to follow java.util.Date protocol
  163.     UErrorCode status = U_ZERO_ERROR;
  164.     UDate dt = cal->getTime(status);
  165.     releaseCalendar(cal);
  166.     if (U_FAILURE(status))
  167.     {
  168.         errln("FAIL: Calendar::getTime failed");
  169.         return 0.0;
  170.     }
  171.     return dt;
  172. }
  173.  
  174. // Utility methods to create a date.  The returned Date is UTC rather than local.
  175. /*Date
  176. CalendarTimeZoneTest::utcDate(int32_t y, int32_t m, int32_t d, int32_t hr, int32_t min, int32_t sec)
  177. {
  178.     Calendar* cal = getCalendar();
  179.     if (cal == 0) return 0.0;
  180.     UErrorCode status = U_ZERO_ERROR;
  181.     Date dt = date(y, m, d, hr, min, sec) +
  182.         cal->get(Calendar::ZONE_OFFSET, status) -
  183.         cal->get(Calendar::DST_OFFSET, status);
  184.     releaseCalendar(cal);
  185.     if (U_FAILURE(status))
  186.     {
  187.         errln("FAIL: Calendar::get failed");
  188.         return 0.0;
  189.     }
  190.     return dt;
  191. }*/
  192.  
  193. // Mimics Date.getYear() etc.
  194. void
  195. CalendarTimeZoneTest::dateToFields(UDate date, int32_t& y, int32_t& m, int32_t& d, int32_t& hr, int32_t& min, int32_t& sec)
  196. {
  197.     Calendar* cal = getCalendar();
  198.     if (cal == 0) return;
  199.     UErrorCode status = U_ZERO_ERROR;
  200.     cal->setTime(date, status);
  201.     y = cal->get(Calendar::YEAR, status) - 1900;
  202.     m = cal->get(Calendar::MONTH, status);
  203.     d = cal->get(Calendar::DATE, status);
  204.     hr = cal->get(Calendar::HOUR_OF_DAY, status);
  205.     min = cal->get(Calendar::MINUTE, status);
  206.     sec = cal->get(Calendar::SECOND, status);
  207.     releaseCalendar(cal);
  208. }
  209.  
  210. //eof
  211.