cal.pub0.org/packages/lib/date-fns/index.ts

66 lines
2.5 KiB
TypeScript
Raw Normal View History

2022-06-28 20:40:58 +00:00
import dayjs, { Dayjs } from "@calcom/dayjs";
Feature/booking page refactor (#3035) * Extracted UI related logic on the DatePicker, stripped out all logic * wip * fixed small regression due to merge * Fix alignment of the chevrons * Added isToday dot, added onMonthChange so we can fetch this month slots * Added includedDates to inverse excludedDates * removed trpcState * Improvements to the state * All params are now dynamic * This builds the flat map so not all paths block on every new build * Added requiresConfirmation * Correctly take into account getFilteredTimes to make the calendar function * Rewritten team availability, seems to work * Circumvent i18n flicker by showing the loader instead * 'You can remove this code. Its not being used now' - Hariom * Nailed a persistent little bug, new Date() caused the current day to flicker on and off * TS fixes * Fix some eventType details in AvailableTimes * '5 / 6 Seats Available' instead of '6 / Seats Available' * More type fixes * Removed unrelated merge artifact * Use WEBAPP_URL instead of hardcoded * Next round of TS fixes * I believe this was mistyped * Temporarily disabled rescheduling 'this is when you originally scheduled', so removed dep * Sorting some dead code * This page has a lot of red, not all related to this PR * A PR to your PR (#3067) * Cleanup * Cleanup * Uses zod to parse params * Type fixes * Fixes ISR * E2E fixes * Disabled dynamic bookings until post v1.7 * More test fixes * Fixed border position (transparent border) to prevent dot from jumping - and possibly fix spacing * Disabled style nitpicks * Delete useSlots.ts Removed early design artifact * Unlock DatePicker locale * Adds mini spinner to DatePicker Co-authored-by: Peer Richelsen <peeroke@gmail.com> Co-authored-by: kodiakhq[bot] <49736102+kodiakhq[bot]@users.noreply.github.com> Co-authored-by: zomars <zomars@me.com>
2022-06-15 20:54:31 +00:00
// converts a date to 2022-04-25 for example.
export const yyyymmdd = (date: Date | Dayjs) =>
date instanceof Date ? dayjs(date).format("YYYY-MM-DD") : date.format("YYYY-MM-DD");
Feature/booking page refactor (#3035) * Extracted UI related logic on the DatePicker, stripped out all logic * wip * fixed small regression due to merge * Fix alignment of the chevrons * Added isToday dot, added onMonthChange so we can fetch this month slots * Added includedDates to inverse excludedDates * removed trpcState * Improvements to the state * All params are now dynamic * This builds the flat map so not all paths block on every new build * Added requiresConfirmation * Correctly take into account getFilteredTimes to make the calendar function * Rewritten team availability, seems to work * Circumvent i18n flicker by showing the loader instead * 'You can remove this code. Its not being used now' - Hariom * Nailed a persistent little bug, new Date() caused the current day to flicker on and off * TS fixes * Fix some eventType details in AvailableTimes * '5 / 6 Seats Available' instead of '6 / Seats Available' * More type fixes * Removed unrelated merge artifact * Use WEBAPP_URL instead of hardcoded * Next round of TS fixes * I believe this was mistyped * Temporarily disabled rescheduling 'this is when you originally scheduled', so removed dep * Sorting some dead code * This page has a lot of red, not all related to this PR * A PR to your PR (#3067) * Cleanup * Cleanup * Uses zod to parse params * Type fixes * Fixes ISR * E2E fixes * Disabled dynamic bookings until post v1.7 * More test fixes * Fixed border position (transparent border) to prevent dot from jumping - and possibly fix spacing * Disabled style nitpicks * Delete useSlots.ts Removed early design artifact * Unlock DatePicker locale * Adds mini spinner to DatePicker Co-authored-by: Peer Richelsen <peeroke@gmail.com> Co-authored-by: kodiakhq[bot] <49736102+kodiakhq[bot]@users.noreply.github.com> Co-authored-by: zomars <zomars@me.com>
2022-06-15 20:54:31 +00:00
export const daysInMonth = (date: Date | Dayjs) =>
date instanceof Date ? dayjs(date).daysInMonth() : date.daysInMonth();
/**
* Expects timeFormat to be either 12 or 24, if null or undefined
* is passed in, we always default back to 24 hour notation.
*/
export const formatTime = (
date: string | Date | Dayjs,
timeFormat?: number | null,
timeZone?: string | null
) =>
timeZone
? dayjs(date)
.tz(timeZone)
.format(timeFormat === 12 ? "h:mma" : "HH:mm")
: dayjs(date).format(timeFormat === 12 ? "h:mma" : "HH:mm");
/**
* Sorts two timezones by their offset from GMT.
*/
export const sortByTimezone = (timezoneA: string, timezoneB: string) => {
const timezoneAGmtOffset = dayjs.utc().tz(timezoneA).utcOffset();
const timezoneBGmtOffset = dayjs.utc().tz(timezoneB).utcOffset();
if (timezoneAGmtOffset === timezoneBGmtOffset) return 0;
return timezoneAGmtOffset < timezoneBGmtOffset ? -1 : 1;
};
/**
* Verifies given time is a day before in timezoneB.
*/
export const isPreviousDayInTimezone = (time: string, timezoneA: string, timezoneB: string) => {
const timeInTimezoneA = formatTime(time, 24, timezoneA);
const timeInTimezoneB = formatTime(time, 24, timezoneB);
if (time === timeInTimezoneB) return false;
// Eg timeInTimezoneA = 12:00 and timeInTimezoneB = 23:00
const hoursTimezoneBIsLater = timeInTimezoneB.localeCompare(timeInTimezoneA) === 1;
// If it is 23:00, does timezoneA come before or after timezoneB in GMT?
const timezoneBIsEarlierTimezone = sortByTimezone(timezoneA, timezoneB) === 1;
return hoursTimezoneBIsLater && timezoneBIsEarlierTimezone;
};
/**
* Verifies given time is a day after in timezoneB.
*/
export const isNextDayInTimezone = (time: string, timezoneA: string, timezoneB: string) => {
const timeInTimezoneA = formatTime(time, 24, timezoneA);
const timeInTimezoneB = formatTime(time, 24, timezoneB);
if (time === timeInTimezoneB) return false;
// Eg timeInTimezoneA = 12:00 and timeInTimezoneB = 09:00
const hoursTimezoneBIsEarlier = timeInTimezoneB.localeCompare(timeInTimezoneA) === -1;
// If it is 09:00, does timezoneA come before or after timezoneB in GMT?
const timezoneBIsLaterTimezone = sortByTimezone(timezoneA, timezoneB) === -1;
return hoursTimezoneBIsEarlier && timezoneBIsLaterTimezone;
};