diff --git a/chrono/datetime.cpp b/chrono/datetime.cpp index 23f2981..d76d865 100644 --- a/chrono/datetime.cpp +++ b/chrono/datetime.cpp @@ -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) { @@ -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. */ diff --git a/chrono/datetime.h b/chrono/datetime.h index 08a620f..02a7ced 100644 --- a/chrono/datetime.h +++ b/chrono/datetime.h @@ -61,7 +61,8 @@ public: 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 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; int year() const; diff --git a/chrono/timespan.cpp b/chrono/timespan.cpp index 59eb4f8..d1931c6 100644 --- a/chrono/timespan.cpp +++ b/chrono/timespan.cpp @@ -34,12 +34,14 @@ TimeSpan TimeSpan::fromString(const string &str, char separator) string::size_type start = 0; string::size_type end = str.find(separator, start); while(true) { - parts.push_back(stringToNumber(str.substr(start, end))); - if(end == string::npos) + parts.push_back(stringToNumber(str.substr(start, end - start))); + if(end == string::npos) { break; + } start = end + 1; - if(start >= str.size()) + if(start >= str.size()) { break; + } end = str.find(separator, start); } switch(parts.size()) {