mathjson-solver 2.3.0__tar.gz → 2.4.0__tar.gz

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (69) hide show
  1. {mathjson_solver-2.3.0 → mathjson_solver-2.4.0}/CHANGELOG.md +6 -0
  2. {mathjson_solver-2.3.0 → mathjson_solver-2.4.0}/PKG-INFO +1 -3
  3. {mathjson_solver-2.3.0 → mathjson_solver-2.4.0}/README.md +0 -2
  4. {mathjson_solver-2.3.0 → mathjson_solver-2.4.0}/docs/README.md +49 -0
  5. {mathjson_solver-2.3.0 → mathjson_solver-2.4.0}/pyproject.toml +1 -1
  6. {mathjson_solver-2.3.0 → mathjson_solver-2.4.0}/src/mathjson_solver/__main__.py +15 -0
  7. mathjson_solver-2.4.0/src/mathjson_solver/_datetime_constructs.py +108 -0
  8. {mathjson_solver-2.3.0 → mathjson_solver-2.4.0}/tests/test_datetime.py +101 -0
  9. mathjson_solver-2.3.0/src/mathjson_solver/_datetime_constructs.py +0 -46
  10. {mathjson_solver-2.3.0 → mathjson_solver-2.4.0}/.gitignore +0 -0
  11. {mathjson_solver-2.3.0 → mathjson_solver-2.4.0}/CODE_OF_CONDUCT.md +0 -0
  12. {mathjson_solver-2.3.0 → mathjson_solver-2.4.0}/CONTRIBUTING.md +0 -0
  13. {mathjson_solver-2.3.0 → mathjson_solver-2.4.0}/LICENSE +0 -0
  14. {mathjson_solver-2.3.0 → mathjson_solver-2.4.0}/SECURITY.md +0 -0
  15. {mathjson_solver-2.3.0 → mathjson_solver-2.4.0}/docs/integration-test-cases.md +0 -0
  16. {mathjson_solver-2.3.0 → mathjson_solver-2.4.0}/src/mathjson_solver/__init__.py +0 -0
  17. {mathjson_solver-2.3.0 → mathjson_solver-2.4.0}/src/mathjson_solver/_arithmetic_constructs.py +0 -0
  18. {mathjson_solver-2.3.0 → mathjson_solver-2.4.0}/src/mathjson_solver/_array_arithmetic_constructs.py +0 -0
  19. {mathjson_solver-2.3.0 → mathjson_solver-2.4.0}/src/mathjson_solver/_array_helpers.py +0 -0
  20. {mathjson_solver-2.3.0 → mathjson_solver-2.4.0}/src/mathjson_solver/_collection_constructs.py +0 -0
  21. {mathjson_solver-2.3.0 → mathjson_solver-2.4.0}/src/mathjson_solver/_combinatorics.py +0 -0
  22. {mathjson_solver-2.3.0 → mathjson_solver-2.4.0}/src/mathjson_solver/_combinatorics_constructs.py +0 -0
  23. {mathjson_solver-2.3.0 → mathjson_solver-2.4.0}/src/mathjson_solver/_common.py +0 -0
  24. {mathjson_solver-2.3.0 → mathjson_solver-2.4.0}/src/mathjson_solver/_comparison_constructs.py +0 -0
  25. {mathjson_solver-2.3.0 → mathjson_solver-2.4.0}/src/mathjson_solver/_construct_shared.py +0 -0
  26. {mathjson_solver-2.3.0 → mathjson_solver-2.4.0}/src/mathjson_solver/_control_flow_constructs.py +0 -0
  27. {mathjson_solver-2.3.0 → mathjson_solver-2.4.0}/src/mathjson_solver/_core_constructs.py +0 -0
  28. {mathjson_solver-2.3.0 → mathjson_solver-2.4.0}/src/mathjson_solver/_exceptions.py +0 -0
  29. {mathjson_solver-2.3.0 → mathjson_solver-2.4.0}/src/mathjson_solver/_functional_constructs.py +0 -0
  30. {mathjson_solver-2.3.0 → mathjson_solver-2.4.0}/src/mathjson_solver/_interpolation.py +0 -0
  31. {mathjson_solver-2.3.0 → mathjson_solver-2.4.0}/src/mathjson_solver/_number_theory.py +0 -0
  32. {mathjson_solver-2.3.0 → mathjson_solver-2.4.0}/src/mathjson_solver/_number_theory_constructs.py +0 -0
  33. {mathjson_solver-2.3.0 → mathjson_solver-2.4.0}/src/mathjson_solver/_special_function_constructs.py +0 -0
  34. {mathjson_solver-2.3.0 → mathjson_solver-2.4.0}/src/mathjson_solver/_special_functions.py +0 -0
  35. {mathjson_solver-2.3.0 → mathjson_solver-2.4.0}/src/mathjson_solver/_statistics_constructs.py +0 -0
  36. {mathjson_solver-2.3.0 → mathjson_solver-2.4.0}/src/mathjson_solver/_string_constructs.py +0 -0
  37. {mathjson_solver-2.3.0 → mathjson_solver-2.4.0}/src/mathjson_solver/_trig_constructs.py +0 -0
  38. {mathjson_solver-2.3.0 → mathjson_solver-2.4.0}/tests/test_TrapezoidalIntegratel.py +0 -0
  39. {mathjson_solver-2.3.0 → mathjson_solver-2.4.0}/tests/test_atindex.py +0 -0
  40. {mathjson_solver-2.3.0 → mathjson_solver-2.4.0}/tests/test_blacklist.py +0 -0
  41. {mathjson_solver-2.3.0 → mathjson_solver-2.4.0}/tests/test_cortexjs_collections.py +0 -0
  42. {mathjson_solver-2.3.0 → mathjson_solver-2.4.0}/tests/test_cortexjs_combinatorics.py +0 -0
  43. {mathjson_solver-2.3.0 → mathjson_solver-2.4.0}/tests/test_cortexjs_compat.py +0 -0
  44. {mathjson_solver-2.3.0 → mathjson_solver-2.4.0}/tests/test_cortexjs_constants_relations.py +0 -0
  45. {mathjson_solver-2.3.0 → mathjson_solver-2.4.0}/tests/test_cortexjs_core.py +0 -0
  46. {mathjson_solver-2.3.0 → mathjson_solver-2.4.0}/tests/test_cortexjs_distributions.py +0 -0
  47. {mathjson_solver-2.3.0 → mathjson_solver-2.4.0}/tests/test_cortexjs_number_theory.py +0 -0
  48. {mathjson_solver-2.3.0 → mathjson_solver-2.4.0}/tests/test_cortexjs_sets.py +0 -0
  49. {mathjson_solver-2.3.0 → mathjson_solver-2.4.0}/tests/test_cortexjs_special_functions.py +0 -0
  50. {mathjson_solver-2.3.0 → mathjson_solver-2.4.0}/tests/test_cortexjs_statistics.py +0 -0
  51. {mathjson_solver-2.3.0 → mathjson_solver-2.4.0}/tests/test_cortexjs_strings.py +0 -0
  52. {mathjson_solver-2.3.0 → mathjson_solver-2.4.0}/tests/test_cortexjs_tier3.py +0 -0
  53. {mathjson_solver-2.3.0 → mathjson_solver-2.4.0}/tests/test_cumulative.py +0 -0
  54. {mathjson_solver-2.3.0 → mathjson_solver-2.4.0}/tests/test_equal.py +0 -0
  55. {mathjson_solver-2.3.0 → mathjson_solver-2.4.0}/tests/test_exceptions.py +0 -0
  56. {mathjson_solver-2.3.0 → mathjson_solver-2.4.0}/tests/test_extract_variables.py +0 -0
  57. {mathjson_solver-2.3.0 → mathjson_solver-2.4.0}/tests/test_findinterval.py +0 -0
  58. {mathjson_solver-2.3.0 → mathjson_solver-2.4.0}/tests/test_gail_model.py +0 -0
  59. {mathjson_solver-2.3.0 → mathjson_solver-2.4.0}/tests/test_interpolate.py +0 -0
  60. {mathjson_solver-2.3.0 → mathjson_solver-2.4.0}/tests/test_legacy_compat.py +0 -0
  61. {mathjson_solver-2.3.0 → mathjson_solver-2.4.0}/tests/test_range.py +0 -0
  62. {mathjson_solver-2.3.0 → mathjson_solver-2.4.0}/tests/test_reduce_simple.py +0 -0
  63. {mathjson_solver-2.3.0 → mathjson_solver-2.4.0}/tests/test_scalars.py +0 -0
  64. {mathjson_solver-2.3.0 → mathjson_solver-2.4.0}/tests/test_scope_isolation.py +0 -0
  65. {mathjson_solver-2.3.0 → mathjson_solver-2.4.0}/tests/test_sets.py +0 -0
  66. {mathjson_solver-2.3.0 → mathjson_solver-2.4.0}/tests/test_simple_cases.py +0 -0
  67. {mathjson_solver-2.3.0 → mathjson_solver-2.4.0}/tests/test_slice.py +0 -0
  68. {mathjson_solver-2.3.0 → mathjson_solver-2.4.0}/tests/test_state_tuples_reduce.py +0 -0
  69. {mathjson_solver-2.3.0 → mathjson_solver-2.4.0}/tests/test_with_pytest.py +0 -0
