#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.Mid(0,2).c_str());
- int minutes = atoi(time.Mid(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
{
long days = atoi(period.c_str());
// find the first non-space and non-number
- int pos = period.find_first_not_of("0123456789 ", 0);
- if (pos >= 0)
+ size_t pos = period.find_first_not_of("0123456789 ", 0);
+ if (pos != std::string::npos)
{
- CStdString units = period.Mid(pos, 3);
- if (units.CompareNoCase("wee") == 0)
+ CStdString units = period.substr(pos, 3);
+ 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;
- int iDayPos = date.Find("day");
- int iPos = date.Find(" ");
- if (iDayPos < iPos && iDayPos > -1)
+ size_t iDayPos = date.find("day");
+ size_t iPos = date.find(" ");
+ if (iDayPos < iPos && iDayPos != std::string::npos)
{
- iDayPos = iPos+1;
- iPos = date.Find(" ",iPos+1);
+ iDayPos = iPos + 1;
+ iPos = date.find(" ", iPos+1);
}
else
iDayPos = 0;
- CStdString strMonth = date.Mid(iDayPos,iPos-iDayPos);
- if (strMonth.empty()) // assume dbdate format
- {
- SetFromDBDate(date);
- return;
- }
+ CStdString strMonth = date.substr(iDayPos, iPos - iDayPos);
+ if (strMonth.empty())
+ return false;
- int iPos2 = date.Find(",");
- CStdString strDay = date.Mid(iPos,iPos2-iPos);
- CStdString strYear = date.Mid(date.Find(" ",iPos2)+1);
+ size_t iPos2 = date.find(",");
+ 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;
- int posT = dateTime.Find("T");
- if(posT >= 0)
+ size_t posT = dateTime.find("T");
+ if(posT != std::string::npos)
{
- date = dateTime.Left(posT);
+ date = dateTime.substr(0, posT);
CStdString::size_type posZ = dateTime.find_first_of("+-Z", posT);
if(posZ == CStdString::npos)
- time = dateTime.Mid(posT+1);
+ time = dateTime.substr(posT + 1);
else
{
- time = dateTime.Mid(posT+1, posZ-posT-1);
- zone = dateTime.Mid(posZ);
+ time = dateTime.substr(posT + 1, posZ - posT - 1);
+ zone = dateTime.substr(posZ);
}
}
else
int year = 0, month = 1, day = 1, hour = 0, min = 0, sec = 0;
if (date.size() >= 4)
- year = atoi(date.Mid(0,4).c_str());
+ year = atoi(date.substr(0, 4).c_str());
if (date.size() >= 10)
{
- month = atoi(date.Mid(5,2).c_str());
- day = atoi(date.Mid(8,2).c_str());
+ month = atoi(date.substr(5, 2).c_str());
+ day = atoi(date.substr(8, 2).c_str());
}
if (time.length() >= 5)
{
- hour = atoi(time.Mid(0,2).c_str());
- min = atoi(time.Mid(3,2).c_str());
+ hour = atoi(time.substr(0, 2).c_str());
+ min = atoi(time.substr(3, 2).c_str());
}
if (time.length() >= 8)
- sec = atoi(time.Mid(6,2).c_str());
+ 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 year = atoi(dateTime.Mid(0,4).c_str());
- int month = atoi(dateTime.Mid(5,2).c_str());
- int day = atoi(dateTime.Mid(8,2).c_str());
- int hour = atoi(dateTime.Mid(11,2).c_str());
- int min = atoi(dateTime.Mid(14,2).c_str());
- int sec = atoi(dateTime.Mid(17,2).c_str());
- SetDateTime(year, month, day, hour, min, sec);
+ int year = atoi(dateTime.substr(0, 4).c_str());
+ int month = atoi(dateTime.substr(5, 2).c_str());
+ int day = atoi(dateTime.substr(8, 2).c_str());
+ 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());
+ 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.Mid(0,2).c_str());
- month = atoi(date.Mid(3,2).c_str());
- year = atoi(date.Mid(6,4).c_str());
+ 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.Mid(0,4).c_str());
- month = atoi(date.Mid(5,2).c_str());
- day = atoi(date.Mid(8,2).c_str());
+ 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;
- hour = atoi(time.Mid(0,2).c_str());
- minute = atoi(time.Mid(3,2).c_str());
- second = atoi(time.Mid(6,2).c_str());
+ hour = atoi(time.substr(0, 2).c_str());
+ 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.Mid(5, 2).c_str(), NULL, 10);
+ int day = strtol(date.substr(5, 2).c_str(), NULL, 10);
- CStdString strMonth = date.Mid(8, 3);
+ CStdString strMonth = date.substr(8, 3);
int month = 0;
for (unsigned int index = 0; index < 12; index++)
{
}
if (month < 1)
- return;
+ return false;
- int year = strtol(date.Mid(12, 4).c_str(), NULL, 10);
- int hour = strtol(date.Mid(17, 2).c_str(), NULL, 10);
- int min = strtol(date.Mid(20, 2).c_str(), NULL, 10);
- int sec = strtol(date.Mid(23, 2).c_str(), NULL, 10);
+ 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
// Prefetch meridiem symbol
const CStdString& strMeridiem=g_langInfo.GetMeridiemSymbol(dateTime.wHour > 11 ? CLangInfo::MERIDIEM_SYMBOL_PM : CLangInfo::MERIDIEM_SYMBOL_AM);
- int length=strFormat.GetLength();
- for (int i=0; i<length; ++i)
+ size_t length = strFormat.size();
+ for (size_t i=0; i < length; ++i)
{
char c=strFormat[i];
if (c=='\'')
{
// To be able to display a "'" in the string,
// find the last "'" that doesn't follow a "'"
- int pos=i+1;
- while(((pos=strFormat.Find(c,pos+1))>-1 && pos<strFormat.GetLength()) && strFormat[pos+1]=='\'') {}
+ size_t pos=i + 1;
+ while(((pos = strFormat.find(c, pos + 1)) != std::string::npos &&
+ pos<strFormat.size()) && strFormat[pos+1]=='\'') {}
CStdString strPart;
- if (pos>-1)
+ if (pos != std::string::npos)
{
// Extract string between ' '
- strPart=strFormat.Mid(i+1, pos-i-1);
+ strPart=strFormat.substr(i + 1, pos - i - 1);
i=pos;
}
else
{
- strPart=strFormat.Mid(i+1, length-i-1);
+ strPart=strFormat.substr(i + 1, length - i - 1);
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
{
SYSTEMTIME dateTime;
GetAsSystemTime(dateTime);
- int length=strFormat.GetLength();
-
- for (int i=0; i<length; ++i)
+ size_t length = strFormat.size();
+ for (size_t i = 0; i < length; ++i)
{
char c=strFormat[i];
if (c=='\'')
{
// To be able to display a "'" in the string,
// find the last "'" that doesn't follow a "'"
- int pos=i+1;
- while(((pos=strFormat.Find(c,pos+1))>-1 && pos<strFormat.GetLength()) && strFormat[pos+1]=='\'') {}
+ size_t pos = i + 1;
+ while(((pos = strFormat.find(c, pos + 1)) != std::string::npos &&
+ pos < strFormat.size()) &&
+ strFormat[pos + 1] == '\'') {}
CStdString strPart;
- if (pos>-1)
+ if (pos != std::string::npos)
{
// Extract string between ' '
- strPart=strFormat.Mid(i+1, pos-i-1);
- i=pos;
+ strPart = strFormat.substr(i + 1, pos - i - 1);
+ i = pos;
}
else
{
- strPart=strFormat.Mid(i+1, length-i-1);
- i=length;
+ 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
{
- int partLength=0;
+ size_t partLength=0;
- int pos=strFormat.find_first_not_of(c,i+1);
- if (pos>-1)
+ size_t pos = strFormat.find_first_not_of(c, i+1);
+ if (pos != std::string::npos)
{
// Get length of the day mask, eg. DDDD
partLength=pos-i;
}
else if (c=='M' || c=='m') // parse month
{
- int partLength=0;
+ size_t partLength=0;
- int pos=strFormat.find_first_not_of(c,i+1);
- if (pos>-1)
+ size_t pos=strFormat.find_first_not_of(c,i+1);
+ if (pos != std::string::npos)
{
// Get length of the month mask, eg. MMMM
partLength=pos-i;
}
else if (c=='Y' || c =='y') // parse year
{
- int partLength=0;
+ size_t partLength = 0;
- int pos=strFormat.find_first_not_of(c,i+1);
- if (pos>-1)
+ size_t pos = strFormat.find_first_not_of(c,i+1);
+ if (pos != std::string::npos)
{
// Get length of the year mask, eg. YYYY
partLength=pos-i;
// 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;