On Sunday, June 21, 2020 at 12:24:57 PM UTC-7, si wrote:
>
> Wow Eric that's great thank you!
>

You are very welcome!
 

> 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.
>

I like what you've done here!

The first trick to calculating the elapsed and total time is to be able to 
get just the last start and stop timestamps.
The next bit is actually more involved than I previously considered.  It 
turns out that simply subtracting the timestamps does NOT give you the 
correct results because the timestamps do not represent "current time as 
seconds", but rather a compound days/hours/minutes/seconds value.  To 
properly compute the difference between two timestamps, we need to first 
convert the timestamps from their compound format into pure seconds.  Then 
we can subtract the values to get the difference.

It took me a while to get it just right, but here's some new code that does 
the job:
\define start_timer()
<$button actions="""
   <$vars time=<<now YYYY0MM0DD0hh0mm0ss0XXX>> >
   <$action-setfield startlist={{{ [{!!startlist}addsuffix[ 
]addsuffix<time>trim[]] }}}/>
   <$action-setfield started=<<time>> stopped="" elapsed=""/>
   <$action-setfield button="stop"/>
   </$vars>
"""> start
</$button>
\end

\define stop_timer()
<$button actions="""
   <$vars time=<<now YYYY0MM0DD0hh0mm0ss0XXX>> >
   <$action-setfield stoplist={{{ [{!!stoplist}addsuffix[ 
]addsuffix<time>trim[]] }}}/>
   <$vars 
      msec={{{ [{!!started}split[]last[5]join[]] }}}
      min={{{ [{!!started}split[]butlast[5]last[2]join[]multiply[60]] }}}
      hour={{{ [{!!started}split[]butlast[7]last[2]join[]multiply[3600]] }}}
      day={{{ [{!!started}split[]butlast[9]last[2]join[]multiply[86400]] 
}}}>
   <$vars start={{{ [<msec>add<min>add<hour>add<day>] }}}>
   <$vars
      msec={{{ [<time>split[]last[5]join[]] }}}
      min={{{ [<time>split[]butlast[5]last[2]join[]multiply[60]] }}}
      hour={{{ [<time>split[]butlast[7]last[2]join[]multiply[3600]] }}}
      day={{{ [<time>split[]butlast[9]last[2]join[]multiply[86400]] }}}>
   <$vars stop={{{ [<msec>add<min>add<hour>add<day>] }}}>

   <$action-setfield stopped=<<time>> elapsed={{{ [<stop>subtract<start>] 
}}}/>
   <$action-setfield total={{{ [{!!total}add{!!elapsed}] }}}/>
   <$action-setfield button="start"/>
   </$vars>
   </$vars>
   </$vars>
   </$vars>
   </$vars>
"""> stop
</$button>
\end

\define reset_timer()
<$button> reset
   <$action-deletefield startlist stoplist started stopped elapsed total 
button />
</$button>
\end

\define showtable()
<$vars
   elapsed_min={{{ [{!!elapsed}divide[60000]trunc[]] }}}
   elapsed_minsec={{{ [<elapsed_min>multiply[60]] }}}
   elapsed_sec={{{ 
[{!!elapsed}divide[1000]subtract<elapsed_minsec>multiply[1000]trunc[]divide[1000]]
 
}}}>
<$vars
   total_min={{{ [{!!total}divide[60000]trunc[]] }}}
   total_minsec={{{ [<total_min>multiply[60]] }}}
   total_sec={{{ 
[{!!total}divide[1000]subtract<total_minsec>multiply[1000]trunc[]divide[1000]] 
}}}>

| started:|<$view field="started" format="date" template="[UTC]MMM DD YYYY 
0hh:0mm:0ss.0XXX" />|
| stopped:|<$view field="stopped" format="date" template="[UTC]MMM DD YYYY 
0hh:0mm:0ss.0XXX" />|
| elapsed:|<<elapsed_min>> minutes <<elapsed_sec>> seconds|
| total:|<<total_min>> minutes <<total_sec>> seconds|
</$vars>
</$vars>
\end

<$reveal default={{!!button}} type="nomatch" text="stop">
   <<start_timer>> <<reset_timer>>
</$reveal>
<$reveal default={{!!button}} type="match" text="stop">
   <<stop_timer>> <<reset_timer>>
</$reveal>

<<showtable>>

Notes:
* I renamed a stuff to hopefully make the code more understandable
* I added a "reset" button to easily clear all the fields and start fresh 
(this was really important during testing!)

As before, simply put the above code into a tiddler (e.g., "Timer") and 
then transclude it in other tiddlers by writing:
{{||Timer}}

I've also uploaded the code to a standalone TW file here: 
http://TiddlyTools.com/timer.html <http://tiddlytools.com/timer.html>, so 
you can just go there and press the "save" button to download a copy

Let me know how it goes...

enjoy,
-e
Eric Shulman
TiddlyTools.com: "Small Tools for Big Ideas!" (tm)
InsideTiddlyWiki: http://TiddlyTools.com/InsideTW



-- 
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/fdf2ddf0-a833-4e9e-b492-ed8524c9396co%40googlegroups.com.

Reply via email to