New subject, 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: 2180
Joined: 21 Jul 2007
Has thanked: 227 times
Been thanked: 429 times

New subject, TimeAddSub_s

Postby bowlesj3 » 14 Jan 2009

Is there a function kicking around to calculate a future or past time_s value. One that can add seconds or subtract seconds from a current time_s value. One that does not abort if the resulting time_s value is ahead of the currenttime_s. One that can put the results in any variable. One that is fast.

The reason is I am trying to get a future time_s value by offset and MC aborts when I do this.

Thanks,
John.
Last edited by bowlesj3 on 15 Jan 2009, edited 9 times in total.

bowlesj3
Posts: 2180
Joined: 21 Jul 2007
Has thanked: 227 times
Been thanked: 429 times

Postby bowlesj3 » 15 Jan 2009

I figured it out. No need for help. Just a little persistence. Here is the code to accomplish it with a print statement output you can try out. I did not try it over the midnight time span. I don't need to worry about this myself.

You could wrap the (long hard to remember) commands shown in the print statement into a simpler easier to remember set of functions that clutter your EL code less.
result = TimeAdd_s(time_s,OneSecond);
result = TimeSubtract_s(time_s,OneHour);

John.

Code: Select all


variables:
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}

Print( File("C:\Access\A_DateTimeLearningTests.txt"),
" Time_s", " " ,
Time_s, " --- " ,

" add 1Sec", " " ,
DateTime2ELTime_s(ELTimeToDateTime_s(Time_s) + OneSecond) , " " ,

" add 1min", " " ,
DateTime2ELTime_s(ELTimeToDateTime_s(Time_s) + OneMinute) , " " ,


" add 1hour", " " ,
DateTime2ELTime_s(ELTimeToDateTime_s(Time_s) + OneHour) , " " ,


" subtract 1Sec", " " ,
DateTime2ELTime_s(ELTimeToDateTime_s(Time_s) - OneSecond) , " " ,

" subtract 1min", " " ,
DateTime2ELTime_s(ELTimeToDateTime_s(Time_s) - OneMinute) , " " ,


" subtact 1hour", " " ,
DateTime2ELTime_s(ELTimeToDateTime_s(Time_s) - OneHour) , " " ,

" ");


Time_s 40630.00 --- add 1Sec 40631.00 add 1min 40730.00 add 1hour 50630.00 subtract 1Sec 40629.00 subtract 1min 40530.00 subtact 1hour 30630.00
Time_s 40640.00 --- add 1Sec 40641.00 add 1min 40740.00 add 1hour 50640.00 subtract 1Sec 40639.00 subtract 1min 40540.00 subtact 1hour 30640.00
Time_s 40650.00 --- add 1Sec 40651.00 add 1min 40750.00 add 1hour 50650.00 subtract 1Sec 40649.00 subtract 1min 40550.00 subtact 1hour 30650.00
Time_s 40700.00 --- add 1Sec 40701.00 add 1min 40800.00 add 1hour 50700.00 subtract 1Sec 40659.00 subtract 1min 40600.00 subtact 1hour 30700.00
Time_s 40710.00 --- add 1Sec 40711.00 add 1min 40810.00 add 1hour 50710.00 subtract 1Sec 40709.00 subtract 1min 40610.00 subtact 1hour 30710.00

bowlesj3
Posts: 2180
Joined: 21 Jul 2007
Has thanked: 227 times
Been thanked: 429 times

Postby bowlesj3 » 15 Jan 2009

Here is that simpler function which does it all (not tested over the midnight however). For speed it might be just as well to use the more complex set of commands above. Probably not much speed difference though.


Code: Select all

