Advertisement
jalih

Calculate the age in PL/I

Oct 31st, 2014
200
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.00 KB | None | 0 0
  1.  
  2. age: procedure options(main);
  3.  
  4. dcl 1 date_type based,
  5. 2 dd pic '99',
  6. 2 mm pic '99',
  7. 2 yyyy pic '9999';
  8.  
  9. dcl format char(8) nonasgn init('DDMMYYYY');
  10.  
  11. dcl (i, xx) fixed bin(31);
  12. dcl (d1, d2) like date_type;
  13. dcl (firstdays, numdays, yeardays) fixed bin(31);
  14. dcl (today, birthday) char(8) date('DDMMYYYY');
  15. dcl (temp, days_to_today) bit(*) controlled;
  16. dcl (y, m, d) fixed bin(31);
  17. dcl input char(20) var;
  18.  
  19. ask: display('Input birthday (DDMMYYYY)') reply(input);
  20. if ^validdate(trim(input), format) then
  21. do;
  22. display('Not a valid birthday!');
  23. goto ask;
  24. end;
  25.  
  26. birthday = trim(input);
  27. today = datetime(format);
  28.  
  29. firstdays = days('0101' || trim(substr(today, 5, 4)), format);
  30.  
  31. yeardays = days('0101' || trim(substr(today, 5, 4) + 1), format) -
  32. days('0101' || trim(substr(today, 5, 4)), format);
  33.  
  34. numdays = days(today, format) - firstdays;
  35.  
  36. allocate temp bit (yeardays);
  37. allocate days_to_today bit(numdays);
  38.  
  39. d1.dd = substr(birthday, 1, 2);
  40. d1.yyyy = substr(today, 5, 4);
  41.  
  42. do i = 1 to 12;
  43. d1.mm = i;
  44. if validdate(string(d1), format) then
  45. do;
  46. xx = days(string(d1), format) - firstdays;
  47. if xx = 0 then
  48. xx = 1;
  49. substr(temp, xx, 1) = '1'b;
  50. end;
  51. else
  52. do;
  53. d2 = d1;
  54. d2.dd = '01';
  55. d2.mm += 1;
  56. xx = (days(string(d2), format) - 1) - firstdays;
  57. substr(temp, xx, 1) = '1'b;
  58. end;
  59. end;
  60.  
  61. days_to_today = substr(temp, 1, numdays);
  62.  
  63. free temp;
  64.  
  65. y = substr(today, 5, 4) - substr(birthday, 5, 4);
  66. m = tally(days_to_today, '1'b) - substr(birthday, 3, 2);
  67. if m < 0 then
  68. do;
  69. m += 12;
  70. y -= 1;
  71. end;
  72. d = numdays - searchr(days_to_today,'1'b);
  73.  
  74. display('You are ' || trim(y) || ' years, '
  75. || trim(m) || ' months and ' || trim(d) || ' days old.');
  76.  
  77. free days_to_today;
  78.  
  79. end age;
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement