F_RELATIVE_DATETIME

///////////////////////////////////////////////////////////////////////////////////////////////////////////////
// Function Name : f_relative_datetime
// Argument Name : adtm_start, Arg Type : DateTime, Pass By : Value
//                           al_offset, Arg Type : Long, Pass By : Value
// Return Type : Datetime
//                        Relative datetime.
//                        If any argument's value is NULL, function returns NULL.
//                        If any argument's value is Invalid, function returns 1900-01-01.
///////////////////////////////////////////////////////////////////////////////////////////////////////////////
// Description:      Given a datetime, find the relative datetime +/- n seconds
///////////////////////////////////////////////////////////////////////////////////////////////////////////////

datetime ldt_null
date ld_sdate
time lt_stime
long ll_date_adjust
long ll_time_adjust, ll_time_test

//Check parameters
If IsNull(adtm_start) or IsNull(al_offset) Then
    SetNull(ldt_null)
    Return ldt_null
End If

//Check for invalid date
If Not f_Is_Valid_DateTime(adtm_start) Then
    Return ldt_null
End If

//Initialize date and time portion
ld_sdate = date(adtm_start)
lt_stime = time(adtm_start)

//Find out how many days are contained
//Note: 86400 is # of seconds in a day
ll_date_adjust = al_offset /  86400
ll_time_adjust = mod(al_offset, 86400)

//Adjust date portion
ld_sdate = RelativeDate(ld_sdate, ll_date_adjust)

//Adjust time portion
//    Allow for time adjustments periods crossing over days
//    Check for time rolling forwards a day
If ll_time_adjust > 0 then

    ll_time_test = SecondsAfter(lt_stime,time('23:59:59'))

    If ll_time_test < ll_time_adjust Then

        ld_sdate = RelativeDate(ld_sdate,1)

        ll_time_adjust = ll_time_adjust - ll_time_test -1

        lt_stime = time('00:00:00')

    End If

    lt_stime = RelativeTime(lt_stime, ll_time_adjust)

//Check for time rolling backwards a day
ElseIf  ll_time_adjust < 0 then

    ll_time_test = SecondsAfter(lt_stime,time('00:00:00'))

    If   ll_time_test > ll_time_adjust Then

        ld_sdate = RelativeDate(ld_sdate,-1)

        ll_time_adjust = ll_time_adjust - ll_time_test +1

        lt_stime = time('23:59:59')

    End If

    lt_stime = RelativeTime(lt_stime, ll_time_adjust)

End If

return(datetime(ld_sdate,lt_stime))

0 comments:

Post a Comment