Opened 9 years ago

Last modified 4 months ago

#2801 assigned Bugs

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

Reported by: pelee@… Owned by: James E. King, III
Milestone: To Be Determined Component: date_time
Version: Boost 1.38.0 Severity: Problem
Keywords: Cc:


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_>
  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 (2)

comment:1 Changed 6 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.

comment:2 Changed 4 months ago by James E. King, III

Milestone: Boost 1.39.0To Be Determined
Owner: changed from az_sw_dude to James E. King, III
Status: newassigned
Version: Boost Release BranchBoost 1.38.0

Modify Ticket

Change Properties
Set your email in Preferences
as assigned The owner will remain James E. King, III.

Add Comment

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

Note: See TracTickets for help on using tickets.