Skip to content

Commit 2cb5ce4

Browse files
committed
update timezone format from en-sg to en-us
1 parent cedc621 commit 2cb5ce4

File tree

6 files changed

+85
-4
lines changed

6 files changed

+85
-4
lines changed

packages/backend/src/apps/delay/__tests__/delay-until.test.ts

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,8 @@ import delayApp from '../index'
1111
const PAST_DATE = '2023-11-08'
1212
const VALID_DATE = '2026-12-31' // long long time later
1313
const VALID_TIME = '12:00'
14+
const VALID_DD_MMM_YYYY_SG = '01 Sept 2026' // test Sept format to convert to en-US
15+
const VALID_DD_MMM_YYYY_US = '01 Sep 2026'
1416
const DEFAULT_TIME = '00:00'
1517
const INVALID_TIME = '25:00'
1618
const INVALID_DATE = '2025-12-32'
@@ -89,6 +91,32 @@ describe('Delay until action', () => {
8991
})
9092
})
9193

94+
it('returns void and maintains Sept format in en-US', async () => {
95+
$.step.parameters = {
96+
delayUntil: VALID_DD_MMM_YYYY_US,
97+
delayUntilTime: VALID_TIME,
98+
}
99+
100+
const result = await delayUntilAction.run($)
101+
expect(result).toBeFalsy()
102+
expect(mocks.setActionItem).toBeCalledWith({
103+
raw: { delayUntil: VALID_DD_MMM_YYYY_US, delayUntilTime: VALID_TIME },
104+
})
105+
})
106+
107+
it('returns void and converts Sept to en-US from en-SG', async () => {
108+
$.step.parameters = {
109+
delayUntil: VALID_DD_MMM_YYYY_SG,
110+
delayUntilTime: VALID_TIME,
111+
}
112+
113+
const result = await delayUntilAction.run($)
114+
expect(result).toBeFalsy()
115+
expect(mocks.setActionItem).toBeCalledWith({
116+
raw: { delayUntil: VALID_DD_MMM_YYYY_US, delayUntilTime: VALID_TIME },
117+
})
118+
})
119+
92120
it('throws step error if delay until has a past timestamp', async () => {
93121
$.step.parameters = {
94122
delayUntil: PAST_DATE,

packages/backend/src/apps/delay/actions/delay-until/index.ts

Lines changed: 19 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -61,8 +61,25 @@ const action: IRawAction = {
6161
delayUntilTimeString,
6262
)
6363

64+
// check if delayUntilString is of dd MMM yyyy format, if so, force en-US to maintain consistency with FormSG
65+
let delayUntilStringUSFormatted = delayUntilString
66+
// Try parsing with en-SG first (for "Sept"), then en-US (for "Sep"), this is for the test case to work because it is not aware of the en-US locale
67+
let dateTime = DateTime.fromFormat(delayUntilString, 'dd MMM yyyy', {
68+
locale: 'en-SG',
69+
})
70+
if (!dateTime.isValid) {
71+
dateTime = DateTime.fromFormat(delayUntilString, 'dd MMM yyyy', {
72+
locale: 'en-US',
73+
})
74+
}
75+
if (dateTime.isValid) {
76+
delayUntilStringUSFormatted = dateTime.toFormat('dd MMM yyyy', {
77+
locale: 'en-US',
78+
})
79+
}
80+
6481
let dataItem = {
65-
delayUntil: delayUntilString,
82+
delayUntil: delayUntilStringUSFormatted,
6683
delayUntilTime: delayUntilTimeString,
6784
}
6885

@@ -85,7 +102,7 @@ const action: IRawAction = {
85102
if (isRetry) {
86103
const dateTimeNow = DateTime.now()
87104
dataItem = {
88-
delayUntil: dateTimeNow.toFormat('dd MMM yyyy'),
105+
delayUntil: dateTimeNow.toFormat('dd MMM yyyy', { locale: 'en-US' }), // Force en-US to maintain consistency with FormSG
89106
delayUntilTime: dateTimeNow.toFormat('HH:mm'),
90107
}
91108
} else {

packages/backend/src/apps/formatter/__tests__/date-time.format.test.ts

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -138,4 +138,27 @@ describe('convert date time', () => {
138138
raw: { result: expectedResult },
139139
})
140140
})
141+
142+
// check if dd LLL yyyy format is corrected to en-US all the time
143+
it('maintains dd LLL yyyy format in en-US', () => {
144+
$.step.parameters = {
145+
dateTimeFormat: 'formsgDateField',
146+
formatDateTimeToFormat: 'dd LLL yyyy',
147+
}
148+
spec.transformData($, '01 Sep 2024')
149+
expect(mocks.setActionItem).toBeCalledWith({
150+
raw: { result: '01 Sep 2024' },
151+
})
152+
})
153+
154+
it('converts dd LLL yyyy format from en-SG to en-US', () => {
155+
$.step.parameters = {
156+
dateTimeFormat: 'formsgDateField',
157+
formatDateTimeToFormat: 'dd LLL yyyy',
158+
}
159+
spec.transformData($, '01 Sept 2024')
160+
expect(mocks.setActionItem).toBeCalledWith({
161+
raw: { result: '01 Sep 2024' },
162+
})
163+
})
141164
})

packages/backend/src/apps/formatter/actions/date-time/transforms/convert-date-time/index.ts

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -50,9 +50,13 @@ export const spec = {
5050
const { dateTimeFormat, formatString } = getParams($)
5151
const dateTime = parseDateTime(dateTimeFormat, valueToTransform)
5252

53+
// check if format is dd LLL yyyy, if so, force en-US to maintain consistency with FormSG
5354
$.setActionItem({
5455
raw: {
55-
result: dateTime.toFormat(formatString),
56+
result:
57+
formatString === 'dd LLL yyyy'
58+
? dateTime.toFormat(formatString, { locale: 'en-US' })
59+
: dateTime.toFormat(formatString),
5660
},
5761
})
5862
} catch (error) {

packages/backend/src/apps/scheduler/__tests__/common/get-date-time-object.test.ts

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,4 +19,11 @@ describe('Test get date time object format', () => {
1919
)
2020
})
2121
})
22+
23+
it('dd MMM yyyy format should be corrected to en-US', () => {
24+
const dateTime = DateTime.local(2025, 9, 1)
25+
expect(getDateTimeObjectRepresentation(dateTime).pretty_date).toEqual(
26+
'01 Sep 2025',
27+
)
28+
})
2229
})

packages/backend/src/apps/scheduler/common/get-date-time-object.ts

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,9 @@ export default function getDateTimeObjectRepresentation(dateTime: DateTime) {
88
return {
99
...defaults,
1010
ISO_date_time: dateTime.toISO(),
11-
pretty_date: dateTime.toFormat('dd MMM yyyy'),
11+
pretty_date: dateTime.toFormat('dd MMM yyyy', {
12+
locale: 'en-US', // Force en-US to maintain consistency with FormSG
13+
}),
1214
pretty_time: dateTime.toLocaleString(DateTime.TIME_WITH_SECONDS),
1315
pretty_day_of_week: dateTime.toFormat('cccc'),
1416
day_of_week: dateTime.weekday,

0 commit comments

Comments
 (0)