Commit e301097d2a

e301097d2ae728efc92f748d42e0f74044a9b238

parent: 22ff42f0ad

Verified · cmc

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

Calc dates: abs of a date, years under 100, business day range

Layout: unified · split

Sources/OrgCore/Compute/Calc.swift +12 −2
@@ -788,7 +788,10 @@ public enum Calc {
788788 guard let m = text.firstMatch(of: /^([0-9]{4})-([0-9]{2})-([0-9]{2})(?: +[A-Za-z]+)?(?: +([0-9]{1,2}):([0-9]{2})(?::([0-9]{2}))?)?$/) else {
789789 throw Unsupported("date syntax")
790790 }
791 let day = Days.absolute(year: Int(m.1)!, month: Int(m.2)!, day: Int(m.3)!)
791 // `math-parse-date-validate`: a year under 100 is 19xx or 20xx.
792 var year = Int(m.1)!
793 if year < 100 { year += year < 40 ? 2000 : 1900 }
794 let day = Days.absolute(year: year, month: Int(m.2)!, day: Int(m.3)!)
792795 guard let hour = m.4.flatMap({ Int($0) }), let minute = m.5.flatMap({ Int($0) }) else { return .date(.int(day)) }
793796 return try dateTime(day, hour * 3600 + minute * 60 + (m.6.flatMap { Int($0) } ?? 0))
794797 }
@@ -848,11 +851,17 @@ public enum Calc {
848851 static func toBusinessDay(_ v: Value) throws -> (Value, holiday: Bool) {
849852 let x = try dayNumber(v)
850853 let day = try floorInt(x)
854 try checkHolidayRange(day)
851855 let weekday = Days.weekday(day)
852856 let delta = day / 7 * 2 + (weekday > 0 ? 1 : 0)
853857 return (try add(.int(day - delta), try subtract(x, .int(day))), weekday == 0 || weekday == 6)
854858 }
855859
860 /// `math-setup-holidays` with a date: Calc's holidays cover the years 1 to 2737.
861 static func checkHolidayRange(_ day: Int) throws {
862 guard (try absolute(1, 1, 1)...absolute(2737, 12, 31)).contains(day) else { throw Unsupported("arguments") }
863 }
864
856865 /// `math-from-business-day`.
857866 static func fromBusinessDay(_ n: Value) throws -> Value {
858867 var day = try floorInt(n)
@@ -869,6 +878,7 @@ public enum Calc {
869878 }
870879 weekday += 1
871880 }
881 try checkHolidayRange(day)
872882 return .date(try add(.int(day), time))
873883 }
874884
@@ -1367,7 +1377,7 @@ public enum Calc {
13671377 return best
13681378 case "abs":
13691379 let x = try one(args)
1370 if case .date = x { return x }
1380 if case .date(let d) = x { return .date(try compare(d, .int(0)) < 0 ? try negate(d) : d) }
13711381 return try compare(x, .int(0)) < 0 ? try negate(x) : x
13721382 case "floor", "ceil", "trunc":
13731383 if args.count == 1, case .date(let d) = args[0] { return .date(try call(name, [d])) }
Tests/OrgCoreTests/CalcTests.swift +8 −1
@@ -107,6 +107,10 @@ struct CalcTests {
107107 "julian(<2026-10-05 Mon>)", "julian(<2026-10-05 Mon 12:00>)", "julian(2461319)", "julian(2461319.5)", "julian(1:2)",
108108 "unixtime(<2026-10-05 Mon>)", "unixtime(<2026-10-05 Mon 10:30>)", "unixtime(1791158400)", "unixtime(1.5)",
109109 "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))",
111 "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 "<0099-01-01 Fri>", "<0050-06-15 Thu 10:30>", "<0100-01-01 Fri>", "year(<0010-01-01 Fri>)", "unixtime(<0001-01-01 Mon>)",
110114 ]
111115
112116 /// Zones without daylight saving time, as `TZ` names them for Emacs and their offsets.
@@ -350,7 +354,10 @@ struct CalcTests {
350354 "max(<2026-10-05 Mon>,1)", "if(<2026-10-05 Mon>,1,2)", "round(<2026-10-05 Mon 13:00>,1)", "date(2026,13,1)",
351355 "date(2026,2,32)", "date(2026.5,1,1)", "date(2026,1,1,10,30,1.5)", "date([1,2])", "year([1,2])",
352356 "newweek(<2026-10-15 Thu>,7)", "newyear(<2026-10-15 Thu>,400)", "incmonth(<2026-10-05 Mon>,1.5)",
353 "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>"] {
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 // 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(-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)"] {
354361 #expect(throws: Calc.Unsupported.self, "\(e)") { try Calc.evaluate(e) }
355362 }
356363 var modes = Calc.Modes()
Tests/OrgCoreTests/TableFormulaTests.swift +1
@@ -76,6 +76,7 @@ struct TableFormulaTests {
7676 "| d | later | T | left |\n|---+-------+---+------|\n| <2026-10-05 Mon 00:00> | | | |\n| <2026-10-05 Mon 23:30> | | | |\n| [2026-10-05 Mon 12:00] | | | |\n#+TBLFM: $2=$1+0.5::$3=$1+1;T::$4=<2026-12-25 Fri>-$1\n",
7777 "| d | y | m | wd | yd | h | next | start | week | year |\n|---+---+---+----+----+---+------+-------+------+------|\n| <2026-10-05 Mon> | | | | | | | | | |\n| <2026-01-31 Sat 09:15> | | | | | | | | | |\n| [2024-02-29 Thu] | | | | | | | | | |\n#+TBLFM: $2=year($1)::$3=month($1)::$4=weekday($1)::$5=yearday($1)::$6=hour($1)+minute($1)::$7=incmonth($1,1)::$8=newmonth($1)::$9=newweek($1,1)::$10=incyear($1)\n",
7878 "| d | due | days | jd | n | back |\n|---+-----+------+----+---+------|\n| <2026-10-09 Fri> | | | | | |\n| <2026-10-10 Sat 08:00> | | | | | |\n#+TBLFM: $2=badd($1,3)::$3=bsub(badd($1,10),$1)::$4=julian(floor($1))::$5=date($1)::$6=date(2026,1,1)+floor($1-date(2026,1,1))\n",
79 "| d | abs | next |\n|---+-----+------|\n| <2026-01-01 Thu> | | |\n| <0070-01-01 Thu> | | |\n| [0010-01-01 Fri] | | |\n#+TBLFM: $2=abs(-$1)::$3=$1+1\n",
7980 "| d |\n|---|\n| <2026-10-05 Mon> |\n| <2026-10-07 Wed 06:00> |\n|---|\n| |\n| |\n| |\n#+TBLFM: @4$1=vmean(@I..@II)::@5$1=vmin(@I..@II)::@6$1=vmax(@I..@II)\n",
8081 ]
8182
docs/manual/guide/10-tables.org +4
@@ -452,6 +452,10 @@ time, even =00:00=, keeps showing one.
452452| =julian(d)= | The Julian day number of /d/, or the date of a Julian day number. |
453453| =unixtime(d)= | Seconds since 1970-01-01 00:00 UTC, or the date of a number of seconds. |
454454
455As in Calc, a timestamp year under 100 is read as 19xx or 20xx: 00 to 39 is 20xx, 40 to
45699 is 19xx. =badd= and =bsub= work only for dates in the years 1 to 2737, the range of
457Calc's holiday table; outside it they need Emacs, which leaves them unevaluated.
458
455459=julian= of a date with a time and =unixtime= use the local time zone. Emacs applies
456460the current year's daylight saving rules to every date, which orgstar doesn't, so in a
457461time zone with daylight saving time these need Emacs.