码迷,mamicode.com
首页 > 其他好文 > 详细

时间戳转换

时间:2016-05-03 23:49:45      阅读:339      评论:0      收藏:0      [点我收藏+]

标签:

#include<stdio.h>

/* date type defination					*/
typedef signed 	 char   rt_int8_t;
typedef signed 	 short  rt_int16_t;
typedef signed 	 long   rt_int32_t;
typedef unsigned char  	rt_uint8_t;
typedef unsigned short  rt_uint16_t;
typedef unsigned long  	rt_uint32_t;
typedef int 			rt_bool_t;

typedef	rt_int8_t		int8_t;
typedef	rt_uint8_t		uint8_t;
typedef	rt_int16_t		int16_t;
typedef	rt_uint16_t		uint16_t;
typedef	rt_int32_t		int32_t;
typedef	rt_uint32_t		uint32_t;

typedef struct _rtc
{
    /* date and time components */
    uint16_t     sec;    //senconds after the minute, 0 to 59
    uint16_t     min;    //minutes after the hour, 0 to 59
    uint16_t     hour;   //hours since midnight, 0 to 23
    uint16_t     mday;   //day of the month, 1 to 31
    uint16_t     month;  //months of the year, 1 to 12
    uint16_t     year;   //years, START_YEAR to START_YEAR+135
    uint16_t     wday;   //days since Sunday, 0 to 6
    uint16_t     yday;   //days of the year, 1 to 366
}RTC_T;

RTC_T  TempTime;
uint8_t  IsLeap(int16_t nYear);
static int16_t Ymd2Wday(int16_t nYear, int16_t nMonth, int16_t nMday);

#define     START_YEAR      (1970) //default 2000, Jan.1, 00:00:00
#define     TIMEZONE_8      (8*60*60)
#define     SEC_IN_DAY      (60*60*24)//total seconds in one day
#define   DAY_IN_YEAR(nYear) (IsLeap(nYear) ? 366 : 365)
const uint8_t mDayNoLeap[13] =
{
    0, 31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31
};
const uint8_t mDayLeap[13] =
{
    0, 31, 29, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31
};

/*******************************************************************************
* Function Name  : MyMakeTime
* Description    : Form a 32 bit second counting value from calendar.
* Input          : pointer to a calendar struct
* Return         : 32 bit second counting value
*******************************************************************************/
static uint32_t MyMakeTime(RTC_T *pCalendar)
{
    uint32_t TotalSeconds = pCalendar->sec;
    int16_t nYear = pCalendar->year;
    int16_t nMonth = pCalendar->month;
    if((nYear < START_YEAR) || (nYear > (START_YEAR + 135)))
    {
        return 0;    //out of year range
    }
    TotalSeconds += (uint32_t)pCalendar->min * 60;//contribution of minutes
    TotalSeconds += (uint32_t)pCalendar->hour * 3600;//contribution of hours
    //contribution of mdays
    TotalSeconds += (uint32_t)(pCalendar->mday - 1) * SEC_IN_DAY;
    if(IsLeap(nYear))
        while(nMonth > 1)
        {
            TotalSeconds += (uint32_t)mDayLeap[--nMonth] * SEC_IN_DAY;
        }
    else
        while(nMonth > 1)//contribution of months
        {
            TotalSeconds += (uint32_t)mDayNoLeap[--nMonth] * SEC_IN_DAY;
        }
    while(nYear > START_YEAR)//contribution of years
    {
        TotalSeconds += (uint32_t)DAY_IN_YEAR(--nYear) * SEC_IN_DAY;
    }
    return TotalSeconds;
}
//end function MyMakeTime
/*******************************************************************************
* Function Name  : MyLocalTime
* Description    : Form a calendar from 32 bit second counting.
* Input          : pointer to a 32 bit second value, option pointer to a struct
* Return         : Calendar structure
*******************************************************************************/
static RTC_T MyLocalTime(uint32_t TotalSecs, RTC_T *pCalendar)
{
    RTC_T Calendar;//Local variables
    uint32_t TotalDays, Remainder;
    Calendar.year = START_YEAR;//Calendar initialization
    Calendar.month = 1;
    Calendar.mday = 1;
    Calendar.yday = 1;
    Calendar.wday = Ymd2Wday(START_YEAR, 1, 1);
    TotalDays = TotalSecs/SEC_IN_DAY;//Split days from seconds
    Remainder = TotalSecs%SEC_IN_DAY;
    Calendar.wday = (TotalDays + Calendar.wday) % 7;//Get week day
    Calendar.hour = Remainder/3600;//Get clock in day
    Calendar.min = (Remainder/60)%60;
    Calendar.sec = Remainder%60;
    while(TotalDays >= DAY_IN_YEAR(Calendar.year))
    {
        TotalDays -= DAY_IN_YEAR(Calendar.year++);
    }
    Calendar.yday += TotalDays;//Get years and days in year
    if(IsLeap(Calendar.year))//Get months and days in month
        while(TotalDays >= mDayLeap[Calendar.month])
        {
            TotalDays -= mDayLeap[Calendar.month++];
        }
    else
        while(TotalDays >= mDayNoLeap[Calendar.month])
        {
            TotalDays -= mDayNoLeap[Calendar.month++];
        }
    Calendar.mday += TotalDays;
    if(pCalendar)//Copy Calendar if necessary
    {
        *pCalendar = Calendar;
    }
    return Calendar;
}
//end function MyLocalTime
/*******************************************************************************
* Function Name  : Ymd2Wday
* Description    : Calculate days in week from year, month, mday.
* Input          : year, month, mday
* Return         : 0--6, Suanday--Saturday
*******************************************************************************/
static int16_t Ymd2Wday(int16_t nYear, int16_t nMonth, int16_t nMday)
{
    uint8_t i ;
    if(IsLeap(nYear))
        for(i = 1; i < nMonth; i++)
        {
            nMday += mDayLeap[i];
        }
    else
        for(i = 1; i < nMonth; i++)
        {
            nMday += mDayNoLeap[i];
        }
    nYear--;
    return  (nYear + nYear/4 - nYear/100 + nYear/400 + nMday)%7;
}
//end function Ymd2Wday
/***********************************************************************
* Function Name  : IsLeap
* Description    : Check whether the past year is leap or not.
* Input          : 4 digits year number
* Return         : 1: leap year. 0: not leap year
***********************************************************************/
uint8_t  IsLeap(int16_t nYear)
{
    if(nYear % 4 != 0)
    {
        return 0;
    }
    if(nYear % 100 != 0)
    {
        return 1;
    }
    return  (uint8_t)(nYear % 400 == 0);
}

void main(void)
{
	uint32_t time;

	TempTime.sec  = 0;
	TempTime.min  = 0;
	TempTime.hour = 0;
	TempTime.mday = 3;
	TempTime.month = 5;
	TempTime.year =2016;
    time = MyMakeTime(&TempTime);
	printf("%d",time);
  while(1);

}

  

时间戳转换

标签:

原文地址:http://www.cnblogs.com/yygsj/p/5456712.html

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