mirror of
https://github.com/pocoproject/poco.git
synced 2025-10-29 04:17:55 +01:00
add struct tm support to DateTime #2365
This commit is contained in:
@@ -23,6 +23,9 @@
|
||||
#include "Poco/Timespan.h"
|
||||
|
||||
|
||||
struct tm;
|
||||
|
||||
|
||||
namespace Poco {
|
||||
|
||||
|
||||
@@ -90,6 +93,9 @@ public:
|
||||
DateTime();
|
||||
/// Creates a DateTime for the current date and time.
|
||||
|
||||
DateTime(const tm& tmStruct);
|
||||
/// Creates a DateTime from tm struct.
|
||||
|
||||
DateTime(const Timestamp& timestamp);
|
||||
/// Creates a DateTime for the date and time given in
|
||||
/// a Timestamp.
|
||||
@@ -220,6 +226,9 @@ public:
|
||||
DateTime& operator += (const Timespan& span);
|
||||
DateTime& operator -= (const Timespan& span);
|
||||
|
||||
tm makeTM() const;
|
||||
/// Converts DateTime to tm struct.
|
||||
|
||||
void makeUTC(int tzd);
|
||||
/// Converts a local time into UTC, by applying the given time zone differential.
|
||||
|
||||
|
||||
@@ -16,6 +16,7 @@
|
||||
#include "Poco/Timespan.h"
|
||||
#include <algorithm>
|
||||
#include <cmath>
|
||||
#include <ctime>
|
||||
|
||||
|
||||
namespace Poco {
|
||||
@@ -43,6 +44,27 @@ DateTime::DateTime()
|
||||
}
|
||||
|
||||
|
||||
DateTime::DateTime(const tm& tmStruct):
|
||||
_year(tmStruct.tm_year + 1900),
|
||||
_month(tmStruct.tm_mon + 1),
|
||||
_day(tmStruct.tm_mday),
|
||||
_hour(tmStruct.tm_hour),
|
||||
_minute(tmStruct.tm_min),
|
||||
_second(tmStruct.tm_sec),
|
||||
_millisecond(0),
|
||||
_microsecond(0)
|
||||
{
|
||||
poco_assert (_year >= 0 && _year <= 9999);
|
||||
poco_assert (_month >= 1 && _month <= 12);
|
||||
poco_assert (_day >= 1 && _day <= daysOfMonth(_year, _month));
|
||||
poco_assert (_hour >= 0 && _hour <= 23);
|
||||
poco_assert (_minute >= 0 && _minute <= 59);
|
||||
poco_assert (_second >= 0 && _second <= 60);
|
||||
|
||||
_utcTime = toUtcTime(toJulianDay(_year, _month, _day)) + 10*(_hour*Timespan::HOURS + _minute*Timespan::MINUTES + _second*Timespan::SECONDS);
|
||||
}
|
||||
|
||||
|
||||
DateTime::DateTime(const Timestamp& timestamp):
|
||||
_utcTime(timestamp.utcTime())
|
||||
{
|
||||
@@ -284,6 +306,28 @@ DateTime& DateTime::operator -= (const Timespan& span)
|
||||
}
|
||||
|
||||
|
||||
tm DateTime::makeTM() const
|
||||
{
|
||||
tm tmStruct;
|
||||
|
||||
tmStruct.tm_sec = _second;
|
||||
tmStruct.tm_min = _minute;
|
||||
tmStruct.tm_hour = _hour;
|
||||
tmStruct.tm_mday = _day;
|
||||
poco_assert (_month > 0);
|
||||
tmStruct.tm_mon = _month - 1;
|
||||
poco_assert (_year >= 1900);
|
||||
tmStruct.tm_year = _year - 1900;
|
||||
tmStruct.tm_wday = dayOfWeek();
|
||||
int doy = dayOfYear();
|
||||
poco_assert (_year >0);
|
||||
tmStruct.tm_yday = doy - 1;
|
||||
tmStruct.tm_isdst = -1;
|
||||
|
||||
return tmStruct;
|
||||
}
|
||||
|
||||
|
||||
void DateTime::makeUTC(int tzd)
|
||||
{
|
||||
operator -= (Timespan(((Timestamp::TimeDiff) tzd)*Timespan::SECONDS));
|
||||
|
||||
@@ -854,6 +854,23 @@ void DateTimeTest::testLeapSeconds()
|
||||
}
|
||||
|
||||
|
||||
void DateTimeTest::testTM()
|
||||
{
|
||||
time_t now;
|
||||
time(&now);
|
||||
tm* pTM = gmtime(&now);
|
||||
DateTime dt(*pTM);
|
||||
assertTrue (dt.second() == pTM->tm_sec);
|
||||
assertTrue (dt.minute() == pTM->tm_min);
|
||||
assertTrue (dt.hour() == pTM->tm_hour);
|
||||
assertTrue (dt.day() == pTM->tm_mday);
|
||||
assertTrue (dt.month() == pTM->tm_mon + 1);
|
||||
assertTrue (dt.year() == pTM->tm_year + 1900);
|
||||
assertTrue (dt.dayOfWeek() == pTM->tm_wday);
|
||||
assertTrue (dt.dayOfYear() == pTM->tm_yday + 1);
|
||||
}
|
||||
|
||||
|
||||
void DateTimeTest::setUp()
|
||||
{
|
||||
}
|
||||
@@ -886,6 +903,7 @@ CppUnit::Test* DateTimeTest::suite()
|
||||
CppUnit_addTest(pSuite, DateTimeTest, testIncrementDecrement);
|
||||
CppUnit_addTest(pSuite, DateTimeTest, testUTC);
|
||||
CppUnit_addTest(pSuite, DateTimeTest, testLeapSeconds);
|
||||
CppUnit_addTest(pSuite, DateTimeTest, testTM);
|
||||
|
||||
return pSuite;
|
||||
}
|
||||
|
||||
@@ -41,6 +41,7 @@ public:
|
||||
void testIncrementDecrement();
|
||||
void testUTC();
|
||||
void testLeapSeconds();
|
||||
void testTM();
|
||||
|
||||
void setUp();
|
||||
void tearDown();
|
||||
|
||||
Reference in New Issue
Block a user