11//
22// Copyright (c) 2009-2011 Artyom Beilis (Tonkikh)
3- // Copyright (c) 2021-2022 Alexander Grund
3+ // Copyright (c) 2021-2024 Alexander Grund
44//
55// Distributed under the Boost Software License, Version 1.0.
66// https://www.boost.org/LICENSE_1_0.txt
2626const std::string test_locale_name = " en_US" ;
2727std::string message_path = " ./" ;
2828
29- #ifdef BOOST_LOCALE_WITH_ICU
29+ #ifndef BOOST_LOCALE_WITH_ICU
30+ # define BOOST_LOCALE_ICU_VERSION 0
31+ # define BOOST_LOCALE_ICU_VERSION_EXACT 0
32+ #else
3033# include < unicode/datefmt.h>
3134# include < unicode/numfmt.h>
3235# include < unicode/timezone.h>
3336# include < unicode/uversion.h>
3437# define BOOST_LOCALE_ICU_VERSION (U_ICU_VERSION_MAJOR_NUM * 100 + U_ICU_VERSION_MINOR_NUM)
3538# define BOOST_LOCALE_ICU_VERSION_EXACT (BOOST_LOCALE_ICU_VERSION * 100 + U_ICU_VERSION_PATCHLEVEL_NUM)
39+ #endif
3640
37- const icu::Locale& get_icu_test_locale ()
41+ using format_style_t = std::ios_base&(std::ios_base&);
42+
43+ namespace {
44+ #ifndef BOOST_LOCALE_WITH_ICU
45+ const std::string icu_full_gmt_name;
46+ // clang-format off
47+ #if BOOST_LOCALE_ICU_VERSION >= 402
48+ std::string get_ICU_currency_iso (...){ return " " ; } // LCOV_EXCL_LINE
49+ #endif
50+ std::string get_ICU_date (...){ return " " ; } // LCOV_EXCL_LINE
51+ std::string get_ICU_datetime (...){ return " " ; } // LCOV_EXCL_LINE
52+ std::string get_ICU_time (...){ return " " ; } // LCOV_EXCL_LINE
53+ // clang-format on
54+ #else
55+ const icu::Locale& get_ICU_test_locale ()
3856{
3957 static icu::Locale locale = icu::Locale::createCanonical (test_locale_name.c_str ());
4058 return locale;
4159}
4260
43- std::string from_icu_string (const icu::UnicodeString& str)
61+ std::string from_ICU_string (const icu::UnicodeString& str)
4462{
4563 return boost::locale::conv::utf_to_utf<char >(str.getBuffer (), str.getBuffer () + str.length ());
4664}
47- #else
48- # define BOOST_LOCALE_ICU_VERSION 0
49- # define BOOST_LOCALE_ICU_VERSION_EXACT 0
50- #endif
5165
5266// Currency style changes between ICU versions, so get "real" value from ICU
53- #if BOOST_LOCALE_ICU_VERSION >= 402
67+ # if BOOST_LOCALE_ICU_VERSION >= 402
5468
55- std::string get_icu_currency_iso (const double value)
69+ std::string get_ICU_currency_iso (const double value)
5670{
57- # if BOOST_LOCALE_ICU_VERSION >= 408
71+ # if BOOST_LOCALE_ICU_VERSION >= 408
5872 auto styleIso = UNUM_CURRENCY_ISO;
59- # else
73+ # else
6074 auto styleIso = icu::NumberFormat::kIsoCurrencyStyle ;
61- # endif
75+ # endif
6276 UErrorCode err = U_ZERO_ERROR;
63- std::unique_ptr<icu::NumberFormat> fmt (icu::NumberFormat::createInstance (get_icu_test_locale (), styleIso, err));
77+ std::unique_ptr<icu::NumberFormat> fmt (icu::NumberFormat::createInstance (get_ICU_test_locale (), styleIso, err));
6478 TEST_REQUIRE (U_SUCCESS (err) && fmt.get ());
6579
6680 icu::UnicodeString tmp;
67- return from_icu_string (fmt->format (value, tmp));
81+ return from_ICU_string (fmt->format (value, tmp));
6882}
6983
70- #endif
71-
72- using format_style_t = std::ios_base&(std::ios_base&);
73-
74- #ifdef BOOST_LOCALE_WITH_ICU
75- std::string get_icu_gmt_name (icu::TimeZone::EDisplayType style)
84+ # endif
85+ std::string get_ICU_gmt_name (icu::TimeZone::EDisplayType style)
7686{
7787 icu::UnicodeString tmp;
78- return from_icu_string (icu::TimeZone::getGMT ()->getDisplayName (false , style, get_icu_test_locale (), tmp));
88+ return from_ICU_string (icu::TimeZone::getGMT ()->getDisplayName (false , style, get_ICU_test_locale (), tmp));
7989}
8090
8191// This changes between ICU versions, e.g. "GMT" or "Greenwich Mean Time"
82- const std::string icu_full_gmt_name = get_icu_gmt_name (icu::TimeZone::EDisplayType::LONG);
92+ const std::string icu_full_gmt_name = get_ICU_gmt_name (icu::TimeZone::EDisplayType::LONG);
8393
84- std::string get_ICU_time (format_style_t style, const time_t ts, const char * tz = nullptr )
94+ std::string get_ICU_date (format_style_t style, const time_t ts)
8595{
8696 using icu::DateFormat;
8797 DateFormat::EStyle icu_style = DateFormat::kDefault ;
8898 namespace as = boost::locale::as;
89- if (style == as::time_short )
99+ if (style == as::date_short )
90100 icu_style = DateFormat::kShort ;
91- else if (style == as::time_medium )
101+ else if (style == as::date_medium )
92102 icu_style = DateFormat::kMedium ;
93- else if (style == as::time_long )
103+ else if (style == as::date_long )
94104 icu_style = DateFormat::kLong ;
95- else if (style == as::time_full )
105+ else if (style == as::date_full )
96106 icu_style = DateFormat::kFull ;
97- std::unique_ptr<icu::DateFormat> fmt (icu::DateFormat::createTimeInstance (icu_style, get_icu_test_locale ()));
98- if (!tz)
99- fmt->setTimeZone (*icu::TimeZone::getGMT ());
100- else
101- fmt->adoptTimeZone (icu::TimeZone::createTimeZone (icu::UnicodeString::fromUTF8 (tz)));
107+ std::unique_ptr<icu::DateFormat> fmt (icu::DateFormat::createDateInstance (icu_style, get_ICU_test_locale ()));
108+ fmt->setTimeZone (*icu::TimeZone::getGMT ());
102109 icu::UnicodeString s;
103- return from_icu_string (fmt->format (ts * 1000 ., s));
110+ return from_ICU_string (fmt->format (ts * 1000 ., s));
104111}
105112
106- std::string get_ICU_date (format_style_t style, const time_t ts)
113+ std::string get_ICU_datetime (format_style_t style, const time_t ts)
107114{
108115 using icu::DateFormat;
109116 DateFormat::EStyle icu_style = DateFormat::kDefault ;
110117 namespace as = boost::locale::as;
111- if (style == as::date_short )
118+ if (style == as::time_short )
112119 icu_style = DateFormat::kShort ;
113- else if (style == as::date_medium )
120+ else if (style == as::time_medium )
114121 icu_style = DateFormat::kMedium ;
115- else if (style == as::date_long )
122+ else if (style == as::time_long )
116123 icu_style = DateFormat::kLong ;
117- else if (style == as::date_full )
124+ else if (style == as::time_full )
118125 icu_style = DateFormat::kFull ;
119- std::unique_ptr<icu::DateFormat> fmt (icu::DateFormat::createDateInstance (icu_style, get_icu_test_locale ()));
126+ std::unique_ptr<icu::DateFormat> fmt (
127+ icu::DateFormat::createDateTimeInstance (icu_style, icu_style, get_ICU_test_locale ()));
120128 fmt->setTimeZone (*icu::TimeZone::getGMT ());
121129 icu::UnicodeString s;
122- return from_icu_string (fmt->format (ts * 1000 ., s));
130+ return from_ICU_string (fmt->format (ts * 1000 ., s));
123131}
124132
125- std::string get_ICU_datetime (format_style_t style, const time_t ts)
133+ std::string get_ICU_time (format_style_t style, const time_t ts, const char * tz = nullptr )
126134{
127135 using icu::DateFormat;
128136 DateFormat::EStyle icu_style = DateFormat::kDefault ;
@@ -135,21 +143,16 @@ std::string get_ICU_datetime(format_style_t style, const time_t ts)
135143 icu_style = DateFormat::kLong ;
136144 else if (style == as::time_full)
137145 icu_style = DateFormat::kFull ;
138- std::unique_ptr<icu::DateFormat> fmt (
139- icu::DateFormat::createDateTimeInstance (icu_style, icu_style, get_icu_test_locale ()));
140- fmt->setTimeZone (*icu::TimeZone::getGMT ());
146+ std::unique_ptr<icu::DateFormat> fmt (icu::DateFormat::createTimeInstance (icu_style, get_ICU_test_locale ()));
147+ if (!tz)
148+ fmt->setTimeZone (*icu::TimeZone::getGMT ());
149+ else
150+ fmt->adoptTimeZone (icu::TimeZone::createTimeZone (icu::UnicodeString::fromUTF8 (tz)));
141151 icu::UnicodeString s;
142- return from_icu_string (fmt->format (ts * 1000 ., s));
152+ return from_ICU_string (fmt->format (ts * 1000 ., s));
143153}
144-
145- #else
146- const std::string icu_full_gmt_name;
147- // clang-format off
148- std::string get_ICU_time (...){ return " " ; } // LCOV_EXCL_LINE
149- std::string get_ICU_datetime (...){ return " " ; } // LCOV_EXCL_LINE
150- std::string get_ICU_date (...){ return " " ; } // LCOV_EXCL_LINE
151- // clang-format on
152154#endif
155+ } // namespace
153156
154157using namespace boost ::locale;
155158
@@ -427,8 +430,8 @@ void test_manip(std::string e_charset = "UTF-8")
427430#if BOOST_LOCALE_ICU_VERSION >= 402
428431 TEST_FMT_PARSE_2 (as::currency, as::currency_national, 1345 , " $1,345.00" );
429432 TEST_FMT_PARSE_2 (as::currency, as::currency_national, 1345.34 , " $1,345.34" );
430- TEST_FMT_PARSE_2 (as::currency, as::currency_iso, 1345 , get_icu_currency_iso (1345 ));
431- TEST_FMT_PARSE_2 (as::currency, as::currency_iso, 1345.34 , get_icu_currency_iso (1345.34 ));
433+ TEST_FMT_PARSE_2 (as::currency, as::currency_iso, 1345 , get_ICU_currency_iso (1345 ));
434+ TEST_FMT_PARSE_2 (as::currency, as::currency_iso, 1345.34 , get_ICU_currency_iso (1345.34 ));
432435#endif
433436 TEST_FMT_PARSE_1 (as::spellout, 10 , " ten" );
434437#if 402 <= BOOST_LOCALE_ICU_VERSION && BOOST_LOCALE_ICU_VERSION < 408
@@ -800,7 +803,7 @@ void test_format_class(std::string charset = "UTF-8")
800803#if BOOST_LOCALE_ICU_VERSION >= 402
801804 TEST_FORMAT_CLS (" {1,cur=nat}" , 1234 , " $1,234.00" );
802805 TEST_FORMAT_CLS (" {1,cur=national}" , 1234 , " $1,234.00" );
803- TEST_FORMAT_CLS (" {1,cur=iso}" , 1234 , get_icu_currency_iso (1234 ));
806+ TEST_FORMAT_CLS (" {1,cur=iso}" , 1234 , get_ICU_currency_iso (1234 ));
804807#endif
805808 TEST_FORMAT_CLS (" {1,spell}" , 10 , " ten" );
806809 TEST_FORMAT_CLS (" {1,spellout}" , 10 , " ten" );
0 commit comments