Thursday, July 5, 2012

Y2K Problem + Day_of_week game! C-GNU/Linux

Hi geeks.. There we can find some of our friends who can find the Day-of-Week just with the date in few counts..! That's interesting game you know ? We can do that game as a program.. Yes we can..
Before 1900, by using some formulas, they used to save the DATE just with 2-Bytes. That was the time Y2K problem occurred. That was a Magic Number and Date encoding problem. That they was unable to save the date within 2 bytes.

Alan Turing says  "I worked on the Y2K recoding for a large Corporation in San Francisco from August 1997 until a week after the turn of 2000. My system solution saved the company 1.5 million dollars. As one of the other posts noted the problem existed because the 19 century value was not included in the year field. For example 1997 was stored as 97. 

The problem was that after the beginning of 2000, all the years would have a lower value than the previous years. Thus any calculations based on the difference of years would be wrong. For example if you had two years (1997 and 2000) and you subtracted 00 from 97 you ended up with 97 but the correct value should have been 3 years difference.

The reason why only two digits were stored for the year is that space used to be very limited in file records back in the 1960s and 1970s and the programmers had to save space.

The solution was to replace the code with a check that if the year was less than 35 then the program would assume that the year was 2035 or less. If it was greater than 2035 then you assume that the year was 1936 or greater.

What this means is that as we approach 2035 the Y2K problem will occur again and all the code will have to be fixed again! But as different companies used a different year the problem will not be as large for any single year. But it will give the current stock of contract programmers a simple task to do at the end of their careers, to keep them busy and out of the way of the younger programmers."




So in my code, Always CENTURY will be 1900. 

  #include<time.h>
  #include<stdlib.h>
  #include<stdio.h>
   void main(void)
    {
      int dt,mt,yr,cent,i,j;
      struct tm time_struct;
      printf("Enter Date:");
      scanf("%d",&dt);
      printf("Enter Month:");
      scanf("%d",&mt);
      printf("Enter Year >1900:");
      scanf("%d",&yr);
      i=yr%1000;
      if(i<1000)
       {
        i=i%100;
       }
      
      cent=yr-i;
      cent=cent-100;
      char days[7][10]={"Sunday", "Monday", "Tuesday", "Wednesday", "Thursday", "Friday", "Saturday"};

      time_struct.tm_year=yr-cent;
      time_struct.tm_mon=mt-1;
      time_struct.tm_mday=dt;
      time_struct.tm_sec=0;
      time_struct.tm_min=0;
      time_struct.tm_hour=0;
      time_struct.tm_isdst=0;

      if(mktime(&time_struct)==-1)
       {
        printf("Error getting time.\n");
        exit(0);
       }

      printf("\n%d-%d-%d is a %s.\n",dt,mt,yr,days[time_struct.tm_wday]);
      return 0;
    }

This code will give you the week day of a inputed date..! Simple and Awesome!


No comments:

Post a Comment