bryntum / support

An issues-only repository for the Bryntum project management component suite which includes powerful Grid, Scheduler, Calendar, Kanban Task Board and Gantt chart components all built in pure JS / CSS / TypeScript
https://www.bryntum.com
54 stars 6 forks source link

Calendar model incorrect duration calculation #8975

Closed marciogurka closed 6 months ago

marciogurka commented 6 months ago

Forum post

"Hey Team,

We just figured that the duration calculation of calendars is incorrect for large timespans. We used this example https://bryntum.com/products/schedulerpro/examples/event-non-working-time/ with the attached code. If you open the dev console you will see that the duration for the two timeranges (01.01.2024 - 30.12.2035, 01.01.2024 - 30.12.2045) is the same (112579200000 ms).

The function is crucial to our business logic so this is a major flaw for us currently.

Thanks in advance."

Go to https://bryntum.com/products/schedulerpro/examples/event-non-working-time/ and use the following code

const scheduler = new SchedulerPro({
  // A Project holds the data and the calculation engine for Scheduler
  //Pro. It also acts as a CrudManager, allowing
  // loading data into all stores at once
  project: {
    autoLoad: true,
    loadUrl: "./data/data.json",
    resourceStore: {
      fields: ["rating"],
    },
  },
  appendTo: "container",
  startDate: "2022-09-04",
  endDate: "2022-09-25",
  barMargin: 10,
  resourceMargin: 20,
  rowHeight: 90,
  eventStyle: null,
  eventColor: null,
  resourceImagePath: "../_shared/images/users/",
  // Custom viewPreset (based on 'hourAndDay') that displays a
  // compact 24 hour bottom header
  viewPreset: "weekAndDayLetter",
  features: {
    // Not using the dependencies feature or nonWorkingTime
    // feature
    dependencies: false,
    nonWorkingTime: false,
    // Using event non-working time feature
    eventNonWorkingTime: true,
    // Custom event tooltip, showing events calendar (if any)
    eventTooltip: {
      template({ eventRecord }) {
        return eventRecord.calendar?.name
          ? `Uses "$
{StringHelper.encodeHtml(eventRecord.calendar.name)}" calendar`
          : "Uses project calendar";
      },
    },
  },
  columns: [
    // Column that displays a thumb for the resource
    {
      type: "resourceInfo",
      text: "Consultant",
      width: 190,
    },
    {
      type: "rating",
      text: "Rating",
      field: "rating",
      width: 180,
    },
  ],
  eventRenderer({ eventRecord, renderData }) {
    renderData.cls +=
      ["cyan", "green", "purple", "apricot"][eventRecord.id % 4] ?? "cyan";
    if (eventRecord.calendar?.id === "no-monday") {
      renderData.iconCls = "b-fa b-fa-calendar-day";
    }
    if (eventRecord.calendar?.id === "tue-thu") {
      renderData.iconCls = "b-fa b-fa-calendar-week";
    }
    return StringHelper.encodeHtml(eventRecord.name);
  },
});
const calendar = {
  id: 1,
  name: "My Calendar",
  intervals: [
    {
      isWorking: true,
      recurrentEndDate: "on Tue at 00:00",
      recurrentStartDate: "on Mon at 00:00",
    },
    {
      isWorking: true,
      recurrentEndDate: "on Wed at 00:00",
      recurrentStartDate: "on Tue at 00:00",
    },
    {
      isWorking: true,
      recurrentEndDate: "on Thu at 00:00",
      recurrentStartDate: "on Wed at 00:00",
    },
    {
      isWorking: true,
      recurrentEndDate: "on Fri at 00:00",
      recurrentStartDate: "on Thu at 00:00",
    },
    {
      isWorking: true,
      recurrentEndDate: "on Sat at 00:00",
      recurrentStartDate: "on Fri at 00:00",
    },
  ],
  unspecifiedTimeIsWorking: false,
};
scheduler.project.calendarManagerStore.add(calendar);
const calendarModel = scheduler.project.calendarManagerStore.getById(1);
const startDate = new Date(2024, 1, 1);
const endDate = new Date(2035, 12, 30);
console.log(calendarModel.calculateDurationMs(startDate, endDate));
const startDate2 = new Date(2024, 1, 1);
const endDate2 = new Date(2045, 12, 30);
console.log(calendarModel.calculateDurationMs(startDate2, endDate2));

the duration in ms in both console logs are the same, even with 10 years difference.

Screenshot 2024-04-09 at 13 05 33
arcady-zherdev commented 6 months ago

Project has a config limiting calendars iteration timspan: https://bryntum.com/products/schedulerpro/docs/api/SchedulerPro/model/ProjectModel#config-maxCalendarRange You need to adjust it accordingly in case you deal with large timespans.