home *** CD-ROM | disk | FTP | other *** search
/ Programming Languages Suite / ProgramD2.iso / Database / CLIPR502.DOS / SOURCE / SAMPLE / DATE.PRG < prev    next >
Encoding:
Text File  |  1993-02-15  |  4.8 KB  |  239 lines

  1. /***
  2. *
  3. *  Date.prg
  4. *
  5. *  Sample user-defined functions for manipulating dates
  6. *
  7. *  Copyright (c) 1993, Computer Associates International Inc.
  8. *  All rights reserved.
  9. *
  10. *  NOTE: compile with /a /m /n /w
  11. *
  12. */
  13.  
  14.  
  15.  
  16. /***
  17. *
  18. *  Mdy( <dDate> ) --> cDate
  19. *
  20. *  Convert a date to a string in the format "month dd, yyyy".
  21. *
  22. *  Parameter:
  23. *     dDate - Date value to convert to a string
  24. *
  25. *  Returns: The date value in "long," string form
  26. *
  27. */
  28. FUNCTION Mdy( dDate )
  29.    
  30.    LOCAL cYear
  31.  
  32.    // Handle SET CENTURY
  33.    IF SUBSTR( SET( _SET_DATEFORMAT ), -4 ) == "YYYY"
  34.       cYear := STR( YEAR( dDate ))
  35.    ELSE
  36.       cYear := " " + SUBSTR( STR( YEAR( dDate )), 4, 2 )
  37.    ENDIF
  38.  
  39.    RETURN ( CMONTH( dDate ) + " " + LTRIM( STR( DAY( dDate ))) + "," + cYear )
  40.  
  41.  
  42.  
  43. /***
  44. *
  45. *  Dmy( <dDate> ) --> cDate
  46. *
  47. *  Convert a date to string formatted as "dd month yyyy".
  48. *
  49. *  Parameter:
  50. *     dDate - Date value to convert
  51. *
  52. *  Returns: The date value in european date format
  53. *
  54. */
  55. FUNCTION Dmy( dDate )
  56.    
  57.    LOCAL cYear
  58.  
  59.    // Handle SET CENTURY
  60.    IF SUBSTR( SET( _SET_DATEFORMAT ), -4 ) == "YYYY"
  61.       cYear := STR( YEAR( dDate ))
  62.    ELSE
  63.       cYear := " " + SUBSTR( STR( YEAR( dDate )), 4, 2 )
  64.    ENDIF
  65.  
  66.    RETURN ( LTRIM( STR( DAY( dDate ))) + " " + CMONTH( dDate ) + cYear )
  67.  
  68.  
  69.  
  70. /***
  71. *
  72. *  DateAsAge( <dDate> ) --> nAge
  73. *
  74. *  Convert a date of birth to an age in years.
  75. *
  76. *  Parameter:
  77. *     dDate - Birthdate for which to calculate the age
  78. *
  79. *  Returns: The number of years elapsed since <dDate>
  80. *
  81. */
  82. FUNCTION DateAsAge( dDate ) 
  83.    
  84.    LOCAL nAge := 0
  85.  
  86.    IF YEAR( DATE() ) > YEAR( dDate )
  87.       
  88.       nAge := YEAR( DATE() ) - YEAR( dDate )
  89.  
  90.       // Decrease the age by one if the date (month/day) has not yet
  91.       // occurred this year
  92.       IF ( MONTH( DATE() ) < MONTH( dDate )  .OR.  ;
  93.          ( MONTH( DATE() ) == MONTH( dDate ) .AND. ;
  94.            DAY( DATE() ) < DAY( dDate )            ;
  95.          ))
  96.          
  97.          --nAge
  98.  
  99.       ENDIF
  100.    ENDIF
  101.  
  102.    RETURN nAge
  103.  
  104.  
  105.  
  106. /***
  107. *
  108. *  AddMonth( <dDate>, <nMonths> ) --> dNewDate
  109. *
  110. *  Calculate a new date by adding a number of months to a given
  111. *  date.
  112. *
  113. *  Parameters:
  114. *     dDate   - Date value to add <nMonths> to
  115. *     nMonths - Number of months to add to <dDate>
  116. *
  117. *  Returns: The date value representing <dDate> + <nMonths>
  118. *
  119. */
  120. FUNCTION AddMonth( dDate, nMonths)
  121.    
  122.    LOCAL nMonth
  123.    LOCAL nDay
  124.    LOCAL nYear
  125.    LOCAL nLimit
  126.    LOCAL nMonthAdd
  127.    LOCAL nYearAdd
  128.    LOCAL dNew
  129.    
  130.    // Break date up into its numeric components
  131.    nMonth := MONTH( dDate )
  132.    nDay   := DAY( dDate )
  133.    nYear  := YEAR( dDate )
  134.  
  135.    // nLimit determines the minimum number of months that will push the
  136.    // date into the next year.  If the number of months added to the date
  137.    // exceeds this limit, the year must be advanced by one
  138.    nLimit := 12 - nMonth + 1
  139.  
  140.    // Compute number of years to add
  141.    nYearAdd := INT( nMonths / 12 )
  142.  
  143.    IF nMonths >= nLimit
  144.       nYearAdd++
  145.    ENDIF
  146.  
  147.    nYear += nYearAdd
  148.  
  149.    // Compute number of months to add and normalize month
  150.    nMonthAdd := nMonths % 12 
  151.    nMonth    := ( nMonth + nMonthAdd ) % 12
  152.  
  153.    // Convert numeric portions to new date
  154.    dNew := NtoD( nMonth, nDay, nYear )
  155.  
  156.    RETURN ( dNew )
  157.  
  158.  
  159.  
  160. /***
  161. *
  162. *  DateAsArray( dDate ) --> aDate
  163. *
  164. *  Convert a date to an array of year, month, and day
  165. *
  166. *  Parameter:
  167. *     dDate - Date value to convert into array form
  168. *
  169. *  Returns: The date in the format { nYear, nMonth, nDay }
  170. *           If the parameter is invalid, an empty array ({}) is returned
  171. *
  172. */
  173. FUNCTION DateAsArray( dDate )
  174.    
  175.    LOCAL aDate := {}
  176.  
  177.    IF VALTYPE( dDate ) == "D"
  178.       aDate := { YEAR( dDate ), MONTH( dDate ), DAY( dDate ) }
  179.    ENDIF
  180.  
  181.    RETURN aDate
  182.  
  183.  
  184.  
  185. /***
  186. *
  187. *  ArrayAsDate( aDate ) --> dDate
  188. *
  189. *  Convert an array of year, month, and day to a date value
  190. *
  191. *  Parameter:
  192. *     aDate - Array holding a date in the form { nYear, nMonth, nDay }
  193. *
  194. *  Returns: aDate in date value form
  195. *
  196. */
  197. FUNCTION ArrayAsDate( aDate )
  198.    RETURN NtoD( aDate[2], aDate[3], aDate[1] )
  199.  
  200.  
  201.  
  202. /***
  203. *
  204. *  DateIsLeap( <dDate> ) --> lLeap
  205. *
  206. *  Determine if the year of a supplied date is a leap year
  207. *
  208. */
  209. FUNCTION DateIsleap( dDate )
  210.    
  211.    LOCAL nYear := YEAR( dDate )
  212.  
  213.    RETURN (( nYear % 4 ) == 0 )    .AND. ;
  214.           ((( nYear % 100 ) != 0 ) .OR.  ;
  215.           (( nYear % 400 ) == 0)   )
  216.  
  217.  
  218.  
  219. /***
  220. *
  221. *  NtoD( nMonth, nDay, nYear ) --> dNew
  222. *
  223. *  Convert a date passed as separate numeric parameters to a date value
  224. *
  225. */
  226. FUNCTION NtoD( nMonth, nDay, nYear )
  227.    
  228.    LOCAL cSavDateFormat := SET( _SET_DATEFORMAT, "MM/DD/YYYY" )
  229.    LOCAL dDate
  230.  
  231.    dDate := CTOD( TRANSFORM( nMonth, "99/" ) + ;
  232.                   TRANSFORM( nDay,   "99/" ) + ;
  233.                   TRANSFORM( nYear,  "9999" )  )
  234.  
  235.    SET( _SET_DATEFORMAT, cSavDateFormat )
  236.  
  237.    RETURN ( dDate )
  238.  
  239.