adamgibbons / ics

iCalendar (ics) file generator for node.js

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

Breaking Update in v2.29.0 - Format of Dates

SudharakaP opened this issue · comments

It seems that version 2.29.0 had a breaking update. Previously the createEvent function used to take moment objects as dates, but starting from 2.29.0 it had strictly migrated to taking arrays as date formats. So for example something like,

const start = moment(this.startsAt).format('YYYY-M-D-H-m').split('-');
const end = moment(this.endsAt).format('YYYY-M-D-H-m').split('-');
...
...
const event = {
  title: this.name,
  description,
  start,
  end,
};
ics.createEvent(event);

worked before, but now we had to change it to,

const start = moment(this.startsAt).format('YYYY-M-D-H-m').split('-');
const end = moment(this.endsAt).format('YYYY-M-D-H-m').split('-');
const startDate = new Date(this.startsAt);
const endDate = new Date(this.endsAt);
const start = [
  startDate.getFullYear(),
  startDate.getMonth(),
  startDate.getDate(),
  startDate.getHours(),
  startDate.getMinutes(),
];
const end = [
  endDate.getFullYear(),
  endDate.getMonth(),
  endDate.getDate(),
  endDate.getHours(),
  endDate.getMinutes(),
];
...
...
const event = {
  title: this.name,
  description,
  start,
  end,
};
ics.createEvent(event);

Otherwise the createEvent function will error out with,

AssertionError: the given combination of arguments (undefined and string) is invalid for this assertion. You can use an array, a map, an object, a set, a string, or a weakset instead of a string

The change we had to do while updating is show in the following PR; https://github.com/opencollective/opencollective-api/pull/6266/files

I would suggest maybe documenting this change somewhere so that people can migrate easily.

Thanks for reporting this - will look into it asap.

@SudharakaP thanks for reporting this as I was not able to figure out why my calendar events stopped working until now. However, while there is an issue for that release, the issue is not with ics no longer accepting moment objects and requiring arrays (the moment.format.split output an array anyways). Instead, the issue is that the moment.format.split outputs an array of strings whereas the javascript Date.getDate outputs a number.
So your solution is the right fix, just not the right reasoning.

Also, you'll want to use the UTC javascript Date functions (i.e. Date.getUTCFullYear, Date.getUTCMonth, Date.getUTCDate, Date.getUTCHours, Date.getUTCMinutes)

EDIT: Annoyingly, javascript Date.getMonth (and UTCMonth) index by 0, so you'll need to do Date.getUTCMonth + 1

@SudharakaP thanks for reporting this as I was not able to figure out why my calendar events stopped working until now. However, while there is an issue for that release, the issue is not with ics no longer accepting moment objects and requiring arrays (the moment.format.split output an array anyways). Instead, the issue is that the moment.format.split outputs an array of strings whereas the javascript Date.getDate outputs a number.
So your solution is the right fix, just not the right reasoning.

Also, you'll want to use the UTC javascript Date functions (i.e. Date.getUTCFullYear, Date.getUTCMonth, Date.getUTCDate, Date.getUTCHours, Date.getUTCMinutes)

EDIT: Annoyingly, javascript Date.getMonth (and UTCMonth) index by 0, so you'll need to do Date.getUTCMonth + 1

Ah, thanks much for the information. 😄