[Formed-commits] r351 - trunk/formed/formed/model
scm-commit@wald.intevation.org
scm-commit at wald.intevation.org
Tue Apr 6 15:48:35 CEST 2010
Author: torsten
Date: 2010-04-06 15:48:34 +0200 (Tue, 06 Apr 2010)
New Revision: 351
Modified:
trunk/formed/formed/model/exprtree.py
Log:
* formed/formed/model/exprtree.py: Added years operator.
Modified: trunk/formed/formed/model/exprtree.py
===================================================================
--- trunk/formed/formed/model/exprtree.py 2010-04-06 10:39:06 UTC (rev 350)
+++ trunk/formed/formed/model/exprtree.py 2010-04-06 13:48:34 UTC (rev 351)
@@ -322,6 +322,19 @@
or a == UNKNOWN_DATE \
or (type(a) in StringTypes and a.lower() == UNKNOWN_STR))
+def YEARS(start, end):
+ """Return the number of complete years between the dates start and end.
+ This is useful for e.g. age calculations. The order of the dates is
+ irrelevant.
+ """
+ if end < start:
+ start, end = end, start
+ years = end.year - start.year
+ if end.month < start.month or (end.month == start.month
+ and end.day < start.day):
+ years -= 1
+ return years
+
NODE_FACTORIES = {
'+': lambda s: OperatorBinary(s.pop(), s.pop(), ADD),
'-': lambda s: OperatorBinary(s.pop(-2), s.pop(), MINUS),
@@ -346,6 +359,7 @@
'isset': lambda s: OperatorUnary(s.pop(), ISSET),
'known': lambda s: OperatorUnary(s.pop(), KNOWN),
'type': lambda s: OperatorUnary(s.pop(), TYPE),
+ 'years': lambda s: OperatorBinary(s.pop(), s.pop(), YEARS),
'and': lambda s: And(s.pop(-2), s.pop()),
'or': lambda s: Or(s.pop(-2), s.pop()),
'today': lambda s: Today(),
More information about the Formed-commits
mailing list