Skip to content

Commit a53f233

Browse files
committed
#459461 - Fixed bug related with dates in evaluation time policy
1 parent 1e42b6d commit a53f233

File tree

3 files changed

+46
-4
lines changed

3 files changed

+46
-4
lines changed

src/app/pages/policies/policy-editor/editor/recipes/timespan-restriction-dialog/timespan-restriction-expression.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,9 @@
11

2-
import {addDays} from 'date-fns';
32
import {policyLeftExpressions} from '../../../../../../shared/models/policy/policy-left-expressions';
43
import {constraint, multi} from '../../../../../../shared/models/policy/ui-policy-expression-utils';
54
import { UiPolicyExpression } from '../../../../../../shared/models/policy/ui-policy-expression';
65
import { OperatorDto } from '../../../../../../shared/models/policy/ui-policy-constraint';
6+
import { adjustDate } from '../../../../../../shared/utils/date-utils';
77

88
export const buildTimespanRestriction = (
99
firstDay: Date,
@@ -13,7 +13,7 @@ export const buildTimespanRestriction = (
1313
constraint(
1414
policyLeftExpressions.policyEvaluationTime,
1515
operator,
16-
value.toISOString(),
16+
adjustDate(value, operator),
1717
);
1818

1919
return multi(

src/app/shared/models/policy/policy-form-adapter.ts

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@ import {PolicyOperatorConfig} from './policy-operators';
44
import { UiPolicyConstraint, UiPolicyLiteral } from './ui-policy-constraint';
55
import { filterNonNull } from '../../utils/array-utils';
66
import { jsonValidator } from 'src/app/shared/validators/json-validator';
7+
import { adjustDate, convertUTCToLocalDate } from 'src/app/shared/utils/date-utils';
78

89
export interface PolicyFormAdapter<T> {
910
displayText: (value: UiPolicyLiteral) => string | null;
@@ -55,7 +56,7 @@ export const localDateAdapter: PolicyFormAdapter<Date | null> = {
5556
if (!value) {
5657
return value;
5758
}
58-
return format(new Date(value), 'yyyy-MM-dd');
59+
return format(convertUTCToLocalDate(value), 'yyyy-MM-dd');
5960
} catch (e) {
6061
return '' + value;
6162
}
@@ -72,7 +73,7 @@ export const localDateAdapter: PolicyFormAdapter<Date | null> = {
7273
return null;
7374
}
7475
},
75-
buildValueFn: (value) => stringLiteral(value?.toISOString()),
76+
buildValueFn: (value, operator) => stringLiteral(adjustDate(value, operator.id)),
7677
emptyConstraintValue: () => ({
7778
operator: 'LT',
7879
right: {

src/app/shared/utils/date-utils.ts

Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,41 @@
1+
import { OperatorDto } from "../models/policy/ui-policy-constraint";
2+
3+
export function adjustDate(date: Date, operator: OperatorDto): string | null {
4+
if (!date) {
5+
return null;
6+
}
7+
8+
if(operator === 'LEQ' || operator === 'LT') {
9+
const adjustedDate = new Date(Date.UTC(
10+
date.getFullYear(),
11+
date.getMonth(),
12+
date.getDate(),
13+
23, 59, 59, 999
14+
));
15+
16+
return adjustedDate.toISOString();
17+
} else {
18+
const adjustedDate = new Date(Date.UTC(
19+
date.getFullYear(),
20+
date.getMonth(),
21+
date.getDate(),
22+
0, 0, 0, 0
23+
));
24+
25+
return adjustedDate.toISOString();
26+
}
27+
28+
}
29+
export function convertUTCToLocalDate(utcISOString: string): Date {
30+
const utcDate = new Date(utcISOString);
31+
32+
return new Date(
33+
utcDate.getUTCFullYear(),
34+
utcDate.getUTCMonth(),
35+
utcDate.getUTCDate(),
36+
utcDate.getUTCHours(),
37+
utcDate.getUTCMinutes(),
38+
utcDate.getUTCSeconds(),
39+
utcDate.getUTCMilliseconds()
40+
);
41+
}

0 commit comments

Comments
 (0)