Skip to content

Commit

Permalink
feat: Apply timezone for supported allday-events
Browse files Browse the repository at this point in the history
For allday events, the timezone is not used if defined. This works for moment-timezone objects and Luxon DateTime objects with a zone set.

Close #592
  • Loading branch information
sebbo2002 committed Mar 31, 2024
1 parent e65bbd3 commit 176352c
Show file tree
Hide file tree
Showing 2 changed files with 9 additions and 2 deletions.
10 changes: 8 additions & 2 deletions src/tools.ts
Original file line number Diff line number Diff line change
Expand Up @@ -54,13 +54,19 @@ export function formatDate (timezone: string | null, d: ICalDateTimeValue, dateo
}
else if(isMoment(d)) {
// @see https://momentjs.com/timezone/docs/#/using-timezones/parsing-in-zone/
const m = timezone ? (isMomentTZ(d) && !d.tz() ? d.clone().tz(timezone) : d) : (floating ? d : d.utc());
const m = timezone
? (isMomentTZ(d) && !d.tz() ? d.clone().tz(timezone) : d)
: (floating || (dateonly && isMomentTZ(d) && d.tz()) ? d : d.utc());

return m.format('YYYYMMDD') + (!dateonly ? (
'T' + m.format('HHmmss') + (floating || timezone ? '' : 'Z')
) : '');
}
else if(isLuxonDate(d)) {
const m = timezone ? d.setZone(timezone) : (floating ? d : d.setZone('utc'));
const m = timezone
? d.setZone(timezone)
: (floating || (dateonly && d.zone.type !== 'system') ? d : d.setZone('utc'));

return m.toFormat('yyyyLLdd') + (!dateonly ? (
'T' + m.toFormat('HHmmss') + (floating || timezone ? '' : 'Z')
) : '');
Expand Down
1 change: 1 addition & 0 deletions src/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -76,6 +76,7 @@ export interface ICalMomentDurationStub {

export interface ICalLuxonDateTimeStub {
setZone(zone?: string): ICalLuxonDateTimeStub;
zone: { type: string; };
toFormat(fmt: string): string;
toJSDate(): Date;
get isValid(): boolean;
Expand Down

0 comments on commit 176352c

Please sign in to comment.