時間戳與時間的換算

 最近對時間戳和時間感興趣,隨百度一下,時間戳, 知道它含義

時間戳(timestamp),一個能表示一份數據在某個特定時間之前已經存在的、 完整的、 可驗證的數據,通常是一個字符序列,唯一地標識某一刻的時間。使用數字簽名技術產生的數據, 簽名的對象包括了原始文件信息、 簽名參數、 簽名時間等信息。廣泛的運用在知識產權保護、 合同簽字、 金融帳務、 電子報價投標、 股票交易等方面。

然後在百度下關於時間戳計算,寫了這個代碼。部分代碼抄自網絡  

 

#include "delay.h"
#define  TOTAL_DAY_PER_YEAR 365
#define  TOTAL_SEC_PER_DATA (24*60*60)   //一天多少秒
#define  TOTAL_SEC_PER_HOUR (60*60)      //一小時多少秒
#define  TOTAL_SEC_PER_MIN  60         //一分多少秒
#define  UTC_BASE_YEAR  1970
typedef  struct 
{
    int RTC_sec;   // seconds after the minute, 0 to 60  //(0 - 60 allows for the occasional leap second) 
    int RTC_min;   // minutes after the hour, 0 to 59 
    int RTC_hour;  // hours since midnight, 0 to 23 
    int RTC_mday;  // day of the month, 1 to 31 
    int RTC_mon;   // months since January, 0 to 11 
    int RTC_Year;  // years since 1900 
//    int tm_wday;   days since Sunday, 0 to 6 
//    int tm_yday;  // days since January 1, 0 to 365
//    int tm_isdst; // Daylight Savings Time flag 
} time ;
const unsigned  char day_per_mon[]={31,28,31,30,31,30,31,31,30,31,30,31};
/********************************************************************
Function: 閏年判斷
INPUT   :
OUTPUT  : 1 是閏年 0 是 平年
NOTE    :
********************************************************************/
unsigned int  Is_leap_year(u16 year)
{
    if((year%100)!=0&&(year%4)==0||(year%400)==0)
    {
        return 1;
    }
    else
    {
     return 0;
    }
}
/********************************************************************
Function:計算每個月天數
INPUT   :
OUTPUT  :
NOTE    :
********************************************************************/
unsigned int Days_of_month(u16 year,u8 month)
{
    if (month != 2) 
    {
        return day_per_mon[month-1];
    } else 
    {
        return day_per_mon[month-1] + Is_leap_year(year); // 閏年加一天 2月份
    }
}
/********************************************************************
Function:
INPUT   :
OUTPUT  :
NOTE    : 轉換出來時間是以 格林威治時間 爲基礎的 其它時區相對調整
********************************************************************/
unsigned long get_time_stamp(void)
{
    unsigned int  i;
    u16 days=0;//天數
    u32 secs;   //秒數
    time   RTC_DateStruct;
	//
	RTC_DateStruct.RTC_Year = 2004;
	RTC_DateStruct.RTC_mon  =1;
	RTC_DateStruct.RTC_mday = 8;
	RTC_DateStruct.RTC_hour =1;
	RTC_DateStruct.RTC_min =15;
	RTC_DateStruct.RTC_sec = 1;
    /* year */
    for(i=UTC_BASE_YEAR;i<RTC_DateStruct.RTC_Year;i++)
     {
        days+=(TOTAL_DAY_PER_YEAR+Is_leap_year(i));
     }   
    /* month */  // 數組中排序是從0 開始的 所以月份要特別處理
    for(i=1;i<RTC_DateStruct.RTC_mon;i++)
    {
        days+=(Days_of_month(RTC_DateStruct.RTC_Year,i));
    }
    /* day */
    days+=(RTC_DateStruct.RTC_mday-1);
    /* sec */
    secs=days*TOTAL_SEC_PER_DATA+(RTC_DateStruct.RTC_hour*TOTAL_SEC_PER_HOUR+RTC_DateStruct.RTC_min*TOTAL_SEC_PER_MIN+RTC_DateStruct.RTC_sec);
    return secs;
     
}
/********************************************************************
Function:
INPUT   :
OUTPUT  :
NOTE    :
********************************************************************/
unsigned long get_time(unsigned long secs )
{
    // 計算出多少天
    unsigned long days_temp,Secs_temp;
	  time   RTC_DateStruct;
	
	RTC_DateStruct.RTC_Year = 0;
	RTC_DateStruct.RTC_mon  =0;
	RTC_DateStruct.RTC_mday = 0;
	RTC_DateStruct.RTC_hour =0;
	RTC_DateStruct.RTC_min =0;
	RTC_DateStruct.RTC_sec = 0;

    days_temp = secs/TOTAL_SEC_PER_DATA;
    Secs_temp = secs-days_temp*TOTAL_SEC_PER_DATA;

    RTC_DateStruct.RTC_Year = UTC_BASE_YEAR;
    while(days_temp> TOTAL_DAY_PER_YEAR) // 大於365 天
    {
        
        days_temp -= (TOTAL_DAY_PER_YEAR +Is_leap_year(RTC_DateStruct.RTC_Year));
			  RTC_DateStruct.RTC_Year++;
    }
    RTC_DateStruct.RTC_mon  = 1; //函數裏面限制
    while(days_temp > Days_of_month(RTC_DateStruct.RTC_Year,RTC_DateStruct.RTC_mon))
   {
       days_temp -= Days_of_month(RTC_DateStruct.RTC_Year,RTC_DateStruct.RTC_mon);
   }
   RTC_DateStruct.RTC_mday =days_temp+1;  // 調整日期。
    
   RTC_DateStruct.RTC_hour= Secs_temp /TOTAL_SEC_PER_HOUR;

   Secs_temp -= RTC_DateStruct.RTC_hour*TOTAL_SEC_PER_HOUR;

   RTC_DateStruct.RTC_min = Secs_temp/TOTAL_SEC_PER_MIN; 
   RTC_DateStruct.RTC_sec =  Secs_temp - RTC_DateStruct.RTC_min* TOTAL_SEC_PER_MIN;
   return 1;
}

 

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章