lib_date.eclmod 3.7 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192
  1. /*##############################################################################
  2. Copyright (C) <2010> <LexisNexis Risk Data Management Inc.>
  3. This program is free software: you can redistribute it and/or modify
  4. it under the terms of the GNU Affero General Public License as
  5. published by the Free Software Foundation, either version 3 of the
  6. License, or (at your option) any later version.
  7. This program is distributed in the hope that it will be useful,
  8. but WITHOUT ANY WARRANTY; without even the implied warranty of
  9. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  10. GNU Affero General Public License for more details.
  11. You should have received a copy of the GNU Affero General Public License
  12. along with this program. If not, see <http://www.gnu.org/licenses/>.
  13. ############################################################################## */
  14. export Date_i2_YYYYMM(unsigned2 i) := if(i=0,
  15. ' ',
  16. (string4)((i-1) div 12 + 1900)+intformat(((i-1) % 12)+1,2,1));
  17. export Date_MMDDYY_I2(string6 s) := IF ( (unsigned8)s=0,
  18. 0,
  19. (unsigned8)(IF(s[5..6]<'40','20','19')+s[5..6]+s[1..4]));
  20. export date_overlap_first(unsigned8 lf, unsigned8 ll,
  21. unsigned8 rf, unsigned8 rl) :=
  22. MAP ( lf > rl => 0,
  23. rf > ll => 0,
  24. lf > rf => lf,
  25. rf );
  26. export date_overlap_last(unsigned8 lf, unsigned8 ll,
  27. unsigned8 rf, unsigned8 rl) :=
  28. MAP ( lf > rl => 0,
  29. rf > ll => 0,
  30. ll > rl => rl,
  31. ll );
  32. export date_overlap(unsigned8 lf, unsigned8 ll,
  33. unsigned8 rf, unsigned8 rl) :=
  34. MAP( date_overlap_last(lf,ll,rf,rl)=0 => if (date_overlap_first(lf,ll,rf,rl)=0,0,1),
  35. date_overlap_first(lf,ll,rf,rl)=0 => 1,
  36. (date_overlap_last(lf,ll,rf,rl) div 100 - date_overlap_first(lf,ll,rf,rl) div 100) * 12 +
  37. date_overlap_last(lf,ll,rf,rl)%100-date_overlap_first(lf,ll,rf,rl) % 100);
  38. export Date_YYYYMM_i2(string6 dte) := if(dte='',0,((integer)(dte[1..4])-1900)*12+(integer)(dte[5..6]));
  39. export LeapYear(integer2 year) := year % 4 = 0 and ( year % 100 != 0 or year % 400 = 0);
  40. INTEGER2 dayofyr(integer1 month,integer1 day) :=
  41. CHOOSE( month,0,31,59,90,120,151,181,212,243,273,304,334 ) + day;
  42. export INTEGER2 DayOfYear(integer4 year,integer1 month,integer1 day) :=
  43. dayofyr(month,day) + IF( LeapYear(year) and month > 2, 1, 0);
  44. export DaysSince1900(integer2 year, integer1 month, integer1 day) :=
  45. ((integer)year-1900)*365+(year-1901) div 4 + DayOfYear(year,month,day);
  46. //this works only with dates in YYYYMMDD format
  47. export integer8 DaysApart(string8 d1, string8 d2) :=
  48. abs(DaysSince1900((integer2)(d1[1..4]), (integer1)(d1[5..6]), (integer1)(d1[7..8])) -
  49. DaysSince1900((integer2)(d2[1..4]), (integer1)(d2[5..6]), (integer1)(d2[7..8])));
  50. export EarliestDate(integer l, integer r) := if ( l=0 or (r<>0 and r<l),r,l);
  51. export earliestdatestring(string20 l,string20 r) := if ( l='' or r<>'' and r<l,r,l);
  52. export Format_Date(integer2 year, integer1 month, integer1 day) :=
  53. (string4)year+'/'+intformat(month,2,1)+'/'+intformat(day,2,1);
  54. import lib_stringlib;
  55. // WORKS FOR DATES IN FORMAT OF YYYYMMDD
  56. today := lib_stringlib.StringLib.getdateYYYYMMDD();
  57. thismonth := (integer2)(today[5..6]);
  58. thisday := (integer2)(today[7..8]);
  59. thisyear := (integer2)(today[1..4]);
  60. export getage(string8 dob) := if(thismonth < (integer2)(dob[5..6]) or
  61. (thismonth = (integer2)(dob[5..6]) and thisday < (integer2)(dob[7..8])),
  62. thisyear - (integer2)(dob[1..4]) - 1,
  63. thisyear - (integer2)(dob[1..4]));
  64. export LatestDate(integer l, integer r) := if ( r>l,r,l);