C语言实现将时间戳转换为年月日时分秒和将年月日时分秒转换为时间戳

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}
点赞
收藏

评论区

加载中...

相关推荐

MySQL:[Err] 1292 - Incorrect datetime value: ‘0000-00-00 00:00:00‘ for column ‘CREATE_TIME‘ at row 1

文章目录问题用navicat导入数据时,报错:原因这是因为当前的MySQL不支持datetime为0的情况。解决修改sql\mode:sql\mode:SQLMode定义了MySQL应支持的SQL语法、数据校验等,这样可以更容易地在不同的环境中使用MySQL。全局s

Oracle 分组与拼接字符串同时使用

SELECTT.,ROWNUMIDFROM(SELECTT.EMPLID,T.NAME,T.BU,T.REALDEPART,T.FORMATDATE,SUM(T.S0)S0,MAX(UPDATETIME)CREATETIME,LISTAGG(TOCHAR(

皕杰报表(关于日期时间时分秒显示不出来)

在使用皕杰报表设计器时,数据据里面是日期型,但当你web预览时候,发现有日期时间类型的数据时分秒显示不出来,只有年月日能显示出来,时分秒显示为0:00:00。1.可以使用tochar解决,数据集用selecttochar(flowdate,"yyyyMMddHH:mm:ss")fromtablename2.也可以把数据库日期类型date改成timestamp

Java日期时间API系列31

  时间戳是指格林威治时间1970年01月01日00时00分00秒起至现在的总毫秒数,是所有时间的基础,其他时间可以通过时间戳转换得到。Java中本来已经有相关获取时间戳的方法,Java8后增加新的类Instant等专用于处理时间戳问题。 1获取时间戳的方法和性能对比1.1获取时间戳方法Java8以前

KVM调整cpu和内存

一.修改kvm虚拟机的配置1、virsheditcentos7找到“memory”和“vcpu”标签,将<namecentos7</name<uuid2220a6d1a36a4fbb8523e078b3dfe795</uuid

Python之time模块的时间戳、时间字符串格式化与转换

Python处理时间和时间戳的内置模块就有time,和datetime两个,本文先说time模块。关于时间戳的几个概念时间戳,根据1970年1月1日00:00:00开始按秒计算的偏移量。时间元组(struct_time),包含9个元素。 time.struct_time(tm_y