TestAppDateTimeFormat.cpp (13148B)
1 #include "gtest/gtest.h" 2 #include "mozilla/gtest/MozAssertions.h" 3 #include "mozilla/intl/AppDateTimeFormat.h" 4 #include "mozilla/intl/DateTimeFormat.h" 5 6 namespace mozilla::intl { 7 using Style = DateTimeFormat::Style; 8 using StyleBag = DateTimeFormat::StyleBag; 9 using ComponentsBag = DateTimeFormat::ComponentsBag; 10 11 static DateTimeFormat::StyleBag ToStyleBag(Maybe<DateTimeFormat::Style> date, 12 Maybe<DateTimeFormat::Style> time) { 13 DateTimeFormat::StyleBag style; 14 style.date = date; 15 style.time = time; 16 return style; 17 } 18 19 TEST(AppDateTimeFormat, FormatPRExplodedTime) 20 { 21 PRTime prTime = 0; 22 PRExplodedTime prExplodedTime; 23 PR_ExplodeTime(prTime, PR_GMTParameters, &prExplodedTime); 24 25 AppDateTimeFormat::sLocale = new nsCString("en-US"); 26 AppDateTimeFormat::DeleteCache(); 27 StyleBag style = ToStyleBag(Some(Style::Long), Some(Style::Long)); 28 29 nsAutoString formattedTime; 30 nsresult rv = 31 AppDateTimeFormat::Format(style, &prExplodedTime, formattedTime); 32 ASSERT_NS_SUCCEEDED(rv); 33 ASSERT_TRUE(formattedTime.Find(u"January") != kNotFound); 34 ASSERT_TRUE(formattedTime.Find(u"1970") != kNotFound); 35 ASSERT_TRUE(formattedTime.Find(u"12:00:00 AM") != kNotFound || 36 formattedTime.Find(u"12:00:00\u202FAM") != kNotFound || 37 formattedTime.Find(u"00:00:00") != kNotFound); 38 39 prExplodedTime = {0, 0, 19, 0, 1, 0, 1970, 4, 0, {(19 * 60), 0}}; 40 41 rv = AppDateTimeFormat::Format(style, &prExplodedTime, formattedTime); 42 43 ASSERT_NS_SUCCEEDED(rv); 44 ASSERT_TRUE(formattedTime.Find(u"January") != kNotFound); 45 ASSERT_TRUE(formattedTime.Find(u"1970") != kNotFound); 46 ASSERT_TRUE(formattedTime.Find(u"12:19:00 AM") != kNotFound || 47 formattedTime.Find(u"12:19:00\u202FAM") != kNotFound || 48 formattedTime.Find(u"00:19:00") != kNotFound); 49 50 prExplodedTime = {0, 0, 0, 7, 1, 51 0, 1970, 4, 0, {(6 * 60 * 60), (1 * 60 * 60)}}; 52 rv = AppDateTimeFormat::Format(style, &prExplodedTime, formattedTime); 53 ASSERT_NS_SUCCEEDED(rv); 54 ASSERT_TRUE(formattedTime.Find(u"January") != kNotFound); 55 ASSERT_TRUE(formattedTime.Find(u"1970") != kNotFound); 56 ASSERT_TRUE(formattedTime.Find(u"7:00:00 AM") != kNotFound || 57 formattedTime.Find(u"7:00:00\u202FAM") != kNotFound || 58 formattedTime.Find(u"07:00:00") != kNotFound); 59 60 prExplodedTime = { 61 0, 0, 29, 11, 1, 62 0, 1970, 4, 0, {(10 * 60 * 60) + (29 * 60), (1 * 60 * 60)}}; 63 rv = AppDateTimeFormat::Format(style, &prExplodedTime, formattedTime); 64 ASSERT_NS_SUCCEEDED(rv); 65 ASSERT_TRUE(formattedTime.Find(u"January") != kNotFound); 66 ASSERT_TRUE(formattedTime.Find(u"1970") != kNotFound); 67 ASSERT_TRUE(formattedTime.Find(u"11:29:00 AM") != kNotFound || 68 formattedTime.Find(u"11:29:00\u202FAM") != kNotFound || 69 formattedTime.Find(u"11:29:00") != kNotFound); 70 71 prExplodedTime = {0, 0, 37, 23, 31, 11, 1969, 3, 364, {-(23 * 60), 0}}; 72 rv = AppDateTimeFormat::Format(style, &prExplodedTime, formattedTime); 73 ASSERT_NS_SUCCEEDED(rv); 74 ASSERT_TRUE(formattedTime.Find(u"December") != kNotFound); 75 ASSERT_TRUE(formattedTime.Find(u"31") != kNotFound); 76 ASSERT_TRUE(formattedTime.Find(u"1969") != kNotFound); 77 ASSERT_TRUE(formattedTime.Find(u"11:37:00 PM") != kNotFound || 78 formattedTime.Find(u"11:37:00\u202FPM") != kNotFound || 79 formattedTime.Find(u"23:37:00") != kNotFound); 80 81 prExplodedTime = {0, 0, 0, 17, 31, 11, 1969, 3, 364, {-(7 * 60 * 60), 0}}; 82 rv = AppDateTimeFormat::Format(style, &prExplodedTime, formattedTime); 83 ASSERT_NS_SUCCEEDED(rv); 84 ASSERT_TRUE(formattedTime.Find(u"December") != kNotFound); 85 ASSERT_TRUE(formattedTime.Find(u"31") != kNotFound); 86 ASSERT_TRUE(formattedTime.Find(u"1969") != kNotFound); 87 ASSERT_TRUE(formattedTime.Find(u"5:00:00 PM") != kNotFound || 88 formattedTime.Find(u"5:00:00\u202FPM") != kNotFound || 89 formattedTime.Find(u"17:00:00") != kNotFound); 90 91 prExplodedTime = { 92 0, 0, 47, 14, 31, 93 11, 1969, 3, 364, {-((10 * 60 * 60) + (13 * 60)), (1 * 60 * 60)}}; 94 rv = AppDateTimeFormat::Format(style, &prExplodedTime, formattedTime); 95 ASSERT_NS_SUCCEEDED(rv); 96 ASSERT_TRUE(formattedTime.Find(u"December") != kNotFound); 97 ASSERT_TRUE(formattedTime.Find(u"31") != kNotFound); 98 ASSERT_TRUE(formattedTime.Find(u"1969") != kNotFound); 99 ASSERT_TRUE(formattedTime.Find(u"2:47:00 PM") != kNotFound || 100 formattedTime.Find(u"2:47:00\u202FPM") != kNotFound || 101 formattedTime.Find(u"14:47:00") != kNotFound); 102 103 ComponentsBag components{}; 104 components.weekday = mozilla::Some(DateTimeFormat::Text::Short); 105 components.timeZoneName = mozilla::Some(DateTimeFormat::TimeZoneName::Short); 106 // From above: Wed, 31 Dec 1969 14:47:00 -09:13 107 rv = AppDateTimeFormat::Format(components, &prExplodedTime, formattedTime); 108 ASSERT_NS_SUCCEEDED(rv); 109 ASSERT_TRUE(formattedTime.Find(u"Wed") != kNotFound); 110 ASSERT_TRUE(formattedTime.Find(u"-09:13") != kNotFound || 111 formattedTime.Find(u"-9:13") != kNotFound); 112 } 113 114 TEST(AppDateTimeFormat, DateFormatSelectors) 115 { 116 PRTime prTime = 0; 117 PRExplodedTime prExplodedTime; 118 PR_ExplodeTime(prTime, PR_GMTParameters, &prExplodedTime); 119 120 AppDateTimeFormat::sLocale = new nsCString("en-US"); 121 AppDateTimeFormat::DeleteCache(); 122 123 nsAutoString formattedTime; 124 125 { 126 ComponentsBag components{}; 127 components.year = Some(DateTimeFormat::Numeric::Numeric); 128 components.month = Some(DateTimeFormat::Month::TwoDigit); 129 130 nsresult rv = 131 AppDateTimeFormat::Format(components, &prExplodedTime, formattedTime); 132 ASSERT_NS_SUCCEEDED(rv); 133 ASSERT_STREQ("01/1970", NS_ConvertUTF16toUTF8(formattedTime).get()); 134 } 135 { 136 ComponentsBag components{}; 137 components.year = Some(DateTimeFormat::Numeric::Numeric); 138 components.month = Some(DateTimeFormat::Month::Long); 139 140 nsresult rv = 141 AppDateTimeFormat::Format(components, &prExplodedTime, formattedTime); 142 ASSERT_NS_SUCCEEDED(rv); 143 ASSERT_STREQ("January 1970", NS_ConvertUTF16toUTF8(formattedTime).get()); 144 } 145 { 146 ComponentsBag components{}; 147 components.month = Some(DateTimeFormat::Month::Long); 148 149 nsresult rv = 150 AppDateTimeFormat::Format(components, &prExplodedTime, formattedTime); 151 ASSERT_NS_SUCCEEDED(rv); 152 ASSERT_STREQ("January", NS_ConvertUTF16toUTF8(formattedTime).get()); 153 } 154 { 155 ComponentsBag components{}; 156 components.weekday = Some(DateTimeFormat::Text::Short); 157 158 nsresult rv = 159 AppDateTimeFormat::Format(components, &prExplodedTime, formattedTime); 160 ASSERT_NS_SUCCEEDED(rv); 161 ASSERT_STREQ("Thu", NS_ConvertUTF16toUTF8(formattedTime).get()); 162 } 163 } 164 165 TEST(AppDateTimeFormat, FormatPRExplodedTimeForeign) 166 { 167 PRTime prTime = 0; 168 PRExplodedTime prExplodedTime; 169 PR_ExplodeTime(prTime, PR_GMTParameters, &prExplodedTime); 170 171 AppDateTimeFormat::sLocale = new nsCString("de-DE"); 172 AppDateTimeFormat::DeleteCache(); 173 StyleBag style = ToStyleBag(Some(Style::Long), Some(Style::Long)); 174 175 nsAutoString formattedTime; 176 nsresult rv = 177 AppDateTimeFormat::Format(style, &prExplodedTime, formattedTime); 178 ASSERT_NS_SUCCEEDED(rv); 179 ASSERT_TRUE(formattedTime.Find(u"1.") != kNotFound); 180 ASSERT_TRUE(formattedTime.Find(u"Januar") != kNotFound); 181 ASSERT_TRUE(formattedTime.Find(u"1970") != kNotFound); 182 ASSERT_TRUE(formattedTime.Find(u"12:00:00 AM") != kNotFound || 183 formattedTime.Find(u"12:00:00\u202FAM") != kNotFound || 184 formattedTime.Find(u"00:00:00") != kNotFound); 185 186 prExplodedTime = {0, 0, 19, 0, 1, 0, 1970, 4, 0, {(19 * 60), 0}}; 187 rv = AppDateTimeFormat::Format(style, &prExplodedTime, formattedTime); 188 ASSERT_NS_SUCCEEDED(rv); 189 ASSERT_TRUE(formattedTime.Find(u"1.") != kNotFound); 190 ASSERT_TRUE(formattedTime.Find(u"Januar") != kNotFound); 191 ASSERT_TRUE(formattedTime.Find(u"1970") != kNotFound); 192 ASSERT_TRUE(formattedTime.Find(u"12:19:00 AM") != kNotFound || 193 formattedTime.Find(u"12:19:00\u202FAM") != kNotFound || 194 formattedTime.Find(u"00:19:00") != kNotFound); 195 196 prExplodedTime = {0, 0, 0, 7, 1, 197 0, 1970, 4, 0, {(6 * 60 * 60), (1 * 60 * 60)}}; 198 rv = AppDateTimeFormat::Format(style, &prExplodedTime, formattedTime); 199 ASSERT_NS_SUCCEEDED(rv); 200 ASSERT_TRUE(formattedTime.Find(u"1.") != kNotFound); 201 ASSERT_TRUE(formattedTime.Find(u"Januar") != kNotFound); 202 ASSERT_TRUE(formattedTime.Find(u"1970") != kNotFound); 203 ASSERT_TRUE(formattedTime.Find(u"7:00:00 AM") != kNotFound || 204 formattedTime.Find(u"7:00:00\u202FAM") != kNotFound || 205 formattedTime.Find(u"07:00:00") != kNotFound); 206 207 prExplodedTime = { 208 0, 0, 29, 11, 1, 209 0, 1970, 4, 0, {(10 * 60 * 60) + (29 * 60), (1 * 60 * 60)}}; 210 rv = AppDateTimeFormat::Format(style, &prExplodedTime, formattedTime); 211 ASSERT_NS_SUCCEEDED(rv); 212 ASSERT_TRUE(formattedTime.Find(u"1.") != kNotFound); 213 ASSERT_TRUE(formattedTime.Find(u"Januar") != kNotFound); 214 ASSERT_TRUE(formattedTime.Find(u"1970") != kNotFound); 215 ASSERT_TRUE(formattedTime.Find(u"11:29:00 AM") != kNotFound || 216 formattedTime.Find(u"11:29:00\u202FAM") != kNotFound || 217 formattedTime.Find(u"11:29:00") != kNotFound); 218 219 prExplodedTime = {0, 0, 37, 23, 31, 11, 1969, 3, 364, {-(23 * 60), 0}}; 220 rv = AppDateTimeFormat::Format(style, &prExplodedTime, formattedTime); 221 ASSERT_NS_SUCCEEDED(rv); 222 ASSERT_TRUE(formattedTime.Find(u"31.") != kNotFound); 223 ASSERT_TRUE(formattedTime.Find(u"Dezember") != kNotFound); 224 ASSERT_TRUE(formattedTime.Find(u"1969") != kNotFound); 225 ASSERT_TRUE(formattedTime.Find(u"11:37:00 PM") != kNotFound || 226 formattedTime.Find(u"11:37:00\u202FPM") != kNotFound || 227 formattedTime.Find(u"23:37:00") != kNotFound); 228 229 prExplodedTime = {0, 0, 0, 17, 31, 11, 1969, 3, 364, {-(7 * 60 * 60), 0}}; 230 rv = AppDateTimeFormat::Format(style, &prExplodedTime, formattedTime); 231 ASSERT_NS_SUCCEEDED(rv); 232 ASSERT_TRUE(formattedTime.Find(u"31.") != kNotFound); 233 ASSERT_TRUE(formattedTime.Find(u"Dezember") != kNotFound); 234 ASSERT_TRUE(formattedTime.Find(u"1969") != kNotFound); 235 ASSERT_TRUE(formattedTime.Find(u"5:00:00 PM") != kNotFound || 236 formattedTime.Find(u"5:00:00\u202FPM") != kNotFound || 237 formattedTime.Find(u"17:00:00") != kNotFound); 238 239 prExplodedTime = { 240 0, 0, 47, 14, 31, 241 11, 1969, 3, 364, {-((10 * 60 * 60) + (13 * 60)), (1 * 60 * 60)}}; 242 rv = AppDateTimeFormat::Format(style, &prExplodedTime, formattedTime); 243 ASSERT_NS_SUCCEEDED(rv); 244 ASSERT_TRUE(formattedTime.Find(u"31.") != kNotFound); 245 ASSERT_TRUE(formattedTime.Find(u"Dezember") != kNotFound); 246 ASSERT_TRUE(formattedTime.Find(u"1969") != kNotFound); 247 ASSERT_TRUE(formattedTime.Find(u"2:47:00 PM") != kNotFound || 248 formattedTime.Find(u"2:47:00\u202FPM") != kNotFound || 249 formattedTime.Find(u"14:47:00") != kNotFound); 250 } 251 252 TEST(AppDateTimeFormat, DateFormatSelectorsForeign) 253 { 254 PRTime prTime = 0; 255 PRExplodedTime prExplodedTime; 256 PR_ExplodeTime(prTime, PR_GMTParameters, &prExplodedTime); 257 258 AppDateTimeFormat::sLocale = new nsCString("de-DE"); 259 AppDateTimeFormat::DeleteCache(); 260 261 nsAutoString formattedTime; 262 { 263 ComponentsBag components{}; 264 components.year = Some(DateTimeFormat::Numeric::Numeric); 265 components.month = Some(DateTimeFormat::Month::TwoDigit); 266 267 nsresult rv = 268 AppDateTimeFormat::Format(components, &prExplodedTime, formattedTime); 269 ASSERT_NS_SUCCEEDED(rv); 270 ASSERT_STREQ("01/1970", NS_ConvertUTF16toUTF8(formattedTime).get()); 271 } 272 { 273 ComponentsBag components{}; 274 components.year = Some(DateTimeFormat::Numeric::Numeric); 275 components.month = Some(DateTimeFormat::Month::Long); 276 277 nsresult rv = 278 AppDateTimeFormat::Format(components, &prExplodedTime, formattedTime); 279 ASSERT_NS_SUCCEEDED(rv); 280 ASSERT_STREQ("Januar 1970", NS_ConvertUTF16toUTF8(formattedTime).get()); 281 } 282 { 283 ComponentsBag components{}; 284 components.weekday = Some(DateTimeFormat::Text::Short); 285 286 nsresult rv = 287 AppDateTimeFormat::Format(components, &prExplodedTime, formattedTime); 288 ASSERT_NS_SUCCEEDED(rv); 289 ASSERT_STREQ("Do", NS_ConvertUTF16toUTF8(formattedTime).get()); 290 } 291 { 292 ComponentsBag components{}; 293 components.weekday = Some(DateTimeFormat::Text::Long); 294 295 nsresult rv = 296 AppDateTimeFormat::Format(components, &prExplodedTime, formattedTime); 297 ASSERT_NS_SUCCEEDED(rv); 298 ASSERT_STREQ("Donnerstag", NS_ConvertUTF16toUTF8(formattedTime).get()); 299 } 300 { 301 ComponentsBag components{}; 302 components.month = Some(DateTimeFormat::Month::Long); 303 304 nsresult rv = 305 AppDateTimeFormat::Format(components, &prExplodedTime, formattedTime); 306 ASSERT_NS_SUCCEEDED(rv); 307 ASSERT_STREQ("Januar", NS_ConvertUTF16toUTF8(formattedTime).get()); 308 } 309 { 310 ComponentsBag components{}; 311 components.weekday = Some(DateTimeFormat::Text::Short); 312 313 nsresult rv = 314 AppDateTimeFormat::Format(components, &prExplodedTime, formattedTime); 315 ASSERT_NS_SUCCEEDED(rv); 316 ASSERT_STREQ("Do", NS_ConvertUTF16toUTF8(formattedTime).get()); 317 } 318 } 319 320 } // namespace mozilla::intl