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.
10 #include "XBDateTime.h"
11 #include "guilib/LocalizeStrings.h"
12 #include "interfaces/legacy/ModuleXbmc.h" //Needed to test getRegion()
17 #include <gtest/gtest.h>
19 class TestDateTime
: public testing::Test
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);
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);
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);
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);
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);
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
= {{
149 {"Sep", "September"},
156 for (const auto& month
: months
)
158 EXPECT_EQ(CDateTime::MonthStringToMonthNum(month
.first
), i
);
159 EXPECT_EQ(CDateTime::MonthStringToMonthNum(month
.second
), i
);
164 // this method is broken as SetFromDBDate() will return true
165 TEST_F(TestDateTime
, DISABLED_SetFromDateString
)
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
)
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);
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
)
199 TEST_F(TestDateTime
, SetFromDBTime
)
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);
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
)
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
)
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();
243 dateTime
.SetFromDBDateTime("1994-11-05 13:15:30");
245 std::string dateTimeStr
= dateTime
.GetAsDBDate() + "T" + dateTime
.GetAsDBTime() + "Z";
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);
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();
271 dateTime1
.SetFromDBDateTime("1991-05-14 12:34:56");
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();
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");
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
)
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);
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)
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);
343 TEST_F(TestDateTime
, GetAsStrings
)
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();
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 "/"
380 // short date formats using "-"
385 // short date formats using "."
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",
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");
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)
426 // std::cout << "GetAsLocalizedDateTime: " << dateTime1.GetAsLocalizedDateTime(false, false)
428 // std::cout << "GetAsLocalizedDateTime: " << dateTime1.GetAsLocalizedDateTime(true, true)
430 // std::cout << "GetAsLocalizedTime: " << dateTime1.GetAsLocalizedTime(TIME_FORMAT(0), false)
432 // std::cout << "GetAsLocalizedTime: " << dateTime1.GetAsLocalizedTime(TIME_FORMAT(0), true)
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
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.
496 dateTime4
.SetDateTime(1991, 05, 9, 12, 34, 56); //Need a single digit date
498 //Test non zero-padded day and short month name.
500 dateTime4
.GetAsLocalizedDate(std::string("D-mmm-YY"), CDateTime::ReturnFormat::CHOICE_YES
),
502 //Test NZP day and NZP month.
504 dateTime4
.GetAsLocalizedDate(std::string("D-M-YY"), CDateTime::ReturnFormat::CHOICE_YES
),
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");
517 EXPECT_EQ(XBMCAddon::xbmc::getRegion("dateshort"), "%-d/%-m/%y");
518 EXPECT_EQ(XBMCAddon::xbmc::getRegion("datelong"), "%a, %-d %B %Y");
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
),
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
),
529 //Test some 'normal' DMY.
531 dateTime4
.GetAsLocalizedDate(std::string("DD/MM/YYYY"), CDateTime::ReturnFormat::CHOICE_YES
),
534 dateTime4
.GetAsLocalizedDate(std::string("DD/MM/YY"), CDateTime::ReturnFormat::CHOICE_YES
),
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");
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)
561 // std::cout << "GetAsLocalizedDateTime: " << dateTime2.GetAsLocalizedDateTime(false, false)
563 // std::cout << "GetAsLocalizedDateTime: " << dateTime2.GetAsLocalizedDateTime(true, true)
565 // std::cout << "GetAsLocalizedTime: " << dateTime2.GetAsLocalizedTime(TIME_FORMAT(0), false)
567 // std::cout << "GetAsLocalizedTime: " << dateTime2.GetAsLocalizedTime(TIME_FORMAT(0), true)
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
)
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
)
636 dateTime
.SetDateTime(1991, 05, 14, 12, 34, 56);
639 dateTime
.GetAsTime(time
);
641 EXPECT_TRUE(dateTime
== time
);
644 TEST_F(TestDateTime
, GetAsTm
)
648 dateTime
.SetDateTime(1991, 05, 14, 12, 34, 56);
651 dateTime
.GetAsTm(time
);
652 EXPECT_TRUE(dateTime
== time
);
655 // same test but opposite daylight saving
658 dateTime
.SetDateTime(1991, 01, 14, 12, 34, 56);
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();
672 dateTime
.SetDateTime(1991, 05, 14, 12, 34, 56);
674 KODI::TIME::FileTime fileTime
;
675 dateTime
.GetAsTimeStamp(fileTime
);
678 EXPECT_TRUE(dateTime
== fileTime
);
681 TEST_F(TestDateTime
, GetAsUTCDateTime
)
683 CDateTimeSpan bias
= CDateTime::GetTimezoneBias();
686 dateTime1
.SetDateTime(1991, 05, 14, 12, 34, 56);
689 dateTime2
= dateTime1
.GetAsUTCDateTime();
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
)
705 TEST_F(TestDateTime
, Reset
)
709 dateTime
.SetDateTime(1991, 05, 14, 12, 34, 56);
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);