1、源代码
#include <stdio.h> #include <time.h> #include <string.h> #define Now_Time_LEN 20 #define Now_Week_Num_LEN 10 char nowTime[10] = {0}; char nowWeek_Num[10] = {0}; void *getNowTime(char *Now_Time, char *Now_Week_Num) { int tmp_day = 0; int tmp_year = 0; int iZoneOffset = 8; int month[] = {0, 31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31}; char acWeek[5] = {0}; char acHour[3] = {0}; char acMin[3] = {0}; char acSec[3] = {0}; memset(Now_Time, 0, Now_Time_LEN + 1); memset(Now_Week_Num, 0, Now_Week_Num_LEN + 1); time_t now; struct tm timenow; now = time(NULL); if (now <= 0 || &timenow == NULL) { return NULL; } (void *)gmtime_r(&now, &timenow); timenow.tm_year += 1900; timenow.tm_mon += 1; if (timenow.tm_mon == 2) // 2 月 { if ((timenow.tm_year % 400 == 0) || (timenow.tm_year % 4 == 0 && timenow.tm_year % 100 != 0)) { month[2] += 1; } } //+ timezone timenow.tm_hour += iZoneOffset; if (timenow.tm_hour >= 24) { tmp_day = 1; timenow.tm_hour %= 24; } timenow.tm_mday += tmp_day; if (timenow.tm_mday > month[timenow.tm_mon]) { timenow.tm_mday = 1; timenow.tm_mon += 1; } if (timenow.tm_mon > 12) { tmp_year = 1; timenow.tm_mon = 1; } timenow.tm_year += tmp_year; strftime(acWeek, sizeof(acWeek), "%a", &timenow); strftime(acHour, sizeof(acHour), "%H", &timenow); strftime(acMin, sizeof(acMin), "%M", &timenow); strftime(acSec, sizeof(acSec), "%S", &timenow); if (!strcmp(acWeek, "Mon")) { strncat(Now_Week_Num, "1", 1); } else if (!strcmp(acWeek, "Tue")) { strncat(Now_Week_Num, "2", 1); } else if (!strcmp(acWeek, "Wed")) { strncat(Now_Week_Num, "3", 1); } else if (!strcmp(acWeek, "Thu")) { strncat(Now_Week_Num, "4", 1); } else if (!strcmp(acWeek, "Fri")) { strncat(Now_Week_Num, "5", 1); } else if (!strcmp(acWeek, "Sat")) { strncat(Now_Week_Num, "6", 1); } else if (!strcmp(acWeek, "Sun")) { strncat(Now_Week_Num, "7", 1); } else { printf("Now_Week_Num get erro !\n"); } strncat(Now_Time, acHour, 2); strncat(Now_Time, ":", 1); strncat(Now_Time, acMin, 2); strncat(Now_Time, ":", 1); //strncat(Now_Time, "00", 2); strncat(Now_Time, acSec, 2); return NULL; } int main() { getNowTime(nowTime, nowWeek_Num); printf("nowTime is %s !\nnowWeek_Num is %s !\n", nowTime, nowWeek_Num); return 0; }2、运行结果