推断的距离1970年1一个月1天从日期数
#include <stdlib.h> /****************************************************************** 特征: 输入日期,计算距离19701月1日的天数 输入: 年月日,输入年份范围【1970,2100】,输入年月日的有效性须要推断 输出: DaysSince1970:距离1970年1月1日的天数 已知 1970年1月1日为星期四 异常时,输出不须要关注 返回: 1 上班 0 休假 -1 表示异常 依据国家规定:周一到周五须要上班。 依据公司规则:月末周六须要上班。 注:因为项目紧张,国家规定的节假日假设在周一到周五须要上班^_^。 举例: 输入:Year = 1970, Month = 1, Day = 3 ,输出 2。返回值为 0 休假 ********************************************************************/ #define IS_LEAP_YEAR(y) ((y) / 4 - (y) / 100 + (y) / 400 - (y - 1) / 4 + (y - 1) / 100 - (y - 1) / 400) int DayNumber[][13] = {{0, 31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31}, {0, 31, 29, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31}}; int dayofweek(int y, int m, int d) // 0 = Sunday { static int t[] = {0, 3, 2, 5, 0, 3, 5, 1, 4, 6, 2, 4}; y -= m < 3; return (y + y / 4 - y / 100 + y / 400 + t[m - 1] + d) % 7; } int accumulationdayfrom1970(int *DaysSince1970, int Year, int Month, int Day) { *DaysSince1970 = 0; for (int i = 1970; i < Year; i++) { if (IS_LEAP_YEAR(i)) { *DaysSince1970 += 366; } else { *DaysSince1970 += 365; } } for (int i = 1; i < Month; i++) { int xs = IS_LEAP_YEAR(Year); *DaysSince1970 += DayNumber[IS_LEAP_YEAR(Year)][i]; } *DaysSince1970 += Day - 1; return *DaysSince1970; } int GetCurrentDate(int *DaysSince1970, int Year, int Month, int Day) { if (NULL == DaysSince1970) { return -1; } if ((Year < 1970) || (Year > 2100)) { return -1; } if ((Month > 12) || (Month <= 0)) { return -1; } int IsLeapYear = IS_LEAP_YEAR(Year); // 闰年返回1 int weekday = dayofweek(Year, Month, Day); if ((Day > DayNumber[IsLeapYear][Month]) || (Day <= 0)) { return -1; } *DaysSince1970 = accumulationdayfrom1970(DaysSince1970, Year, Month, Day); if ((0 < weekday) && (weekday < 6)) { return 1; } else if (0 == weekday) { return 0; } if (weekday == 6) { if (IsLeapYear) { switch (Month) { case 1: case 3: case 5: case 7: case 8: case 10: case 12: if ((25 <= Day) && (Day <= 31)) { return 1; } else { return 0; } break; case 4: case 6: case 9: case 11: if ((24 <= Day) && (Day <= 30)) { return 1; } else { return 0; } break; case 2: if ((23 <= Day) && (Day <= 29)) { return 1; } else { return 0; } break; default: ; } } else { switch (Month) { case 1: case 3: case 5: case 7: case 8: case 10: case 12: if ((25 <= Day) && (Day <= 31)) { return 1; } else { return 0; } break; case 4: case 6: case 9: case 11: if ((24 <= Day) && (Day <= 30)) { return 1; } else { return 0; } break; case 2: if ((22 <= Day) && (Day <= 28)) { return 1; } else { return 0; } break; default: ; } } } return -1; }
版权声明:本文博主原创文章,博客,未经同意不得转载。