Skip to content

Commit

Permalink
handle timezones
Browse files Browse the repository at this point in the history
  • Loading branch information
alexd-bes committed Aug 13, 2024
1 parent c320a4f commit 7ecc324
Show file tree
Hide file tree
Showing 3 changed files with 11 additions and 7 deletions.
10 changes: 6 additions & 4 deletions packages/datatrak-web-server/src/routes/TasksRoute.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
* Tupaia
* Copyright (c) 2017 - 2024 Beyond Essential Systems Pty Ltd
*/
import { sub } from 'date-fns';
import { setMilliseconds, sub } from 'date-fns';
import { Request } from 'express';
import { Route } from '@tupaia/server-boilerplate';
import { parse } from 'cookie';
Expand Down Expand Up @@ -56,14 +56,16 @@ export class TasksRoute extends Route<TasksRequest> {
filters.forEach(({ id, value }) => {
if (value === '' || value === undefined || value === null) return;
if (id === 'due_date') {
const dateObj = new Date(value);
// set the time to the end of the day to get the full range of the day, and apply milliseconds to ensure the range is inclusive
const endDateObj = setMilliseconds(new Date(value), 999);
// subtract 23 hours, 59 minutes, 59 seconds to get the start of the day. This is because the filters always send the end of the day, and we need a range to handle the values being saved in the database as unix timestamps based on the user's timezone.
const startDate = sub(dateObj, { hours: 23, minutes: 59, seconds: 59 }).getTime();
const endDate = dateObj.getTime();
const startDate = sub(endDateObj, { hours: 23, minutes: 59, seconds: 59 }).getTime();
const endDate = endDateObj.getTime();
this.filters[id] = {
comparator: 'BETWEEN',
comparisonValue: [startDate, endDate],
};

return;
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,6 @@ export const formatTaskChanges = (task: Input) => {
taskDetails.due_date = null;
} else if (dueDate) {
// apply status and due date only if not a repeating task
// set due date to end of day
const unix = new Date(dueDate).getTime();

taskDetails.due_date = unix;
Expand Down
7 changes: 5 additions & 2 deletions packages/datatrak-web/src/features/Tasks/DueDatePicker.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
* Copyright (c) 2017 - 2024 Beyond Essential Systems Pty Ltd
*/
import React, { useEffect, useState } from 'react';
import { isValid } from 'date-fns';
import { format, isValid } from 'date-fns';
import styled from 'styled-components';
import { DatePicker } from '@tupaia/ui-components';

Expand Down Expand Up @@ -70,7 +70,10 @@ export const DueDatePicker = ({
if (!newValue) return setDate('');
if (!isValid(new Date(newValue))) return setDate('');
const endOfDay = new Date(new Date(newValue).setHours(23, 59, 59, 999));
const newDate = endOfDay.toString();

// format the date to include timezone
const newDate = format(endOfDay, `yyyy-MM-dd HH:mm:ss.SSSXXX`);

setDate(newDate);
};

Expand Down

0 comments on commit 7ecc324

Please sign in to comment.