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