fork(3) download
  1. //Taken from: http://p...content-available-to-author-only...e.org/349916#57,61,162,175
  2. //see also http://w...content-available-to-author-only...s.com/forums/showthread.php?t=38143
  3.  
  4. //
  5. // Copyright (c) Microsoft Corporation. All rights reserved.
  6. //
  7. //
  8. // Use of this source code is subject to the terms of the Microsoft end-user
  9. // license agreement (EULA) under which you licensed this SOFTWARE PRODUCT.
  10. // If you did not accept the terms of the EULA, you are not authorized to use
  11. // this source code. For a copy of the EULA, please see the LICENSE.RTF on your
  12. // install media.
  13. //
  14. //------------------------------------------------------------------------------
  15. //
  16. // Copyright (C) 2004-2007, Freescale Semiconductor, Inc. All Rights Reserved.
  17. // THIS SOURCE CODE, AND ITS USE AND DISTRIBUTION, IS SUBJECT TO THE TERMS
  18. // AND CONDITIONS OF THE APPLICABLE LICENSE AGREEMENT
  19. //
  20. //------------------------------------------------------------------------------
  21. //
  22. // Module: rtc.c
  23. //
  24. // PQOAL Real-time clock (RTC) routines for the MC13783 PMIC RTC.
  25. //
  26. //------------------------------------------------------------------------------
  27.  
  28.  
  29. #include <stdio.h>
  30.  
  31. // Global Variables
  32. //These macro define some default information of RTC
  33. #define ORIGINYEAR 1980 // the begin year
  34. #define MAXYEAR (ORIGINYEAR + 100) // the maxium year
  35. #define JAN1WEEK 2 // Jan 1 1980 is a Tuesday
  36. #define GetDayOfWeek(X) (((X-1)+JAN1WEEK)%7)
  37.  
  38. #define UINT8 unsigned char
  39.  
  40. static const UINT8 monthtable[12]
  41. = {31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31};
  42. static const UINT8 monthtable_leap[12]
  43. = {31, 29, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31};
  44.  
  45.  
  46.  
  47. //------------------------------------------------------------------------------
  48. //
  49. // Function: IsLeapYear
  50. //
  51. // Local helper function checks if the year is a leap year
  52. //
  53. // Parameters:
  54. //
  55. // Returns:
  56. //
  57. //
  58. //------------------------------------------------------------------------------
  59. static int IsLeapYear(int Year)
  60. {
  61. int Leap;
  62.  
  63. Leap = 0;
  64. if ((Year % 4) == 0) {
  65. Leap = 1;
  66. if ((Year % 100) == 0) {
  67. Leap = (Year%400) ? 0 : 1;
  68. }
  69. }
  70.  
  71. return (Leap);
  72. }
  73.  
  74.  
  75. int main(void) {
  76.  
  77. int days;
  78.  
  79. scanf("%d",&days);
  80.  
  81. /*
  82. Фрагмент кода для разбора даты:
  83. вход: days - количество прошедших дней,
  84.   начиная с 1 января 1980
  85. на выходе: year - год
  86.   month - месяц
  87.   days - день
  88.   */
  89.  
  90.  
  91. //taken from: BOOL ConvertDays(UINT32 days, SYSTEMTIME* lpTime)
  92. //******************************************************************************
  93. int dayofweek, month, year;
  94. UINT8 *month_tab;
  95.  
  96. //Calculate current day of the week
  97. dayofweek = GetDayOfWeek(days);
  98.  
  99. year = ORIGINYEAR;
  100.  
  101. while (days > 365)
  102. {
  103. if (IsLeapYear(year))
  104. {
  105. if (days > 366)
  106. {
  107. days -= 366;
  108. year += 1;
  109. }
  110. }
  111. else
  112. {
  113. days -= 365;
  114. year += 1;
  115. }
  116. }
  117.  
  118.  
  119. // Determine whether it is a leap year
  120. month_tab = (UINT8 *)((IsLeapYear(year))? monthtable_leap : monthtable);
  121.  
  122. for (month=0; month<12; month++)
  123. {
  124. if (days <= month_tab[month])
  125. break;
  126. days -= month_tab[month];
  127. }
  128.  
  129. month += 1;
  130. //******************************************************************************
  131.  
  132. printf("Date is %2d:%2d:%4d\n",days,month,year);
  133.  
  134. printf("Day of week: %d\n",dayofweek);
  135.  
  136. return 0;
  137. }
  138.  
Success #stdin #stdout 0s 2056KB
stdin
367
stdout
Date is  1: 1:1981
Day of week: 4