码迷,mamicode.com
首页 > 编程语言 > 详细

时间戳与日期时间互转C语言

时间:2016-06-13 11:20:22      阅读:794      评论:0      收藏:0      [点我收藏+]

标签:

/*
 * ctime.h
 *
 *  Created on: May 19, 2016
 *      Author: root
 */
 
#ifndef CTIME_H_
#define CTIME_H_
#include "common/micro_type.h"
#define OFFSET_SECOND     946684800  /* ??1970/1/1/0/0/0??2000/1/1/0/0/0??????????  */
//#define OFFSET_SECOND   0          /* ??2000/1/1/0/0/0??2000/1/1/0/0/0??????????  */
#define SECOND_OF_DAY     86400      /* 1???????                                     */
 
typedef struct date_time
{
    uint16   iYear;
    uint16   iMon;
    uint16   iDay;
 
    uint16   iHour;
    uint16   iMin;
    uint16   iSec;
    uint16   iMsec;
} DATE_TIME;
 
void GetDateTimeFromSecond(unsigned long lSec, DATE_TIME *tTime);
#endif /* CTIME_H_ */
 

/*
 * ctime.c
 *
 *  Created on: May 19, 2016
 *      Author: root
 */
#include "common/micro_type.h"
#include "ctime.h"
uint8 DayOfMon[12] = {31,28,31,30,31,30,31,31,30,31,30,31};
 
/* ??????1970/1/1/0/0/0?????? */
unsigned long GetSecondTime(DATE_TIME *date_time)
{
    uint16 iYear, iMon, iDay, iHour, iMin, iSec;
    iYear = date_time->iYear;
    iMon = date_time->iMon;
    iDay = date_time->iDay;
    iHour = date_time->iHour;
    iMin = date_time->iMin;
    iSec = date_time->iSec;
 
    uint16 i, Cyear=0;
    unsigned long CountDay=0;
 
    for(i=1970; i<iYear; i++)     /* ???1970???????????????? */
    {
        if(((i%4==0) && (i%100!=0)) || (i%400==0))  Cyear++;
    }
    CountDay = Cyear * 366 + (iYear-1970-Cyear) * 365;
    for(i=1; i<iMon; i++)
    {
        if((i==2) && (((iYear%4==0)&&(iYear%100!=0)) || (iYear%400==0)))
            CountDay += 29;
        else
            CountDay += DayOfMon[i-1];
    }
    CountDay += (iDay-1);
 
    CountDay = CountDay*SECOND_OF_DAY + (unsigned long)iHour*3600 + (unsigned long)iMin*60 + iSec;
    return CountDay;
}
 
/* ????????????1970/1/1/0/0?????? */
void GetDateTimeFromSecond(unsigned long lSec, DATE_TIME *tTime)
{
    uint16 i,j,iDay;
    unsigned long lDay;
 
    lDay = lSec / SECOND_OF_DAY;        /* ???????????? */
    lSec = lSec % SECOND_OF_DAY;
 
    i = 1970;
    while(lDay > 365)
    {
      if(((i%4==0)&&(i%100!=0)) || (i%400==0))    /* ???? */
        lDay -= 366;
      else
        lDay -= 365;
      i++;
    }
    if((lDay == 365) && !(((i%4==0)&&(i%100!=0)) || (i%400==0)))  /* ??? */
    {
      lDay -= 365;
      i++;
    }
    tTime->iYear = i;     /* ?õ???? */
    for(j=0;j<12;j++)     /* ?????·? */
    {
      if((j==1) && (((i%4==0)&&(i%100!=0)) || (i%400==0)))
        iDay = 29;
      else
        iDay = DayOfMon[j];
      if(lDay >= iDay) lDay -= iDay;
      else break;
    }
    tTime->iMon  = j+1;
    tTime->iDay  = lDay+1;
    tTime->iHour = ((lSec / 3600)+8)%24;//这里注意,世界时间已经加上北京时间差8,
    tTime->iMin  = (lSec % 3600) / 60;
    tTime->iSec  = (lSec % 3600) % 60;
}

时间戳与日期时间互转C语言

标签:

原文地址:http://www.cnblogs.com/lort/p/5579640.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!