added new method to construct DateTime instance from timestamp

This commit is contained in:
Martchus 2015-06-21 21:41:56 +02:00
parent 6bf736af0e
commit 8c09772802
2 changed files with 12 additions and 11 deletions

View File

@ -44,6 +44,16 @@ inline bool inRangeExclMax(num1 val, num2 min, num3 max)
* be added by leap seconds).
*/
/*!
* \brief Constructs a new DateTime object from the specified \a timeStamp.
*/
DateTime DateTime::fromTimeStamp(time_t timeStamp)
{
struct tm *timeinfo = localtime(&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);
}
/*!
* Parses the given std::string \a str as DateTime.
*/
@ -164,17 +174,7 @@ const char *DateTime::printDayOfWeek(DayOfWeek dayOfWeek, bool abbreviation)
*/
DateTime DateTime::now()
{
time_t rawtime;
struct tm *timeinfo;
time(&rawtime);
timeinfo = localtime(&rawtime);
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);
return DateTime(time(nullptr));
}
/*!

View File

@ -62,6 +62,7 @@ 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);
constexpr uint64 totalTicks() const;
int year() const;