Skip to content

Commit

Permalink
🐛 Fix daily start time timezone issues
Browse files Browse the repository at this point in the history
  • Loading branch information
oliverbutler committed May 15, 2022
1 parent 1445b8d commit cc3a1e9
Show file tree
Hide file tree
Showing 3 changed files with 49 additions and 5 deletions.
5 changes: 3 additions & 2 deletions apps/xeo/components/Sprint/SprintCreate/SprintCreate.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ import { Button } from '@xeo/ui/lib/Button/Button';
import { SettingsPanel } from 'components/PageLayouts/SettingsPanel/SettingsPanel';
import { NotionSprintSelector } from './NotionSprintSelector';
import { NotionDatabase } from '@prisma/client';
import { getUTCTimeFromTime, Time } from 'utils/date/date';

export interface SprintCreateForm {
startDate: string;
Expand All @@ -22,7 +23,7 @@ export interface SprintCreateForm {
notionSprintValue: SprintSelectOption | null | undefined;
sprintGoal: string;
teamSpeed: number;
dayStartTime: string;
dayStartTime: Time;
devs: SprintCapacityDev[];
}

Expand Down Expand Up @@ -74,7 +75,7 @@ export const SprintCreate: React.FunctionComponent<Props> = ({ database }) => {
endDate: dayjs(data.endDate).toISOString(),
teamSpeed: data.teamSpeed,
notionSprintValue: data.notionSprintValue.value,
dayStartTime: data.dayStartTime,
dayStartTime: getUTCTimeFromTime(data.dayStartTime),
developers: data.devs.map((dev) => ({
name: dev.name,
capacity: dev.capacity.map((capacity) =>
Expand Down
7 changes: 4 additions & 3 deletions apps/xeo/components/Sprint/SprintEdit/SprintEdit.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@ import { SettingsPanel } from 'components/PageLayouts/SettingsPanel/SettingsPane
import { Tooltip } from 'components/Tooltip/Tooltip';
import { NotionSprintSelector } from '../SprintCreate/NotionSprintSelector';
import { SprintSelectOption } from '../SprintCreate/SprintCreate';
import { getTimeFromUTCTime, getUTCTimeFromTime, Time } from 'utils/date/date';

interface Props {
sprint: Sprint;
Expand All @@ -35,7 +36,7 @@ interface SprintEditForm {
notionSprintValue: SprintSelectOption;
sprintGoal: string;
teamSpeed: number;
dayStartTime: string;
dayStartTime: Time;
devs: SprintCapacityDev[];
}

Expand Down Expand Up @@ -67,7 +68,7 @@ export const SprintEdit: React.FunctionComponent<Props> = ({
value: sprint.notionSprintValue,
label: sprint.notionSprintValue,
},
dayStartTime: sprint.dailyStartTime,
dayStartTime: getTimeFromUTCTime(sprint.dailyStartTime as Time),
sprintName: sprint.name,
sprintGoal: sprint.sprintGoal,
teamSpeed: sprint.teamSpeed,
Expand Down Expand Up @@ -96,7 +97,7 @@ export const SprintEdit: React.FunctionComponent<Props> = ({
endDate: dayjs(data.endDate).toISOString(),
teamSpeed: data.teamSpeed,
notionSprintValue: data.notionSprintValue.value,
dayStartTime: data.dayStartTime,
dayStartTime: getUTCTimeFromTime(data.dayStartTime),
developers: data.devs.map((dev) => ({
name: dev.name,
capacity: dev.capacity
Expand Down
42 changes: 42 additions & 0 deletions apps/xeo/utils/date/date.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
import dayjs from 'dayjs';

var utc = require('dayjs/plugin/utc');
var timezone = require('dayjs/plugin/timezone');
dayjs.extend(utc);
dayjs.extend(timezone);

export type Time = string;

const isStringTime = (time: string | Time): time is Time => {
const [hours, minutes] = time.split(':');

if (Number(hours) < 0 || Number(hours) > 23) return false;
if (Number(minutes) < 0 || Number(minutes) > 59) return false;

return true;
};

export const getTimeFromUTCTime = (utcTime: Time): Time => {
if (!isStringTime(utcTime)) throw new Error('Time is not a valid string');

const day = dayjs()
.utc()
.set('hours', Number(utcTime.split(':')[0]))
.set('minutes', Number(utcTime.split(':')[1]))
// @ts-ignore
.tz(dayjs.tz.guess());

return day.format('HH:mm') as Time;
};

export const getUTCTimeFromTime = (time: Time): Time => {
if (!isStringTime(time)) throw new Error('Time is not a valid string');

const day = dayjs()
.set('hours', Number(time.split(':')[0]))
.set('minutes', Number(time.split(':')[1]))
// @ts-ignore
.tz('UTC');

return day.format('HH:mm') as Time;
};

0 comments on commit cc3a1e9

Please sign in to comment.