Modify

Opened 9 years ago

Last modified 5 years ago

#2801 new Bugs

gregorian_calendar_base: incorrectly assumes that sizeof(int)==sizeof(long)

Reported by: pelee@… Owned by: az_sw_dude
Milestone: Boost 1.39.0 Component: date_time
Version: Boost Release Branch Severity: Problem
Keywords: Cc:

Description

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
    
  }

Attachments (0)

Change History (1)

comment:1 Changed 5 years ago by Ben Craig <ben.craig@…>

This is causing a ton of warnings for my builds of Apache Thrift. Apache Thrift doesn't directly use date_time, but it does use thread. Boost 1.50's thread library pulls in date_time.

I am also affected by #2818 and #2819.

Modify Ticket

Change Properties
Set your email in Preferences
Action
as new The owner will remain az_sw_dude.

Add Comment


E-mail address and name can be saved in the Preferences.

 
Note: See TracTickets for help on using tickets.