[PVR][Estuary] Timer settings dialog: Show client name in timer type selection dialog...
[xbmc.git] / xbmc / XBDateTime.cpp
blob3e24f050f4f90ea3ff7c7ee6d028a4414dc58b6f
1 /*
2 * Copyright (C) 2005-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 "XBDateTime.h"
11 #include "LangInfo.h"
12 #include "guilib/LocalizeStrings.h"
13 #include "utils/Archive.h"
14 #include "utils/StringUtils.h"
15 #include "utils/XTimeUtils.h"
16 #include "utils/log.h"
18 #include <cstdlib>
20 #define SECONDS_PER_DAY 86400L
21 #define SECONDS_PER_HOUR 3600L
22 #define SECONDS_PER_MINUTE 60L
23 #define SECONDS_TO_FILETIME 10000000L
25 static const char *DAY_NAMES[] = { "Sun", "Mon", "Tue", "Wed", "Thu", "Fri", "Sat" };
26 static const char *MONTH_NAMES[] = { "Jan", "Feb", "Mar", "Apr", "May", "Jun", "Jul", "Aug", "Sep", "Oct", "Nov", "Dec" };
28 /////////////////////////////////////////////////
30 // CDateTimeSpan
33 CDateTimeSpan::CDateTimeSpan()
35 m_timeSpan.highDateTime = 0;
36 m_timeSpan.lowDateTime = 0;
39 CDateTimeSpan::CDateTimeSpan(const CDateTimeSpan& span)
41 m_timeSpan.highDateTime = span.m_timeSpan.highDateTime;
42 m_timeSpan.lowDateTime = span.m_timeSpan.lowDateTime;
45 CDateTimeSpan::CDateTimeSpan(int day, int hour, int minute, int second)
47 SetDateTimeSpan(day, hour, minute, second);
50 bool CDateTimeSpan::operator >(const CDateTimeSpan& right) const
52 return KODI::TIME::CompareFileTime(&m_timeSpan, &right.m_timeSpan) > 0;
55 bool CDateTimeSpan::operator >=(const CDateTimeSpan& right) const
57 return operator >(right) || operator ==(right);
60 bool CDateTimeSpan::operator <(const CDateTimeSpan& right) const
62 return KODI::TIME::CompareFileTime(&m_timeSpan, &right.m_timeSpan) < 0;
65 bool CDateTimeSpan::operator <=(const CDateTimeSpan& right) const
67 return operator <(right) || operator ==(right);
70 bool CDateTimeSpan::operator ==(const CDateTimeSpan& right) const
72 return KODI::TIME::CompareFileTime(&m_timeSpan, &right.m_timeSpan) == 0;
75 bool CDateTimeSpan::operator !=(const CDateTimeSpan& right) const
77 return !operator ==(right);
80 CDateTimeSpan CDateTimeSpan::operator +(const CDateTimeSpan& right) const
82 CDateTimeSpan left(*this);
84 LARGE_INTEGER timeLeft;
85 left.ToLargeInt(timeLeft);
87 LARGE_INTEGER timeRight;
88 right.ToLargeInt(timeRight);
90 timeLeft.QuadPart+=timeRight.QuadPart;
92 left.FromLargeInt(timeLeft);
94 return left;
97 CDateTimeSpan CDateTimeSpan::operator -(const CDateTimeSpan& right) const
99 CDateTimeSpan left(*this);
101 LARGE_INTEGER timeLeft;
102 left.ToLargeInt(timeLeft);
104 LARGE_INTEGER timeRight;
105 right.ToLargeInt(timeRight);
107 timeLeft.QuadPart-=timeRight.QuadPart;
109 left.FromLargeInt(timeLeft);
111 return left;
114 const CDateTimeSpan& CDateTimeSpan::operator +=(const CDateTimeSpan& right)
116 LARGE_INTEGER timeThis;
117 ToLargeInt(timeThis);
119 LARGE_INTEGER timeRight;
120 right.ToLargeInt(timeRight);
122 timeThis.QuadPart+=timeRight.QuadPart;
124 FromLargeInt(timeThis);
126 return *this;
129 const CDateTimeSpan& CDateTimeSpan::operator -=(const CDateTimeSpan& right)
131 LARGE_INTEGER timeThis;
132 ToLargeInt(timeThis);
134 LARGE_INTEGER timeRight;
135 right.ToLargeInt(timeRight);
137 timeThis.QuadPart-=timeRight.QuadPart;
139 FromLargeInt(timeThis);
141 return *this;
144 void CDateTimeSpan::ToLargeInt(LARGE_INTEGER& time) const
146 time.u.HighPart = m_timeSpan.highDateTime;
147 time.u.LowPart = m_timeSpan.lowDateTime;
150 void CDateTimeSpan::FromLargeInt(const LARGE_INTEGER& time)
152 m_timeSpan.highDateTime = time.u.HighPart;
153 m_timeSpan.lowDateTime = time.u.LowPart;
156 void CDateTimeSpan::SetDateTimeSpan(int day, int hour, int minute, int second)
158 LARGE_INTEGER time;
159 ToLargeInt(time);
161 time.QuadPart= static_cast<long long>(day) *SECONDS_PER_DAY*SECONDS_TO_FILETIME;
162 time.QuadPart+= static_cast<long long>(hour) *SECONDS_PER_HOUR*SECONDS_TO_FILETIME;
163 time.QuadPart+= static_cast<long long>(minute) *SECONDS_PER_MINUTE*SECONDS_TO_FILETIME;
164 time.QuadPart+= static_cast<long long>(second) *SECONDS_TO_FILETIME;
166 FromLargeInt(time);
169 void CDateTimeSpan::SetFromTimeString(const std::string& time) // hh:mm
171 if (time.size() >= 5 && time[2] == ':')
173 int hour = atoi(time.substr(0, 2).c_str());
174 int minutes = atoi(time.substr(3, 2).c_str());
175 SetDateTimeSpan(0,hour,minutes,0);
179 int CDateTimeSpan::GetDays() const
181 LARGE_INTEGER time;
182 ToLargeInt(time);
184 return (int)(time.QuadPart/SECONDS_TO_FILETIME)/SECONDS_PER_DAY;
187 int CDateTimeSpan::GetHours() const
189 LARGE_INTEGER time;
190 ToLargeInt(time);
192 return (int)((time.QuadPart/SECONDS_TO_FILETIME)%SECONDS_PER_DAY)/SECONDS_PER_HOUR;
195 int CDateTimeSpan::GetMinutes() const
197 LARGE_INTEGER time;
198 ToLargeInt(time);
200 return (int)((time.QuadPart/SECONDS_TO_FILETIME%SECONDS_PER_DAY)%SECONDS_PER_HOUR)/SECONDS_PER_MINUTE;
203 int CDateTimeSpan::GetSeconds() const
205 LARGE_INTEGER time;
206 ToLargeInt(time);
208 return (int)(((time.QuadPart/SECONDS_TO_FILETIME%SECONDS_PER_DAY)%SECONDS_PER_HOUR)%SECONDS_PER_MINUTE)%SECONDS_PER_MINUTE;
211 int CDateTimeSpan::GetSecondsTotal() const
213 LARGE_INTEGER time;
214 ToLargeInt(time);
216 return (int)(time.QuadPart/SECONDS_TO_FILETIME);
219 void CDateTimeSpan::SetFromPeriod(const std::string &period)
221 long days = atoi(period.c_str());
222 // find the first non-space and non-number
223 size_t pos = period.find_first_not_of("0123456789 ", 0);
224 if (pos != std::string::npos)
226 std::string units = period.substr(pos, 3);
227 if (StringUtils::EqualsNoCase(units, "wee"))
228 days *= 7;
229 else if (StringUtils::EqualsNoCase(units, "mon"))
230 days *= 31;
233 SetDateTimeSpan(days, 0, 0, 0);
236 /////////////////////////////////////////////////
238 // CDateTime
241 CDateTime::CDateTime()
243 Reset();
246 CDateTime::CDateTime(const KODI::TIME::SystemTime& time)
248 // we store internally as a FileTime
249 m_state = ToFileTime(time, m_time) ? valid : invalid;
252 CDateTime::CDateTime(const KODI::TIME::FileTime& time) : m_time(time)
254 SetValid(true);
257 CDateTime::CDateTime(const CDateTime& time) : m_time(time.m_time)
259 m_state=time.m_state;
262 CDateTime::CDateTime(const time_t& time)
264 m_state = ToFileTime(time, m_time) ? valid : invalid;
267 CDateTime::CDateTime(const tm& time)
269 m_state = ToFileTime(time, m_time) ? valid : invalid;
272 CDateTime::CDateTime(int year, int month, int day, int hour, int minute, int second)
274 SetDateTime(year, month, day, hour, minute, second);
277 CDateTime CDateTime::GetCurrentDateTime()
279 // get the current time
280 KODI::TIME::SystemTime time;
281 KODI::TIME::GetLocalTime(&time);
283 return CDateTime(time);
286 CDateTime CDateTime::GetUTCDateTime()
288 CDateTime time(GetCurrentDateTime());
289 time += GetTimezoneBias();
290 return time;
293 const CDateTime& CDateTime::operator=(const KODI::TIME::SystemTime& right)
295 m_state = ToFileTime(right, m_time) ? valid : invalid;
297 return *this;
300 const CDateTime& CDateTime::operator=(const KODI::TIME::FileTime& right)
302 m_time=right;
303 SetValid(true);
305 return *this;
308 const CDateTime& CDateTime::operator =(const time_t& right)
310 m_state = ToFileTime(right, m_time) ? valid : invalid;
312 return *this;
315 const CDateTime& CDateTime::operator =(const tm& right)
317 m_state = ToFileTime(right, m_time) ? valid : invalid;
319 return *this;
322 bool CDateTime::operator >(const CDateTime& right) const
324 return operator >(right.m_time);
327 bool CDateTime::operator >=(const CDateTime& right) const
329 return operator >(right) || operator ==(right);
332 bool CDateTime::operator <(const CDateTime& right) const
334 return operator <(right.m_time);
337 bool CDateTime::operator <=(const CDateTime& right) const
339 return operator <(right) || operator ==(right);
342 bool CDateTime::operator ==(const CDateTime& right) const
344 return operator ==(right.m_time);
347 bool CDateTime::operator !=(const CDateTime& right) const
349 return !operator ==(right);
352 bool CDateTime::operator>(const KODI::TIME::FileTime& right) const
354 return KODI::TIME::CompareFileTime(&m_time, &right) > 0;
357 bool CDateTime::operator>=(const KODI::TIME::FileTime& right) const
359 return operator >(right) || operator ==(right);
362 bool CDateTime::operator<(const KODI::TIME::FileTime& right) const
364 return KODI::TIME::CompareFileTime(&m_time, &right) < 0;
367 bool CDateTime::operator<=(const KODI::TIME::FileTime& right) const
369 return operator <(right) || operator ==(right);
372 bool CDateTime::operator==(const KODI::TIME::FileTime& right) const
374 return KODI::TIME::CompareFileTime(&m_time, &right) == 0;
377 bool CDateTime::operator!=(const KODI::TIME::FileTime& right) const
379 return !operator ==(right);
382 bool CDateTime::operator>(const KODI::TIME::SystemTime& right) const
384 KODI::TIME::FileTime time;
385 ToFileTime(right, time);
387 return operator >(time);
390 bool CDateTime::operator>=(const KODI::TIME::SystemTime& right) const
392 return operator >(right) || operator ==(right);
395 bool CDateTime::operator<(const KODI::TIME::SystemTime& right) const
397 KODI::TIME::FileTime time;
398 ToFileTime(right, time);
400 return operator <(time);
403 bool CDateTime::operator<=(const KODI::TIME::SystemTime& right) const
405 return operator <(right) || operator ==(right);
408 bool CDateTime::operator==(const KODI::TIME::SystemTime& right) const
410 KODI::TIME::FileTime time;
411 ToFileTime(right, time);
413 return operator ==(time);
416 bool CDateTime::operator!=(const KODI::TIME::SystemTime& right) const
418 return !operator ==(right);
421 bool CDateTime::operator >(const time_t& right) const
423 KODI::TIME::FileTime time;
424 ToFileTime(right, time);
426 return operator >(time);
429 bool CDateTime::operator >=(const time_t& right) const
431 return operator >(right) || operator ==(right);
434 bool CDateTime::operator <(const time_t& right) const
436 KODI::TIME::FileTime time;
437 ToFileTime(right, time);
439 return operator <(time);
442 bool CDateTime::operator <=(const time_t& right) const
444 return operator <(right) || operator ==(right);
447 bool CDateTime::operator ==(const time_t& right) const
449 KODI::TIME::FileTime time;
450 ToFileTime(right, time);
452 return operator ==(time);
455 bool CDateTime::operator !=(const time_t& right) const
457 return !operator ==(right);
460 bool CDateTime::operator >(const tm& right) const
462 KODI::TIME::FileTime time;
463 ToFileTime(right, time);
465 return operator >(time);
468 bool CDateTime::operator >=(const tm& right) const
470 return operator >(right) || operator ==(right);
473 bool CDateTime::operator <(const tm& right) const
475 KODI::TIME::FileTime time;
476 ToFileTime(right, time);
478 return operator <(time);
481 bool CDateTime::operator <=(const tm& right) const
483 return operator <(right) || operator ==(right);
486 bool CDateTime::operator ==(const tm& right) const
488 KODI::TIME::FileTime time;
489 ToFileTime(right, time);
491 return operator ==(time);
494 bool CDateTime::operator !=(const tm& right) const
496 return !operator ==(right);
499 CDateTime CDateTime::operator +(const CDateTimeSpan& right) const
501 CDateTime left(*this);
503 LARGE_INTEGER timeLeft;
504 left.ToLargeInt(timeLeft);
506 LARGE_INTEGER timeRight;
507 right.ToLargeInt(timeRight);
509 timeLeft.QuadPart+=timeRight.QuadPart;
511 left.FromLargeInt(timeLeft);
513 return left;
516 CDateTime CDateTime::operator -(const CDateTimeSpan& right) const
518 CDateTime left(*this);
520 LARGE_INTEGER timeLeft;
521 left.ToLargeInt(timeLeft);
523 LARGE_INTEGER timeRight;
524 right.ToLargeInt(timeRight);
526 timeLeft.QuadPart-=timeRight.QuadPart;
528 left.FromLargeInt(timeLeft);
530 return left;
533 const CDateTime& CDateTime::operator +=(const CDateTimeSpan& right)
535 LARGE_INTEGER timeThis;
536 ToLargeInt(timeThis);
538 LARGE_INTEGER timeRight;
539 right.ToLargeInt(timeRight);
541 timeThis.QuadPart+=timeRight.QuadPart;
543 FromLargeInt(timeThis);
545 return *this;
548 const CDateTime& CDateTime::operator -=(const CDateTimeSpan& right)
550 LARGE_INTEGER timeThis;
551 ToLargeInt(timeThis);
553 LARGE_INTEGER timeRight;
554 right.ToLargeInt(timeRight);
556 timeThis.QuadPart-=timeRight.QuadPart;
558 FromLargeInt(timeThis);
560 return *this;
563 CDateTimeSpan CDateTime::operator -(const CDateTime& right) const
565 CDateTimeSpan left;
567 LARGE_INTEGER timeLeft;
568 left.ToLargeInt(timeLeft);
570 LARGE_INTEGER timeThis;
571 ToLargeInt(timeThis);
573 LARGE_INTEGER timeRight;
574 right.ToLargeInt(timeRight);
576 timeLeft.QuadPart=timeThis.QuadPart-timeRight.QuadPart;
578 left.FromLargeInt(timeLeft);
580 return left;
583 CDateTime::operator KODI::TIME::FileTime() const
585 return m_time;
588 void CDateTime::Archive(CArchive& ar)
590 if (ar.IsStoring())
592 ar<<(int)m_state;
593 if (m_state==valid)
595 KODI::TIME::SystemTime st;
596 GetAsSystemTime(st);
597 ar<<st;
600 else
602 Reset();
603 int state;
604 ar >> state;
605 m_state = CDateTime::STATE(state);
606 if (m_state==valid)
608 KODI::TIME::SystemTime st;
609 ar>>st;
610 ToFileTime(st, m_time);
615 void CDateTime::Reset()
617 SetDateTime(1601, 1, 1, 0, 0, 0);
618 SetValid(false);
621 void CDateTime::SetValid(bool yesNo)
623 m_state=yesNo ? valid : invalid;
626 bool CDateTime::IsValid() const
628 return m_state==valid;
631 bool CDateTime::ToFileTime(const KODI::TIME::SystemTime& time, KODI::TIME::FileTime& fileTime) const
633 return KODI::TIME::SystemTimeToFileTime(&time, &fileTime) == 1 &&
634 (fileTime.lowDateTime > 0 || fileTime.highDateTime > 0);
637 bool CDateTime::ToFileTime(const time_t& time, KODI::TIME::FileTime& fileTime) const
639 long long ll = time;
640 ll *= 10000000ll;
641 ll += 0x19DB1DED53E8000LL;
643 fileTime.lowDateTime = (DWORD)(ll & 0xFFFFFFFF);
644 fileTime.highDateTime = (DWORD)(ll >> 32);
646 return true;
649 bool CDateTime::ToFileTime(const tm& time, KODI::TIME::FileTime& fileTime) const
651 KODI::TIME::SystemTime st = {};
653 st.year = time.tm_year + 1900;
654 st.month = time.tm_mon + 1;
655 st.dayOfWeek = time.tm_wday;
656 st.day = time.tm_mday;
657 st.hour = time.tm_hour;
658 st.minute = time.tm_min;
659 st.second = time.tm_sec;
661 return SystemTimeToFileTime(&st, &fileTime) == 1;
664 void CDateTime::ToLargeInt(LARGE_INTEGER& time) const
666 time.u.HighPart = m_time.highDateTime;
667 time.u.LowPart = m_time.lowDateTime;
670 void CDateTime::FromLargeInt(const LARGE_INTEGER& time)
672 m_time.highDateTime = time.u.HighPart;
673 m_time.lowDateTime = time.u.LowPart;
676 bool CDateTime::SetFromDateString(const std::string &date)
678 //! @todo STRING_CLEANUP
679 if (date.empty())
681 SetValid(false);
682 return false;
685 if (SetFromDBDate(date))
686 return true;
688 const char* months[] = {"january","february","march","april","may","june","july","august","september","october","november","december",NULL};
689 int j=0;
690 size_t iDayPos = date.find("day");
691 size_t iPos = date.find(' ');
692 if (iDayPos < iPos && iDayPos != std::string::npos)
694 iDayPos = iPos + 1;
695 iPos = date.find(' ', iPos+1);
697 else
698 iDayPos = 0;
700 std::string strMonth = date.substr(iDayPos, iPos - iDayPos);
701 if (strMonth.empty())
702 return false;
704 size_t iPos2 = date.find(',');
705 std::string strDay = (date.size() >= iPos) ? date.substr(iPos, iPos2-iPos) : "";
706 std::string strYear = date.substr(date.find(' ', iPos2) + 1);
707 while (months[j] && StringUtils::CompareNoCase(strMonth, months[j]) != 0)
708 j++;
709 if (!months[j])
710 return false;
712 return SetDateTime(atol(strYear.c_str()),j+1,atol(strDay.c_str()),0,0,0);
715 int CDateTime::GetDay() const
717 KODI::TIME::SystemTime st;
718 GetAsSystemTime(st);
720 return st.day;
723 int CDateTime::GetMonth() const
725 KODI::TIME::SystemTime st;
726 GetAsSystemTime(st);
728 return st.month;
731 int CDateTime::GetYear() const
733 KODI::TIME::SystemTime st;
734 GetAsSystemTime(st);
736 return st.year;
739 int CDateTime::GetHour() const
741 KODI::TIME::SystemTime st;
742 GetAsSystemTime(st);
744 return st.hour;
747 int CDateTime::GetMinute() const
749 KODI::TIME::SystemTime st;
750 GetAsSystemTime(st);
752 return st.minute;
755 int CDateTime::GetSecond() const
757 KODI::TIME::SystemTime st;
758 GetAsSystemTime(st);
760 return st.second;
763 int CDateTime::GetDayOfWeek() const
765 KODI::TIME::SystemTime st;
766 GetAsSystemTime(st);
768 return st.dayOfWeek;
771 int CDateTime::GetMinuteOfDay() const
773 KODI::TIME::SystemTime st;
774 GetAsSystemTime(st);
775 return st.hour * 60 + st.minute;
778 bool CDateTime::SetDateTime(int year, int month, int day, int hour, int minute, int second)
780 KODI::TIME::SystemTime st = {};
782 st.year = year;
783 st.month = month;
784 st.day = day;
785 st.hour = hour;
786 st.minute = minute;
787 st.second = second;
789 m_state = ToFileTime(st, m_time) ? valid : invalid;
790 return m_state == valid;
793 bool CDateTime::SetDate(int year, int month, int day)
795 return SetDateTime(year, month, day, 0, 0, 0);
798 bool CDateTime::SetTime(int hour, int minute, int second)
800 // 01.01.1601 00:00:00 is 0 as filetime
801 return SetDateTime(1601, 1, 1, hour, minute, second);
804 void CDateTime::GetAsSystemTime(KODI::TIME::SystemTime& time) const
806 FileTimeToSystemTime(&m_time, &time);
809 #define UNIX_BASE_TIME 116444736000000000LL /* nanoseconds since epoch */
810 void CDateTime::GetAsTime(time_t& time) const
812 long long ll = (static_cast<long long>(m_time.highDateTime) << 32) + m_time.lowDateTime;
813 time=(time_t)((ll - UNIX_BASE_TIME) / 10000000);
816 void CDateTime::GetAsTm(tm& time) const
818 KODI::TIME::SystemTime st;
819 GetAsSystemTime(st);
821 time.tm_year = st.year - 1900;
822 time.tm_mon = st.month - 1;
823 time.tm_wday = st.dayOfWeek;
824 time.tm_mday = st.day;
825 time.tm_hour = st.hour;
826 time.tm_min = st.minute;
827 time.tm_sec = st.second;
829 mktime(&time);
832 void CDateTime::GetAsTimeStamp(KODI::TIME::FileTime& time) const
834 KODI::TIME::LocalFileTimeToFileTime(&m_time, &time);
837 std::string CDateTime::GetAsDBDate() const
839 KODI::TIME::SystemTime st;
840 GetAsSystemTime(st);
842 return StringUtils::Format("{:04}-{:02}-{:02}", st.year, st.month, st.day);
845 std::string CDateTime::GetAsDBTime() const
847 KODI::TIME::SystemTime st;
848 GetAsSystemTime(st);
850 return StringUtils::Format("{:02}:{:02}:{:02}", st.hour, st.minute, st.second);
853 std::string CDateTime::GetAsDBDateTime() const
855 KODI::TIME::SystemTime st;
856 GetAsSystemTime(st);
858 return StringUtils::Format("{:04}-{:02}-{:02} {:02}:{:02}:{:02}", st.year, st.month, st.day,
859 st.hour, st.minute, st.second);
862 std::string CDateTime::GetAsSaveString() const
864 KODI::TIME::SystemTime st;
865 GetAsSystemTime(st);
867 return StringUtils::Format("{:04}{:02}{:02}_{:02}{:02}{:02}", st.year, st.month, st.day, st.hour,
868 st.minute, st.second);
871 bool CDateTime::SetFromUTCDateTime(const CDateTime &dateTime)
873 CDateTime tmp(dateTime);
874 tmp -= GetTimezoneBias();
876 m_time = tmp.m_time;
877 m_state = tmp.m_state;
878 return m_state == valid;
881 static bool bGotTimezoneBias = false;
883 void CDateTime::ResetTimezoneBias(void)
885 bGotTimezoneBias = false;
888 CDateTimeSpan CDateTime::GetTimezoneBias(void)
890 static CDateTimeSpan timezoneBias;
892 if (!bGotTimezoneBias)
894 bGotTimezoneBias = true;
895 KODI::TIME::TimeZoneInformation tz;
896 switch (KODI::TIME::GetTimeZoneInformation(&tz))
898 case KODI::TIME::KODI_TIME_ZONE_ID_DAYLIGHT:
899 timezoneBias = CDateTimeSpan(0, 0, tz.bias + tz.daylightBias, 0);
900 break;
901 case KODI::TIME::KODI_TIME_ZONE_ID_STANDARD:
902 timezoneBias = CDateTimeSpan(0, 0, tz.bias + tz.standardBias, 0);
903 break;
904 case KODI::TIME::KODI_TIME_ZONE_ID_UNKNOWN:
905 timezoneBias = CDateTimeSpan(0, 0, tz.bias, 0);
906 break;
910 return timezoneBias;
913 bool CDateTime::SetFromUTCDateTime(const time_t &dateTime)
915 CDateTime tmp(dateTime);
916 return SetFromUTCDateTime(tmp);
919 bool CDateTime::SetFromW3CDate(const std::string &dateTime)
921 std::string date;
923 size_t posT = dateTime.find('T');
924 if(posT != std::string::npos)
925 date = dateTime.substr(0, posT);
926 else
927 date = dateTime;
929 int year = 0, month = 1, day = 1;
931 if (date.size() >= 4)
932 year = atoi(date.substr(0, 4).c_str());
934 if (date.size() >= 10)
936 month = atoi(date.substr(5, 2).c_str());
937 day = atoi(date.substr(8, 2).c_str());
940 CDateTime tmpDateTime(year, month, day, 0, 0, 0);
941 if (tmpDateTime.IsValid())
942 *this = tmpDateTime;
944 return IsValid();
947 bool CDateTime::SetFromW3CDateTime(const std::string &dateTime, bool ignoreTimezone /* = false */)
949 std::string date, time, zone;
951 size_t posT = dateTime.find('T');
952 if(posT != std::string::npos)
954 date = dateTime.substr(0, posT);
955 std::string::size_type posZ = dateTime.find_first_of("+-Z", posT);
956 if(posZ == std::string::npos)
957 time = dateTime.substr(posT + 1);
958 else
960 time = dateTime.substr(posT + 1, posZ - posT - 1);
961 zone = dateTime.substr(posZ);
964 else
965 date = dateTime;
967 int year = 0, month = 1, day = 1, hour = 0, min = 0, sec = 0;
969 if (date.size() >= 4)
970 year = atoi(date.substr(0, 4).c_str());
972 if (date.size() >= 10)
974 month = atoi(date.substr(5, 2).c_str());
975 day = atoi(date.substr(8, 2).c_str());
978 if (time.length() >= 5)
980 hour = atoi(time.substr(0, 2).c_str());
981 min = atoi(time.substr(3, 2).c_str());
984 if (time.length() >= 8)
985 sec = atoi(time.substr(6, 2).c_str());
987 CDateTime tmpDateTime(year, month, day, hour, min, sec);
988 if (!tmpDateTime.IsValid())
989 return false;
991 if (!ignoreTimezone && !zone.empty())
993 // check if the timezone is UTC
994 if (StringUtils::StartsWith(zone, "Z"))
995 return SetFromUTCDateTime(tmpDateTime);
996 else
998 // retrieve the timezone offset (ignoring the + or -)
999 CDateTimeSpan zoneSpan; zoneSpan.SetFromTimeString(zone.substr(1));
1000 if (zoneSpan.GetSecondsTotal() != 0)
1002 if (StringUtils::StartsWith(zone, "+"))
1003 tmpDateTime -= zoneSpan;
1004 else if (StringUtils::StartsWith(zone, "-"))
1005 tmpDateTime += zoneSpan;
1010 *this = tmpDateTime;
1011 return IsValid();
1014 bool CDateTime::SetFromDBDateTime(const std::string &dateTime)
1016 // assumes format YYYY-MM-DD HH:MM:SS
1017 if (dateTime.size() == 19)
1019 int year = atoi(dateTime.substr(0, 4).c_str());
1020 int month = atoi(dateTime.substr(5, 2).c_str());
1021 int day = atoi(dateTime.substr(8, 2).c_str());
1022 int hour = atoi(dateTime.substr(11, 2).c_str());
1023 int min = atoi(dateTime.substr(14, 2).c_str());
1024 int sec = atoi(dateTime.substr(17, 2).c_str());
1025 return SetDateTime(year, month, day, hour, min, sec);
1027 return false;
1030 bool CDateTime::SetFromDBDate(const std::string &date)
1032 if (date.size() < 10)
1033 return false;
1034 // assumes format:
1035 // YYYY-MM-DD or DD-MM-YYYY
1036 const static std::string sep_chars = "-./";
1037 int year = 0, month = 0, day = 0;
1038 if (sep_chars.find(date[2]) != std::string::npos)
1040 day = atoi(date.substr(0, 2).c_str());
1041 month = atoi(date.substr(3, 2).c_str());
1042 year = atoi(date.substr(6, 4).c_str());
1044 else if (sep_chars.find(date[4]) != std::string::npos)
1046 year = atoi(date.substr(0, 4).c_str());
1047 month = atoi(date.substr(5, 2).c_str());
1048 day = atoi(date.substr(8, 2).c_str());
1050 return SetDate(year, month, day);
1053 bool CDateTime::SetFromDBTime(const std::string &time)
1055 if (time.size() < 5)
1056 return false;
1058 int hour;
1059 int minute;
1061 int second = 0;
1062 // HH:MM or HH:MM:SS
1063 hour = atoi(time.substr(0, 2).c_str());
1064 minute = atoi(time.substr(3, 2).c_str());
1065 // HH:MM:SS
1066 if (time.size() == 8)
1067 second = atoi(time.substr(6, 2).c_str());
1069 return SetTime(hour, minute, second);
1072 bool CDateTime::SetFromRFC1123DateTime(const std::string &dateTime)
1074 std::string date = dateTime;
1075 StringUtils::Trim(date);
1077 if (date.size() != 29)
1078 return false;
1080 int day = strtol(date.substr(5, 2).c_str(), NULL, 10);
1082 std::string strMonth = date.substr(8, 3);
1083 int month = 0;
1084 for (unsigned int index = 0; index < 12; index++)
1086 if (strMonth == MONTH_NAMES[index])
1088 month = index + 1;
1089 break;
1093 if (month < 1)
1094 return false;
1096 int year = strtol(date.substr(12, 4).c_str(), NULL, 10);
1097 int hour = strtol(date.substr(17, 2).c_str(), NULL, 10);
1098 int min = strtol(date.substr(20, 2).c_str(), NULL, 10);
1099 int sec = strtol(date.substr(23, 2).c_str(), NULL, 10);
1101 return SetDateTime(year, month, day, hour, min, sec);
1104 CDateTime CDateTime::FromDateString(const std::string &date)
1106 CDateTime dt;
1107 dt.SetFromDateString(date);
1108 return dt;
1111 CDateTime CDateTime::FromDBDateTime(const std::string &dateTime)
1113 CDateTime dt;
1114 dt.SetFromDBDateTime(dateTime);
1115 return dt;
1118 CDateTime CDateTime::FromDBDate(const std::string &date)
1120 CDateTime dt;
1121 dt.SetFromDBDate(date);
1122 return dt;
1125 CDateTime CDateTime::FromDBTime(const std::string &time)
1127 CDateTime dt;
1128 dt.SetFromDBTime(time);
1129 return dt;
1132 CDateTime CDateTime::FromW3CDate(const std::string &date)
1134 CDateTime dt;
1135 dt.SetFromW3CDate(date);
1136 return dt;
1139 CDateTime CDateTime::FromW3CDateTime(const std::string &date, bool ignoreTimezone /* = false */)
1141 CDateTime dt;
1142 dt.SetFromW3CDateTime(date, ignoreTimezone);
1143 return dt;
1146 CDateTime CDateTime::FromUTCDateTime(const CDateTime &dateTime)
1148 CDateTime dt;
1149 dt.SetFromUTCDateTime(dateTime);
1150 return dt;
1153 CDateTime CDateTime::FromUTCDateTime(const time_t &dateTime)
1155 CDateTime dt;
1156 dt.SetFromUTCDateTime(dateTime);
1157 return dt;
1160 CDateTime CDateTime::FromRFC1123DateTime(const std::string &dateTime)
1162 CDateTime dt;
1163 dt.SetFromRFC1123DateTime(dateTime);
1164 return dt;
1167 std::string CDateTime::GetAsLocalizedTime(const std::string &format, bool withSeconds) const
1169 std::string strOut;
1170 const std::string& strFormat = format.empty() ? g_langInfo.GetTimeFormat() : format;
1172 KODI::TIME::SystemTime dateTime;
1173 GetAsSystemTime(dateTime);
1175 // Prefetch meridiem symbol
1176 const std::string& strMeridiem =
1177 CLangInfo::MeridiemSymbolToString(dateTime.hour > 11 ? MeridiemSymbolPM : MeridiemSymbolAM);
1179 size_t length = strFormat.size();
1180 for (size_t i=0; i < length; ++i)
1182 char c=strFormat[i];
1183 if (c=='\'')
1185 // To be able to display a "'" in the string,
1186 // find the last "'" that doesn't follow a "'"
1187 size_t pos=i + 1;
1188 while(((pos = strFormat.find(c, pos + 1)) != std::string::npos &&
1189 pos<strFormat.size()) && strFormat[pos+1]=='\'') {}
1191 std::string strPart;
1192 if (pos != std::string::npos)
1194 // Extract string between ' '
1195 strPart=strFormat.substr(i + 1, pos - i - 1);
1196 i=pos;
1198 else
1200 strPart=strFormat.substr(i + 1, length - i - 1);
1201 i=length;
1204 StringUtils::Replace(strPart, "''", "'");
1206 strOut+=strPart;
1208 else if (c=='h' || c=='H') // parse hour (H="24 hour clock")
1210 int partLength=0;
1212 int pos=strFormat.find_first_not_of(c,i+1);
1213 if (pos>-1)
1215 // Get length of the hour mask, eg. HH
1216 partLength=pos-i;
1217 i=pos-1;
1219 else
1221 // mask ends at the end of the string, extract it
1222 partLength=length-i;
1223 i=length;
1226 int hour = dateTime.hour;
1227 if (c=='h')
1228 { // recalc to 12 hour clock
1229 if (hour > 11)
1230 hour -= (12 * (hour > 12));
1231 else
1232 hour += (12 * (hour < 1));
1235 // Format hour string with the length of the mask
1236 std::string str;
1237 if (partLength==1)
1238 str = std::to_string(hour);
1239 else
1240 str = StringUtils::Format("{:02}", hour);
1242 strOut+=str;
1244 else if (c=='m') // parse minutes
1246 int partLength=0;
1248 int pos=strFormat.find_first_not_of(c,i+1);
1249 if (pos>-1)
1251 // Get length of the minute mask, eg. mm
1252 partLength=pos-i;
1253 i=pos-1;
1255 else
1257 // mask ends at the end of the string, extract it
1258 partLength=length-i;
1259 i=length;
1262 // Format minute string with the length of the mask
1263 std::string str;
1264 if (partLength==1)
1265 str = std::to_string(dateTime.minute);
1266 else
1267 str = StringUtils::Format("{:02}", dateTime.minute);
1269 strOut+=str;
1271 else if (c=='s') // parse seconds
1273 int partLength=0;
1275 int pos=strFormat.find_first_not_of(c,i+1);
1276 if (pos>-1)
1278 // Get length of the seconds mask, eg. ss
1279 partLength=pos-i;
1280 i=pos-1;
1282 else
1284 // mask ends at the end of the string, extract it
1285 partLength=length-i;
1286 i=length;
1289 if (withSeconds)
1291 // Format seconds string with the length of the mask
1292 std::string str;
1293 if (partLength==1)
1294 str = std::to_string(dateTime.second);
1295 else
1296 str = StringUtils::Format("{:02}", dateTime.second);
1298 strOut+=str;
1300 else
1301 strOut.erase(strOut.size()-1,1);
1303 else if (c=='x') // add meridiem symbol
1305 int pos=strFormat.find_first_not_of(c,i+1);
1306 if (pos>-1)
1308 // Get length of the meridiem mask
1309 i=pos-1;
1311 else
1313 // mask ends at the end of the string, extract it
1314 i=length;
1317 strOut+=strMeridiem;
1319 else // everything else pass to output
1320 strOut+=c;
1323 return strOut;
1326 std::string CDateTime::GetAsLocalizedDate(bool longDate/*=false*/) const
1328 return GetAsLocalizedDate(g_langInfo.GetDateFormat(longDate));
1331 std::string CDateTime::GetAsLocalizedDate(const std::string &strFormat) const
1333 std::string strOut;
1335 KODI::TIME::SystemTime dateTime;
1336 GetAsSystemTime(dateTime);
1338 size_t length = strFormat.size();
1339 for (size_t i = 0; i < length; ++i)
1341 char c=strFormat[i];
1342 if (c=='\'')
1344 // To be able to display a "'" in the string,
1345 // find the last "'" that doesn't follow a "'"
1346 size_t pos = i + 1;
1347 while(((pos = strFormat.find(c, pos + 1)) != std::string::npos &&
1348 pos < strFormat.size()) &&
1349 strFormat[pos + 1] == '\'') {}
1351 std::string strPart;
1352 if (pos != std::string::npos)
1354 // Extract string between ' '
1355 strPart = strFormat.substr(i + 1, pos - i - 1);
1356 i = pos;
1358 else
1360 strPart = strFormat.substr(i + 1, length - i - 1);
1361 i = length;
1363 StringUtils::Replace(strPart, "''", "'");
1364 strOut+=strPart;
1366 else if (c=='D' || c=='d') // parse days
1368 size_t partLength=0;
1370 size_t pos = strFormat.find_first_not_of(c, i+1);
1371 if (pos != std::string::npos)
1373 // Get length of the day mask, eg. DDDD
1374 partLength=pos-i;
1375 i=pos-1;
1377 else
1379 // mask ends at the end of the string, extract it
1380 partLength=length-i;
1381 i=length;
1384 // Format string with the length of the mask
1385 std::string str;
1386 if (partLength==1) // single-digit number
1387 str = std::to_string(dateTime.day);
1388 else if (partLength==2) // two-digit number
1389 str = StringUtils::Format("{:02}", dateTime.day);
1390 else // Day of week string
1392 int wday = dateTime.dayOfWeek;
1393 if (wday < 1 || wday > 7) wday = 7;
1394 str = g_localizeStrings.Get((c =='d' ? 40 : 10) + wday);
1396 strOut+=str;
1398 else if (c=='M' || c=='m') // parse month
1400 size_t partLength=0;
1402 size_t pos=strFormat.find_first_not_of(c,i+1);
1403 if (pos != std::string::npos)
1405 // Get length of the month mask, eg. MMMM
1406 partLength=pos-i;
1407 i=pos-1;
1409 else
1411 // mask ends at the end of the string, extract it
1412 partLength=length-i;
1413 i=length;
1416 // Format string with the length of the mask
1417 std::string str;
1418 if (partLength==1) // single-digit number
1419 str = std::to_string(dateTime.month);
1420 else if (partLength==2) // two-digit number
1421 str = StringUtils::Format("{:02}", dateTime.month);
1422 else // Month string
1424 int wmonth = dateTime.month;
1425 if (wmonth < 1 || wmonth > 12) wmonth = 12;
1426 str = g_localizeStrings.Get((c =='m' ? 50 : 20) + wmonth);
1428 strOut+=str;
1430 else if (c=='Y' || c =='y') // parse year
1432 size_t partLength = 0;
1434 size_t pos = strFormat.find_first_not_of(c,i+1);
1435 if (pos != std::string::npos)
1437 // Get length of the year mask, eg. YYYY
1438 partLength=pos-i;
1439 i=pos-1;
1441 else
1443 // mask ends at the end of the string, extract it
1444 partLength=length-i;
1445 i=length;
1448 // Format string with the length of the mask
1449 std::string str = std::to_string(dateTime.year); // four-digit number
1450 if (partLength <= 2)
1451 str.erase(0, 2); // two-digit number
1453 strOut+=str;
1455 else // everything else pass to output
1456 strOut+=c;
1459 return strOut;
1462 std::string CDateTime::GetAsLocalizedDateTime(bool longDate/*=false*/, bool withSeconds/*=true*/) const
1464 return GetAsLocalizedDate(longDate) + ' ' + GetAsLocalizedTime("", withSeconds);
1467 std::string CDateTime::GetAsLocalizedTime(TIME_FORMAT format, bool withSeconds /* = false */) const
1469 const std::string timeFormat = g_langInfo.GetTimeFormat();
1470 bool use12hourclock = timeFormat.find('h') != std::string::npos;
1471 switch (format)
1473 case TIME_FORMAT_GUESS:
1474 return GetAsLocalizedTime("", withSeconds);
1475 case TIME_FORMAT_SS:
1476 return GetAsLocalizedTime("ss", true);
1477 case TIME_FORMAT_MM:
1478 return GetAsLocalizedTime("mm", true);
1479 case TIME_FORMAT_MM_SS:
1480 return GetAsLocalizedTime("mm:ss", true);
1481 case TIME_FORMAT_HH: // this forces it to a 12 hour clock
1482 return GetAsLocalizedTime(use12hourclock ? "h" : "HH", false);
1483 case TIME_FORMAT_HH_SS:
1484 return GetAsLocalizedTime(use12hourclock ? "h:ss" : "HH:ss", true);
1485 case TIME_FORMAT_HH_MM:
1486 return GetAsLocalizedTime(use12hourclock ? "h:mm" : "HH:mm", false);
1487 case TIME_FORMAT_HH_MM_XX:
1488 return GetAsLocalizedTime(use12hourclock ? "h:mm xx" : "HH:mm", false);
1489 case TIME_FORMAT_HH_MM_SS:
1490 return GetAsLocalizedTime(use12hourclock ? "hh:mm:ss" : "HH:mm:ss", true);
1491 case TIME_FORMAT_HH_MM_SS_XX:
1492 return GetAsLocalizedTime(use12hourclock ? "hh:mm:ss xx" : "HH:mm:ss", true);
1493 case TIME_FORMAT_H:
1494 return GetAsLocalizedTime("h", false);
1495 case TIME_FORMAT_M:
1496 return GetAsLocalizedTime("m", false);
1497 case TIME_FORMAT_H_MM_SS:
1498 return GetAsLocalizedTime("h:mm:ss", true);
1499 case TIME_FORMAT_H_MM_SS_XX:
1500 return GetAsLocalizedTime("h:mm:ss xx", true);
1501 case TIME_FORMAT_XX:
1502 return use12hourclock ? GetAsLocalizedTime("xx", false) : "";
1503 default:
1504 break;
1506 return GetAsLocalizedTime("", false);
1509 CDateTime CDateTime::GetAsUTCDateTime() const
1511 CDateTime time(m_time);
1512 time += GetTimezoneBias();
1513 return time;
1516 std::string CDateTime::GetAsRFC1123DateTime() const
1518 CDateTime time(GetAsUTCDateTime());
1520 int weekDay = time.GetDayOfWeek();
1521 if (weekDay < 0)
1522 weekDay = 0;
1523 else if (weekDay > 6)
1524 weekDay = 6;
1525 if (weekDay != time.GetDayOfWeek())
1526 CLog::Log(LOGWARNING, "Invalid day of week {} in {}", time.GetDayOfWeek(),
1527 time.GetAsDBDateTime());
1529 int month = time.GetMonth();
1530 if (month < 1)
1531 month = 1;
1532 else if (month > 12)
1533 month = 12;
1534 if (month != time.GetMonth())
1535 CLog::Log(LOGWARNING, "Invalid month {} in {}", time.GetMonth(), time.GetAsDBDateTime());
1537 return StringUtils::Format("{}, {:02} {} {:04} {:02}:{:02}:{:02} GMT", DAY_NAMES[weekDay],
1538 time.GetDay(), MONTH_NAMES[month - 1], time.GetYear(), time.GetHour(),
1539 time.GetMinute(), time.GetSecond());
1542 std::string CDateTime::GetAsW3CDate() const
1544 KODI::TIME::SystemTime st;
1545 GetAsSystemTime(st);
1547 return StringUtils::Format("{:04}-{:02}-{:02}", st.year, st.month, st.day);
1550 std::string CDateTime::GetAsW3CDateTime(bool asUtc /* = false */) const
1552 CDateTime w3cDate = *this;
1553 if (asUtc)
1554 w3cDate = GetAsUTCDateTime();
1555 KODI::TIME::SystemTime st;
1556 w3cDate.GetAsSystemTime(st);
1558 std::string result = StringUtils::Format("{:04}-{:02}-{:02}T{:02}:{:02}:{:02}", st.year, st.month,
1559 st.day, st.hour, st.minute, st.second);
1560 if (asUtc)
1561 return result + "Z";
1563 CDateTimeSpan bias = GetTimezoneBias();
1564 return result + StringUtils::Format("{}{:02}:{:02}", (bias.GetSecondsTotal() >= 0 ? '+' : '-'),
1565 abs(bias.GetHours()), abs(bias.GetMinutes()));
1568 int CDateTime::MonthStringToMonthNum(const std::string& month)
1570 const char* months[] = {"january","february","march","april","may","june","july","august","september","october","november","december"};
1571 const char* abr_months[] = {"jan", "feb", "mar", "apr", "may", "jun", "jul", "aug", "sep", "oct", "nov", "dec"};
1573 int i = 0;
1574 for (; i < 12 && !StringUtils::EqualsNoCase(month, months[i]) && !StringUtils::EqualsNoCase(month, abr_months[i]); i++);
1575 i++;
1577 return i;