IBM Mainframe Forum Index
 
Log In
 
IBM Mainframe Forum Index Mainframe: Search IBM Mainframe Forum: FAQ Register
 

Logic is to check for leap year


IBM Mainframe Forums -> COBOL Programming
Post new topic   Reply to topic
View previous topic :: View next topic  
Author Message
arvind.m

Active User


Joined: 28 Aug 2008
Posts: 205
Location: Hyderabad

PostPosted: Mon Jul 13, 2009 2:02 pm
Reply with quote

Hi All,

The below logic is to check for leap year. but there is some problem in the below problem. even for 2007, the logic is going to "NOT ON SIZE ERROR" which is wrong. I'm using Enterprise COBOL. is the version of cobol has to do anything with the above mentioned problem.

Code:


05  WS-TEST-YEAR             PIC 9(04)     VALUE ZEROS.
***
***
IF WS-MED-DATE-MM = 02                         
   COMPUTE WS-TEST-YEAR  =  WS-MED-DATE-YYYY   
                         /  4                 
      ON SIZE ERROR                           
         MOVE 28          TO  WS-MED-DATE-DD   
      NOT ON SIZE ERROR                       
         MOVE 29          TO  WS-MED-DATE-DD   
   END-COMPUTE                                 
Back to top
View user's profile Send private message
Anuj Dhawan

Superior Member


Joined: 22 Apr 2006
Posts: 6250
Location: Mumbai, India

PostPosted: Mon Jul 13, 2009 5:12 pm
Reply with quote

Code is behaving the way it was told to, from the Manuals:
Quote:
ON SIZE ERROR statement1

Statement1 is replaced by the statement(s) that are to be executed whenever a SIZE ERROR condition occurs (i.e., whenever the integer portion of the result of the arithmetic operation exceeds the largest value that can be contained in the integer portion of the recieving field).
In your case WS-MED-DATE-YYYY = 2007 so WS-MED-DATE-YYYY / 4 = 501.75, integer part is less than 9999 (pic 9(4)), so there is NO ON SIZE ERROR.
Back to top
View user's profile Send private message
dick scherrer

Moderator Emeritus


Joined: 23 Nov 2006
Posts: 19244
Location: Inside the Matrix

PostPosted: Tue Jul 14, 2009 12:22 am
Reply with quote

Hello,

Said slightly differently, you have chosen an incorrect way to check for leap year.

With only a simple divide, there are conditions that are not met when determining leap year.

Suggest you check out the "rules" for determining what is a leap year. Hint - century years and millenium years need special consideration.
Back to top
View user's profile Send private message
Anuj Dhawan

Superior Member


Joined: 22 Apr 2006
Posts: 6250
Location: Mumbai, India

PostPosted: Tue Jul 14, 2009 11:28 am
Reply with quote

Quote:
the logic is going to "NOT ON SIZE ERROR" which is wrong.
This is not the correct logic to find out a leap-year, so if the control is going to "NOT ON SIZE ERROR" that's not wrong.

Just for fun, here is a C Code icon_smile.gif :

Code:
#include <stdio.h>
#include <conio.h>
void main()
{
 int year,leap;
 clrscr();
 printf("enter the year:-  ");
 scanf("%d",&year);
 if ((year%100)==0)
  leap=(year/400)*400;
 else
  leap=(year/4)*4;
 if (leap==year)
  printf("
%d is a leap year.",year);
 else
  printf("
%d is not a leap year.",year);
 getch();
}
Back to top
View user's profile Send private message
arvind.m

Active User


Joined: 28 Aug 2008
Posts: 205
Location: Hyderabad

PostPosted: Tue Jul 14, 2009 11:36 am
Reply with quote

Hi Anuj/Dick,

Thank you for you time and help. Actually the code was developed by my client. we have changed the logic after searching in the internet. The issue is resolved but wanted to know the reason.

Quote:

Leap Year Calculation

In general terms the algorithm for calculating a leap year is as follows...

A year will be a leap year if it is divisible by 4 but not by 100. If a year is divisible by 4 and by 100, it is not a leap year unless it is also divisible by 400.

Thus years such as 1996, 1992, 1988 and so on are leap years because they are divisible by 4 but not by 100. For century years, the 400 rule is important. Thus, century years 1900, 1800 and 1700 while all still divisible by 4 are also exactly divisible by 100. As they are not further divisible by 400, they are not leap years.
Back to top
View user's profile Send private message
Anuj Dhawan

Superior Member


Joined: 22 Apr 2006
Posts: 6250
Location: Mumbai, India

PostPosted: Tue Jul 14, 2009 1:09 pm
Reply with quote

A very informal definition is, in the Gregorian calendar, current standard calendar in most of the world, most years that are divisible by 4 are leap years. But in leap year, the month of February has 29 days instead of 28. One extra day is added to compensate for the fact that a period of 365 days is shorter than a solar year by almost 6 hours.

When you dig into a bit more, every year has 365.242374 days which are rounded to 365.2425. Decimal part .2400 is rounded to one day (approx.) in every four years and Decimal part .0025 is rounded to one day in every 400 years.
Back to top
View user's profile Send private message
Anuj Dhawan

Superior Member


Joined: 22 Apr 2006
Posts: 6250
Location: Mumbai, India

PostPosted: Tue Jul 14, 2009 1:12 pm
Reply with quote

Why Leap Years Are Used . . . and there is lot more on Google.
Back to top
View user's profile Send private message
View previous topic :: :: View next topic  
Post new topic   Reply to topic View Bookmarks
All times are GMT + 6 Hours
Forum Index -> COBOL Programming

 


Similar Topics
Topic Forum Replies
No new posts SCOPE PENDING option -check data DB2 2
No new posts Check data with Exception Table DB2 0
No new posts How to check whether who renamed the ... JCL & VSAM 3
No new posts No ++JCLIN, APPLY CHECK job JCL & VSAM 1
No new posts How to extract the data for current y... DFSORT/ICETOOL 8
Search our Forums:

Back to Top