1#include<stdio.h> 2#include<string.h> 3 4typedef unsigned int time_t; 5 6struct tm { 7 int tm_sec; /* 秒 – 取值区间为[0,59] */ 8 int tm_min; /* 分 - 取值区间为[0,59] */ 9 int tm_hour; /* 时 - 取值区间为[0,23] */ 10 int tm_mday; /* 一个月中的日期 - 取值区间为[1,31] */ 11 int tm_mon; /* 月份(从一月开始,0代表一月) - 取值区间为[0,11] */ 12 int tm_year; /* 年份,其值等于实际年份减去1900 */ 13}; 14const char Days[12] = {31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31}; 15void localtime(time_t time,struct tm *t) 16{ 17 unsigned int Pass4year; 18 int hours_per_year; 19 20 if(time < 0) 21 { 22 time = 0; 23 } 24 //取秒时间 25 t->tm_sec=(int)(time % 60); 26 time /= 60; 27 //取分钟时间 28 t->tm_min=(int)(time % 60); 29 time /= 60; 30 //取过去多少个四年,每四年有 1461*24 小时 31 Pass4year=((unsigned int)time / (1461L * 24L)); 32 //计算年份 33 t->tm_year=(Pass4year << 2) + 1970; 34 //四年中剩下的小时数 35 time %= 1461L * 24L; 36 //校正闰年影响的年份,计算一年中剩下的小时数 37 for (;;) 38 { 39 //一年的小时数 40 hours_per_year = 365 * 24; 41 //判断闰年 42 if ((t->tm_year & 3) == 0) 43 { 44 //是闰年,一年则多24小时,即一天 45 hours_per_year += 24; 46 } 47 if (time < hours_per_year) 48 { 49 break; 50 } 51 t->tm_year++; 52 time -= hours_per_year; 53 } 54 //小时数 55 t->tm_hour=(int)(time % 24); 56 //一年中剩下的天数 57 time /= 24; 58 //假定为闰年 59 time++; 60 //校正闰年的误差,计算月份,日期 61 if((t->tm_year & 3) == 0) 62 { 63 if (time > 60) 64 { 65 time--; 66 } 67 else 68 { 69 if (time == 60) 70 { 71 t->tm_mon = 1; 72 t->tm_mday = 29; 73 return ; 74 } 75 } 76 } 77 //计算月日 78 for (t->tm_mon = 0; Days[t->tm_mon] < time;t->tm_mon++) 79 { 80 time -= Days[t->tm_mon]; 81 } 82 83 t->tm_mday = (int)(time); 84 85 return; 86} 87 88static time_t mon_yday[2][12] = 89{ 90 {0,31, 59, 90, 120, 151, 181, 212, 243, 273, 304, 334}, 91 {0,31, 60, 91, 121, 152, 182, 213, 244, 274, 305, 335}, 92}; 93 94int isleap(int year) 95{ 96 return (year) % 4 == 0 && ((year) % 100 != 0 || (year) % 400 == 0); 97} 98 99time_t mktime(struct tm dt) 100{ 101 time_t result; 102 int i =0; 103 // 以平年时间计算的秒数 104 result = (dt.tm_year - 1970) * 365 * 24 * 3600 + 105 (mon_yday[isleap(dt.tm_year)][dt.tm_mon-1] + dt.tm_mday - 1) * 24 * 3600 + 106 dt.tm_hour * 3600 + dt.tm_min * 60 + dt.tm_sec; 107 // 加上闰年的秒数 108 for(i=1970; i < dt.tm_year; i++) 109 { 110 if(isleap(i)) 111 { 112 result += 24 * 3600; 113 } 114 } 115 return(result); 116} 117 118void main() 119{ 120 time_t time = 0; 121 time_t time2 = 0; 122 long i = 0; 123 struct tm t; 124 //2018-01-01 01:01:01 125 time = 1514768461; 126 // 验证一个周期4年 一天打印一次 127 for(i=0;i<(4*365+1);i++) 128 { 129 localtime(time,&t); 130 printf("A time:%d\r\n",time); 131 printf("A %04d-%02d-%02d %02d:%02d:%02d\r\n",t.tm_year,t.tm_mon+1,t.tm_mday,t.tm_hour,t.tm_min,t.tm_sec); 132 133 t.tm_mon+=1; //转换时月份需要加1,因为月份是从0开始的 134 time2 = mktime(t); //将localtime得到年月日时分秒再次转换成时间戳,验证算法是否正确 135 printf("B time:%d\r\n",time2); 136 memset((void*)&t,0x00,sizeof(t)); 137 localtime(time2,&t); 138 printf("B %04d-%02d-%02d %02d:%02d:%02d\r\n",t.tm_year,t.tm_mon+1,t.tm_mday,t.tm_hour,t.tm_min,t.tm_sec); 139 memset((void*)&t,0x00,sizeof(t)); 140 time += 24*3600; 141 } 142 143 return; 144}
C语言实现将时间戳转换为年月日时分秒和将年月日时分秒转换为时间戳
Wesley13
2021-10-11
1783 0 0
点赞
收藏
评论区
加载中...