fix: Update business hour calculation (#4496)

This commit is contained in:
Pranav Raj S 2022-04-18 18:15:20 +05:30 committed by GitHub
parent e010f0c6f0
commit 17fb6b8d55
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
7 changed files with 125 additions and 46 deletions

View file

@ -2,7 +2,6 @@ import fromUnixTime from 'date-fns/fromUnixTime';
import format from 'date-fns/format';
import isToday from 'date-fns/isToday';
import isYesterday from 'date-fns/isYesterday';
import parseISO from 'date-fns/parseISO';
export const formatUnixDate = (date, dateFormat = 'MMM dd, yyyy') => {
const unixDate = fromUnixTime(date);
@ -20,10 +19,14 @@ export const formatDigitToString = val => {
return val > 9 ? `${val}` : `0${val}`;
};
export const buildDateFromTime = (hr, min, utcOffset, date = new Date()) => {
const today = format(date, 'yyyy-MM-dd');
const hour = formatDigitToString(hr);
const minute = formatDigitToString(min);
const timeString = `${today}T${hour}:${minute}:00${utcOffset}`;
return parseISO(timeString);
export const isTimeAfter = (h1, m1, h2, m2) => {
if (h1 < h2) {
return false;
}
if (h1 === h2) {
return m1 >= m2;
}
return true;
};

View file

@ -1,8 +1,8 @@
import {
formatDate,
formatUnixDate,
buildDateFromTime,
formatDigitToString,
isTimeAfter,
} from '../DateHelper';
describe('#DateHelper', () => {
@ -44,27 +44,21 @@ describe('#DateHelper', () => {
})
).toEqual('Yesterday');
});
describe('#buildDate', () => {
it('returns correctly parsed date', () => {
const date = new Date();
date.setFullYear(2021);
date.setMonth(2);
date.setDate(5);
const result = buildDateFromTime(12, 15, '.465Z', date);
expect(result + '').toEqual(
'Fri Mar 05 2021 12:15:00 GMT+0000 (Coordinated Universal Time)'
);
});
});
describe('#formatDigitToString', () => {
it('returns date compatabile string from number is less than 9', () => {
expect(formatDigitToString(8)).toEqual('08');
});
describe('#formatDigitToString', () => {
it('returns date compatabile string from number is less than 9', () => {
expect(formatDigitToString(8)).toEqual('08');
});
it('returns date compatabile string from number is greater than 9', () => {
expect(formatDigitToString(11)).toEqual('11');
});
it('returns date compatabile string from number is greater than 9', () => {
expect(formatDigitToString(11)).toEqual('11');
});
});
describe('#isTimeAfter', () => {
it('return correct values', () => {
expect(isTimeAfter(5, 30, 9, 30)).toEqual(false);
expect(isTimeAfter(9, 30, 9, 30)).toEqual(true);
expect(isTimeAfter(9, 29, 9, 30)).toEqual(false);
expect(isTimeAfter(11, 59, 12, 0)).toEqual(false);
});
});