1 /************************************************************************* 2 * 2016 and later: Unicode, Inc. and others. 3 * License & terms of use: http://www.unicode.org/copyright.html#License 4 ************************************************************************* 5 ************************************************************************* 6 * COPYRIGHT: 7 * Copyright (c) 1999-2003, International Business Machines Corporation and 8 * others. All Rights Reserved. 9 *************************************************************************/ 10 11 #include "unicode/unistr.h" 12 #include "unicode/calendar.h" 13 #include "unicode/datefmt.h" 14 #include <stdio.h> 15 #include <stdlib.h> 16 #include "util.h" 17 18 using namespace icu; 19 20 /** 21 * If the ID supplied to TimeZone is not a valid system ID, 22 * TimeZone::createTimeZone() will return a GMT zone object. In order 23 * to detect this error, we check the ID of the returned zone against 24 * the ID we requested. If they don't match, we fail with an error. 25 */ 26 TimeZone* createZone(const UnicodeString& id) { 27 UnicodeString str; 28 TimeZone* zone = TimeZone::createTimeZone(id); 29 if (zone->getID(str) != id) { 30 delete zone; 31 printf("Error: TimeZone::createTimeZone("); 32 uprintf(id); 33 printf(") returned zone with ID "); 34 uprintf(str); 35 printf("\n"); 36 exit(1); 37 } 38 return zone; 39 } 40 41 int main(int argc, char **argv) { 42 43 Calendar *cal; 44 TimeZone *zone; 45 DateFormat *fmt; 46 UErrorCode status = U_ZERO_ERROR; 47 UnicodeString str; 48 UDate date; 49 50 // The languages in which we will display the date 51 static char* LANGUAGE[] = { 52 "en", "de", "fr" 53 }; 54 static const int32_t N_LANGUAGE = sizeof(LANGUAGE)/sizeof(LANGUAGE[0]); 55 56 // The time zones in which we will display the time 57 static char* TIMEZONE[] = { 58 "America/Los_Angeles", 59 "America/New_York", 60 "Europe/Paris", 61 "Europe/Berlin" 62 }; 63 static const int32_t N_TIMEZONE = sizeof(TIMEZONE)/sizeof(TIMEZONE[0]); 64 65 // Create a calendar 66 cal = Calendar::createInstance(status); 67 check(status, "Calendar::createInstance"); 68 zone = createZone("GMT"); // Create a GMT zone 69 cal->adoptTimeZone(zone); 70 cal->clear(); 71 cal->set(1999, Calendar::JUNE, 4); 72 date = cal->getTime(status); 73 check(status, "Calendar::getTime"); 74 75 for (int32_t i=0; i<N_LANGUAGE; ++i) { 76 Locale loc(LANGUAGE[i]); 77 78 // Create a formatter for DATE and TIME 79 fmt = DateFormat::createDateTimeInstance( 80 DateFormat::kFull, DateFormat::kFull, loc); 81 82 for (int32_t j=0; j<N_TIMEZONE; ++j) { 83 84 cal->adoptTimeZone(createZone(TIMEZONE[j])); 85 fmt->setCalendar(*cal); 86 87 // Format the date 88 str.remove(); 89 fmt->format(date, str, status); 90 91 // Display the formatted date string 92 printf("Date (%s, %s): ", LANGUAGE[i], TIMEZONE[j]); 93 uprintf(escape(str)); 94 printf("\n\n"); 95 } 96 97 delete fmt; 98 } 99 100 printf("Exiting successfully\n"); 101 return 0; 102 } 103