Skip to content

Commit b40128d

Browse files
committed
check and use formatter datetime formats
1 parent 2652a08 commit b40128d

File tree

4 files changed

+58
-34
lines changed

4 files changed

+58
-34
lines changed
Lines changed: 2 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
import { DateTime } from 'luxon'
1+
import { generateTimestampFromFormats } from '@/helpers/generate-timestamp-from-formats'
22

33
const VALID_DATETIME_FORMATS = [
44
'yyyy-MM-dd HH:mm',
@@ -8,22 +8,5 @@ const VALID_DATETIME_FORMATS = [
88

99
export default function generateTimestamp(date: string, time: string): number {
1010
const datetimeString = `${date} ${time}`
11-
// check through our accepted formats
12-
for (const datetimeFormat of VALID_DATETIME_FORMATS) {
13-
// check both en-SG and en-US because Sept accepted for SG but Sep accepted for US
14-
let datetime = DateTime.fromFormat(datetimeString, datetimeFormat, {
15-
locale: 'en-SG',
16-
})
17-
if (datetime.isValid) {
18-
return datetime.toMillis()
19-
}
20-
21-
datetime = DateTime.fromFormat(datetimeString, datetimeFormat, {
22-
locale: 'en-US',
23-
})
24-
if (datetime.isValid) {
25-
return datetime.toMillis()
26-
}
27-
}
28-
return NaN
11+
return generateTimestampFromFormats(datetimeString, VALID_DATETIME_FORMATS)
2912
}

packages/backend/src/apps/toolbox/__tests__/common/condition-is-true.test.ts

Lines changed: 10 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -94,16 +94,12 @@ describe('Condition is true', () => {
9494
expect(result).toEqual(expectedResult)
9595
})
9696

97-
/**
98-
* Typical datetime formats:
99-
* yyyy-MM-dd HH:mm
100-
* dd MMM yyyy
101-
* dd/MM/yyyy HH:mm:ss
102-
* ISO format
103-
*/
97+
// check all date formats
10498
it.each([
105-
{ text: '2024-11-06 12:00', expectedResult: true },
106-
{ text: '03/11/2024 12:00:30', expectedResult: false },
99+
{ text: '05/11/24', expectedResult: true }, // 'dd/LL/yy'
100+
{ text: '03/11/2024', expectedResult: false }, // 'dd/LL/yyyy'
101+
{ text: '05 Nov 2024', expectedResult: true }, // 'dd LLL yyyy'
102+
{ text: '03 November 2024', expectedResult: false }, // 'dd LLLL yyyy'
107103
])('supports before', ({ text, expectedResult }) => {
108104
const result = conditionIsTrue({
109105
field: '04 Nov 2024',
@@ -116,8 +112,9 @@ describe('Condition is true', () => {
116112
})
117113

118114
it.each([
119-
{ text: '2024-11-06T12:00:00.500+08:00', expectedResult: false },
120-
{ text: '03 Nov 2024 23:59', expectedResult: true },
115+
{ text: '2024/11/03', expectedResult: true }, // 'yyyy/LL/dd'
116+
{ text: '04 Nov 2024 12:01 AM', expectedResult: false }, // 'dd LLL yyyy hh:mm a'
117+
{ text: '03 Nov 2024 11:59:59 PM', expectedResult: true }, // 'dd LLL yyyy hh:mm:ss a'
121118
])('supports after', ({ text, expectedResult }) => {
122119
const result = conditionIsTrue({
123120
field: '04 Nov 2024',
@@ -173,8 +170,8 @@ describe('Condition is true', () => {
173170
it.each([
174171
{ field: 10, condition: 'gte', text: 'abc' },
175172
{ field: 'abc', condition: 'lt', text: 10 },
176-
{ field: '04 Nov 2024', condition: 'before', text: 'abc' },
177-
{ field: 'abc', condition: 'before', text: '04 Nov 2024' },
173+
{ field: '04 Sep 2024', condition: 'before', text: 'abc' },
174+
{ field: '123', condition: 'before', text: '04 Nov 2024' },
178175
])(
179176
'throws an error for invalid field or value for comparison',
180177
({ field, condition, text }) => {

packages/backend/src/apps/toolbox/common/condition-is-true.ts

Lines changed: 21 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,7 @@
11
import type { IJSONObject, IJSONValue } from '@plumber/types'
22

3+
import { generateTimestampFromFormats } from '@/helpers/generate-timestamp-from-formats'
4+
35
function compareNumbers(
46
field: IJSONValue,
57
condition: 'gte' | 'gt' | 'lte' | 'lt',
@@ -26,17 +28,34 @@ function compareNumbers(
2628
}
2729
}
2830

31+
// support only formatter date formats
32+
const VALID_DATETIME_FORMATS = [
33+
'dd/LL/yy',
34+
'dd/LL/yyyy',
35+
'dd LLL yyyy',
36+
'dd LLLL yyyy',
37+
'yyyy/LL/dd',
38+
'dd LLL yyyy hh:mm a',
39+
'dd LLL yyyy hh:mm:ss a',
40+
]
41+
2942
function compareDates(
3043
field: IJSONValue,
3144
condition: 'before' | 'after',
3245
value: IJSONValue,
3346
) {
34-
const fieldTimestamp = new Date(field as string).getTime()
47+
const fieldTimestamp = generateTimestampFromFormats(
48+
field as string,
49+
VALID_DATETIME_FORMATS,
50+
)
3551
if (isNaN(fieldTimestamp)) {
3652
throw new Error('Invalid date used in field for comparison')
3753
}
3854

39-
const valueTimestamp = new Date(value as string).getTime()
55+
const valueTimestamp = generateTimestampFromFormats(
56+
value as string,
57+
VALID_DATETIME_FORMATS,
58+
)
4059
if (isNaN(valueTimestamp)) {
4160
throw new Error('Invalid date used in value for comparison')
4261
}
Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
import { DateTime } from 'luxon'
2+
3+
export function generateTimestampFromFormats(
4+
datetimeString: string,
5+
datetimeFormats: string[],
6+
): number {
7+
// check through the list of formats
8+
for (const datetimeFormat of datetimeFormats) {
9+
// check both en-SG and en-US because Sept accepted for SG but Sep accepted for US
10+
let datetime = DateTime.fromFormat(datetimeString, datetimeFormat, {
11+
locale: 'en-SG',
12+
})
13+
if (datetime.isValid) {
14+
return datetime.toMillis()
15+
}
16+
17+
datetime = DateTime.fromFormat(datetimeString, datetimeFormat, {
18+
locale: 'en-US',
19+
})
20+
if (datetime.isValid) {
21+
return datetime.toMillis()
22+
}
23+
}
24+
return NaN
25+
}

0 commit comments

Comments
 (0)