Tinderbox User-to-User Forum (for formal tech support please email: info@eastgate.com)
Tinderbox Users >> Agent, Actions, Rules & Automation >> Calculating "days since" for simple task assistant

Message started by Sumner Gerard on Aug 3rd, 2008, 1:01pm

Title: Calculating "days since" for simple task assistant
Post by Sumner Gerard on Aug 3rd, 2008, 1:01pm

I record in a daybook somewhat irregularly recurring activities that don't necessary occur in sequence. Call activity one "jump up", activity two "run in circles" and activity three "dance and shout."  

I want to see in one convenient place how many days have passed since I last jumped, last ran in circles, last danced and shouted, etc.  For me more feature-filled task management assistants end up cluttered with so many jumps, runs, and shouts scattered among "due," "overdue," and "followup" that they end up too poorly maintained to be useful.

I can use agents to collect date-sorted occurrences of each tracked activity in separate containers and then use #last(JumpContainer, 1) | #last(RunContainer,1) | #last(ShoutContainer, 1) | .... in an agent query to collect the most recent occurrence of each activity.  

But I can't figure out how to do the date arithmetic for "days since" and have that dispayed in the relevant note titles. I tried "today"-Created + " days : " + $Name in DisplayExpression. I tried using a rule to set a number user attribute to "today"-created.  I tried using days(Created, "today"), which looked promising, but today assigns a value of 244 for a note Created "2008.07.31 9:28 PM".

The "News about Tinderbox" page on the website, referring to Tinderbox 3.6.2. (I'm running 4.2.5), states that " subtracting two dates and assigning the result to an integer yields the number of days between the two dates. "

aTbRef, describing "Date Attributes", states: "Dates can be added/subtracted/compared in mathematical fashion, as per number data types. The resulting 'string' can be formatted by means of date format codes."  aTbRef (under "Date Operators", which I found in a TBX version but can't seem to find by searching the online version) lists the days(date1,date2) I tried as described above.

I am obviously missing something obvious. Improperly mixing strings and numbers? How does one get "days since"?

Title: Re: Calculating "days since" for simple task
Post by Mark Bernstein on Aug 3rd, 2008, 1:15pm

If Date and Date2 are two Date attributes, and N is a numerical attribute, the rule

    N=$Date-$Date2    (corrected from earlier posting)

sets N to the number of days between Date and Date2.

If Date is a Date attribute, then the rule

    N=$Date-"June 1, 2008"

sets N to the number of days since June 1.  


gives the expected result, 31, if Date is September 3, 2008 4pm and today is August 3, 2008 2pm.

Title: Re: Calculating "days since" for simple task assistant
Post by Sumner Gerard on Aug 3rd, 2008, 8:57pm

Thanks. I was confused by the N=$Date-Date2 (instead of N=$Date-$Date2) and N=$Date="today" but, encouraged that I was close, resumed experimenting.  

It seems: a) one can't do the date arithmetic in DisplayExpression but must assign the result first to a numerical attribute, and b) one has to use N="today"-$Created (not my original N="today"-Created), where N is a numerical attribute I've named DaysSince.

DaysSince=days($Created, "today") as opposed to my original days(Created,"today") also seems to work.

So a rule DaysSince="today"-$Created or DaysSince=days($Created,"today") together with a DisplayExpression something like $Name + "  (" + format($DaysSince,0) + " days since)" seems to do the trick.

Title: Re: Calculating "days since" for simple task assistant
Post by Mark Anderson on Aug 4th, 2008, 4:52am

I think part of the problem is you've (unintentionally) conflated two issues: date arithmetic and parsing expressions. The problem arises because the common maths symbols (+-*/) all have other meanings in different contexts; plus is the string concatenate, minus can be a text hyphen, multiply can be a text asterisk, and divide can be either a forward slash or a POSIX path folder delimiter.

Regular Expressions inside TB itself can find these symbols in your data but ultimately it (TB) must rely on context to figure out whether '-' is a hyphen or a subtraction and so on. The flip side is that for the learner, such areas of confusion even if understood in principle aren't easy to predict when writing actions and rules. This thread is worth reading to get an understanding of why your DisplayExpression doesn't work as expected - you have to allow for how TB can mis-interpret your intent for text as maths and vice versa.

I'm sorry if the aTbRef page confused you (I'll review that text) but I think you interpreted it a little narrowly.  You can do maths using Date format attribute data, it is the final output of that maths that is a string (except where stated). The most usual exception are functions like days() that document that they return a number (of days) - this is so the figure can be used in further calculation.  With care it can be turned into a string. The latter is a scenario where you might want to use a secondary attribute - for instance to concatenate a number to a string to make it all a string before passing it through any further expression evaluation, such as DisplayExpression.

