// Function Name : f_closest_date
// Argument Name : as_dayname, Arg Type : String, Pass By : Value
// ad_testdate, Arg Type : Date, Pass By : Value
// Return Type : Date
//
// Purpose: returns the date that is the closest day name (as_dayname)
// to as_testdate
///////////////////////////////////////////////////////////////////////////////////////////////////////////////
// Arguments: as_dayname a valid name of a day (Sunday, Monday, ...)
// ad_testdate any valid date
//
// Returns: date the date which is:
// 1) the closest date to as_test date AND
// 2) which has the same day name as as_dayname
///////////////////////////////////////////////////////////////////////////////////////////////////////////////
integer li_testdate
integer li_dayname
integer li_difference
// Obtain the day number of ad_testdate and as_dayname
li_testdate = DayNumber (ad_testdate)
Choose Case as_dayname
Case 'Sunday'
li_dayname = 1
Case 'Monday'
li_dayname = 2
Case 'Tuesday'
li_dayname = 3
Case 'Wednesday'
li_dayname = 4
Case 'Thursday'
li_dayname = 5
Case 'Friday'
li_dayname = 6
Case 'Saturday'
li_dayname = 7
End Choose
// If ad_testdate is already the same day name as as_dayname, then
// return ad_testdate, else calculate the closest date.
If li_dayname = li_testdate Then
Return ad_testdate
Else
li_difference = li_dayname - li_testdate
If li_difference < -3 Then
li_difference = li_difference + 7
Else
If li_difference > 3 Then
li_difference = li_difference - 7
End If
End If
Return RelativeDate (ad_testdate, li_difference)
End If
// Argument Name : as_dayname, Arg Type : String, Pass By : Value
// ad_testdate, Arg Type : Date, Pass By : Value
// Return Type : Date
//
// Purpose: returns the date that is the closest day name (as_dayname)
// to as_testdate
///////////////////////////////////////////////////////////////////////////////////////////////////////////////
// Arguments: as_dayname a valid name of a day (Sunday, Monday, ...)
// ad_testdate any valid date
//
// Returns: date the date which is:
// 1) the closest date to as_test date AND
// 2) which has the same day name as as_dayname
///////////////////////////////////////////////////////////////////////////////////////////////////////////////
integer li_testdate
integer li_dayname
integer li_difference
// Obtain the day number of ad_testdate and as_dayname
li_testdate = DayNumber (ad_testdate)
Choose Case as_dayname
Case 'Sunday'
li_dayname = 1
Case 'Monday'
li_dayname = 2
Case 'Tuesday'
li_dayname = 3
Case 'Wednesday'
li_dayname = 4
Case 'Thursday'
li_dayname = 5
Case 'Friday'
li_dayname = 6
Case 'Saturday'
li_dayname = 7
End Choose
// If ad_testdate is already the same day name as as_dayname, then
// return ad_testdate, else calculate the closest date.
If li_dayname = li_testdate Then
Return ad_testdate
Else
li_difference = li_dayname - li_testdate
If li_difference < -3 Then
li_difference = li_difference + 7
Else
If li_difference > 3 Then
li_difference = li_difference - 7
End If
End If
Return RelativeDate (ad_testdate, li_difference)
End If
0 comments:
Post a Comment