Commit fe1e04c329

fe1e04c32951f60e6adf8ad452907423911caa2e

parent: e301097d2a

Verified · cmc

cmc <hello@cleberg.net> · 2026-11-20 21:25 UTC

Calc dates: abs of day 0 needs Emacs

Layout: unified · split

Sources/OrgCore/Compute/Calc.swift +5 −1
@@ -1377,7 +1377,11 @@ public enum Calc {
1377 return best 1377 return best
1378 case "abs": 1378 case "abs":
1379 let x = try one(args) 1379 let x = try one(args)
1380 if case .date(let d) = x { return .date(try compare(d, .int(0)) < 0 ? try negate(d) : d) } 1380 if case .date(let d) = x {
1381 // Calc leaves abs of day number 0 unevaluated.
1382 if d == .int(0) { throw Unsupported("date arithmetic") }
1383 return .date(try compare(d, .int(0)) < 0 ? try negate(d) : d)
1384 }
1381 return try compare(x, .int(0)) < 0 ? try negate(x) : x 1385 return try compare(x, .int(0)) < 0 ? try negate(x) : x
1382 case "floor", "ceil", "trunc": 1386 case "floor", "ceil", "trunc":
1383 if args.count == 1, case .date(let d) = args[0] { return .date(try call(name, [d])) } 1387 if args.count == 1, case .date(let d) = args[0] { return .date(try call(name, [d])) }
Tests/OrgCoreTests/CalcTests.swift +4 −1
@@ -108,6 +108,7 @@ struct CalcTests {
108 "unixtime(<2026-10-05 Mon>)", "unixtime(<2026-10-05 Mon 10:30>)", "unixtime(1791158400)", "unixtime(1.5)", 108 "unixtime(<2026-10-05 Mon>)", "unixtime(<2026-10-05 Mon 10:30>)", "unixtime(1791158400)", "unixtime(1.5)",
109 "unixtime(0)", "unixtime(-86400)", "unixtime(<1970-01-01 Thu>)", 109 "unixtime(0)", "unixtime(-86400)", "unixtime(<1970-01-01 Thu>)",
110 "abs(-<2026-10-05 Mon>)", "abs(date(-1,1,1))", "abs(-<2026-10-05 Mon 06:00>)", "abs(date(1,1,1))", 110 "abs(-<2026-10-05 Mon>)", "abs(date(-1,1,1))", "abs(-<2026-10-05 Mon 06:00>)", "abs(date(1,1,1))",
111 "abs(date(1,1,1)-1.5)", "abs(date(1,1,1)-0.25)",
111 "badd(date(2737,12,30),1)", "badd(date(1,1,3),-1)", "bsub(date(2737,12,31),date(1,1,1))", 112 "badd(date(2737,12,30),1)", "badd(date(1,1,3),-1)", "bsub(date(2737,12,31),date(1,1,1))",
112 "<0070-01-01 Thu>", "<0001-01-01 Mon>", "<0010-01-01 Fri>", "<0000-01-01 Sat>", "<0039-12-31 Sat>", "<0040-01-01 Sun>", 113 "<0070-01-01 Thu>", "<0001-01-01 Mon>", "<0010-01-01 Fri>", "<0000-01-01 Sat>", "<0039-12-31 Sat>", "<0040-01-01 Sun>",
113 "<0099-01-01 Fri>", "<0050-06-15 Thu 10:30>", "<0100-01-01 Fri>", "year(<0010-01-01 Fri>)", "unixtime(<0001-01-01 Mon>)", 114 "<0099-01-01 Fri>", "<0050-06-15 Thu 10:30>", "<0100-01-01 Fri>", "year(<0010-01-01 Fri>)", "unixtime(<0001-01-01 Mon>)",
@@ -357,7 +358,9 @@ struct CalcTests {
357 "badd(<2026-10-09 Fri>,<2026-10-09 Fri>)", "bsub(5,1)", "now(5)", "julian(<2026-10-05 Mon>,5)", "1<<2026-10-05 Mon>", 358 "badd(<2026-10-09 Fri>,<2026-10-09 Fri>)", "bsub(5,1)", "now(5)", "julian(<2026-10-05 Mon>,5)", "1<<2026-10-05 Mon>",
358 // Outside the years 1 to 2737, Calc's holiday range. 359 // Outside the years 1 to 2737, Calc's holiday range.
359 "badd(date(2737,12,31),1)", "badd(<2026-10-05 Mon>,200000)", "bsub(date(2738,1,10),date(2026,1,1))", 360 "badd(date(2737,12,31),1)", "badd(<2026-10-05 Mon>,200000)", "bsub(date(2738,1,10),date(2026,1,1))",
360 "badd(date(-100,1,3),1)", "badd(date(1,1,3),-3)", "bsub(date(1,1,10),date(-1,12,20))", "badd(date(10000,1,3),1)"] { 361 "badd(date(-100,1,3),1)", "badd(date(1,1,3),-3)", "bsub(date(1,1,10),date(-1,12,20))", "badd(date(10000,1,3),1)",
362 // Calc leaves abs of day number 0 unevaluated.
363 "abs(date(1,1,1)-1)", "abs(date(-1,12,31))"] {
361 #expect(throws: Calc.Unsupported.self, "\(e)") { try Calc.evaluate(e) } 364 #expect(throws: Calc.Unsupported.self, "\(e)") { try Calc.evaluate(e) }
362 } 365 }
363 var modes = Calc.Modes() 366 var modes = Calc.Modes()
docs/manual/guide/10-tables.org +2 −1
@@ -433,7 +433,8 @@ A date is its day number underneath, counted from 1 January of year 1, so the su
433dates is a number and =vmean= of two dates is the day number halfway between them. 433dates is a number and =vmean= of two dates is the day number halfway between them.
434=vsum=, =vmax=, =vmin=, =max=, =min=, =abs= and the comparisons work on dates; =floor=, 434=vsum=, =vmax=, =vmin=, =max=, =min=, =abs= and the comparisons work on dates; =floor=,
435=ceil=, =trunc= and =round= keep a date and drop or round its time. A timestamp with a 435=ceil=, =trunc= and =round= keep a date and drop or round its time. A timestamp with a
436time, even =00:00=, keeps showing one. 436time, even =00:00=, keeps showing one. Calc leaves =abs= of day 0 (=<-1-12-31>=)
437unevaluated, so that needs Emacs.
437 438
438| Function | Result | 439| Function | Result |
439|--------------------------------------------+------------------------------------------------------------------------------------| 440|--------------------------------------------+------------------------------------------------------------------------------------|