Remember, if you write a multi-part/stage action or rule that doesn't work, make a small test TBX (or test section in you TBX) and break out each stage and look at the result. That way you can see where TB is not giving you the result expected. Often the format of the incorrect result will indicate what's happening and help indicate an alternate approach. User attributes can help, if you think of them as like code variables; the counter-intuitive part is that each attribute exists for all notes and attributes are normally seen by users as being visible things - not under-the-hood calculations. If you get past that last oddity, TB's not phased by a few extra attributes.

Title: Date arithmetic in DisplayExpression
Post by Mark Bernstein on Aug 4th, 2008, 11:04am

Why doesn't $Date1-$Date2 return the expected value in DisplayExpression?

Tinderbox recognizes the special case where you're subtracting two dates in order to make a number. But DisplayExpression is assembling a string; Tinderbox isn't currently clever enough to anticipate what you want.

Title: Re: Calculating "days since" for simple task assistant
Post by Mark Bernstein on Aug 4th, 2008, 4:10pm

Upon further investigation, it looks like we will be able to support the expected behavior for Date1-$Date2 in string contexts like DisplayExpression in an upcoming release.

Title: Re: Calculating "days since" for simple task assistant
Post by Sumner Gerard on Aug 4th, 2008, 5:54pm

Thanks much to both for your helpful explanations and pointers. I'm sure I've conflated and confused quite a bit.  Definitely not the fault of aTbRef. It's a very valuable resource, as is the manual and this forum.

Other than placing demands on DisplayExpression parsing for which it is not thus far intended (instead assign values to attributes first and then assemble them in DisplayExpression), what tripped me up most, I think, was using 'Created' in a rule when TBX was looking for '$Created'. Am still a little vague as to why this is so. By contrast, 'today', '"today"', and '$today' all seem acceptable.  I suspect this may have to do with 'Created' being an attribute and 'today' not.

Title: Re: Calculating "days since" for simple task assistant
Post by Mark Anderson on Aug 4th, 2008, 6:13pm

A handy rule of thumb is that any attribute on the right side of an '=' assignment needs a $ prefix so TB knows to what you refer:
OK: MyAttr = $OtherAttr
Not OK: MyAttr = OtherAttr

Placeholders for dates (today, year, etc.) don't take a $ prefix.  If you use action code that refer to attributes, such as any(), said attributes also need a $ prefix.

Incidentally, you can use a $ on the left side of an assignment, though few examples use this syntax:
$MyAttr = $OtherAttr

Title: Re: Calculating "days since" for simple task assistant
Post by Sumner Gerard on Aug 5th, 2008, 5:12pm

And, it seems, you also can use a $ with date placeholders without unexpected results.

Thanks for the rule of thumb. It helps break my association of $ with strings that can't be added to numbers.  Interesting the $ is not generally used on both sides of an assignment.  Such, I guess, is the nature of idioms.

Title: Re: Calculating "days since" for simple task assistant
Post by Mark Anderson on Aug 5th, 2008, 6:12pm

Date placeholders - I certainly not acquire the habit of using $ with them - the fact that syntax works now is probably accidental and something that may get 'fixed' (due to unrelated work elsewhere) in later version.

With left-side attributes the 'rule' is in principle that you do use a $ prefix like the right-side but it may be omitted and usually is by long-standing users of the app (ergo older demos, examples, etc.).

Tinderbox User-to-User Forum (for formal tech support please email: info@eastgate.com) » Powered by YaBB 2.2.1!
YaBB © 2000-2008. All Rights Reserved.