Welcome, Guest. Please Login
Tinderbox
  News:
IMPORTANT MESSAGE! This forum has now been replaced by a new forum at http://forum.eastgate.com and no further posting or member registration is allowed. The forum is still accessible via read-only access for reference purposes. If you wish to discuss content here, please use the new forum. N.B. - posting in the new forum requires a fresh registration in the new forum (sorry - member data can't be ported).
  HomeHelpSearchLogin  
 
Pages: 1
Send Topic Print
Reporting a duration (Read 5711 times)
Mark Anderson
YaBB Administrator
*
Offline

User - not staff!

Posts: 5689
Southsea, UK
Reporting a duration
Jul 4th, 2012, 7:07am
 
Whilst doing some import/export stuff, I wondered how I might replicate data from another app that was reporting an event duration. IOW, if I imported this event info, changed start or end date, it would be nice to pass back a correct duration. Here's a little rule I used for my Event prototype, set up via a code note. WARNING - this version has an edge case error, use the revised example further down-thread

Code:
$TempDuration = minutes($StartDate,$EndDate);
if($TempDuration > 1440) {
	$Days = days($StartDate,$EndDate);
} else {
	$Days = 0;
};
if($TempDuration > 60) {
	$Hours = (mod($TempDuration,1440))/60;
	if($Hours > 0){
		$Hours = round($Hours-0.5);
	}else{
		 $Hours = round($Hours +0.5);
	}
} else {
	$Hours = 0;
};
if($TempDuration) {
	$Minutes = mod((mod($TempDuration,1440)),60);
} else {
	$Minutes = 0;
};
$Duration = $Days + " days, " + $Hours + " hours, " + $Minutes + " minutes"; 


The $TempDuration was only used as I didn't want to nest functions, though I then managed to happily nest 2 mod() functions.

Attributes needed (with attribute data type):
  • $TempDuration - number type.
  • $Days - number type.
  • $Hours - number type.
  • $Minutes - number type.
  • $Duration - string type.
The $Hours bit is more complex as we've no hour() operator unlike days(0 and minutes(). So, we mod() out the whole days from $TempDuration (which is in minutes) and then divide by 60. But we only want whole hours and TB's round() works in the normal fashion rounding either down or up depending on the decimal places in the figure. So we have to add custom 'floor' code, so as to always round down. The floor code I used came from here. Later: there are two edge case issues missed here, fixed in my next example of the code further below. I forgot to test for exactly 1 day (i.e. $TempDuration == 1440), and that if $TempDuration is a whole day or multiple thereof, to ignore hour/minute tests and set those attributes to zero.

You could extend the granularity to years/months if desired. Given all the conditionals you can see why it makes sense to use a code note rather than try and type all this in the rule box on the Rename dialog.
Enjoy!

Edited:
Added info of attribute types, flagged error in code sample.
Back to top
 
« Last Edit: Jan 02nd, 2014, 5:32am by Mark Anderson »  

--
Mark Anderson
TB user and Wiki Gardener
aTbRef v6
(TB consulting - email me)
WWW shoantel   IP Logged
Andrej Močan
Full Member
*
Offline



Posts: 8
Ljubljana, Slovenia
Re: Reporting a duration
Reply #1 - Jan 1st, 2014, 3:23pm
 
I tried to use your code but it seems that Tinderbox is ignoring the whole section of code after the first line

$TempDuration = minutes($StartDate,$EndDate)


$TempDuration returns result in minutes. I tried to add the code into an Event prototype and since it didn't work I tried also with an agent, no success. Would you know what is wrong?

Thanks
Andrej
Back to top
 
 
  IP Logged
Mark Anderson
YaBB Administrator
*
Offline

User - not staff!

Posts: 5689
Southsea, UK
Re: Reporting a duration
Reply #2 - Jan 1st, 2014, 5:34pm
 
