[Boost-bugs] [Boost C++ Libraries] #2801: gregorian_calendar_base: incorrectly assumes that sizeof(int)==sizeof(long)

Subject: [Boost-bugs] [Boost C++ Libraries] #2801: gregorian_calendar_base: incorrectly assumes that sizeof(int)==sizeof(long)
From: Boost C++ Libraries (noreply_at_[hidden])
Date: 2009-02-25 20:54:04


#2801: gregorian_calendar_base: incorrectly assumes that sizeof(int)==sizeof(long)
----------------------------------+-----------------------------------------
 Reporter: pelee_at_[hidden] | Owner: az_sw_dude
     Type: Bugs | Status: new
Milestone: Boost 1.39.0 | Component: date_time
  Version: Boost Release Branch | Severity: Problem
 Keywords: |
----------------------------------+-----------------------------------------
 boost::date_time::gregorian_calendar_base() returns an int, but uses
 several local variables of type unsigned long, leading to warnings about
 possible loss of data when converting from unsigned long to int on 64 bit
 OSes that use the LP64 data model.

 Here is a rewrite of the function that generates no warnings when compiled
 with -Wshorten-64-to-32 under GCC 4.0.1 on Mac OS 10.5:

 {{{
   template<typename ymd_type_, typename date_int_type_>
   BOOST_DATE_TIME_INLINE
   int
   gregorian_calendar_base<ymd_type_,date_int_type_>::week_number(const
 ymd_type& ymd) {
     date_int_type_ julianbegin =
 julian_day_number(ymd_type(ymd.year,1,1));
     date_int_type_ juliantoday = julian_day_number(ymd);
     date_int_type_ day = (julianbegin + 3) % 7;
     date_int_type_ week = (juliantoday + day - julianbegin + 4)/7;

     if ((week >= 1) && (week <= 52)) {
       return static_cast<int>(week);
     }

     if ((week == 53)) {
       if((day==6) ||(day == 5 && is_leap_year(ymd.year))) {
         return static_cast<int>(week); //under these circumstances week ==
 53.
       } else {
         return 1; //monday - wednesday is in week 1 of next year
       }
     }
     //if the week is not in current year recalculate using the previous
 year as the beginning year
     else if (week == 0) {
       julianbegin = julian_day_number(ymd_type(static_cast<unsigned
 short>(ymd.year-1),1,1));
       juliantoday = julian_day_number(ymd);
       day = (julianbegin + 3) % 7;
       week = (juliantoday + day - julianbegin + 4)/7;
       return static_cast<int>(week);
     }

     return static_cast<int>(week); //not reachable -- well except if day
 == 5 and is_leap_year != true

   }

 }}}

-- 
Ticket URL: <https://svn.boost.org/trac/boost/ticket/2801>
Boost C++ Libraries <http://www.boost.org/>
Boost provides free peer-reviewed portable C++ source libraries.

This archive was generated by hypermail 2.1.7 : 2017-02-16 18:49:59 UTC