Advertisement
Guest User

Untitled

a guest
Nov 25th, 2014
161
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.61 KB | None | 0 0
  1. #include <stdio.h>
  2. #include <stdlib.h>
  3. #include <stdbool.h>
  4. #include <unistd.h>
  5. #include <windows.h>
  6. short Year, Month, Day, Weekday;
  7. short Hour, Minute, Second;
  8. short DaysThisMonth;
  9.  
  10. char Months[13][4] = { " ", "JAN", "FEB", "MAR", "APR", "MAY", "JUN",
  11. "JUL", "AUG", "SEP", "OCT", "NOV", "DEC"};
  12.  
  13. char Days[8][12] = {" ", "SUNDAY ", "MONDAY ", "TUESDAY ",
  14. "WEDNESDAY ", "THURSDAY", "FRIDAY ", "SATURDAY "};
  15.  
  16. short DaysInMonth[13] = { 0, 31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31};
  17.  
  18. void ShowTime(void)
  19. {
  20. printf("%d %d %d %d:%d:%d\n",Year,Month,Day,Hour,Minute,Second);
  21.  
  22. }
  23. bool IsLeapYear(short thisyear)
  24. // is thisyear a leap year?
  25. {
  26. bool result;
  27. result = false;
  28. if ( (thisyear % 1000) == 0 )
  29. { // millenium
  30. result = ((thisyear / 1000) % 2) == 0;
  31. } // millenium
  32. else
  33. { // not millenium
  34. if ( (thisyear % 400) == 0)
  35. { // every 400 years
  36. result = true;
  37. } // every 400 years
  38. else
  39. { // not 400 years
  40. if ( (thisyear % 100) == 0)
  41. { // every 100 years
  42. result = false;
  43. } // every 100 years
  44. else
  45. { // not 100 years
  46. if ( (thisyear % 4) == 0)
  47. { // every 4 years
  48. result = false;
  49. } // every 4 years
  50. } // not 100 years
  51. } // not 400 years
  52. } // not millenium
  53. return result;
  54. }
  55.  
  56. short DaysInThisMonth(short year, short month)
  57. // how many days in year, month
  58. {
  59. short days;
  60. days = 28;
  61. if (month == 2)
  62. { // February
  63. if ( IsLeapYear(year) ) days = 29;
  64. } // February
  65. else
  66. { // Not February
  67. days = DaysInMonth[month];
  68. } // Not February
  69. return days;
  70. }
  71. void init_Date_Time(void)
  72. {
  73. Year = 2000;
  74. Month = 2;
  75. Day = 28;
  76. Weekday = 3; // Sunday = 1
  77.  
  78. Hour = 23;
  79. Minute = 59;
  80. Second = 55;
  81.  
  82. DaysThisMonth = DaysInThisMonth(Year, Month);
  83. }
  84.  
  85. int main()
  86. {
  87. init_Date_Time();
  88. while(1)
  89. {
  90. Sleep(1000);
  91. if (++Second >= 60)
  92. { // next mimute
  93. Second = 0;
  94. if (++Minute >= 60)
  95. { // next hour
  96. Minute = 0;
  97. if (++Hour >=24)
  98. { // next day
  99. Hour = 0;
  100. if (++Weekday > 7) Weekday = 1;
  101. if (++Day > DaysThisMonth)
  102. { // next month
  103. Day = 1;
  104. ++Month;
  105. DaysThisMonth = DaysInThisMonth(Year, Month);
  106. if (Month > 12)
  107. { // new year
  108. ++Year;
  109. Month = 1;
  110. DaysThisMonth = 31; // January
  111. } // new year
  112. } // next month
  113. } // next day
  114. } // next hour
  115. } // next minute
  116. ShowTime();
  117. }
  118. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement