#include "guilib/LocalizeStrings.h"
#include "utils/log.h"
#include "utils/StringUtils.h"
+#ifdef TARGET_POSIX
+#include "XTimeUtils.h"
+#include "XFileUtils.h"
+#else
+#include <Windows.h>
+#endif
#define SECONDS_PER_DAY 86400UL
#define SECONDS_PER_HOUR 3600UL
void CDateTimeSpan::SetFromTimeString(const CStdString& time) // hh:mm
{
- int hour = atoi(time.substr(0, 2).c_str());
- int minutes = atoi(time.substr(3, 2).c_str());
- SetDateTimeSpan(0,hour,minutes,0);
+ if (time.size() >= 5 && time[2] == ':')
+ {
+ int hour = atoi(time.substr(0, 2).c_str());
+ int minutes = atoi(time.substr(3, 2).c_str());
+ SetDateTimeSpan(0,hour,minutes,0);
+ }
}
int CDateTimeSpan::GetDays() const
if (pos != std::string::npos)
{
CStdString units = period.substr(pos, 3);
- if (units.CompareNoCase("wee") == 0)
+ if (StringUtils::EqualsNoCase(units, "wee"))
days *= 7;
- else if (units.CompareNoCase("mon") == 0)
+ else if (StringUtils::EqualsNoCase(units, "mon"))
days *= 31;
}
m_time.dwLowDateTime=time.u.LowPart;
}
-void CDateTime::SetFromDateString(const CStdString &date)
+bool CDateTime::SetFromDateString(const CStdString &date)
{
+ /* TODO:STRING_CLEANUP */
if (date.empty())
{
SetValid(false);
- return;
+ return false;
}
+ if (SetFromDBDate(date))
+ return true;
+
const char* months[] = {"january","february","march","april","may","june","july","august","september","october","november","december",NULL};
int j=0;
size_t iDayPos = date.find("day");
iDayPos = 0;
CStdString strMonth = date.substr(iDayPos, iPos - iDayPos);
- if (strMonth.empty()) // assume dbdate format
- {
- SetFromDBDate(date);
- return;
- }
+ if (strMonth.empty())
+ return false;
size_t iPos2 = date.find(",");
- CStdString strDay = date.substr(iPos, iPos2-iPos);
+ CStdString strDay = (date.size() >= iPos) ? date.substr(iPos, iPos2-iPos) : "";
CStdString strYear = date.substr(date.find(" ", iPos2) + 1);
while (months[j] && stricmp(strMonth.c_str(),months[j]) != 0)
j++;
if (!months[j])
- return;
+ return false;
- SetDateTime(atol(strYear.c_str()),j+1,atol(strDay.c_str()),0,0,0);
+ return SetDateTime(atol(strYear.c_str()),j+1,atol(strDay.c_str()),0,0,0);
}
int CDateTime::GetDay() const
return st.wHour*60+st.wMinute;
}
-void CDateTime::SetDateTime(int year, int month, int day, int hour, int minute, int second)
+bool CDateTime::SetDateTime(int year, int month, int day, int hour, int minute, int second)
{
SYSTEMTIME st;
ZeroMemory(&st, sizeof(SYSTEMTIME));
st.wSecond=second;
m_state = ToFileTime(st, m_time) ? valid : invalid;
+ return m_state == valid;
}
-void CDateTime::SetDate(int year, int month, int day)
+bool CDateTime::SetDate(int year, int month, int day)
{
- SetDateTime(year, month, day, 0, 0, 0);
+ return SetDateTime(year, month, day, 0, 0, 0);
}
-void CDateTime::SetTime(int hour, int minute, int second)
+bool CDateTime::SetTime(int hour, int minute, int second)
{
// 01.01.1601 00:00:00 is 0 as filetime
- SetDateTime(1601, 1, 1, hour, minute, second);
+ return SetDateTime(1601, 1, 1, hour, minute, second);
}
void CDateTime::GetAsSystemTime(SYSTEMTIME& time) const
return StringUtils::Format("%04i%02i%02i_%02i%02i%02i", st.wYear, st.wMonth, st.wDay, st.wHour, st.wMinute, st.wSecond);;
}
-void CDateTime::SetFromUTCDateTime(const CDateTime &dateTime)
+bool CDateTime::SetFromUTCDateTime(const CDateTime &dateTime)
{
CDateTime tmp(dateTime);
tmp -= GetTimezoneBias();
m_time = tmp.m_time;
m_state = tmp.m_state;
+ return m_state == valid;
}
static bool bGotTimezoneBias = false;
return timezoneBias;
}
-void CDateTime::SetFromUTCDateTime(const time_t &dateTime)
+bool CDateTime::SetFromUTCDateTime(const time_t &dateTime)
{
CDateTime tmp(dateTime);
- SetFromUTCDateTime(tmp);
+ return SetFromUTCDateTime(tmp);
}
-void CDateTime::SetFromW3CDate(const CStdString &dateTime)
+bool CDateTime::SetFromW3CDate(const CStdString &dateTime)
{
CStdString date, time, zone;
if (time.length() >= 8)
sec = atoi(time.substr(6, 2).c_str());
- SetDateTime(year, month, day, hour, min, sec);
+ return SetDateTime(year, month, day, hour, min, sec);
}
-void CDateTime::SetFromDBDateTime(const CStdString &dateTime)
+bool CDateTime::SetFromDBDateTime(const CStdString &dateTime)
{
// assumes format YYYY-MM-DD HH:MM:SS
if (dateTime.size() == 19)
int hour = atoi(dateTime.substr(11, 2).c_str());
int min = atoi(dateTime.substr(14, 2).c_str());
int sec = atoi(dateTime.substr(17, 2).c_str());
- SetDateTime(year, month, day, hour, min, sec);
+ return SetDateTime(year, month, day, hour, min, sec);
}
+ return false;
}
-void CDateTime::SetFromDBDate(const CStdString &date)
+bool CDateTime::SetFromDBDate(const CStdString &date)
{
+ if (date.size() < 10)
+ return false;
// assumes format:
// YYYY-MM-DD or DD-MM-YYYY
+ const static std::string sep_chars = "-./";
int year = 0, month = 0, day = 0;
- if (date.size() > 2 && (date[2] == '-' || date[2] == '.'))
+ if (sep_chars.find(date[2]) != std::string::npos)
{
day = atoi(date.substr(0, 2).c_str());
month = atoi(date.substr(3, 2).c_str());
year = atoi(date.substr(6, 4).c_str());
}
- else
+ else if (sep_chars.find(date[4]) != std::string::npos)
{
year = atoi(date.substr(0, 4).c_str());
month = atoi(date.substr(5, 2).c_str());
day = atoi(date.substr(8, 2).c_str());
}
- SetDate(year, month, day);
+ return SetDate(year, month, day);
}
-void CDateTime::SetFromDBTime(const CStdString &time)
+bool CDateTime::SetFromDBTime(const CStdString &time)
{
+ if (time.size() < 8)
+ return false;
// assumes format:
// HH:MM:SS
int hour, minute, second;
minute = atoi(time.substr(3, 2).c_str());
second = atoi(time.substr(6, 2).c_str());
- SetTime(hour, minute, second);
+ return SetTime(hour, minute, second);
}
-void CDateTime::SetFromRFC1123DateTime(const CStdString &dateTime)
+bool CDateTime::SetFromRFC1123DateTime(const CStdString &dateTime)
{
CStdString date = dateTime;
StringUtils::Trim(date);
if (date.size() != 29)
- return;
+ return false;
int day = strtol(date.substr(5, 2).c_str(), NULL, 10);
}
if (month < 1)
- return;
+ return false;
int year = strtol(date.substr(12, 4).c_str(), NULL, 10);
int hour = strtol(date.substr(17, 2).c_str(), NULL, 10);
int min = strtol(date.substr(20, 2).c_str(), NULL, 10);
int sec = strtol(date.substr(23, 2).c_str(), NULL, 10);
- SetDateTime(year, month, day, hour, min, sec);
+ return SetDateTime(year, month, day, hour, min, sec);
}
CStdString CDateTime::GetAsLocalizedTime(const CStdString &format, bool withSeconds) const
i=length;
}
- strPart.Replace("''", "'");
+ StringUtils::Replace(strPart, "''", "'");
strOut+=strPart;
}
strOut+=str;
}
else
- strOut.Delete(strOut.GetLength()-1,1);
+ strOut.erase(strOut.size()-1,1);
}
else if (c=='x') // add meridiem symbol
{
strPart = strFormat.substr(i + 1, length - i - 1);
i = length;
}
- strPart.Replace("''", "'");
+ StringUtils::Replace(strPart, "''", "'");
strOut+=strPart;
}
else if (c=='D' || c=='d') // parse days
// Format string with the length of the mask
CStdString str = StringUtils::Format("%d", dateTime.wYear); // four-digit number
- if (partLength<=2)
- str.Delete(0, 2); // two-digit number
+ if (partLength <= 2)
+ str.erase(0, 2); // two-digit number
strOut+=str;
}
const char* abr_months[] = {"jan", "feb", "mar", "apr", "may", "jun", "jul", "aug", "sep", "oct", "nov", "dec"};
int i = 0;
- for (; i < 12 && month.CompareNoCase(months[i]) != 0 && month.CompareNoCase(abr_months[i]) != 0; i++);
+ for (; i < 12 && !StringUtils::EqualsNoCase(month, months[i]) && !StringUtils::EqualsNoCase(month, abr_months[i]); i++);
i++;
return i;