bug-commoncpp
[Top][All Lists]
Advanced

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

time_t Time::getTime() doesn't work


From: Idar Tollefsen
Subject: time_t Time::getTime() doesn't work
Date: Thu, 22 Apr 2004 13:17:15 +0200
User-agent: Mozilla Thunderbird 0.5 (Macintosh/20040208)

Hello,

The function mentioned in the subject doesn't work. It uses mktime() to construct a time_t, but the struct tm it sends in has it's year set to 0, which would generate a time_t value greater than what time_t can hold, and mktime() returns -1 to indicate failure.

This function can never have worked, or it was implemented and tested on some platform where time_t was defined to something larger than I've seen on any platform I've tested it on. On the platforms I use, OS X and FreeBSD (4.9 on Alpha, 5.2 on i686), mktime() with a year value in the tm struct of less than 2 will fail.

I also fail to see why it's desirable to use mktime() at all since the Time class already stores it value as seconds. Conversely, I have attached a patch that fixes the problem by simply converting the internally stored seconds to time_t.


Sincerely,
Idar Tollefsen

--- src/date.cpp.orig   Sun Mar 28 23:09:14 2004
+++ src/date.cpp        Thu Apr 15 10:49:06 2004
@@ -474,18 +474,7 @@
 
 time_t Time::getTime(void) const
 {
-       char buf[7];
-       struct tm dt;
-       memset(&dt, 0, sizeof(dt));
-       fromSeconds(buf);
-       ZNumber nhour(buf, 2);
-       ZNumber nminute(buf + 2, 2);
-       ZNumber nsecond(buf + 4, 2);
-       
-       dt.tm_hour = nhour();
-       dt.tm_min = nminute();
-       dt.tm_sec = nsecond();
-       return mktime(&dt);
+    return static_cast<time_t>(seconds);
 }
 
 int Time::getHour(void) const

reply via email to

[Prev in Thread] Current Thread [Next in Thread]