A_TimeAddSub_s

Studies that have been contributed to the community by other users. If you’ve got something useful to share, that’s great!
bowlesj3
Posts: 1977
Joined: 21 Jul 2007
Has thanked: 155 times
Been thanked: 395 times

A_TimeAddSub_s

Postby bowlesj3 » 15 Nov 2009

Hi,

Learning to add and subtract time with the seconds can be a challenge. Remembering how it works is not that easy either. Once I figured it out I put together this function to document it and make it straight forward. You can use the function or learn from it and extract what you need to do the calculations directly.

You can cut and paste this in to a new function with the name I gave it and it will compile and give you the proper lineup.


John.

Code: Select all



{
Sample Calls
Result_s = A_TimeAddSub_s(Time_s, "A", 1, "S");  {Time_s add 1 second}
Result_s = A_TimeAddSub_s(Time_s, "A", 5, "M");  {Time_s add 5 minutes}
Result_s = A_TimeAddSub_s(Time_s, "A", 3, "H");  {Time_s add 3 hours}
Result_s = A_TimeAddSub_s(Time_s, "S", 3, "S");  {Time_s subtract 3 seconds}
Result_s = A_TimeAddSub_s(Time_s, "S", 3, "M");  {Time_s subtract 3 minutes}
Result_s = A_TimeAddSub_s(Time_s, "S", 2, "H");  {Time_s subtract 2 hours}
}
{
This is a test caller you can use to learn to trust it. Just copy the actual code out to a study, compile it and test it on a seconds chart.
inputs: Units(3);
Print( File("C:\A_DateTimeLearningTests1.txt"),
        " Time_s", " " ,
       Time_s, " --- " ,

    " add ", NumToStr(units,0), " Sec", " " ,
       A_TimeAddSub_s(Time_s,"A", units, "S") , " -" ,
 
       " add ", NumToStr(units,0), " min", " " ,
       A_TimeAddSub_s(Time_s,"A", units, "M") , " -" ,

       " add ", NumToStr(units,0), " hour", " " ,
        A_TimeAddSub_s(Time_s,"A", units, "H") , " -" ,
 
     " sub ", NumToStr(units,0), " Sec", " " ,
        A_TimeAddSub_s(Time_s,"S", units, "S") , " -" ,
 
       " sub ", NumToStr(units,0), " min", " " ,
        A_TimeAddSub_s(Time_s,"S", units, "M") , " -" ,
 
       " sub ", NumToStr(units,0), " hour", " " ,
       A_TimeAddSub_s(Time_s,"S", units, "H") , " " ,
 
                  " ");
}



inputs:
   TimeIn(NumericSimple),
   AddSub(StringSimple),
   UnitsIn(NumericSimple),
   UnitsToAdd(StringSimple);
   
variables:
   AddSubAmt(0),
   OneHour(1/24), {1 divided by the number of hours in a day}
   OneMinute(1/1440), {1 divided by the number of minutes in a day}
   OneSecond(1/86400); {1 divided by the number of seconds in a day}
   
if AddSub = "A" then
   AddSubAmt = 1;
if AddSub = "S" then
   AddSubAmt = -1;
 
if AddSubAmt = 0 then
   A_TimeAddSub_s = 0
else
   begin
   if UnitsToAdd = "S" then
      A_TimeAddSub_s = DateTime2ELTime_s(ELTimeToDateTime_s(TimeIn) + ((UnitsIn * OneSecond) * AddSubAmt));
   if UnitsToAdd = "M" then
      A_TimeAddSub_s = DateTime2ELTime_s(ELTimeToDateTime_s(TimeIn) + ((UnitsIn * OneMinute) * AddSubAmt));
   if UnitsToAdd = "H" then
      A_TimeAddSub_s = DateTime2ELTime_s(ELTimeToDateTime_s(TimeIn) + ((UnitsIn * OneHour) * AddSubAmt));
   end;
     
   

Return to “User Contributed Studies and Indicator Library”