{A_TimeAddSub_s}
{
Result_s = A_TimeAddSub_s(Time_s, "A", 1, "S");
Result_s = A_TimeAddSub_s(Time_s, "A", 5, "M");
Result_s = A_TimeAddSub_s(Time_s, "A", 3, "H");
Result_s = A_TimeAddSub_s(Time_s, "A", 3, "H");
Result_s = A_TimeAddSub_s(Time_s, "S", 1, "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;

Here is the caller test.

Code: Select all




Print( File("C:\Access\A_DateTimeLearningTests1.txt"),
" Time_s", " " ,
Time_s, " --- " ,

" add 1Sec", " " ,
A_TimeAddSub_s(Time_s,"A", 1, "S") , " " ,

" add 1min", " " ,
A_TimeAddSub_s(Time_s,"A", 1, "M") , " " ,

" add 1hour", " " ,
A_TimeAddSub_s(Time_s,"A", 1, "H") , " " ,

" subtract 1Sec", " " ,
A_TimeAddSub_s(Time_s,"S", 1, "S") , " " ,

" subtract 1min", " " ,
A_TimeAddSub_s(Time_s,"S", 1, "M") , " " ,

" subtact 1hour", " " ,
A_TimeAddSub_s(Time_s,"S", 1, "H") , " " ,

" ");
Result is the same.
Last edited by bowlesj3 on 15 Jan 2009, edited 1 time in total.

bowlesj3
Posts: 2180
Joined: 21 Jul 2007
Has thanked: 227 times
Been thanked: 429 times

Postby bowlesj3 » 15 Jan 2009

I just ran another test with units of 3 instead of 1 (see below) and it does work.

Code: Select all


Print( File("C:\Access\A_DateTimeLearningTests1.txt"),
" Time_s", " " ,
Time_s, " --- " ,

" add 1Sec", " " ,
A_TimeAddSub_s(Time_s,"A", 3, "S") , " " ,

" add 1min", " " ,
A_TimeAddSub_s(Time_s,"A", 3, "M") , " " ,

" add 1hour", " " ,
A_TimeAddSub_s(Time_s,"A", 3, "H") , " " ,

" subtract 1Sec", " " ,
A_TimeAddSub_s(Time_s,"S", 3, "S") , " " ,

" subtract 1min", " " ,
A_TimeAddSub_s(Time_s,"S", 3, "M") , " " ,

" subtact 1hour", " " ,
A_TimeAddSub_s(Time_s,"S", 3, "H") , " " ,

" ");

bowlesj3
Posts: 2180
Joined: 21 Jul 2007
Has thanked: 227 times
Been thanked: 429 times

Postby bowlesj3 » 16 Jan 2009

It appears to work over the midnight as well. I cleaned up the test code and included it below with the results. My checks were not all that thorough so if you are using this you should do your own checks.




Code: Select all

inputs: Units(3);

Print( File("C:\Access\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") , " " ,

" ");
Time_s 235740.00 --- add 3 Sec 235743.00 - add 3 min 40.00 - add 3 hour 25740.00 - sub 3 Sec 235737.00 - sub 3 min 235440.00 - sub 3 hour 205740.00
Time_s 235750.00 --- add 3 Sec 235753.00 - add 3 min 50.00 - add 3 hour 25750.00 - sub 3 Sec 235747.00 - sub 3 min 235450.00 - sub 3 hour 205750.00
Time_s 235800.00 --- add 3 Sec 235803.00 - add 3 min 100.00 - add 3 hour 25800.00 - sub 3 Sec 235757.00 - sub 3 min 235500.00 - sub 3 hour 205800.00
Time_s 235810.00 --- add 3 Sec 235813.00 - add 3 min 110.00 - add 3 hour 25810.00 - sub 3 Sec 235807.00 - sub 3 min 235510.00 - sub 3 hour 205810.00
Time_s 235820.00 --- add 3 Sec 235823.00 - add 3 min 120.00 - add 3 hour 25820.00 - sub 3 Sec 235817.00 - sub 3 min 235520.00 - sub 3 hour 205820.00
Time_s 235830.00 --- add 3 Sec 235833.00 - add 3 min 130.00 - add 3 hour 25830.00 - sub 3 Sec 235827.00 - sub 3 min 235530.00 - sub 3 hour 205830.00
Time_s 235840.00 --- add 3 Sec 235843.00 - add 3 min 140.00 - add 3 hour 25840.00 - sub 3 Sec 235837.00 - sub 3 min 235540.00 - sub 3 hour 205840.00
Time_s 235850.00 --- add 3 Sec 235853.00 - add 3 min 150.00 - add 3 hour 25850.00 - sub 3 Sec 235847.00 - sub 3 min 235550.00 - sub 3 hour 205850.00
Time_s 235900.00 --- add 3 Sec 235903.00 - add 3 min 200.00 - add 3 hour 25900.00 - sub 3 Sec 235857.00 - sub 3 min 235600.00 - sub 3 hour 205900.00
Time_s 235910.00 --- add 3 Sec 235913.00 - add 3 min 210.00 - add 3 hour 25910.00 - sub 3 Sec 235907.00 - sub 3 min 235610.00 - sub 3 hour 205910.00
Time_s 235920.00 --- add 3 Sec 235923.00 - add 3 min 220.00 - add 3 hour 25920.00 - sub 3 Sec 235917.00 - sub 3 min 235620.00 - sub 3 hour 205920.00
Time_s 235930.00 --- add 3 Sec 235933.00 - add 3 min 230.00 - add 3 hour 25930.00 - sub 3 Sec 235927.00 - sub 3 min 235630.00 - sub 3 hour 205930.00
Time_s 235940.00 --- add 3 Sec 235943.00 - add 3 min 240.00 - add 3 hour 25940.00 - sub 3 Sec 235937.00 - sub 3 min 235640.00 - sub 3 hour 205940.00
Time_s 235950.00 --- add 3 Sec 235953.00 - add 3 min 250.00 - add 3 hour 25950.00 - sub 3 Sec 235947.00 - sub 3 min 235650.00 - sub 3 hour 205950.00


Time_s 0.00 --- add 3 Sec 3.00 - add 3 min 300.00 - add 3 hour 30000.00 - sub 3 Sec 235957.00 - sub 3 min 235700.00 - sub 3 hour 210000.00
Time_s 10.00 --- add 3 Sec 13.00 - add 3 min 310.00 - add 3 hour 30010.00 - sub 3 Sec 7.00 - sub 3 min 235710.00 - sub 3 hour 210010.00
Time_s 20.00 --- add 3 Sec 23.00 - add 3 min 320.00 - add 3 hour 30020.00 - sub 3 Sec 17.00 - sub 3 min 235720.00 - sub 3 hour 210020.00
Time_s 30.00 --- add 3 Sec 33.00 - add 3 min 330.00 - add 3 hour 30030.00 - sub 3 Sec 27.00 - sub 3 min 235730.00 - sub 3 hour 210030.00
Time_s 40.00 --- add 3 Sec 43.00 - add 3 min 340.00 - add 3 hour 30040.00 - sub 3 Sec 37.00 - sub 3 min 235740.00 - sub 3 hour 210040.00
Time_s 50.00 --- add 3 Sec 53.00 - add 3 min 350.00 - add 3 hour 30050.00 - sub 3 Sec 47.00 - sub 3 min 235750.00 - sub 3 hour 210050.00
Time_s 100.00 --- add 3 Sec 103.00 - add 3 min 400.00 - add 3 hour 30100.00 - sub 3 Sec 57.00 - sub 3 min 235800.00 - sub 3 hour 210100.00
Time_s 110.00 --- add 3 Sec 113.00 - add 3 min 410.00 - add 3 hour 30110.00 - sub 3 Sec 107.00 - sub 3 min 235810.00 - sub 3 hour 210110.00
Time_s 120.00 --- add 3 Sec 123.00 - add 3 min 420.00 - add 3 hour 30120.00 - sub 3 Sec 117.00 - sub 3 min 235820.00 - sub 3 hour 210120.00
Time_s 130.00 --- add 3 Sec 133.00 - add 3 min 430.00 - add 3 hour 30130.00 - sub 3 Sec 127.00 - sub 3 min 235830.00 - sub 3 hour 210130.00
Time_s 140.00 --- add 3 Sec 143.00 - add 3 min 440.00 - add 3 hour 30140.00 - sub 3 Sec 137.00 - sub 3 min 235840.00 - sub 3 hour 210140.00


Return to “User Contributed Studies and Indicator Library”