Debian Codegraph @

misc/icu4c-tools/icu4c_date_format.cpp

raw
1 #include <iostream>
2 #include <sstream>
3 #include <unicode/smpdtfmt.h>
4 #include <unicode/timezone.h>
5
6 static std::vector<std::string> split(const std::string &s, char delimiter) {
7 std::vector<std::string> tokens;
8 std::string token;
9 std::istringstream tokenStream(s);
10 while (std::getline(tokenStream, token, delimiter)) {
11 tokens.push_back(token);
12 }
13 return tokens;
14 }
15
16 static UDate parse_time_str(const char *time_str) {
17 UErrorCode status = U_ZERO_ERROR;
18 icu::UnicodeString fauxISO8601("yyyy-MM-dd'T'hh:mm:ss'Z'");
19 auto fmt = new icu::SimpleDateFormat(fauxISO8601, status);
20 fmt->setTimeZone(*icu::TimeZone::getGMT());
21 UDate date = fmt->parse(icu::UnicodeString(time_str), status);
22 if (U_FAILURE(status)) {
23 std::cerr << "Failed to parse time string: " << time_str << std::endl;
24 exit(1);
25 }
26 return date;
27 }
28
29 static std::vector<icu::Locale> parse_locales(const char *locales_str) {
30 auto locales = std::vector<icu::Locale>{};
31 for (auto token : split(locales_str, ',')) {
32 auto loc = icu::Locale(token.c_str());
33 if (loc.isBogus()) {
34 std::cerr << "Invalid locale: " << token << std::endl;
35 exit(1);
36 }
37 locales.push_back(loc);
38 }
39 return locales;
40 }
41
42 static std::vector<icu::TimeZone *> parse_timezones(const char *timezones_str) {
43 auto timezones = std::vector<icu::TimeZone *>{};
44 for (auto token : split(timezones_str, ',')) {
45 auto tz = icu::TimeZone::createTimeZone(token.c_str());
46 if (tz == nullptr) {
47 std::cerr << "Invalid timezone: " << token << std::endl;
48 exit(1);
49 }
50 timezones.push_back(tz);
51 }
52 return timezones;
53 }
54
55 int main() {
56 UErrorCode status = U_ZERO_ERROR;
57 const char *timezones_str = getenv("TEST_TIMEZONES");
58 const char *locales_str = getenv("TEST_LOCALES");
59 const char *time_str = getenv("TEST_TIME");
60 const char *time_format_str = getenv("TEST_TIME_FORMAT");
61
62 if (!timezones_str || !locales_str) {
63 std::cerr << "Please set TEST_TIMEZONES, TEST_LOCALES environment variables"
64 << std::endl;
65 return 1;
66 }
67
68 if (time_str == nullptr) {
69 time_str = "2025-03-04T13:53:00Z";
70 std::cerr << "Defaulting TEST_TIME to " << time_str << std::endl;
71 }
72
73 if (time_format_str == nullptr) {
74 time_format_str = "z:zz:zzz:zzzz";
75 std::cerr << "Defaulting TEST_TIME_FORMAT to " << time_format_str
76 << std::endl;
77 }
78
79 auto date = parse_time_str(time_str);
80 auto timezones = parse_timezones(timezones_str);
81 auto locales = parse_locales(locales_str);
82
83 for (auto tz : timezones) {
84 icu::UnicodeString tzid;
85 tz->getID(tzid);
86 std::string tzid_str;
87 tzid.toUTF8String(tzid_str);
88 for (auto loc : locales) {
89 auto fmt = new icu::SimpleDateFormat(time_format_str, loc, status);
90 fmt->setTimeZone(*tz);
91 icu::UnicodeString name;
92 fmt->format(date, name);
93 std::string result;
94 name.toUTF8String(result);
95 std::cout << tzid_str << "\t" << loc.getName() << "\t" << result
96 << std::endl;
97 delete fmt;
98 }
99 }
100 return 0;
101 }