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