Merge pull request #26220 from 78andyp/blurayfixes
[xbmc.git] / xbmc / test / TestDateTime.cpp
blobf586d79ed56b8753491643f3e8cf73ad849c8214
1 /*
2 * Copyright (C) 2015-2018 Team Kodi
3 * This file is part of Kodi - https://kodi.tv
5 * SPDX-License-Identifier: GPL-2.0-or-later
6 * See LICENSES/README.md for more information.
7 */
9 #include "LangInfo.h"
10 #include "XBDateTime.h"
11 #include "guilib/LocalizeStrings.h"
12 #include "interfaces/legacy/ModuleXbmc.h" //Needed to test getRegion()
14 #include <array>
15 #include <iostream>
17 #include <gtest/gtest.h>
19 class TestDateTime : public testing::Test
21 protected:
22 TestDateTime() = default;
23 ~TestDateTime() override = default;
26 TEST_F(TestDateTime, DateTimeOperators)
28 CDateTime dateTime1(1991, 5, 14, 12, 34, 56);
29 CDateTime dateTime2(1991, 5, 14, 12, 34, 57);
31 EXPECT_TRUE(dateTime1 < dateTime2);
32 EXPECT_FALSE(dateTime1 > dateTime2);
33 EXPECT_FALSE(dateTime1 == dateTime2);
36 TEST_F(TestDateTime, FileTimeOperators)
38 CDateTime dateTime1(1991, 5, 14, 12, 34, 56);
39 CDateTime dateTime2(1991, 5, 14, 12, 34, 57);
41 KODI::TIME::FileTime fileTime1;
42 KODI::TIME::FileTime fileTime2;
44 dateTime1.GetAsTimeStamp(fileTime1);
45 dateTime2.GetAsTimeStamp(fileTime2);
47 CDateTime dateTime3(fileTime1);
49 EXPECT_TRUE(dateTime3 < fileTime2);
50 EXPECT_FALSE(dateTime3 > fileTime2);
51 EXPECT_FALSE(dateTime3 == fileTime2);
54 TEST_F(TestDateTime, SystemTimeOperators)
56 CDateTime dateTime1(1991, 5, 14, 12, 34, 56);
57 CDateTime dateTime2(1991, 5, 14, 12, 34, 57);
59 KODI::TIME::SystemTime systemTime;
60 dateTime2.GetAsSystemTime(systemTime);
62 EXPECT_TRUE(dateTime1 < systemTime);
63 EXPECT_FALSE(dateTime1 > systemTime);
64 EXPECT_FALSE(dateTime1 == systemTime);
67 TEST_F(TestDateTime, TimeTOperators)
69 CDateTime dateTime1(1991, 5, 14, 12, 34, 56);
70 CDateTime dateTime2(1991, 5, 14, 12, 34, 57);
72 time_t time;
73 dateTime2.GetAsTime(time);
75 EXPECT_TRUE(dateTime1 < time);
76 EXPECT_FALSE(dateTime1 > time);
77 EXPECT_FALSE(dateTime1 == time);
80 TEST_F(TestDateTime, TmOperators)
83 CDateTime dateTime1(1991, 5, 14, 12, 34, 56);
85 tm t1;
86 dateTime1.GetAsTm(t1);
88 EXPECT_FALSE(dateTime1 < t1);
89 EXPECT_FALSE(dateTime1 > t1);
90 EXPECT_TRUE(dateTime1 == t1);
92 CDateTime dateTime2(1991, 5, 14, 12, 34, 57);
94 tm t2;
95 dateTime2.GetAsTm(t2);
97 EXPECT_TRUE(dateTime1 < t2);
98 EXPECT_FALSE(dateTime1 > t2);
99 EXPECT_FALSE(dateTime1 == t2);
102 // same test but opposite daylight saving
104 CDateTime dateTime1(1991, 1, 14, 12, 34, 56);
106 tm t1;
107 dateTime1.GetAsTm(t1);
109 EXPECT_FALSE(dateTime1 < t1);
110 EXPECT_FALSE(dateTime1 > t1);
111 EXPECT_TRUE(dateTime1 == t1);
113 CDateTime dateTime2(1991, 1, 14, 12, 34, 57);
115 tm t2;
116 dateTime2.GetAsTm(t2);
118 EXPECT_TRUE(dateTime1 < t2);
119 EXPECT_FALSE(dateTime1 > t2);
120 EXPECT_FALSE(dateTime1 == t2);
124 // no way to test this platform agnostically (for now) so just log it.
125 TEST_F(TestDateTime, GetCurrentDateTime)
127 auto date = CDateTime::GetCurrentDateTime();
128 std::cout << "Current Date: " << date.GetAsDBDateTime() << std::endl;
131 // no way to test this platform agnostically (for now) so just log it.
132 TEST_F(TestDateTime, GetUTCDateTime)
134 auto date = CDateTime::GetUTCDateTime();
135 std::cout << "Current Date UTC: " << date.GetAsDBDateTime() << std::endl;
138 TEST_F(TestDateTime, MonthStringToMonthNum)
140 std::array<std::pair<std::string, std::string>, 12> months = {{
141 {"Jan", "January"},
142 {"Feb", "February"},
143 {"Mar", "March"},
144 {"Apr", "April"},
145 {"May", "May"},
146 {"Jun", "June"},
147 {"Jul", "July"},
148 {"Aug", "August"},
149 {"Sep", "September"},
150 {"Oct", "October"},
151 {"Nov", "November"},
152 {"Dec", "December"},
155 int i = 1;
156 for (const auto& month : months)
158 EXPECT_EQ(CDateTime::MonthStringToMonthNum(month.first), i);
159 EXPECT_EQ(CDateTime::MonthStringToMonthNum(month.second), i);
160 i++;
164 // this method is broken as SetFromDBDate() will return true
165 TEST_F(TestDateTime, DISABLED_SetFromDateString)
167 CDateTime dateTime;
168 EXPECT_TRUE(dateTime.SetFromDateString("tuesday may 14, 1991"));
170 std::cout << "year: " << dateTime.GetYear() << std::endl;
171 std::cout << "month: " << dateTime.GetMonth() << std::endl;
172 std::cout << "day: " << dateTime.GetDay() << std::endl;
174 EXPECT_EQ(dateTime.GetYear(), 1991);
175 EXPECT_EQ(dateTime.GetMonth(), 5);
176 EXPECT_EQ(dateTime.GetDay(), 14);
179 TEST_F(TestDateTime, SetFromDBDate)
181 CDateTime dateTime;
182 EXPECT_TRUE(dateTime.SetFromDBDate("1991-05-14"));
183 EXPECT_EQ(dateTime.GetYear(), 1991);
184 EXPECT_EQ(dateTime.GetMonth(), 5);
185 EXPECT_EQ(dateTime.GetDay(), 14);
187 dateTime.Reset();
188 EXPECT_TRUE(dateTime.SetFromDBDate("02-01-1993"));
189 EXPECT_EQ(dateTime.GetYear(), 1993);
190 EXPECT_EQ(dateTime.GetMonth(), 1);
191 EXPECT_EQ(dateTime.GetDay(), 2);
194 // disabled on osx and freebsd as their mktime functions
195 // don't work for dates before 1900
196 #if defined(TARGET_DARWIN_OSX) || defined(TARGET_FREEBSD)
197 TEST_F(TestDateTime, DISABLED_SetFromDBTime)
198 #else
199 TEST_F(TestDateTime, SetFromDBTime)
200 #endif
202 CDateTime dateTime1;
203 EXPECT_TRUE(dateTime1.SetFromDBTime("12:34"));
204 EXPECT_EQ(dateTime1.GetHour(), 12);
205 EXPECT_EQ(dateTime1.GetMinute(), 34);
206 EXPECT_EQ(dateTime1.GetSecond(), 0);
208 CDateTime dateTime2;
209 EXPECT_TRUE(dateTime2.SetFromDBTime("12:34:56"));
210 EXPECT_EQ(dateTime2.GetHour(), 12);
211 EXPECT_EQ(dateTime2.GetMinute(), 34);
212 EXPECT_EQ(dateTime2.GetSecond(), 56);
215 TEST_F(TestDateTime, SetFromDBDateTime)
217 CDateTime dateTime;
218 EXPECT_TRUE(dateTime.SetFromDBDateTime("1991-05-14 12:34:56"));
219 EXPECT_EQ(dateTime.GetYear(), 1991);
220 EXPECT_EQ(dateTime.GetMonth(), 5);
221 EXPECT_EQ(dateTime.GetDay(), 14);
222 EXPECT_EQ(dateTime.GetHour(), 12);
223 EXPECT_EQ(dateTime.GetMinute(), 34);
224 EXPECT_EQ(dateTime.GetSecond(), 56);
227 TEST_F(TestDateTime, SetFromW3CDate)
229 CDateTime dateTime;
230 EXPECT_TRUE(dateTime.SetFromW3CDate("1994-11-05T13:15:30Z"));
231 EXPECT_EQ(dateTime.GetYear(), 1994);
232 EXPECT_EQ(dateTime.GetMonth(), 11);
233 EXPECT_EQ(dateTime.GetDay(), 5);
234 EXPECT_EQ(dateTime.GetHour(), 0);
235 EXPECT_EQ(dateTime.GetMinute(), 0);
236 EXPECT_EQ(dateTime.GetSecond(), 0);
239 TEST_F(TestDateTime, SetFromW3CDateTime)
241 CDateTimeSpan bias = CDateTime::GetTimezoneBias();
242 CDateTime dateTime;
243 dateTime.SetFromDBDateTime("1994-11-05 13:15:30");
244 dateTime += bias;
245 std::string dateTimeStr = dateTime.GetAsDBDate() + "T" + dateTime.GetAsDBTime() + "Z";
247 CDateTime dateTime1;
248 EXPECT_TRUE(dateTime1.SetFromW3CDateTime(dateTimeStr));
249 EXPECT_EQ(dateTime1.GetYear(), 1994);
250 EXPECT_EQ(dateTime1.GetMonth(), 11);
251 EXPECT_EQ(dateTime1.GetDay(), 5);
252 EXPECT_EQ(dateTime1.GetHour(), 13);
253 EXPECT_EQ(dateTime1.GetMinute(), 15);
254 EXPECT_EQ(dateTime1.GetSecond(), 30);
256 CDateTime dateTime2;
257 EXPECT_TRUE(dateTime2.SetFromW3CDateTime("1994-11-05T08:15:30-05:00"));
258 EXPECT_EQ(dateTime2.GetYear(), 1994);
259 EXPECT_EQ(dateTime2.GetMonth(), 11);
260 EXPECT_EQ(dateTime2.GetDay(), 5);
261 EXPECT_EQ(dateTime2.GetHour(), 13);
262 EXPECT_EQ(dateTime2.GetMinute(), 15);
263 EXPECT_EQ(dateTime2.GetSecond(), 30);
266 TEST_F(TestDateTime, SetFromUTCDateTime)
268 CDateTimeSpan bias = CDateTime::GetTimezoneBias();
270 CDateTime dateTime1;
271 dateTime1.SetFromDBDateTime("1991-05-14 12:34:56");
272 dateTime1 += bias;
274 CDateTime dateTime2;
275 EXPECT_TRUE(dateTime2.SetFromUTCDateTime(dateTime1));
276 EXPECT_EQ(dateTime2.GetYear(), 1991);
277 EXPECT_EQ(dateTime2.GetMonth(), 5);
278 EXPECT_EQ(dateTime2.GetDay(), 14);
279 EXPECT_EQ(dateTime2.GetHour(), 12);
280 EXPECT_EQ(dateTime2.GetMinute(), 34);
281 EXPECT_EQ(dateTime2.GetSecond(), 56);
283 const time_t time = 674224496 + bias.GetSecondsTotal();
285 CDateTime dateTime3;
286 EXPECT_TRUE(dateTime3.SetFromUTCDateTime(time));
287 EXPECT_EQ(dateTime3.GetYear(), 1991);
288 EXPECT_EQ(dateTime3.GetMonth(), 5);
289 EXPECT_EQ(dateTime3.GetDay(), 14);
290 EXPECT_EQ(dateTime3.GetHour(), 12);
291 EXPECT_EQ(dateTime3.GetMinute(), 34);
292 EXPECT_EQ(dateTime3.GetSecond(), 56);
295 TEST_F(TestDateTime, SetFromRFC1123DateTime)
297 std::string dateTime1("Mon, 21 Oct 2018 12:16:24 GMT");
299 CDateTime dateTime2;
300 EXPECT_TRUE(dateTime2.SetFromRFC1123DateTime(dateTime1));
301 EXPECT_EQ(dateTime2.GetYear(), 2018);
302 EXPECT_EQ(dateTime2.GetMonth(), 10);
303 EXPECT_EQ(dateTime2.GetDay(), 21);
304 EXPECT_EQ(dateTime2.GetHour(), 12);
305 EXPECT_EQ(dateTime2.GetMinute(), 16);
306 EXPECT_EQ(dateTime2.GetSecond(), 24);
309 TEST_F(TestDateTime, SetDateTime)
311 CDateTime dateTime1;
312 EXPECT_TRUE(dateTime1.SetDateTime(1991, 05, 14, 12, 34, 56));
313 EXPECT_EQ(dateTime1.GetYear(), 1991);
314 EXPECT_EQ(dateTime1.GetMonth(), 5);
315 EXPECT_EQ(dateTime1.GetDay(), 14);
316 EXPECT_EQ(dateTime1.GetHour(), 12);
317 EXPECT_EQ(dateTime1.GetMinute(), 34);
318 EXPECT_EQ(dateTime1.GetSecond(), 56);
320 CDateTime dateTime2;
321 EXPECT_TRUE(dateTime2.SetDate(1991, 05, 14));
322 EXPECT_EQ(dateTime2.GetYear(), 1991);
323 EXPECT_EQ(dateTime2.GetMonth(), 5);
324 EXPECT_EQ(dateTime2.GetDay(), 14);
325 EXPECT_EQ(dateTime2.GetHour(), 0);
326 EXPECT_EQ(dateTime2.GetMinute(), 0);
327 EXPECT_EQ(dateTime2.GetSecond(), 0);
329 // disabled on osx and freebsd as their mktime functions
330 // don't work for dates before 1900
331 #if !defined(TARGET_DARWIN_OSX) && !defined(TARGET_FREEBSD)
332 CDateTime dateTime3;
333 EXPECT_TRUE(dateTime3.SetTime(12, 34, 56));
334 EXPECT_EQ(dateTime3.GetYear(), 1601);
335 EXPECT_EQ(dateTime3.GetMonth(), 1);
336 EXPECT_EQ(dateTime3.GetDay(), 1);
337 EXPECT_EQ(dateTime3.GetHour(), 12);
338 EXPECT_EQ(dateTime3.GetMinute(), 34);
339 EXPECT_EQ(dateTime3.GetSecond(), 56);
340 #endif
343 TEST_F(TestDateTime, GetAsStrings)
345 CDateTime dateTime;
346 dateTime.SetDateTime(1991, 05, 14, 12, 34, 56);
348 EXPECT_EQ(dateTime.GetAsSaveString(), "19910514_123456");
349 EXPECT_EQ(dateTime.GetAsDBDateTime(), "1991-05-14 12:34:56");
350 EXPECT_EQ(dateTime.GetAsDBDate(), "1991-05-14");
351 EXPECT_EQ(dateTime.GetAsDBTime(), "12:34:56");
352 EXPECT_EQ(dateTime.GetAsW3CDate(), "1991-05-14");
355 // disabled because we have no way to validate these values
356 // GetTimezoneBias() always returns a positive value so
357 // there is no way to detect the direction of the offset
358 TEST_F(TestDateTime, DISABLED_GetAsStringsWithBias)
360 CDateTimeSpan bias = CDateTime::GetTimezoneBias();
362 CDateTime dateTime;
363 dateTime.SetDateTime(1991, 05, 14, 12, 34, 56);
365 CDateTime dateTimeWithBias(dateTime);
366 dateTimeWithBias += bias;
368 EXPECT_EQ(dateTime.GetAsRFC1123DateTime(), "Tue, 14 May 1991 20:34:56 GMT");
369 EXPECT_EQ(dateTime.GetAsW3CDateTime(false), "1991-05-14T12:34:56+08:00");
370 EXPECT_EQ(dateTime.GetAsW3CDateTime(true), "1991-05-14T20:34:56Z");
373 TEST_F(TestDateTime, GetAsLocalized)
375 // short date formats using "/"
376 // "DD/MM/YYYY",
377 // "MM/DD/YYYY",
378 // "YYYY/MM/DD",
379 // "D/M/YYYY",
380 // short date formats using "-"
381 // "DD-MM-YYYY",
382 // "MM-DD-YYYY",
383 // "YYYY-MM-DD",
384 // "YYYY-M-D",
385 // short date formats using "."
386 // "DD.MM.YYYY",
387 // "DD.M.YYYY",
388 // "D.M.YYYY",
389 // "D. M. YYYY",
390 // "YYYY.MM.DD"
392 // "DDDD, D MMMM YYYY",
393 // "DDDD, DD MMMM YYYY",
394 // "DDDD, D. MMMM YYYY",
395 // "DDDD, DD. MMMM YYYY",
396 // "DDDD, MMMM D, YYYY",
397 // "DDDD, MMMM DD, YYYY",
398 // "DDDD D MMMM YYYY",
399 // "DDDD DD MMMM YYYY",
400 // "DDDD D. MMMM YYYY",
401 // "DDDD DD. MMMM YYYY",
402 // "D. MMMM YYYY",
403 // "DD. MMMM YYYY",
404 // "D. MMMM. YYYY",
405 // "DD. MMMM. YYYY",
406 // "YYYY. MMMM. D"
408 ASSERT_TRUE(g_localizeStrings.Load(g_langInfo.GetLanguagePath(), "resource.language.en_gb"));
410 // 24 hour clock must be set before time format
411 g_langInfo.Set24HourClock(false);
412 g_langInfo.SetTimeFormat("hh:mm:ss");
414 g_langInfo.SetShortDateFormat("MM/DD/YYYY");
415 g_langInfo.SetLongDateFormat("DDDD, DD MMMM YYYY");
417 CDateTime dateTime1;
418 dateTime1.SetDateTime(1991, 05, 14, 12, 34, 56);
420 // std::cout << "GetAsLocalizedDate: " << dateTime1.GetAsLocalizedDate(false) << std::endl;
421 // std::cout << "GetAsLocalizedDate: " << dateTime1.GetAsLocalizedDate(true) << std::endl;
422 // std::cout << "GetAsLocalizedDate: " << dateTime1.GetAsLocalizedDate(std::string("dd-mm-yyyy")) << std::endl;
423 // std::cout << "GetAsLocalizedTime: " << dateTime1.GetAsLocalizedTime("hh-mm-ss", true) << std::endl;
424 // std::cout << "GetAsLocalizedTime: " << dateTime1.GetAsLocalizedTime("hh-mm-ss", false)
425 // << std::endl;
426 // std::cout << "GetAsLocalizedDateTime: " << dateTime1.GetAsLocalizedDateTime(false, false)
427 // << std::endl;
428 // std::cout << "GetAsLocalizedDateTime: " << dateTime1.GetAsLocalizedDateTime(true, true)
429 // << std::endl;
430 // std::cout << "GetAsLocalizedTime: " << dateTime1.GetAsLocalizedTime(TIME_FORMAT(0), false)
431 // << std::endl;
432 // std::cout << "GetAsLocalizedTime: " << dateTime1.GetAsLocalizedTime(TIME_FORMAT(0), true)
433 // << std::endl;
435 // std::cout << "1: " << dateTime1.GetAsLocalizedTime(TIME_FORMAT(1)) << std::endl;
436 // std::cout << "2: " << dateTime1.GetAsLocalizedTime(TIME_FORMAT(2)) << std::endl;
437 // std::cout << "3: " << dateTime1.GetAsLocalizedTime(TIME_FORMAT(3)) << std::endl;
438 // std::cout << "4: " << dateTime1.GetAsLocalizedTime(TIME_FORMAT(4)) << std::endl;
439 // std::cout << "5: " << dateTime1.GetAsLocalizedTime(TIME_FORMAT(5)) << std::endl;
440 // std::cout << "6: " << dateTime1.GetAsLocalizedTime(TIME_FORMAT(6)) << std::endl;
441 // std::cout << "7: " << dateTime1.GetAsLocalizedTime(TIME_FORMAT(7)) << std::endl;
442 // std::cout << "8: " << dateTime1.GetAsLocalizedTime(TIME_FORMAT(8)) << std::endl;
443 // std::cout << "14: " << dateTime1.GetAsLocalizedTime(TIME_FORMAT(14)) << std::endl;
444 // std::cout << "15: " << dateTime1.GetAsLocalizedTime(TIME_FORMAT(15)) << std::endl;
445 // std::cout << "16: " << dateTime1.GetAsLocalizedTime(TIME_FORMAT(16)) << std::endl;
446 // std::cout << "19: " << dateTime1.GetAsLocalizedTime(TIME_FORMAT(19)) << std::endl;
447 // std::cout << "27: " << dateTime1.GetAsLocalizedTime(TIME_FORMAT(27)) << std::endl;
448 // std::cout << "32: " << dateTime1.GetAsLocalizedTime(TIME_FORMAT(32)) << std::endl;
449 // std::cout << "64: " << dateTime1.GetAsLocalizedTime(TIME_FORMAT(64)) << std::endl;
450 // std::cout << "128: " << dateTime1.GetAsLocalizedTime(TIME_FORMAT(128)) << std::endl;
451 // std::cout << "256: " << dateTime1.GetAsLocalizedTime(TIME_FORMAT(256)) << std::endl;
453 EXPECT_EQ(dateTime1.GetAsLocalizedDate(false), "05/14/1991");
454 EXPECT_EQ(dateTime1.GetAsLocalizedDate(true), "Tuesday, 14 May 1991");
455 EXPECT_EQ(dateTime1.GetAsLocalizedDate(std::string("dd-mm-yyyy")),
456 "14-05-1991"); // need to force overload function
457 EXPECT_EQ(dateTime1.GetAsLocalizedTime("hh-mm-ss", true), "12-34-56");
458 EXPECT_EQ(dateTime1.GetAsLocalizedTime("hh-mm-ss", false), "12-34");
459 EXPECT_EQ(dateTime1.GetAsLocalizedDateTime(false, false), "05/14/1991 12:34");
460 EXPECT_EQ(dateTime1.GetAsLocalizedDateTime(true, true), "Tuesday, 14 May 1991 12:34:56");
462 EXPECT_EQ(dateTime1.GetAsLocalizedTime(TIME_FORMAT(0), false), "12:34");
463 EXPECT_EQ(dateTime1.GetAsLocalizedTime(TIME_FORMAT(0), true), "12:34:56");
465 EXPECT_EQ(dateTime1.GetAsLocalizedTime(TIME_FORMAT(1)), "56");
466 EXPECT_EQ(dateTime1.GetAsLocalizedTime(TIME_FORMAT(2)), "34");
467 EXPECT_EQ(dateTime1.GetAsLocalizedTime(TIME_FORMAT(3)), "34:56");
468 EXPECT_EQ(dateTime1.GetAsLocalizedTime(TIME_FORMAT(4)), "12");
469 EXPECT_EQ(dateTime1.GetAsLocalizedTime(TIME_FORMAT(5)), "12:56");
470 EXPECT_EQ(dateTime1.GetAsLocalizedTime(TIME_FORMAT(6)), "12:34");
471 EXPECT_EQ(dateTime1.GetAsLocalizedTime(TIME_FORMAT(7)), "12:34:56");
472 EXPECT_EQ(dateTime1.GetAsLocalizedTime(TIME_FORMAT(8)), "PM");
473 EXPECT_EQ(dateTime1.GetAsLocalizedTime(TIME_FORMAT(14)), "12:34 PM");
474 EXPECT_EQ(dateTime1.GetAsLocalizedTime(TIME_FORMAT(15)), "12:34:56 PM");
475 EXPECT_EQ(dateTime1.GetAsLocalizedTime(TIME_FORMAT(16)), "12");
476 EXPECT_EQ(dateTime1.GetAsLocalizedTime(TIME_FORMAT(19)), "12:34:56");
477 EXPECT_EQ(dateTime1.GetAsLocalizedTime(TIME_FORMAT(27)), "12:34:56 PM");
479 //Test abbreviated month and short year formats
480 CDateTime dateTime3;
481 dateTime3.SetDateTime(1991, 05, 9, 12, 34, 56); //Need a single digit date
483 g_langInfo.SetShortDateFormat("DD-mmm-YY");
484 g_langInfo.SetLongDateFormat("ddd, D MMMM YYYY");
486 //Actual formatted date
487 //Test short month name and 2 digit year.
488 EXPECT_EQ(dateTime3.GetAsLocalizedDate(false), "09-May-91");
489 //Test short day name and single digit day number.
490 EXPECT_EQ(dateTime3.GetAsLocalizedDate(true), "Thu, 9 May 1991");
492 //Test that the Python date formatting string is returned instead
493 //of the actual formatted date string.
495 CDateTime dateTime4;
496 dateTime4.SetDateTime(1991, 05, 9, 12, 34, 56); //Need a single digit date
498 //Test non zero-padded day and short month name.
499 EXPECT_EQ(
500 dateTime4.GetAsLocalizedDate(std::string("D-mmm-YY"), CDateTime::ReturnFormat::CHOICE_YES),
501 "%-d-%b-%y");
502 //Test NZP day and NZP month.
503 EXPECT_EQ(
504 dateTime4.GetAsLocalizedDate(std::string("D-M-YY"), CDateTime::ReturnFormat::CHOICE_YES),
505 "%-d-%-m-%y");
507 g_langInfo.SetShortDateFormat("D/M/YY");
508 g_langInfo.SetLongDateFormat("ddd, D MMMM YYYY");
510 //Test getRegion() here because it is directly reliant on GetAsLocalizedDate()
511 //and the windows-specific formatting happens in getRegion().
512 #ifdef TARGET_WINDOWS
513 //Windows is handled differently because that's what ModuleXbmc.cpp does.
514 EXPECT_EQ(XBMCAddon::xbmc::getRegion("dateshort"), "%#d/%#m/%y");
515 EXPECT_EQ(XBMCAddon::xbmc::getRegion("datelong"), "%a, %#d %B %Y");
516 #else
517 EXPECT_EQ(XBMCAddon::xbmc::getRegion("dateshort"), "%-d/%-m/%y");
518 EXPECT_EQ(XBMCAddon::xbmc::getRegion("datelong"), "%a, %-d %B %Y");
519 #endif
521 //Test short day name, short month name and 2 digit year.
522 EXPECT_EQ(dateTime4.GetAsLocalizedDate(std::string("ddd, DD-mmm-YY"),
523 CDateTime::ReturnFormat::CHOICE_YES),
524 "%a, %d-%b-%y");
525 //Test as above but with 4 digit year.
526 EXPECT_EQ(dateTime4.GetAsLocalizedDate(std::string("ddd, DD-mmm-YYYY"),
527 CDateTime::ReturnFormat::CHOICE_YES),
528 "%a, %d-%b-%Y");
529 //Test some 'normal' DMY.
530 EXPECT_EQ(
531 dateTime4.GetAsLocalizedDate(std::string("DD/MM/YYYY"), CDateTime::ReturnFormat::CHOICE_YES),
532 "%d/%m/%Y");
533 EXPECT_EQ(
534 dateTime4.GetAsLocalizedDate(std::string("DD/MM/YY"), CDateTime::ReturnFormat::CHOICE_YES),
535 "%d/%m/%y");
537 // not possible to use these three
538 // EXPECT_EQ(dateTime1.GetAsLocalizedTime(TIME_FORMAT(32)), "");
539 // EXPECT_EQ(dateTime1.GetAsLocalizedTime(TIME_FORMAT(64)), "");
540 // EXPECT_EQ(dateTime1.GetAsLocalizedTime(TIME_FORMAT(128)), "");
542 EXPECT_EQ(dateTime1.GetAsLocalizedTime(TIME_FORMAT(256)), "34");
545 // 24 hour clock must be set before time format
546 g_langInfo.Set24HourClock(true);
547 g_langInfo.SetTimeFormat("h:m:s");
549 g_langInfo.SetShortDateFormat("YYYY-M-D");
550 g_langInfo.SetLongDateFormat("DDDD, MMMM D, YYYY");
552 CDateTime dateTime2;
553 dateTime2.SetDateTime(2020, 2, 3, 4, 5, 6);
555 // std::cout << "GetAsLocalizedDate: " << dateTime2.GetAsLocalizedDate(false) << std::endl;
556 // std::cout << "GetAsLocalizedDate: " << dateTime2.GetAsLocalizedDate(true) << std::endl;
557 // std::cout << "GetAsLocalizedDate: " << dateTime2.GetAsLocalizedDate(std::string("dd-mm-yyyy")) << std::endl;
558 // std::cout << "GetAsLocalizedTime: " << dateTime2.GetAsLocalizedTime("hh-mm-ss", true) << std::endl;
559 // std::cout << "GetAsLocalizedTime: " << dateTime2.GetAsLocalizedTime("hh-mm-ss", false)
560 // << std::endl;
561 // std::cout << "GetAsLocalizedDateTime: " << dateTime2.GetAsLocalizedDateTime(false, false)
562 // << std::endl;
563 // std::cout << "GetAsLocalizedDateTime: " << dateTime2.GetAsLocalizedDateTime(true, true)
564 // << std::endl;
565 // std::cout << "GetAsLocalizedTime: " << dateTime2.GetAsLocalizedTime(TIME_FORMAT(0), false)
566 // << std::endl;
567 // std::cout << "GetAsLocalizedTime: " << dateTime2.GetAsLocalizedTime(TIME_FORMAT(0), true)
568 // << std::endl;
570 // std::cout << "1: " << dateTime2.GetAsLocalizedTime(TIME_FORMAT(1)) << std::endl;
571 // std::cout << "2: " << dateTime2.GetAsLocalizedTime(TIME_FORMAT(2)) << std::endl;
572 // std::cout << "3: " << dateTime2.GetAsLocalizedTime(TIME_FORMAT(3)) << std::endl;
573 // std::cout << "4: " << dateTime2.GetAsLocalizedTime(TIME_FORMAT(4)) << std::endl;
574 // std::cout << "5: " << dateTime2.GetAsLocalizedTime(TIME_FORMAT(5)) << std::endl;
575 // std::cout << "6: " << dateTime2.GetAsLocalizedTime(TIME_FORMAT(6)) << std::endl;
576 // std::cout << "7: " << dateTime2.GetAsLocalizedTime(TIME_FORMAT(7)) << std::endl;
577 // std::cout << "8: " << dateTime2.GetAsLocalizedTime(TIME_FORMAT(8)) << std::endl;
578 // std::cout << "14: " << dateTime2.GetAsLocalizedTime(TIME_FORMAT(14)) << std::endl;
579 // std::cout << "15: " << dateTime2.GetAsLocalizedTime(TIME_FORMAT(15)) << std::endl;
580 // std::cout << "16: " << dateTime2.GetAsLocalizedTime(TIME_FORMAT(16)) << std::endl;
581 // std::cout << "19: " << dateTime2.GetAsLocalizedTime(TIME_FORMAT(19)) << std::endl;
582 // std::cout << "27: " << dateTime2.GetAsLocalizedTime(TIME_FORMAT(27)) << std::endl;
583 // std::cout << "32: " << dateTime2.GetAsLocalizedTime(TIME_FORMAT(32)) << std::endl;
584 // std::cout << "64: " << dateTime2.GetAsLocalizedTime(TIME_FORMAT(64)) << std::endl;
585 // std::cout << "128: " << dateTime2.GetAsLocalizedTime(TIME_FORMAT(128)) << std::endl;
586 // std::cout << "256: " << dateTime2.GetAsLocalizedTime(TIME_FORMAT(256)) << std::endl;
588 EXPECT_EQ(dateTime2.GetAsLocalizedDate(false), "2020-2-3");
589 EXPECT_EQ(dateTime2.GetAsLocalizedDate(true), "Monday, February 3, 2020");
590 EXPECT_EQ(dateTime2.GetAsLocalizedDate(std::string("dd-mm-yyyy")),
591 "03-02-2020"); // need to force overload function
592 EXPECT_EQ(dateTime2.GetAsLocalizedTime("hh-mm-ss", true), "04-05-06");
593 EXPECT_EQ(dateTime2.GetAsLocalizedTime("hh-mm-ss", false), "04-05");
594 EXPECT_EQ(dateTime2.GetAsLocalizedDateTime(false, false), "2020-2-3 4:5");
595 EXPECT_EQ(dateTime2.GetAsLocalizedDateTime(true, true), "Monday, February 3, 2020 4:5:6");
597 EXPECT_EQ(dateTime2.GetAsLocalizedTime(TIME_FORMAT(0), false), "4:5");
598 EXPECT_EQ(dateTime2.GetAsLocalizedTime(TIME_FORMAT(0), true), "4:5:6");
600 EXPECT_EQ(dateTime2.GetAsLocalizedTime(TIME_FORMAT(1)), "06");
601 EXPECT_EQ(dateTime2.GetAsLocalizedTime(TIME_FORMAT(2)), "05");
602 EXPECT_EQ(dateTime2.GetAsLocalizedTime(TIME_FORMAT(3)), "05:06");
603 EXPECT_EQ(dateTime2.GetAsLocalizedTime(TIME_FORMAT(4)), "04");
604 EXPECT_EQ(dateTime2.GetAsLocalizedTime(TIME_FORMAT(5)), "04:06");
605 EXPECT_EQ(dateTime2.GetAsLocalizedTime(TIME_FORMAT(6)), "04:05");
606 EXPECT_EQ(dateTime2.GetAsLocalizedTime(TIME_FORMAT(7)), "04:05:06");
607 EXPECT_EQ(dateTime2.GetAsLocalizedTime(TIME_FORMAT(8)), "");
608 EXPECT_EQ(dateTime2.GetAsLocalizedTime(TIME_FORMAT(14)), "04:05");
609 EXPECT_EQ(dateTime2.GetAsLocalizedTime(TIME_FORMAT(15)), "04:05:06");
610 EXPECT_EQ(dateTime2.GetAsLocalizedTime(TIME_FORMAT(16)), "4");
611 EXPECT_EQ(dateTime2.GetAsLocalizedTime(TIME_FORMAT(19)), "4:05:06");
612 EXPECT_EQ(dateTime2.GetAsLocalizedTime(TIME_FORMAT(27)), "4:05:06 AM");
614 // not possible to use these three
615 // EXPECT_EQ(dateTime2.GetAsLocalizedTime(TIME_FORMAT(32)), "");
616 // EXPECT_EQ(dateTime2.GetAsLocalizedTime(TIME_FORMAT(64)), "");
617 // EXPECT_EQ(dateTime2.GetAsLocalizedTime(TIME_FORMAT(128)), "");
619 EXPECT_EQ(dateTime2.GetAsLocalizedTime(TIME_FORMAT(256)), "5");
622 TEST_F(TestDateTime, GetAsSystemTime)
624 CDateTime dateTime;
625 dateTime.SetDateTime(1991, 05, 14, 12, 34, 56);
627 KODI::TIME::SystemTime systemTime;
628 dateTime.GetAsSystemTime(systemTime);
630 EXPECT_TRUE(dateTime == systemTime);
633 TEST_F(TestDateTime, GetAsTime)
635 CDateTime dateTime;
636 dateTime.SetDateTime(1991, 05, 14, 12, 34, 56);
638 time_t time;
639 dateTime.GetAsTime(time);
641 EXPECT_TRUE(dateTime == time);
644 TEST_F(TestDateTime, GetAsTm)
647 CDateTime dateTime;
648 dateTime.SetDateTime(1991, 05, 14, 12, 34, 56);
650 tm time;
651 dateTime.GetAsTm(time);
652 EXPECT_TRUE(dateTime == time);
655 // same test but opposite daylight saving
657 CDateTime dateTime;
658 dateTime.SetDateTime(1991, 01, 14, 12, 34, 56);
660 tm time;
661 dateTime.GetAsTm(time);
662 EXPECT_TRUE(dateTime == time);
666 // Disabled pending std::chrono and std::date changes.
667 TEST_F(TestDateTime, DISABLED_GetAsTimeStamp)
669 CDateTimeSpan bias = CDateTime::GetTimezoneBias();
671 CDateTime dateTime;
672 dateTime.SetDateTime(1991, 05, 14, 12, 34, 56);
674 KODI::TIME::FileTime fileTime;
675 dateTime.GetAsTimeStamp(fileTime);
676 dateTime += bias;
678 EXPECT_TRUE(dateTime == fileTime);
681 TEST_F(TestDateTime, GetAsUTCDateTime)
683 CDateTimeSpan bias = CDateTime::GetTimezoneBias();
685 CDateTime dateTime1;
686 dateTime1.SetDateTime(1991, 05, 14, 12, 34, 56);
688 CDateTime dateTime2;
689 dateTime2 = dateTime1.GetAsUTCDateTime();
690 dateTime2 -= bias;
692 EXPECT_EQ(dateTime2.GetYear(), 1991);
693 EXPECT_EQ(dateTime2.GetMonth(), 5);
694 EXPECT_EQ(dateTime2.GetDay(), 14);
695 EXPECT_EQ(dateTime2.GetHour(), 12);
696 EXPECT_EQ(dateTime2.GetMinute(), 34);
697 EXPECT_EQ(dateTime2.GetSecond(), 56);
700 // disabled on osx and freebsd as their mktime functions
701 // don't work for dates before 1900
702 #if defined(TARGET_DARWIN_OSX) || defined(TARGET_FREEBSD)
703 TEST_F(TestDateTime, DISABLED_Reset)
704 #else
705 TEST_F(TestDateTime, Reset)
706 #endif
708 CDateTime dateTime;
709 dateTime.SetDateTime(1991, 05, 14, 12, 34, 56);
711 dateTime.Reset();
713 EXPECT_EQ(dateTime.GetYear(), 1601);
714 EXPECT_EQ(dateTime.GetMonth(), 1);
715 EXPECT_EQ(dateTime.GetDay(), 1);
716 EXPECT_EQ(dateTime.GetHour(), 0);
717 EXPECT_EQ(dateTime.GetMinute(), 0);
718 EXPECT_EQ(dateTime.GetSecond(), 0);