added DateTime::fromTimeStampGmt()

This commit is contained in:
Martchus 2016-01-27 00:14:20 +01:00
parent e4910171e8
commit 3c9f95059e
3 changed files with 22 additions and 5 deletions

View File

@ -44,7 +44,7 @@ inline bool inRangeExclMax(num1 val, num2 min, num3 max)
*/ */
/*! /*!
* \brief Constructs a new DateTime object from the specified \a timeStamp. * \brief Constructs a new DateTime object with the local time from the specified \a timeStamp.
*/ */
DateTime DateTime::fromTimeStamp(time_t timeStamp) DateTime DateTime::fromTimeStamp(time_t timeStamp)
{ {
@ -57,6 +57,20 @@ DateTime DateTime::fromTimeStamp(time_t timeStamp)
} }
} }
/*!
* \brief Constructs a new DateTime object with the GMT time from the specified \a timeStamp.
*/
DateTime DateTime::fromTimeStampGmt(time_t timeStamp)
{
if(timeStamp) {
struct tm *timeinfo = gmtime(&timeStamp);
return DateTime::fromDateAndTime(timeinfo->tm_year + 1900, timeinfo->tm_mon + 1, timeinfo->tm_mday,
timeinfo->tm_hour, timeinfo->tm_min, timeinfo->tm_sec < 60 ? timeinfo->tm_sec : 59, 0);
} else {
return DateTime();
}
}
/*! /*!
* \brief Parses the given std::string \a str as DateTime. * \brief Parses the given std::string \a str as DateTime.
*/ */

View File

@ -61,7 +61,8 @@ public:
static DateTime fromTime(int hour = 0, int minute = 0, int second = 0, double millisecond = 0.0); static DateTime fromTime(int hour = 0, int minute = 0, int second = 0, double millisecond = 0.0);
static DateTime fromDateAndTime(int year = 1, int month = 1, int day = 1, int hour = 0, int minute = 0, int second = 0, double millisecond = 0.0); static DateTime fromDateAndTime(int year = 1, int month = 1, int day = 1, int hour = 0, int minute = 0, int second = 0, double millisecond = 0.0);
static DateTime fromString(const std::string &str); static DateTime fromString(const std::string &str);
static DateTime fromTimeStamp(time_t timestamp); static DateTime fromTimeStamp(time_t timeStamp);
static DateTime fromTimeStampGmt(time_t timeStamp);
constexpr uint64 totalTicks() const; constexpr uint64 totalTicks() const;
int year() const; int year() const;

View File

@ -34,12 +34,14 @@ TimeSpan TimeSpan::fromString(const string &str, char separator)
string::size_type start = 0; string::size_type start = 0;
string::size_type end = str.find(separator, start); string::size_type end = str.find(separator, start);
while(true) { while(true) {
parts.push_back(stringToNumber<double>(str.substr(start, end))); parts.push_back(stringToNumber<double>(str.substr(start, end - start)));
if(end == string::npos) if(end == string::npos) {
break; break;
}
start = end + 1; start = end + 1;
if(start >= str.size()) if(start >= str.size()) {
break; break;
}
end = str.find(separator, start); end = str.find(separator, start);
} }
switch(parts.size()) { switch(parts.size()) {