#include <stdlib.h> /****************************************************************** 功能: 输入年月日,计算距离1970年1月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; }
判断距离1970年1月1日的天数
时间: 2024-11-02 12:19:14