Commit e301097d2a
Verified · cmc
Layout: unified · split
Sources/OrgCore/Compute/Calc.swift +12 −2
| @@ -788,7 +788,10 @@ public enum Calc { | |||
| 788 | 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 { | 788 | 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 { |
| 789 | throw Unsupported("date syntax") | 789 | throw Unsupported("date syntax") |
| 790 | } | 790 | } |
| 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)!) | ||
| 792 | guard let hour = m.4.flatMap({ Int($0) }), let minute = m.5.flatMap({ Int($0) }) else { return .date(.int(day)) } | 795 | guard let hour = m.4.flatMap({ Int($0) }), let minute = m.5.flatMap({ Int($0) }) else { return .date(.int(day)) } |
| 793 | return try dateTime(day, hour * 3600 + minute * 60 + (m.6.flatMap { Int($0) } ?? 0)) | 796 | return try dateTime(day, hour * 3600 + minute * 60 + (m.6.flatMap { Int($0) } ?? 0)) |
| 794 | } | 797 | } |
| @@ -848,11 +851,17 @@ public enum Calc { | |||
| 848 | static func toBusinessDay(_ v: Value) throws -> (Value, holiday: Bool) { | 851 | static func toBusinessDay(_ v: Value) throws -> (Value, holiday: Bool) { |
| 849 | let x = try dayNumber(v) | 852 | let x = try dayNumber(v) |
| 850 | let day = try floorInt(x) | 853 | let day = try floorInt(x) |
| 854 | try checkHolidayRange(day) | ||
| 851 | let weekday = Days.weekday(day) | 855 | let weekday = Days.weekday(day) |
| 852 | let delta = day / 7 * 2 + (weekday > 0 ? 1 : 0) | 856 | let delta = day / 7 * 2 + (weekday > 0 ? 1 : 0) |
| 853 | return (try add(.int(day - delta), try subtract(x, .int(day))), weekday == 0 || weekday == 6) | 857 | return (try add(.int(day - delta), try subtract(x, .int(day))), weekday == 0 || weekday == 6) |
| 854 | } | 858 | } |
| 855 | 859 | ||
| 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 | |||
| 856 | /// `math-from-business-day`. | 865 | /// `math-from-business-day`. |
| 857 | static func fromBusinessDay(_ n: Value) throws -> Value { | 866 | static func fromBusinessDay(_ n: Value) throws -> Value { |
| 858 | var day = try floorInt(n) | 867 | var day = try floorInt(n) |
| @@ -869,6 +878,7 @@ public enum Calc { | |||
| 869 | } | 878 | } |
| 870 | weekday += 1 | 879 | weekday += 1 |
| 871 | } | 880 | } |
| 881 | try checkHolidayRange(day) | ||
| 872 | return .date(try add(.int(day), time)) | 882 | return .date(try add(.int(day), time)) |
| 873 | } | 883 | } |
| 874 | 884 | ||
| @@ -1367,7 +1377,7 @@ public enum Calc { | |||
| 1367 | return best | 1377 | return best |
| 1368 | case "abs": | 1378 | case "abs": |
| 1369 | let x = try one(args) | 1379 | 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) } |
| 1371 | return try compare(x, .int(0)) < 0 ? try negate(x) : x | 1381 | return try compare(x, .int(0)) < 0 ? try negate(x) : x |
| 1372 | case "floor", "ceil", "trunc": | 1382 | case "floor", "ceil", "trunc": |
| 1373 | if args.count == 1, case .date(let d) = args[0] { return .date(try call(name, [d])) } | 1383 | 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 { | |||
| 107 | "julian(<2026-10-05 Mon>)", "julian(<2026-10-05 Mon 12:00>)", "julian(2461319)", "julian(2461319.5)", "julian(1:2)", | 107 | "julian(<2026-10-05 Mon>)", "julian(<2026-10-05 Mon 12:00>)", "julian(2461319)", "julian(2461319.5)", "julian(1:2)", |
| 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))", | ||
| 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>)", | ||
| 110 | ] | 114 | ] |
| 111 | 115 | ||
| 112 | /// Zones without daylight saving time, as `TZ` names them for Emacs and their offsets. | 116 | /// Zones without daylight saving time, as `TZ` names them for Emacs and their offsets. |
| @@ -350,7 +354,10 @@ struct CalcTests { | |||
| 350 | "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)", | 354 | "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)", |
| 351 | "date(2026,2,32)", "date(2026.5,1,1)", "date(2026,1,1,10,30,1.5)", "date([1,2])", "year([1,2])", | 355 | "date(2026,2,32)", "date(2026.5,1,1)", "date(2026,1,1,10,30,1.5)", "date([1,2])", "year([1,2])", |
| 352 | "newweek(<2026-10-15 Thu>,7)", "newyear(<2026-10-15 Thu>,400)", "incmonth(<2026-10-05 Mon>,1.5)", | 356 | "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)"] { | ||
| 354 | #expect(throws: Calc.Unsupported.self, "\(e)") { try Calc.evaluate(e) } | 361 | #expect(throws: Calc.Unsupported.self, "\(e)") { try Calc.evaluate(e) } |
| 355 | } | 362 | } |
| 356 | var modes = Calc.Modes() | 363 | var modes = Calc.Modes() |
Tests/OrgCoreTests/TableFormulaTests.swift +1
| @@ -76,6 +76,7 @@ struct TableFormulaTests { | |||
| 76 | "| 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", | 76 | "| 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", |
| 77 | "| 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", | 77 | "| 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", |
| 78 | "| 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", | 78 | "| 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", | ||
| 79 | "| 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", | 80 | "| 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", |
| 80 | ] | 81 | ] |
| 81 | 82 | ||
docs/manual/guide/10-tables.org +4
| @@ -452,6 +452,10 @@ time, even =00:00=, keeps showing one. | |||
| 452 | | =julian(d)= | The Julian day number of /d/, or the date of a Julian day number. | | 452 | | =julian(d)= | The Julian day number of /d/, or the date of a Julian day number. | |
| 453 | | =unixtime(d)= | Seconds since 1970-01-01 00:00 UTC, or the date of a number of seconds. | | 453 | | =unixtime(d)= | Seconds since 1970-01-01 00:00 UTC, or the date of a number of seconds. | |
| 454 | 454 | ||
| 455 | As in Calc, a timestamp year under 100 is read as 19xx or 20xx: 00 to 39 is 20xx, 40 to | ||
| 456 | 99 is 19xx. =badd= and =bsub= work only for dates in the years 1 to 2737, the range of | ||
| 457 | Calc's holiday table; outside it they need Emacs, which leaves them unevaluated. | ||
| 458 | |||
| 455 | =julian= of a date with a time and =unixtime= use the local time zone. Emacs applies | 459 | =julian= of a date with a time and =unixtime= use the local time zone. Emacs applies |
| 456 | the current year's daylight saving rules to every date, which orgstar doesn't, so in a | 460 | the current year's daylight saving rules to every date, which orgstar doesn't, so in a |
| 457 | time zone with daylight saving time these need Emacs. | 461 | time zone with daylight saving time these need Emacs. |