TimeNum Addition & Subtraction

Hi, looking for a function that can add/subtract from TimeNum(). Tried using

dt = DateTimeConvert( 2, Null, 211700 );
newTime = DateTimeToStr( DateTimeAdd( dt, -90, in1Minute ), 0 );

without success. ‘newTime’ ends up equal to 1970-01-01 without a time portion. When using ‘2’ for the parameter, DateTimeToStr returns an empty string.

Below is my implementation for subtraction only. Any ideas/improvements would be great. Thanks

//    set period in minutes
//    get hour & minute components

period    = 90;    //Param( "Period in minutes:", 90, 1, 1440, 1 );
perHrs    = period / 60;
perHrPt   = int( perHrs );
perMnPt   = period - ( perHrPt * 60 );

//    set end time

endTime   = "211700";    //ParamStr( "End time as string (hhmmss):", "1700" );

// handle subtraction that passes over midnight
// add 'endTime' hour to 240000 to 

len       = StrLen( endTime );

endHr     = IIf( len == 6, StrToNum( StrLeft( endTime, 2 ) ),
            IIf( len == 5, StrToNum( StrLeft( endTime, 1 ) ),
            0 ));

endHr     = 240000 + ( endHr * 10000 );

//    get 'endTime' minute

endMn     = IIf( len == 6, StrToNum( StrMid( endTime, 2, 2 ) ),
            IIf( len == 5, StrToNum( StrMid( endTime, 1, 2 ) ),
            IIf( len == 4, StrToNum( StrLeft( endTime, 2 ) ),
            IIf( len == 3, StrToNum( StrLeft( endTime, 1 ) ),
            0 ))));

//    do subtraction

newHr     = endHr - ( perHrPt * 10000 );
newMin    = ( endMn - perMnPt );

//    adjust if hour threshold is passed

if( newMin < 0 )
{
    newHr = newHr - 10000;
    newMin= 60 + newMin;
}

//    finalize new time

newTime = newHr + ( newMin * 100 );
newTime = IIf( newTime >= 240000, newTime - 240000, newTime );

@unkunk2 have you tried using DateTimeDiff function,

https://www.amibroker.com/guide/afl/datetimediff.html

@portfoliobuilder, thanks for the reply. i’m after a function that given a time of day say 21:17:00, and a period, say 90 minutes, will return 19:47:00 = ( 21:17:00 - 90 minutes ). my understanding of DateTimeDiff is that it will return the ‘90’ given the 2 bookend times of 19:47:00 and 21:17:00. though i certainly could be missing something.

@unkunk2 OK I see, then try DateTimeAdd

http://www.amibroker.com/guide/afl/datetimeadd.html

@portfoliobuilder thanks, tried that. see the first block of AFL in my original post

@portfoliobuilder to expand on my previous reply. the issue with DateTimeAdd is it requires a value in the DateTime format. TimeNum’s format is a number. so i tried converting a TimeNum to DateTime format using DateTimeConvert with format = 2 which converts from DateNum & TimeNum to DateTime. the issue there was a DateNum appears to be required…i have only a TimeNum.

many thanks to @portfoliobuilder for getting the creative juices flowing again:

dn = LastValue( DateNum() );             // any 'DateNum' will work
tn = 211700;                             // reference time
dt = DateTimeConvert( 2, dn, tn );       // create 'DateTime'
dt2 = DateTimeAdd( dt, -90, in1Minute ); // do arithmetic
nt = DateTimeConvert( 1, dt2 );          // grab time portion of 'dt2'
7 Likes