Opened 13 years ago

Closed 13 years ago

#3470 closed Bugs (fixed)

Bug in boost::date_time::c_time::localtime(), boost::date_time::c_time::gmtime()

Reported by: Farid Zaripov <faridz@…> Owned by: Andrey Semashev
Milestone: Boost 1.41.0 Component: date_time
Version: Boost 1.40.0 Severity: Problem
Keywords: gmtime, localtime Cc:

Description

The code below fails on asserts on MSVC 8

    const std::time_t tt = std::time(0);
    std::tm tms;
    tms.tm_hour = 28;
    const std::tm * ptm = boost::date_time::c_time::localtime(&tt, &tms);
    assert(&tms == ptm);
    assert(28 != tms.tm_hour);

The proposed patch:

Index: c_time.hpp
===================================================================
--- c_time.hpp	(revision 56335)
+++ c_time.hpp	(working copy)
@@ -80,18 +80,20 @@
       inline
       static std::tm* localtime(const std::time_t* t, std::tm* result)
       {
-        result = std::localtime(t);
-        if (!result)
+        std::tm* const tmp = std::localtime(t);
+        if (!tmp)
           boost::throw_exception(std::runtime_error("could not convert calendar time to local time"));
+        *result = *tmp;
         return result;
       }
       //! requires a pointer to a user created std::tm struct
       inline
       static std::tm* gmtime(const std::time_t* t, std::tm* result)
       {
-        result = std::gmtime(t);
-        if (!result)
+        std::tm* const tmp = std::gmtime(t);
+        if (!tmp)
           boost::throw_exception(std::runtime_error("could not convert calendar time to UTC time"));
+        *result = *tmp;
         return result;
       }
 #if (defined(_MSC_VER) && (_MSC_VER >= 1400))

Change History (3)

comment:1 by Andrey Semashev, 13 years ago

Severity: ShowstopperProblem

Hmm... According to the docs the functions should return the pointer to tm passed as an argument, so technically this is a bug. However, I don't see a good rationale for this requirement as it only introduces unnecessary data copying. Also, this code is there for ages and no one has brought it up. Maybe we're better off correcting the docs. And a new, more C++-style interface might also be a good idea.

Do you have a rationale for this change? A motivating example, perhaps?

comment:2 by Andrey Semashev, 13 years ago

(In [56548]) Refs #3470. Modified documentation for c_time functions to reflect the actual behavior.

comment:3 by Andrey Semashev, 13 years ago

Resolution: fixed
Status: newclosed

(In [56549]) Fixes #3470. Merged from trunk revision 56548.

Note: See TracTickets for help on using tickets.