Skip to content

Commit 8da2323

Browse files
fix(math): respect locale for full slash dates (#738)
1 parent c83bc25 commit 8da2323

8 files changed

Lines changed: 199 additions & 34 deletions

File tree

src/renderer/composables/__tests__/useMathEngine.math.test.ts

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,10 @@ describe('arithmetic', () => {
1919
it('subtraction', () => expectValue('20 - 3', '17'))
2020
it('multiplication', () => expectValue('4 * 5', '20'))
2121
it('division', () => expectValue('100 / 4', '25'))
22+
it('prefers division over ambiguous slash dates', () => {
23+
expectNumericClose('4/3', 4 / 3, 4)
24+
expectNumericClose('22/11', 2, 4)
25+
})
2226
it('parentheses', () => expectValue('(2 + 3) * 4', '20'))
2327
it('exponent', () => expectValue('2 ^ 10', '1,024'))
2428
it('negative numbers', () => expectValue('-5 + 3', '-2'))
@@ -33,6 +37,32 @@ describe('arithmetic', () => {
3337
})
3438
})
3539

40+
describe('locale-aware full slash dates', () => {
41+
it('uses mdy parsing for en-US', () => {
42+
setFormatSettings('en-US', 6, 'numeric')
43+
44+
const result = evalLine('11/22/2005')
45+
expect(result.type).toBe('date')
46+
expect(result.value).toContain(
47+
new Date(2005, 10, 22).toLocaleDateString('en-US'),
48+
)
49+
50+
expectNumericClose('22/11/2005', 22 / 11 / 2005, 6)
51+
})
52+
53+
it('uses dmy parsing for en-GB', () => {
54+
setFormatSettings('en-GB', 6, 'numeric')
55+
56+
const result = evalLine('22/11/2005')
57+
expect(result.type).toBe('date')
58+
expect(result.value).toContain(
59+
new Date(2005, 10, 22).toLocaleDateString('en-GB'),
60+
)
61+
62+
expectNumericClose('11/22/2005', 11 / 22 / 2005, 6)
63+
})
64+
})
65+
3666
describe('rounding', () => {
3767
it('to 2 dp', () => expectValue('1/3 to 2 dp', '0.33'))
3868
it('to 5 digits', () => expectValue('pi to 5 digits', '3.14159'))

src/renderer/composables/__tests__/useMathEngine.timezones.test.ts

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -103,6 +103,14 @@ describe('time zones', () => {
103103
expectDateWithYear('2026-03-06 2:30 pm PST in Berlin', '2026')
104104
})
105105

106+
it('uses locale-aware full slash date in zoned expressions', () => {
107+
setFormatSettings('en-GB', 6, 'numeric')
108+
109+
const result = evalLine('22/11/2005 PST in Berlin')
110+
expect(result.type).toBe('date')
111+
expect(result.value).toContain('2005')
112+
})
113+
106114
it('Mar 6 2026 PST in Berlin', () => {
107115
expectDateWithYear('Mar 6 2026 PST in Berlin', '2026')
108116
})

src/renderer/composables/math-notebook/math-engine/evaluators/dateArithmetic.ts

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@ import { splitTopLevelAddSub } from '../utils'
88
interface DateArithmeticDeps {
99
mathEvaluate: (expression: string, scope: Record<string, any>) => any
1010
formatResult: (result: any) => any
11+
locale: string
1112
}
1213

1314
function evaluateDateLikeExpression(
@@ -16,14 +17,15 @@ function evaluateDateLikeExpression(
1617
scope: Record<string, any>,
1718
deps: DateArithmeticDeps,
1819
) {
19-
const timeZoneResult = evaluateTimeZoneLine(expression, now)
20+
const timeZoneResult = evaluateTimeZoneLine(expression, now, deps.locale)
2021
if (timeZoneResult?.rawResult instanceof Date) {
2122
return timeZoneResult.rawResult
2223
}
2324

2425
const localTemporalResult = parseExplicitLocalTemporalExpression(
2526
expression,
2627
now,
28+
deps.locale,
2729
)
2830
if (localTemporalResult) {
2931
return localTemporalResult.date

src/renderer/composables/math-notebook/math-engine/pipeline/evaluate.ts

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -201,11 +201,13 @@ export function evaluateClassifiedLine(
201201
mathEvaluate: (expression: string, nextScope: Record<string, any>) =>
202202
math.evaluate(expression, nextScope),
203203
formatResult,
204+
locale: activeLocale,
204205
}
205206
return evaluateMathPath(
206207
trimmed,
207208
processed,
208209
currentDate,
210+
activeLocale,
209211
scope,
210212
math,
211213
mathDeps,

src/renderer/composables/math-notebook/math-engine/pipeline/evaluateMath.ts

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@ export function evaluateMathPath(
1515
trimmed: string,
1616
processed: string,
1717
currentDate: Date,
18+
locale: string,
1819
scope: Record<string, any>,
1920
math: MathEvaluatorInstance,
2021
mathDeps: MathDeps,
@@ -50,6 +51,7 @@ export function evaluateMathPath(
5051
const localTemporalResult = parseExplicitLocalTemporalExpression(
5152
processed,
5253
currentDate,
54+
locale,
5355
)
5456
if (localTemporalResult) {
5557
return toEvaluatedLine(

src/renderer/composables/math-notebook/math-engine/pipeline/evaluateRoutes.ts

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -134,6 +134,7 @@ export function evaluatePrimaryIntent(
134134
mathEvaluate: (expression, nextScope) =>
135135
math.evaluate(expression, nextScope),
136136
formatResult,
137+
locale: activeLocale,
137138
},
138139
)
139140

@@ -157,6 +158,7 @@ export function evaluatePrimaryIntent(
157158
mathEvaluate: (expression, nextScope) =>
158159
math.evaluate(expression, nextScope),
159160
formatResult,
161+
locale: activeLocale,
160162
},
161163
)
162164

src/renderer/composables/math-notebook/math-engine/pipeline/evaluateTypes.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -41,4 +41,5 @@ export interface EvaluatedLine {
4141
export interface MathDeps {
4242
mathEvaluate: (expression: string, scope: Record<string, any>) => any
4343
formatResult: LineFormatter['formatResult']
44+
locale: string
4445
}

0 commit comments

Comments
 (0)