Some odd edge cases there, but this seems to work. I also adjusted the string creation at the end to allow for day vs. days etc. Here's the revised code, which works for me in v5.12.2:
Code:
$TempDuration = minutes($StartDate,$EndDate);
if($TempDuration >= 1440) {
	$Days = days($StartDate,$EndDate);
} else {
	$Days = 0;
};
if(mod($TempDuration,1440)!=0) {
	if($TempDuration > 60) {
		$Hours = (mod($TempDuration,1440))/60;
		if($Hours > 0) {
			$Hours = round($Hours-0.5);
		} else {
			$Hours = round($Hours+0.5);
		}
	} else {
		$Hours = 0;
	};
	if($TempDuration) {
		$Minutes = mod((mod($TempDuration,1440)),60);
	} else {
		$Minutes = 0;
	};
} else {
	$Hours = 0;
	$Minutes = 0;
};
if($Days == 1) {
	$Duration = $Days + " day, ";
} else {
	$Duration = $Days + " days, ";
};
if($Minutes == 1) {
	$Duration = $Duration + $Hours + " hour, ";
} else {
	$Duration = $Duration + $Hours + " hours, ";
};
if($Minutes == 1) {
	$Duration = $Duration + $Minutes + " minute";
} else {
	$Duration = $Duration + $Minutes + " minutes";
};
 


The edge case I'd missed was that I forgot to allow for the $TempDuration being exactly one or more days.

Attributes needed (with attribute data type):
  • $TempDuration - number type.
  • $Days - number type.
  • $Hours - number type.
  • $Minutes - number type.
  • $Duration - string type.
Edited:
Added missing zero-ing out of hours/minutes for whole day(s), improved whitespace formatting in code sample, added listing of attribute (types) required.
Back to top
 
« Last Edit: Jan 2nd, 2014, 5:33am by Mark Anderson »  

--
Mark Anderson
TB user and Wiki Gardener
aTbRef v6
(TB consulting - email me)
WWW shoantel   IP Logged
Andrej Močan
Full Member
*
Offline



Posts: 8
Ljubljana, Slovenia
Re: Reporting a duration
Reply #3 - Jan 2nd, 2014, 3:52am
 
The code still returns just duration in minutes. Maybe I'm doing something wrong. Could you please check https://drive.google.com/file/d/0B4vAK70uxEixdTdnLUxybXRNVFk/edit?usp=sharing. It's just a sample with Prototypes and two diary test notes.

Thanks,
Andrej
Back to top
 
 
  IP Logged
Mark Anderson
YaBB Administrator
*
Offline

User - not staff!

Posts: 5689
Southsea, UK
Re: Reporting a duration
Reply #4 - Jan 2nd, 2014, 5:19am
 
Several obvious set-up issues:
  • No $Days attribute.
  • $TempDuration, $Days, $Hours, $Minutes are all Number-type attributes - you've used String-type (the default if you don't select a type when creating the attribute). Testing mod($String,1440) is not the same as mod($Number,1440)
  • I think you had at least one colon (:) instead of a semi-colon (;) in your rule. Just typing/pasting big code section into a rule box is a pain as the formatting is lost and it's hard to see everything - this is where the 'code note' concept really helps.
I've fixed the above errors and a fixed copy of your file is here: test_file.zip. In it I've implemented the rule via a code note, added the 'hidden' attributes to the Events prototype for testing (that way they can easily be removed when happy). Also I've revealed the attributes in column view which is another simple way of reviewing attributes (especially numbers) during testing.

I'll also clarify attribute data types up-thread for later readers.
Back to top
 
« Last Edit: Jan 2nd, 2014, 5:19am by Mark Anderson »  

--
Mark Anderson
TB user and Wiki Gardener
aTbRef v6
(TB consulting - email me)
WWW shoantel   IP Logged
Andrej Močan
Full Member
*
Offline



Posts: 8
Ljubljana, Slovenia
Re: Reporting a duration
Reply #5 - Jan 2nd, 2014, 6:27am
 
I'm sorry, I'm using Tinderbox just for a week or so and I'm quite inexperienced.

Thank you
Andrej
Back to top
 
 
  IP Logged
Mark Anderson
YaBB Administrator
*
Offline

User - not staff!

Posts: 5689
Southsea, UK
Re: Reporting a duration
Reply #6 - Jan 2nd, 2014, 9:09am
 
Don't apologise - one reason for The whole a forum like this is so more experienced folk can help those still finding their way. Open-ended apps aren't easy to explain so experimenting with a bit of hand-holding is a great way to bootstrap yourself into more advanced use. Keep asking questions!
Back to top
 
 

--
Mark Anderson
TB user and Wiki Gardener
aTbRef v6
(TB consulting - email me)
WWW shoantel   IP Logged
Pages: 1
Send Topic Print