!*********************************************************************** ! Copyright (c) 1996,1997 Axis Developments ! All Rights Reserved !*********************************************************************** ! ! Library : DateFunc.SQC ! ! Report Name : Date functions Library ! ! Date Created : February 20, 1996 ! ! Author : Robert Goshko ! Axis Developments ! #12 Glamorgan Drive ! Sherwood Park, Alberta, Canada ! T8A 2Y4 ! ! rgoshko@ibm.net ! ! Description : This library includes several functions for ! performing several different calculations on dates in ! the SQR environment as there are no built in date ! functions. ! ! It is noted that some of the functions supplied in ! this library are duplicated by some database ! platforms. The intention of this library is to be ! database independant. ! ! Disclaimer : This software is provided "as is" there is no ! gauruntee provided or implied by the author. ! !*********************************************************************** ! ! Functions : Get-Date-Number( #Year, #Month, #Date, :#Number ) ! * Will return the date number within the current ! year (eg. 1996-01-01 is day number 1). ! Get-Month-First-Last-Dates( #Year, #Month, ! :$FirstDate, :$LastDate ) ! * Will return the dates (format yyyy-mm-dd) of the ! first and last days of the month. ! Get-Month-Name( #Month, :$MonthName ) ! * Will return the character name of the month. ! Get-Day-Of-Week( $Date, :#DOW ) ! * Will return the number of the day of the week ! that the date falls on (eg. 1996-01-01 falls on ! a Monday, so a 2 is returned). Sunday is day 1 ! and Saturday is day 7. ! Convert-DOW-To-Word( #DOW, :$DayOfWeek ) ! * Will return the name of the number of the day of ! the week. ! Get-Number-Of-Work-Days( #Year, #Month, :#Work ) ! * Will return the number of working days (Monday ! to Friday) within a specific month. ! Get-Work-Days-Between( $Start, $End, :#Work ) ! * Will return the number of working days (Monday ! to Friday) between any to dates. ! Subtract-From-Date( $Date, #Value, $Type, :$NewDate ) ! * Will subtract the number of days, months, or ! years from a date. ! Convert-To-Julian( $Date, :#JD ) ! * Will convert a Gregorian date (yyyy-mm-dd) to ! a Julian date (number of days elapsed since ! December 31, 4714 B.C.). ! Convert-From-Julian( #JD, :$Date ) ! * Will Convert a Julian date to a Gregorian date ! (yyyy-mm-dd). ! Add-To-Date( $Date, #Value, $Type, :$NewDate ) ! * Will add the specified number of days, months, ! or years to the date. ! Date-Difference( $FromDate, $ToDate, $Type, :#Value ) ! * Will return the number of days, months, or years ! between two dates. ! Calculate-Age ( $BirthDate, $AsOfDate, :#Age ) ! * Will determine a persons age as of the date ! provided. ! !*********************************************************************** ! ! - Usage Notes - ! ! Dates : When ever a date is in string format ($Date), it ! MUST be in the format of yyyy-mm-dd (4 digit year, ! 2 digit month with leading zero, and 2 digit day with ! leading zeros. ! ! Julian : When using the Julian date functions, the accuracy is ! good for any dates after December 31, 1600 A.D. ! (This is due to the fact that our current calendar ! started on October 15, 1582 and not January 1, 0001). ! !*********************************************************************** ! ! - Special Thanks - ! ! Grant Foster - From the American Association of Variable Star ! Observers (AASVO) for help in the Gregorian to Julian ! to Gregorian date conversion calculations. ! !*********************************************************************** ! ! - Change Log - ! ! %%n%% programmer , ! %%n%% ! %%n%% change description ! !*********************************************************************** ! ! *** NOTE *** NOTE *** NOTE *** NOTE *** NOTE *** NOTE *** ! ! Please complete all information in the header so that during ! future changes/upgrades, report usage can be quickly determined. ! ! When making changes, enter verbose descriptions so that in the ! future, some one knows what was done to this report. ! !*********************************************************************** #IFNDEF INC__DATEFUNC_SQC ! This will deture multiple #DEFINE INC__DATEFUNC_SQC 1 ! inclusions of this file !***************************************************************** ! ! Procedure : Get-Date-Number ! ! Author : Robert Goshko ! ! Date Created : February 20, 1996 ! ! Description : This function will take a year, month, and date ! and return the number of that date within the ! current year (eg. Febuary 20, 1996 is the 51st ! day of 1996) ! ! - Variables - ! ! Input : #Year - The year (use 1996 not 96 ) ! #Month - The month (1 to 12) ! #Date - The date (1 to 31) ! Returned : #Number - The number of the date in the given ! year ! Modified : n/a ! Used/Created : #Leap1 - First variable used in the leap year ! calculation ! #Leap2 - Second variable used in the leap year ! calculation ! #Leap3 - Third variable used in the leap year ! calculation ! #Leap - The final variable used in the leap year ! calculation ! #Base - Used in calculating the base number of ! days in the year ! ! - Tables - ! ! Select : n/a ! Insert : n/a ! Update : n/a ! Delete : n/a ! !***************************************************************** BEGIN-PROCEDURE Get-Date-Number ( #Year, #Month, #Date, :#Number ) LET #Leap1 = TRUNC( ( ( 4 - ( TRUNC( ( #Year % 4 ), 0 ) ) ) / 4 ), 0 ) LET #Leap2 = TRUNC( ( ( 100 - ( TRUNC( ( #Year % 100 ), 0 ) ) ) / 100 ), 0 ) LET #Leap3 = TRUNC( ( ( 400 - ( TRUNC( ( #Year % 400 ), 0 ) ) ) / 400 ), 0 ) LET #Leap = ( 2 - #Leap1 + #Leap2 - #Leap3 ) * ( TRUNC( ( ( #Month + 9 ) / 12 ), 0 ) ) LET #Base = TRUNC( 30 * ( #Month - 1 ) + #Date + ( #Month + ( TRUNC( ( #Month / 9 ), 0 ) ) ) / 2, 0 ) LET #Number = #Base - #Leap !***************************************************************************** ! ! This is the original Modula-2 code that his function is based on. Since ! it uses integers, ANYTHING after the decimal place is dropped (not rounded) ! that is why there are all the TRUNC functions used in my calculations since ! SQR does not have strict integer variables. ! ! result := 30 * (month -1) + day + ! (month + (month DIV 9)) DIV 2 - ! (2 - (4 - (year MOD 4)) DIV 4 + ! (100 - (year MOD 100)) DIV 100 - ! (400 - (year MOD 400)) DIV 400) * ((month + 9) DIV 12); ! !***************************************************************************** END-PROCEDURE !***************************************************************** ! ! Procedure : Get-Month-First-Last-Dates ! ! Author : Robert Goshko ! ! Date Created : February 20, 1996 ! ! Description : This procedure will take a month and a year and ! return the first date of the month, and the last ! date of a month. ! ! - Variables - ! ! Input : #Year - The year (use 1996 not 96 ) ! #Month - The month (1 to 12) ! Returned : $FirstDate - The first date of the month ! ( Format: yyyy-mm-dd ) ! $LastDate - The last date of the month ! ( Format: yyyy-mm-dd ) ! Modified : n/a ! Used/Created : $Year - The #Year variable converted to text ! $Month - The #Month variable converted to text ! padded with a leading 0 for single ! digits ! #Start - The number in the current year of the ! first day of the month ! #End - The number in the current year of the ! first day of the next month ( + the number ! of days in this year if it is January 1 ) ! #OffSet - The number of days (total) in this year ! when the calculating for December ! #Days - The number of days in the month, and the ! date of the last day of the month ! $Days - The #Days variable converted to a string ! ! - Tables - ! ! Select : n/a ! Insert : n/a ! Update : n/a ! Delete : n/a ! !***************************************************************** BEGIN-PROCEDURE Get-Month-First-Last-Dates ( #Year, #Month, :$FirstDate, :$LastDate ) LET $Year = TO_CHAR( #Year ) LET $Month = EDIT( TO_CHAR( #Month ), '00' ) STRING $Year $Month '01' BY '-' INTO $FirstDate DO Get-Date-Number ( #Year, #Month, 1, #Start ) ADD 1 TO #Month IF ( #Month = 13 ) LET #Month = 1 ADD 1 TO #Year END-IF DO Get-Date-Number( #Year, #Month, 1, #End ) SUBTRACT 1 FROM #Month IF ( #Month = 0 ) SUBTRACT 1 FROM #Year LET #Month = 12 DO Get-Date-Number( #Year, #Month, 31, #OffSet ) ELSE LET #OffSet = 0 END-IF ADD #OffSet TO #End LET #Days = #End - #Start LET $Days = TO_CHAR( #Days ) STRING $Year $Month $Days BY '-' INTO $LastDate END-PROCEDURE !***************************************************************** ! ! Procedure : Get-Month-Name ! ! Author : Robert Goshko ! ! Date Created : February 20, 1996 ! ! Description : This function takes the number of the month and ! returns the name of the month. ! ! - Variables - ! ! Input : #Month - The number of the month (1-12) ! Returend : $MonthName - The name of the month ! Modified : n/a ! Used/Created : n/a ! ! - Tables - ! ! Select : n/a ! Insert : n/a ! Update : n/a ! Delete : n/a ! !***************************************************************** BEGIN-PROCEDURE Get-Month-Name ( #Month, :$MonthName ) EVALUATE #Month WHEN = 1 LET $MonthName = 'January' BREAK WHEN = 2 LET $MonthName = 'February' BREAK WHEN = 3 LET $MonthName = 'March' BREAK WHEN = 4 LET $MonthName = 'April' BREAK WHEN = 5 LET $MonthName = 'May' BREAK WHEN = 6 LET $MonthName = 'June' BREAK WHEN = 7 LET $MonthName = 'July' BREAK WHEN = 8 LET $MonthName = 'August' BREAK WHEN = 9 LET $MonthName = 'September' BREAK WHEN = 10 LET $MonthName = 'October' BREAK WHEN = 11 LET $MonthName = 'November' BREAK WHEN = 12 LET $MonthName = 'December' BREAK END-EVALUATE END-PROCEDURE !***************************************************************** ! ! Procedure : Get-Day-Of-Week ! ! Author : Robert Goshko ! ! Date Created : February 21, 1996 ! ! Description : This function will take a date and return a ! number relating to the day of the week that the ! date falls on ! ! Number Day Of Week ! ------ ----------- ! 1 Sunday ! 2 Monday ! 3 Tuesday ! 4 Wednesday ! 5 Thursday ! 6 Friday ! 7 Saturday ! ! - Variables - ! ! Input : $Date - The date that you wan to find the day of ! week for (Format: yyyy-mm-dd) ! Returned : #DOW - The day of the week number ! Modified : n/a ! Used/Created : #JD - The Julian date for thr $Date ! ! - Tables - ! ! Select : n/a ! Insert : n/a ! Update : n/a ! Delete : n/a ! !***************************************************************** BEGIN-PROCEDURE Get-Day-Of-Week ( $Date, :#DOW ) DO Convert-To-Julian( $Date, #JD ) LET #DOW = ( ( #JD + 1 ) % 7 ) + 1 END-PROCEDURE !***************************************************************** ! ! Procedure : Convert-DOW-To-Word ! ! Author : Robert Goshko ! ! Date Created : Febuary 21, 1996 ! ! Description : This procedure will convert the #DOW variable ! returned from Get-Day-Of-Week to the word for the ! day of the week. ! ! - Variables - ! ! Input : #DOW - The day of the week (1-7) ! Returned : #DayOfWeek - The String name for the day of the ! week ! Modified : n/a ! Used/Created : n/a ! ! - Tables - ! ! Select : n/a ! Insert : n/a ! Update : n/a ! Delete : n/a ! !***************************************************************** BEGIN-PROCEDURE Convert-DOW-To-Word ( #DOW, :$DayOfWeek ) EVALUATE #DOW WHEN = 1 LET $DayOfWeek = 'Sunday' BREAK WHEN = 2 LET $DayOfWeek = 'Monday' BREAK WHEN = 3 LET $DayOfWeek = 'Tuesday' BREAK WHEN = 4 LET $DayOfWeek = 'Wednesday' BREAK WHEN = 5 LET $DayOfWeek = 'Thursday' BREAK WHEN = 6 LET $DayOfWeek = 'Friday' BREAK WHEN = 7 LET $DayOfWeek = 'Saturday' BREAK END-EVALUATE END-PROCEDURE !***************************************************************** ! ! Procedure : Get-Number-Of-Work-Days ! ! Author : Robert Goshko ! ! Date Created : February 21, 1996 ! ! Description : This procedure will return the number of work ! days (5 per week) for any given year nad month. ! (Holidays are NOT subtracted) ! ! - Variables - ! ! Input : #Year - The year (in 4 digit format) ! #Month - The month (1 - 12) ! Returned : #Work - The raw number of working days (including ! any holidays that fall within that month) ! Modified : n/a ! Used/Created : $Start - The start date of the month (yyyy-mm-dd) ! $End - The end date of the month (yyyy-mm-dd) ! #DOW - The day of the week for the first date of ! the month ! #Days - The number of days in the month ! #Count - Counter used in going through all of ! the days in the month ! ! - Tables - ! ! Select : n/a ! Insert : n/a ! Update : n/a ! Delete : n/a ! !***************************************************************** BEGIN-PROCEDURE Get-Number-Of-Work-Days ( #Year, #Month, :#Work ) DO Get-Month-First-Last-Dates( #Year, #Month, $Start, $End ) DO Get-Day-Of-Week( $Start, #DOW ) LET #Days = ( TO_NUMBER( SUBSTR( $End, 9, 2 ) ) ) LET #Work = 7 - #DOW IF #DOW = 1 SUBTRACT 1 FROM #Work END-IF LET #Count = ( 7 - #DOW ) + 1 WHILE #Count < #Days ADD 5 TO #Work ADD 7 TO #Count END-WHILE LET #Work = #Work - ( ( #Count - #Days ) - 1 ) END-PROCEDURE !***************************************************************** ! ! Procedure : Get-Work-Days-Between ! ! Author : Robert Goshko ! ! Date Created : February 21, 1996 ! ! Description : This procedure will calculate the number of ! working days (Monday to Friday) between any two ! dates. ! ! - Variables - ! ! Input : $Start - The start date (Format: yyyy-mm-dd) ! $End - The end date (Format: yyyy-mm-dd) ! Returned : #Work - The number of work days ! Modified : n/a ! Used/Created : #DOW - The day of the week that the $Start date ! falls on ! #StartJD - The Julian date for $Start ! #EndJD - The Julian date for $End ! #Count - The counter used in the loop to move ! through the weeks ! ! - Tables - ! ! Select : n/a ! Insert : n/a ! Update : n/a ! Delete : n/a ! !***************************************************************** BEGIN-PROCEDURE Get-Work-Days-Between ( $Start, $End, :#Work ) ! DO Get-Day-Of-Week( $Start, #DOW ) DO Convert-To-Julian( $Start, #StartJD ) DO Convert-To-Julian( $End, #EndJD ) LET #DOW = ( ( #StartJD + 1 ) % 7 ) + 1 #DEBUGD DISPLAY ' Start Date : ' NOLINE #DEBUGD DISPLAY $Start NOLINE #DEBUGD DISPLAY ' - JD : ' NOLINE #DEBUGD DISPLAY #StartJD #DEBUGD DISPLAY ' End Date : ' NOLINE #DEBUGD DISPLAY $End NOLINE #DEBUGD DISPLAY ' - JD : ' NOLINE #DEBUGD DISPLAY #EndJD #DEBUGD DISPLAY 'DOW For Start Date : ' NOLINE #DEBUGD DISPLAY #DOW IF ( #StartJD < #EndJD ) LET #Work = 7 - #DOW IF #DOW = 1 SUBTRACT 1 FROM #Work END-IF LET #Count = ( 7 - #DOW ) + #StartJD #DEBUGD DISPLAY ' Work : ' NOLINE #DEBUGD DISPLAY #Work #DEBUGD DISPLAY ' Count : ' NOLINE #DEBUGD DISPLAY #Count #DEBUGD DISPLAY ' ' #DEBUGD DISPLAY ' ***** Loop Start *****' WHILE #Count < #EndJD ADD 5 TO #Work ADD 7 TO #Count #DEBUGD DISPLAY ' Work : ' NOLINE #DEBUGD DISPLAY #Work #DEBUGD DISPLAY ' Count : ' NOLINE #DEBUGD DISPLAY #Count #DEBUGD DISPLAY ' ' END-WHILE #DEBUGD DISPLAY ' ***** Loop End *****' IF #Count > #EndJD LET #Work = #Work - ( ( #Count - #EndJD ) - 1 ) END-IF ELSE LET #Work = 0 END-IF #DEBUGD DISPLAY ' Work : ' NOLINE #DEBUGD DISPLAY #Work #DEBUGD DISPLAY ' ' END-PROCEDURE !***************************************************************** ! ! Procedure : Subtract-From-Date ! ! Author : Robert Goshko ! ! Date Created : February 21, 1996 ! ! Description : This procedure will subtract a value (either ! days, months, or years) from a date and return ! the new date. ! ! - Variables - ! ! Input : $Date - The data that you want to subtract from ! (Format: yyyy-mm-dd) ! #Value - The number that you want to subtract ! from the $Date (NOTE: Whole values only) ! $Type - The type of the value: ! D = Days ! M = Months ! Y = Years ! Returned : $NewDate - The date after the calculation ! Modified : n/a ! Used/Created : #Year - The year from the $Date ! #Month - The month from the $Date ! #Day - The day from the $Date ! #ShortYear - The last two digits of the year from ! $Date (Used in detecting centurys) ! #JD - The Julian date of the $Date ! #Count - Used in the loops for subtracting months ! and years ! $Year - The new year value formatted and stored ! as a string ! $Month - The new month value formatted and stored ! as a string ! $Day - The new day value formatted and stored ! as a string ! ! - Tables - ! ! Select : n/a ! Insert : n/a ! Update : n/a ! Delete : n/a ! !***************************************************************** BEGIN-PROCEDURE Subtract-From-Date ( $Date, #Value, $Type, :$NewDate ) LET #Year = ( TO_NUMBER( SUBSTR( $Date, 1, 4 ) ) ) LET #Month = ( TO_NUMBER( SUBSTR( $Date, 6, 2 ) ) ) LET #Day = ( TO_NUMBER( SUBSTR( $Date, 9, 2 ) ) ) LET #ShortYear = ( TO_NUMBER( SUBSTR( $Date, 3, 2 ) ) ) DO Convert-To-Julian( $Date, #JD ) LET $Type = UPPER( $Type ) EVALUATE $Type WHEN = 'D' LET #JD = #JD - #Value DO Convert-From-Julian( #JD, $NewDate ) BREAK WHEN = 'M' LET #Count = 0 WHILE #Count < #Value SUBTRACT 1 FROM #Month IF ( #Month < 1 ) LET #Month = 12 SUBTRACT 1 FROM #Year END-IF ADD 1 TO #Count END-WHILE IF ( ( #Day > 28 ) AND ( #Month = 2 ) ) IF ( ( ( #ShortYear = 0 ) AND ( ( #Year % 400 ) = 0 ) ) OR ( ( #ShortYear <> 0 ) AND ( ( #Year % 4 ) = 0 ) ) ) LET #Day = 29 ELSE LET #Day = 28 END-IF ELSE IF ( ( #Month = 1 ) OR ( #Month = 3 ) OR ( #Month = 5 ) OR ( #Month = 7 ) OR ( #Month = 8 ) OR ( #Month = 10 ) OR ( #Month = 12 ) ) IF ( #Day = 30 ) LET #Day = 31 END-IF ELSE IF ( #Day = 31 ) LET #Day = 30 END-IF END-IF END-IF LET $Year = EDIT( #Year, '0000' ) LET $Month = EDIT( #Month, '00' ) LET $Day = EDIT( #Day, '00' ) STRING $Year $Month $Day BY '-' INTO $NewDate BREAK WHEN = 'Y' LET #Count = 0 WHILE #Count < #Value SUBTRACT 1 FROM #Year ADD 1 TO #Count END-WHILE IF ( ( #Day >= 28 ) AND ( #Month = 2 ) ) IF ( ( ( #ShortYear = 0 ) AND ( ( #Year % 400 ) = 0 ) ) OR ( ( #ShortYear <> 0 ) AND ( ( #Year % 4 ) = 0 ) ) ) LET #Day = 29 ELSE LET #Day = 28 END-IF ELSE IF ( ( #Month = 1 ) OR ( #Month = 3 ) OR ( #Month = 5 ) OR ( #Month = 7 ) OR ( #Month = 8 ) OR ( #Month = 10 ) OR ( #Month = 12 ) ) IF ( #Day = 30 ) LET #Day = 31 END-IF ELSE IF ( #Day = 31 ) LET #Day = 30 END-IF END-IF END-IF LET $Year = EDIT( #Year, '0000' ) LET $Month = EDIT( #Month, '00' ) LET $Day = EDIT( #Day, '00' ) STRING $Year $Month $Day BY '-' INTO $NewDate BREAK END-EVALUATE END-PROCEDURE !***************************************************************** ! ! Procedure : Convert-To-Julian ! ! Author : Robert Goshko ! ! Date Created : March 6, 1996 ! ! Description : This procedure will take a standard date ! (Gregorian) and convert it to it's Julian ! counterpart (the number of days elapsed since ! December 31, 4714 B.C.). ! ! ** NOTE: This procedure only works for Gregorian ! dates AFTER 1600 A.D. ! ! - Variables - ! ! Input : $Date - The Gregorian date value to convert ! (Format: yyyy-mm-dd) ! Returned : #JD - The Julian date value ! Modified : n/a ! Used/Created : #Year - The year from $Date ! #Month - The month from $Date ! #Day - The day from $Date ! #Yr - The number of years since 1601 A.D. ! #DateNum - The number of the date in the current ! year ! ! - Tables - ! ! Select : n/a ! Insert : n/a ! Update : n/a ! Delete : n/a ! !***************************************************************** BEGIN-PROCEDURE Convert-To-Julian ( $Date, :#JD ) LET #Year = ( TO_NUMBER( SUBSTR( $Date, 1, 4 ) ) ) LET #Month = ( TO_NUMBER( SUBSTR( $Date, 6, 2 ) ) ) LET #Day = ( TO_NUMBER( SUBSTR( $Date, 9, 2 ) ) ) LET #Yr = ( #Year - 1601 ) LET #JD = 2305813 WHILE ( #Yr >= 400 ) LET #Yr = #Yr - 400 LET #JD = #JD + 146097 END-WHILE WHILE ( #Yr >= 100 ) LET #Yr = #Yr - 100 LET #JD = #JD + 36524 END-WHILE WHILE ( #Yr >= 4 ) LET #Yr = #Yr - 4 LET #JD = #JD + 1461 END-WHILE DO Get-Date-Number( #Year, #Month, #Day, #DateNum ) LET #JD = #JD + ( 365 * #Yr ) + #DateNum END-PROCEDURE !***************************************************************** ! ! Procedure : Convert-From-Julian ! ! Author : Robert Goshko ! ! Date Created : February 29, 1996 ! ! Description : This procedure will take a Julian date (number of ! day elapsed sine December 21, 4714 B.C.) and ! convert that to it's Gregorian (standard date) ! format. ! ! NOTE: When division occures in this conversion ! it is integer division, so anything right ! of the decimal place is ignored. No ! rounding is done. ! ! - Variables - ! ! Input : #JD - The Julian date ! Returned : $Date - The Gregorian date (Format: yyyy-mm-dd) ! Modified : n/a ! Used/Created : #L, #N, #I, #J - Variables used in the conversion ! process ! #Day - The Gregorian day ! #Month - The Gregorian month ! #Year - The Gregorian year ! $Day - The new day value formatted and stored ! as a string ! $Month - The new month value formatted and stored ! as a string ! $Year - The new year value formatted and stored ! as a string ! ! - Tables - ! ! Select : n/a ! Insert : n/a ! Update : n/a ! Delete : n/a ! !***************************************************************** BEGIN-PROCEDURE Convert-From-Julian ( #JD, :$Date ) LET #L = #JD + 68569 LET #N = TRUNC( ( ( 4 * #L ) / 146097 ), 0 ) LET #L = #L - ( TRUNC( ( ( 146097 * #N + 3 ) / 4 ), 0 ) ) LET #I = TRUNC( ( ( 4000 * ( #L + 1 ) ) / 1461001 ), 0 ) LET #L = #L - ( TRUNC( ( ( 1461 * #I ) / 4 ), 0 ) ) + 31 LET #J = TRUNC( ( ( 80 * #L ) / 2447 ), 0 ) LET #Day = #L - ( TRUNC( ( ( 2447 * #J ) / 80 ), 0 ) ) LET #L = TRUNC( ( #J / 11 ), 0 ) LET #Month = #J + 2 - 12 * #L LET #Year = 100 * ( #N - 49 ) + #I + #L LET $Day = EDIT( #Day, '00' ) LET $Month = EDIT( #Month, '00' ) LET $Year = EDIT( #Year, '0000' ) STRING $Year $Month $Day BY '-' INTO $Date END-PROCEDURE !***************************************************************** ! ! Procedure : Add-To-Date ! ! Author : Robert Goshko ! ! Date Created : March 6, 1996 ! ! Description : This procedure will add a value (either days, ! months, or years) to a date and return the new ! date. ! ! - Variables - ! ! Input : $Date - The data that you want to subtract from ! (Format: yyyy-mm-dd) ! #Value - The number that you want to add to $Date ! (NOTE: Whole values only) ! $Type - The type of the value: ! D = Days ! M = Months ! Y = Years ! Returned : $NewDate - The date after the calculation ! Modified : n/a ! Used/Created : #Year - The year from the $Date ! #Month - The month from the $Date ! #Day - The day from the $Date ! #ShortYear - The last two digits of the year from ! $Date (Used in detecting centurys) ! #JD - The Julian date of the $Date ! #Count - Used in the loops for adding months and ! years ! $Year - The new year value formatted and stored ! as a string ! $Month - The new month value formatted and stored ! as a string ! $Day - The new day value formatted and stored ! as a string ! ! - Tables - ! ! Select : n/a ! Insert : n/a ! Update : n/a ! Delete : n/a ! !***************************************************************** BEGIN-PROCEDURE Add-To-Date ( $Date, #Value, $Type, :$NewDate ) LET #Year = ( TO_NUMBER( SUBSTR( $Date, 1, 4 ) ) ) LET #Month = ( TO_NUMBER( SUBSTR( $Date, 6, 2 ) ) ) LET #Day = ( TO_NUMBER( SUBSTR( $Date, 9, 2 ) ) ) LET #ShortYear = ( TO_NUMBER( SUBSTR( $Date, 3, 2 ) ) ) DO Convert-To-Julian( $Date, #JD ) LET $Type = UPPER( $Type ) EVALUATE $Type WHEN = 'D' LET #JD = #JD + #Value DO Convert-From-Julian( #JD, $NewDate ) BREAK WHEN = 'M' LET #Count = 0 WHILE #Count < #Value ADD 1 TO #Month IF ( #Month > 12 ) LET #Month = 1 ADD 1 TO #Year END-IF ADD 1 TO #Count END-WHILE IF ( ( #Day > 28 ) AND ( #Month = 2 ) ) IF ( ( ( #ShortYear = 0 ) AND ( ( #Year % 400 ) = 0 ) ) OR ( ( #ShortYear <> 0 ) AND ( ( #Year % 4 ) = 0 ) ) ) LET #Day = 29 ELSE LET #Day = 28 END-IF ELSE IF ( ( #Month = 1 ) OR ( #Month = 3 ) OR ( #Month = 5 ) OR ( #Month = 7 ) OR ( #Month = 8 ) OR ( #Month = 10 ) OR ( #Month = 12 ) ) IF ( #Day = 30 ) LET #Day = 31 END-IF ELSE IF ( #Day = 31 ) LET #Day = 30 END-IF END-IF END-IF LET $Year = EDIT( #Year, '0000' ) LET $Month = EDIT( #Month, '00' ) LET $Day = EDIT( #Day, '00' ) STRING $Year $Month $Day BY '-' INTO $NewDate BREAK WHEN = 'Y' LET #Count = 0 WHILE #Count < #Value ADD 1 TO #Year ADD 1 TO #Count END-WHILE IF ( ( #Day >= 28 ) AND ( #Month = 2 ) ) IF ( ( ( #ShortYear = 0 ) AND ( ( #Year % 400 ) = 0 ) ) OR ( ( #ShortYear <> 0 ) AND ( ( #Year % 4 ) = 0 ) ) ) LET #Day = 29 ELSE LET #Day = 28 END-IF ELSE IF ( ( #Month = 1 ) OR ( #Month = 3 ) OR ( #Month = 5 ) OR ( #Month = 7 ) OR ( #Month = 8 ) OR ( #Month = 10 ) OR ( #Month = 12 ) ) IF ( #Day = 30 ) LET #Day = 31 END-IF ELSE IF ( #Day = 31 ) LET #Day = 30 END-IF END-IF END-IF LET $Year = EDIT( #Year, '0000' ) LET $Month = EDIT( #Month, '00' ) LET $Day = EDIT( #Day, '00' ) STRING $Year $Month $Day BY '-' INTO $NewDate BREAK END-EVALUATE END-PROCEDURE !***************************************************************** ! ! Procedure : Date-Difference ! ! Author : Robert Goshko ! ! Date Created : March 8, 1996 ! ! Description : This procedure will return the difference between ! two dates (either days, months, or years). ! ! - Variables - ! ! Input : $FromDate - The starting date ! (Format: yyyy-mm-dd) ! $ToDate - The end date (must be greater than the ! start date - Format: yyyy-mm-dd) ! $Type - The type of the value that you wanted. ! D = Days ! M = Months ! Y = Years ! Returned : #Value - The value that you want ! Modified : n/a ! Used/Created : #FromJD - The Julian from date ! #ToJD - The Julian to date ! ! - Tables - ! ! Select : n/a ! Insert : n/a ! Update : n/a ! Delete : n/a ! !***************************************************************** BEGIN-PROCEDURE Date-Difference ( $FromDate, $ToDate, $Type, :#Value ) LET #Value = 0 DO Convert-To-Julian( $FromDate, #FromJD ) DO Convert-To-Julian( $ToDate, #ToJD ) IF ( #FromJD < #ToJD ) LET #Value = #ToJD - #FromJD LET $Type = UPPER( $Type ) EVALUATE $Type WHEN = 'D' LET #Value = #Value BREAK WHEN = 'M' LET #Value = TRUNC( ( #Value / ( 365 / 12 ) ), 0 ) BREAK WHEN = 'Y' LET #Value = TRUNC( ( #Value / 365 ), 0 ) BREAK WHEN-OTHER LET #Value = 0 BREAK END-EVALUATE END-IF END-PROCEDURE !***************************************************************** ! ! Procedure : Calculate-Age ! ! Author : Robert Goshko ! ! Date Created : November 5, 1996 ! ! Description : This procedure will calculate a persons age very ! accuratly, except in cases where the person was ! born on a Febuary 29. ! ! - Variables - ! ! Input : $BirthDate - The persons birth date ! $AsOfDate - The date that you want to know the ! person age ! Returned : #Age - The person age in years ! Modified : n/a ! Used/Created : #AsOfJD - The as of date converted to Julian ! $StartDate - The date that is used in the loop ! #StartJD - The start date converted to Julian ! ! - Tables - ! ! Select : n/a ! Insert : n/a ! Update : n/a ! Delete : n/a ! !***************************************************************** BEGIN-PROCEDURE Calculate-Age ( $BirthDate, $AsOfDate, :#Age ) DO Convert-To-Julian( $AsOfDate, #AsOfJD ) DO Add-To-Date( $BirthDate, 1, 'Y', $StartDate ) DO Convert-To-Julian( $StartDate, #StartJD ) LET #Age = 0 WHILE( #StartJD <= #AsOfJD ) ADD 1 TO #Age DO Add-To-Date( $StartDate, 1, 'Y', $StartDate ) DO Convert-To-Julian( $StartDate, #StartJD ) END-WHILE END-PROCEDURE !***************************************************************** #ENDIF ! For INC__DATEFUNC_SQC !*****************************************************************