Wow Eric that's great thank you!

I think what's missing is the ability to record time for the same task 
across multiple sessions.

I've edited some of your code so that the start/stop buttons don't erase 
the field, but just add the new timestamps to the end of the field. This 
way the "start" and "stop" fields contain a list of *all *the times you 
started and stopped the timer.

What I can't figure out is how to calculate the total elapsed time. What I 
need to do is calculate the difference between the *last* timestamp in the 
"start" and "stop" fields each time I press the stop button, then sum all 
of these values.

Here is an edited version of your code:

\define startbutton()
<$button> start
   <$set name="current-time" value=<<now YYYY0MM0DD0hh0mm0ss0XXX>> >
   <$action-setfield start={{{ [{!!start}addsuffix[ ]addsuffix<current-time
>] }}}/>
   <$action-setfield current-button="stop"/>
   </$set>
</$button>
\end

\define stopbutton()
<$button> stop
   <$set name="current-time" value=<<now YYYY0MM0DD0hh0mm0ss0XXX>> >
   <$action-setfield stop={{{ [{!!stop}addsuffix[ ]addsuffix<current-time>] 
}}}/>
   <$action-setfield elapsed={{{ [{!!stop}subtract{!!start}] }}}/>
   <$action-setfield current-button="start"/>
   </$set>
</$button>
\end

\define showtable()
<$vars min={{{ [{!!elapsed}divide[60000]trunc[]] }}}>
<$vars minsec={{{ [<min>multiply[60]] }}}>
<$vars sec={{{ [{!!elapsed}divide[1000]subtract<minsec>multiply[1000]trunc[]
divide[1000]] }}}>

| started:|<$view field="start" format="date" template="[UTC]MMM DD YYYY 
0hh:0mm:0ss.0XXX" />|
| stopped:| <$view field="stop" format="date" template="[UTC]MMM DD YYYY 
0hh:0mm:0ss.0XXX" />|
| elapsed:|<<min>> minutes <<sec>> seconds|
</$vars>
</$vars>
</$vars>
\end

<$reveal default={{!!current-button}} type="nomatch" text="stop">
   <<startbutton>>
</$reveal>
<$reveal default={{!!current-button}} type="match" text="stop">
   <<stopbutton>>
</$reveal>

<<showtable>>

Any ideas how I can calculate the total elapsed time? Or maybe there is a 
better overall approach?



On Sunday, 21 June 2020 17:33:31 UTC+1, Eric Shulman wrote:
>
> On Sunday, June 21, 2020 at 5:08:10 AM UTC-7, si wrote:
>>
>> I use TiddlyWiki as a task manager, among other things, and I thought it 
>> would be cool to know how long I spent working on a completed task.
>> Off the top of my head I imagine having a "start/stop" button that will 
>> alternately add a timestamp to the fields "start" and "stop". Then I could 
>> (hopefully) create a macro that calculates the total time spent on the task.
>>
>
> I just put this together:
> \define startbutton()
> <$button> start
>    <$action-setfield start=<<now YYYY0MM0DD0hh0mm0ss0XXX>>/>
>    <$action-setfield stop=""/>
>    <$action-setfield elapsed=""/>
> </$button>
> \end
>
> \define stopbutton()
> <$button> stop
>    <$action-setfield stop=<<now YYYY0MM0DD0hh0mm0ss0XXX>>/>
>    <$action-setfield elapsed={{{ [{!!stop}subtract{!!start}] }}}/>
> </$button>
> \end
>
> \define showtable()
> <$vars min={{{ [{!!elapsed}divide[60000]trunc[]] }}}>
> <$vars minsec={{{ [<min>multiply[60]] }}}>
> <$vars sec={{{ 
> [{!!elapsed}divide[1000]subtract<minsec>multiply[1000]trunc[]divide[1000]] 
> }}}>
>
> | started:|<$view field="start" format="date" template="[UTC]MMM DD YYYY 
> 0hh:0mm:0ss.0XXX" />|
> | stopped:| <$view field="stop" format="date" template="[UTC]MMM DD YYYY 
> 0hh:0mm:0ss.0XXX" />|
> | elapsed:|<<min>> minutes <<sec>> seconds|
> </$vars>
> </$vars>
> </$vars>
> \end
>
> <$reveal default={{!!start}} type="match" text="">
>    <<startbutton>>
> </$reveal>
> <$reveal default={{!!start}} type="nomatch" text="">
>    <$reveal default={{!!stop}} type="match" text="">
>       <<stopbutton>>
>    </$reveal>
>    <$reveal default={{!!stop}} type="nomatch" text="">
>       <<startbutton>>
>    </$reveal>
> </$reveal>
> <<showtable>>
>
> notes:
> * startbutton() sets the "start" field using a complete zero-padded time 
> value including milliseconds.  This is the same format used by the TWCore 
> for the "created" and "modified" fields.
> * stopbutton() sets the "stop" field and calculates elapsed time (i.e, 
> stop - start)
> * showtable() calculates the elapsed time as minutes and seconds and 
> displays the results as a nicely formatted table, where:
>    * minutes ("min") are truncated to the nearest integer
>    * then multiplied by 60 to get the number of seconds in those whole 
> minutes ("minsec")
>    * seconds ("sec") subtracts the whole minutes (as "minsec") and then 
> rounds to 3 decimal places (i.e., milliseconds) by multiplying by 1000, 
> truncating, and dividing by 1000
> * The first $reveal shows the startbutton if no start value is stored
> * The second $reveal shows the stopbutton if a start value is stored but 
> no stop value is stored
> * The third $reveal shows the startbutton again if both start and stop 
> values are stored
>
> To use it:
> * place the above code into a tiddler named "Timer"
> * then, in any tiddler, simply write:
> {{||Timer}}
>
> Give it a try and let me know how it goes...
>
> enjoy,
> -e
>

-- 
You received this message because you are subscribed to the Google Groups 
"TiddlyWiki" group.
To unsubscribe from this group and stop receiving emails from it, send an email 
to tiddlywiki+unsubscr...@googlegroups.com.
To view this discussion on the web visit 
https://groups.google.com/d/msgid/tiddlywiki/29ae5102-7f07-4d40-b781-d11d205f8c60o%40googlegroups.com.

Reply via email to