程序代码:
#include <stdio.h>
#include <time.h>
const int wsec = 604800;
const int dsec = 86400;
const int hsec = 3600;
int main(void) {
struct tm input, *lzerow, *lzerod, *lzeroh;
time_t iTime, zTimeW, zTimeD, zTimeH, lTime = time(NULL);
int year, mon, day, hour, min, sec;
printf("输入年月日时分秒 以空格分隔 如 2015 1 8 20 50 30\n");
scanf("%d %d %d %d %d %d", &year, &mon, &day, &hour, &min, &sec);
input.tm_year = year - 1900;
input.tm_mon = mon - 1;
input.tm_mday = day;
input.tm_hour = hour;
input.tm_min = min;
input.tm_sec = sec;
iTime = mktime(&input);
printf("LocalTime = %lld\n", lTime);
lzerow = lzerod = lzeroh = localtime(&lTime);
lzerow->tm_min = lzerod->tm_min = lzeroh->tm_min = 0;
lzerow->tm_sec = lzerod->tm_sec = lzeroh->tm_sec = 0;
zTimeH = mktime(lzeroh);
printf("ZeroTimeHour = %lld\n", zTimeH);
lzerod->tm_hour = 0;
zTimeD = mktime(lzerod);
printf("ZeroTimeDay = %lld\n", zTimeD);
lzerow->tm_yday -= lzerow->tm_wday - 1;
lzerow->tm_mday -= lzerow->tm_wday - 1;
lzerow->tm_wday = 1;
lzerow->tm_hour = 0;
zTimeW = mktime(lzerow);
printf("ZeroTimeWeek = %lld\n", zTimeW);
if(iTime >= zTimeH && iTime <= zTimeH + hsec) {
printf("在同一小时内\n");
return 0;
} else {
printf("不在同一小时内\n");
}
if(iTime >= zTimeD && iTime <= zTimeD + dsec) {
printf("在同一天内\n");
return 0;
} else {
printf("不在同一天内\n");
}
if(iTime >= zTimeW && iTime <= zTimeW + wsec) {
printf("在同一周内\n");
} else {
printf("不在同一周内\n");
}
return 0;
}