@@ -5,6 +5,12 @@ All notable changes to this project will be documented in this file.
5
5
  The format is based on [Keep a Changelog](https://keepachangelog.com/en/1.1.0/),
6
6
  and this project adheres to [Semantic Versioning](https://semver.org/).
7
7
 
8
+ ## [2.4.0] - 2026-12-02
9
+
10
+ ### Added
11
+
12
+ - **`DateDiffSeconds`, `DateDiffMinutes`, `DateDiffHours`, `DateDiffDays`, `DateDiffYears`** - `end - start` between two ISO date/datetime strings, as a number (negative if `end` precedes `start`). `DateDiffYears` returns complete calendar years (an age), with February 29 start dates completing their year on March 1 in non-leap years. Mixing timezone-aware and naive inputs raises an error.
13
+
8
14
  ## [2.3.0] - 2026-09-21
9
15
 
10
16
  ### Changed
@@ -1,6 +1,6 @@
1
1
  Metadata-Version: 2.5
2
2
  Name: mathjson-solver
3
- Version: 2.3.0
3
+ Version: 2.4.0
4
4
  Summary: Utilities for MathJSON evaluation
5
5
  Project-URL: Homepage, https://github.com/LongenesisLtd/mathjson-solver
6
6
  Project-URL: Bug Tracker, https://github.com/LongenesisLtd/mathjson-solver/issues
@@ -53,8 +53,6 @@ Coming from 1.x? Version 2 changed `Log`'s meaning — see [Migrating from 1.x](
53
53
  * **Data Processing:** Apply mathematical transformations to datasets
54
54
  * **Health Applications:** Calculate medical scores, dosages, or risk assessments
55
55
 
56
- **What's still missing:** a ready-to-use, user-facing editor for authoring MathJSON itself — today, the expressions have to come from somewhere else (a form builder, a generated JSON structure, hand-written JSON). CortexJS ships [MathLive's mathfield](https://mathlive.io/mathfield/) for entering math, but it's built for typing LaTeX-style notation (e-learning quizzes, scientific computing, calculators) — not for assembling a formula that references named fields from a form. Worth evaluating on its own merits rather than assumed to transfer.
57
-
58
56
  ## Installation
59
57
 
60
58
  ```bash
@@ -33,8 +33,6 @@ Coming from 1.x? Version 2 changed `Log`'s meaning — see [Migrating from 1.x](
33
33
  * **Data Processing:** Apply mathematical transformations to datasets
34
34
  * **Health Applications:** Calculate medical scores, dosages, or risk assessments
35
35
 
36
- **What's still missing:** a ready-to-use, user-facing editor for authoring MathJSON itself — today, the expressions have to come from somewhere else (a form builder, a generated JSON structure, hand-written JSON). CortexJS ships [MathLive's mathfield](https://mathlive.io/mathfield/) for entering math, but it's built for typing LaTeX-style notation (e-learning quizzes, scientific computing, calculators) — not for assembling a formula that references named fields from a form. Worth evaluating on its own merits rather than assumed to transfer.
37
-
38
36
  ## Installation
39
37
 
40
38
  ```bash
@@ -1701,6 +1701,50 @@ Creates a time delta representing a number of minutes.
1701
1701
  ["Add", "2025-01-10T10:00:00", ["TimeDeltaMinutes", 45]] # "2025-01-10T10:45:00"
1702
1702
  ```
1703
1703
 
1704
+ ### Date Differences
1705
+
1706
+ `DateDiff*` constructs take a start and an end date or datetime (ISO strings, or the output of `Today`, `Now`, `Strptime`) and return a number: `end - start`. The result is negative when `end` is before `start`. Date-only strings are treated as midnight. Mixing timezone-aware and timezone-naive inputs raises an error. Naive inputs are compared as-is, with no timezone or DST adjustment.
1707
+
1708
+ #### DateDiffSeconds
1709
+ Seconds from start to end.
1710
+
1711
+ ```python
1712
+ ["DateDiffSeconds", "2025-01-10T10:00:00", "2025-01-10T10:01:30"] # 90
1713
+ ```
1714
+
1715
+ #### DateDiffMinutes
1716
+ Minutes from start to end. Fractional when the difference is not a whole number of minutes.
1717
+
1718
+ ```python
1719
+ ["DateDiffMinutes", "2025-01-10T10:00:00", "2025-01-10T10:45:00"] # 45
1720
+ ```
1721
+
1722
+ #### DateDiffHours
1723
+ Hours from start to end. Fractional when the difference is not a whole number of hours.
1724
+
1725
+ ```python
1726
+ ["DateDiffHours", "2025-01-10", "2025-01-11"] # 24
1727
+ ["DateDiffHours", "2025-01-10T10:00:00", "2025-01-10T10:30:00"] # 0.5
1728
+ ```
1729
+
1730
+ #### DateDiffDays
1731
+ Days from start to end. Fractional when the difference is not a whole number of days; wrap in `Floor` for whole days.
1732
+
1733
+ ```python
1734
+ ["DateDiffDays", "2000-01-15", ["Today"]] # 9818
1735
+ ["DateDiffDays", "2025-01-10", "2025-01-11T12:00:00"] # 1.5
1736
+ ["Floor", ["DateDiffDays", "2025-01-10", "2025-01-11T12:00:00"]] # 1
1737
+ ```
1738
+
1739
+ #### DateDiffYears
1740
+ Number of complete calendar years from start to end, as an integer (an age). A year is complete once the month, day and time of day of the start have been reached; a February 29 start completes its year on March 1 in non-leap years.
1741
+
1742
+ ```python
1743
+ ["DateDiffYears", "2000-06-15", "2026-06-14"] # 25
1744
+ ["DateDiffYears", "2000-06-15", "2026-06-15"] # 26
1745
+ ["DateDiffYears", "dob", ["Today"]] # age in years
1746
+ ```
1747
+
1704
1748
  ### Combining Date Operations
1705
1749
 
1706
1750
  Date functions can be chained together for complex date calculations:
@@ -2275,6 +2319,11 @@ Evaluating a `["Function", ...]` expression outside of such a context (i.e. not
2275
2319
  - [TimeDeltaWeeks](#timedeltaweeks) - Week time delta
2276
2320
  - [TimeDeltaHours](#timedeltahours) - Hour time delta
2277
2321
  - [TimeDeltaMinutes](#timedeltaminutes) - Minute time delta
2322
+ - [DateDiffSeconds](#datediffseconds) - Seconds between two dates
2323
+ - [DateDiffMinutes](#datediffminutes) - Minutes between two dates
2324
+ - [DateDiffHours](#datediffhours) - Hours between two dates
2325
+ - [DateDiffDays](#datediffdays) - Days between two dates
2326
+ - [DateDiffYears](#datediffyears) - Complete years between two dates (age)
2278
2327
 
2279
2328
  ### Trigonometric Functions
2280
2329
  - [Sin](#sin) - Sine
@@ -4,7 +4,7 @@ build-backend = "hatchling.build"
4
4
 
5
5
  [project]
6
6
  name = "mathjson-solver"
7
- version = "2.3.0"
7
+ version = "2.4.0"
8
8
  authors = [{name = "Martins Mednis", email = "mrt@mednis.info"}]
9
9
  description = "Utilities for MathJSON evaluation"
10
10
  readme = "README.md"
@@ -192,6 +192,11 @@ from ._datetime_constructs import (
192
192
  TimeDeltaMinutes,
193
193
  TimeDeltaHours,
194
194
  TimeDeltaWeeks,
195
+ DateDiffSeconds,
196
+ DateDiffMinutes,
197
+ DateDiffHours,
198
+ DateDiffDays,
199
+ DateDiffYears,
195
200
  )
196
201
  from ._functional_constructs import (
197
202
  Fold,
@@ -496,6 +501,11 @@ constructs = {
496
501
  "TimeDeltaHours": TimeDeltaHours,
497
502
  "TimeDeltaMinutes": TimeDeltaMinutes,
498
503
  "TimeDeltaDays": TimeDeltaDays,
504
+ "DateDiffSeconds": DateDiffSeconds,
505
+ "DateDiffMinutes": DateDiffMinutes,
506
+ "DateDiffHours": DateDiffHours,
507
+ "DateDiffDays": DateDiffDays,
508
+ "DateDiffYears": DateDiffYears,
499
509
  "Function": Function,
500
510
  "Variable": Variable,
501
511
  "MultiplyByScalar": MultiplyByScalar,
@@ -974,6 +984,11 @@ def extract_variables(
974
984
  "TimeDeltaHours",
975
985
  "TimeDeltaMinutes",
976
986
  "TimeDeltaDays",
987
+ "DateDiffSeconds",
988
+ "DateDiffMinutes",
989
+ "DateDiffHours",
990
+ "DateDiffDays",
991
+ "DateDiffYears",
977
992
  "Function",
978
993
  "Variable",
979
994
  "MultiplyByScalar",
@@ -0,0 +1,108 @@
1
+ """
2
+ Date/time constructs.
3
+ """
4
+
5
+ import datetime
6
+ from ._common import (
7
+ _try_parse_datetime,
8
+ )
9
+
10
+
11
+ def Strptime(f, c, solver_parameters, s):
12
+ datetime_str = f(s[1], c)
13
+ parameters = f(s[2], c)
14
+ return datetime.datetime.strptime(datetime_str, parameters).isoformat()
15
+
16
+
17
+ def Strftime(f, c, solver_parameters, s):
18
+ dt = _try_parse_datetime(f(s[1], c))
19
+ if not isinstance(dt, (datetime.datetime, datetime.date)):
20
+ raise ValueError(f"Strftime: could not parse input as datetime: {dt!r}")
21
+ parameters = f(s[2], c)
22
+ return dt.strftime(parameters)
23
+
24
+
25
+ def Now(f, c, solver_parameters, s):
26
+ return datetime.datetime.now().isoformat()
27
+
28
+
29
+ def Today(f, c, solver_parameters, s):
30
+ return datetime.date.today().isoformat()
31
+
32
+
33
+ def TimeDeltaDays(f, c, solver_parameters, s):
34
+ return datetime.timedelta(days=f(s[1], c))
35
+
36
+
37
+ def TimeDeltaMinutes(f, c, solver_parameters, s):
38
+ return datetime.timedelta(minutes=f(s[1], c))
39
+
40
+
41
+ def TimeDeltaHours(f, c, solver_parameters, s):
42
+ return datetime.timedelta(hours=f(s[1], c))
43
+
44
+
45
+ def TimeDeltaWeeks(f, c, solver_parameters, s):
46
+ return datetime.timedelta(weeks=f(s[1], c))
47
+
48
+
49
+ def _to_datetime(value, name):
50
+ dt = _try_parse_datetime(value)
51
+ if isinstance(dt, datetime.datetime):
52
+ return dt
53
+ if isinstance(dt, datetime.date):
54
+ return datetime.datetime(dt.year, dt.month, dt.day)
55
+ raise ValueError(f"{name}: could not parse input as datetime: {value!r}")
56
+
57
+
58
+ def _datediff_seconds(f, c, s, name):
59
+ start = _to_datetime(f(s[1], c), name)
60
+ end = _to_datetime(f(s[2], c), name)
61
+ try:
62
+ return (end - start).total_seconds()
63
+ except TypeError:
64
+ raise ValueError(
65
+ f"{name}: cannot mix timezone-aware and timezone-naive datetimes"
66
+ )
67
+
68
+
69
+ def _whole_if_integral(x):
70
+ return int(x) if float(x).is_integer() else x
71
+
72
+
73
+ def DateDiffSeconds(f, c, solver_parameters, s):
74
+ return _whole_if_integral(_datediff_seconds(f, c, s, "DateDiffSeconds"))
75
+
76
+
77
+ def DateDiffMinutes(f, c, solver_parameters, s):
78
+ return _whole_if_integral(_datediff_seconds(f, c, s, "DateDiffMinutes") / 60)
79
+
80
+
81
+ def DateDiffHours(f, c, solver_parameters, s):
82
+ return _whole_if_integral(_datediff_seconds(f, c, s, "DateDiffHours") / 3600)
83
+
84
+
85
+ def DateDiffDays(f, c, solver_parameters, s):
86
+ return _whole_if_integral(_datediff_seconds(f, c, s, "DateDiffDays") / 86400)
87
+
88
+
89
+ def DateDiffYears(f, c, solver_parameters, s):
90
+ """Number of complete calendar years from s[1] to s[2] (negative if reversed)."""
91
+ start = _to_datetime(f(s[1], c), "DateDiffYears")
92
+ end = _to_datetime(f(s[2], c), "DateDiffYears")
93
+ if (start.tzinfo is None) != (end.tzinfo is None):
94
+ raise ValueError(
95
+ "DateDiffYears: cannot mix timezone-aware and timezone-naive datetimes"
96
+ )
97
+ sign = 1
98
+ if end < start:
99
+ start, end = end, start
100
+ sign = -1
101
+
102
+ def anniversary_key(dt):
103
+ return (dt.month, dt.day, dt.hour, dt.minute, dt.second, dt.microsecond)
104
+
105
+ years = end.year - start.year
106
+ if anniversary_key(end) < anniversary_key(start):
107
+ years -= 1
108
+ return sign * years
@@ -231,3 +231,104 @@ class TestDatetimeWithVariables:
231
231
  solver = create_solver({"start_date": "2025-06-01"})
232
232
  result = solver(["Strftime", ["Add", "start_date", ["TimeDeltaDays", 10]], "%Y-%m-%d"])
233
233
  assert result == "2025-06-11"
234
+
235
+
236
+ class TestDateDiff:
237
+ """Test DateDiff* constructs: DateDiffX(start, end) = end - start."""
238
+
239
+ def test_seconds(self):
240
+ solver = create_solver({})
241
+ assert solver(["DateDiffSeconds", "2025-01-10T10:00:00", "2025-01-10T10:01:30"]) == 90
242
+
243
+ def test_minutes(self):
244
+ solver = create_solver({})
245
+ assert solver(["DateDiffMinutes", "2025-01-10T10:00:00", "2025-01-10T10:45:00"]) == 45
246
+
247
+ def test_hours(self):
248
+ solver = create_solver({})
249
+ assert solver(["DateDiffHours", "2025-01-10", "2025-01-11"]) == 24
250
+
251
+ def test_hours_fractional(self):
252
+ solver = create_solver({})
253
+ assert solver(["DateDiffHours", "2025-01-10T10:00:00", "2025-01-10T10:30:00"]) == 0.5
254
+
255
+ def test_days_date_strings(self):
256
+ solver = create_solver({})
257
+ assert solver(["DateDiffDays", "2000-01-15", "2026-12-02"]) == 9818
258
+
259
+ def test_days_negative_when_reversed(self):
260
+ solver = create_solver({})
261
+ assert solver(["DateDiffDays", "2025-01-11", "2025-01-10"]) == -1
262
+
263
+ def test_days_mixed_date_and_datetime(self):
264
+ solver = create_solver({})
265
+ assert solver(["DateDiffDays", "2025-01-10", "2025-01-11T12:00:00"]) == 1.5
266
+
267
+ def test_days_with_strptime_and_floor(self):
268
+ solver = create_solver({})
269
+ result = solver(
270
+ ["Floor", ["DateDiffDays",
271
+ ["Strptime", "10/Jan/2025", "%d/%b/%Y"],
272
+ "2025-01-11T12:00:00"]]
273
+ )
274
+ assert result == 1
275
+
276
+ def test_days_with_today(self):
277
+ solver = create_solver({})
278
+ assert solver(["DateDiffDays", ["Today"], ["Add", ["Today"], ["TimeDeltaDays", 7]]]) == 7
279
+
280
+ def test_days_with_variables(self):
281
+ solver = create_solver({"dob": "2000-01-15", "ref": "2000-02-15"})
282
+ assert solver(["DateDiffDays", "dob", "ref"]) == 31
283
+
284
+ def test_years_before_birthday(self):
285
+ solver = create_solver({})
286
+ assert solver(["DateDiffYears", "2000-06-15", "2026-06-14"]) == 25
287
+
288
+ def test_years_on_birthday(self):
289
+ solver = create_solver({})
290
+ assert solver(["DateDiffYears", "2000-06-15", "2026-06-15"]) == 26
291
+
292
+ def test_years_after_birthday(self):
293
+ solver = create_solver({})
294
+ assert solver(["DateDiffYears", "2000-06-15", "2026-12-02"]) == 26
295
+
296
+ def test_years_same_day_is_zero(self):
297
+ solver = create_solver({})
298
+ assert solver(["DateDiffYears", "2025-03-01", "2025-03-01"]) == 0
299
+
300
+ def test_years_leap_day_birthday(self):
301
+ solver = create_solver({})
302
+ assert solver(["DateDiffYears", "2000-02-29", "2025-02-28"]) == 24
303
+ assert solver(["DateDiffYears", "2000-02-29", "2025-03-01"]) == 25
304
+ assert solver(["DateDiffYears", "2000-02-29", "2024-02-29"]) == 24
305
+
306
+ def test_years_time_of_day_matters(self):
307
+ solver = create_solver({})
308
+ assert solver(["DateDiffYears", "2000-06-15T12:00:00", "2001-06-15T11:59:59"]) == 0
309
+ assert solver(["DateDiffYears", "2000-06-15T12:00:00", "2001-06-15T12:00:00"]) == 1
310
+
311
+ def test_years_negative_when_reversed(self):
312
+ solver = create_solver({})
313
+ assert solver(["DateDiffYears", "2026-12-02", "2000-06-15"]) == -26
314
+
315
+ def test_unparseable_input_raises(self):
316
+ solver = create_solver({})
317
+ with pytest.raises(Exception, match="could not parse"):
318
+ solver(["DateDiffDays", "not a date", "2025-01-01"])
319
+
320
+ def test_none_input_raises(self):
321
+ solver = create_solver({"d": None})
322
+ with pytest.raises(Exception, match="could not parse"):
323
+ solver(["DateDiffYears", "d", "2025-01-01"])
324
+
325
+ def test_mixed_timezone_awareness_raises(self):
326
+ solver = create_solver({})
327
+ with pytest.raises(Exception, match="timezone"):
328
+ solver(["DateDiffSeconds", "2025-01-01T00:00:00+00:00", "2025-01-02T00:00:00"])
329
+ with pytest.raises(Exception, match="timezone"):
330
+ solver(["DateDiffYears", "2025-01-01T00:00:00+00:00", "2025-01-02T00:00:00"])
331
+
332
+ def test_timezone_aware_inputs(self):
333
+ solver = create_solver({})
334
+ assert solver(["DateDiffHours", "2025-01-01T00:00:00+00:00", "2025-01-01T00:00:00+02:00"]) == -2
@@ -1,46 +0,0 @@
1
- """
2
- Date/time constructs.
3
- """
4
-
5
- import datetime
6
- from ._common import (
7
- _try_parse_datetime,
8
- )
9
-
10
-
11
- def Strptime(f, c, solver_parameters, s):
12
- datetime_str = f(s[1], c)
13
- parameters = f(s[2], c)
14
- return datetime.datetime.strptime(datetime_str, parameters).isoformat()
15
-
16
-
17
- def Strftime(f, c, solver_parameters, s):
18
- dt = _try_parse_datetime(f(s[1], c))
19
- if not isinstance(dt, (datetime.datetime, datetime.date)):
20
- raise ValueError(f"Strftime: could not parse input as datetime: {dt!r}")
21
- parameters = f(s[2], c)
22
- return dt.strftime(parameters)
23
-
24
-
25
- def Now(f, c, solver_parameters, s):
26
- return datetime.datetime.now().isoformat()
27
-
28
-
29
- def Today(f, c, solver_parameters, s):
30
- return datetime.date.today().isoformat()
31
-
32
-
33
- def TimeDeltaDays(f, c, solver_parameters, s):
34
- return datetime.timedelta(days=f(s[1], c))
35
-
36
-
37
- def TimeDeltaMinutes(f, c, solver_parameters, s):
38
- return datetime.timedelta(minutes=f(s[1], c))
39
-
40
-
41
- def TimeDeltaHours(f, c, solver_parameters, s):
42
- return datetime.timedelta(hours=f(s[1], c))
43
-
44
-
45
- def TimeDeltaWeeks(f, c, solver_parameters, s):
46
- return datetime.timedelta(weeks=f(s[1], c))
File without changes