时间戳与时间的换算

 最近对时间戳和时间感兴趣,随百度一下,时间戳, 知道它含义

时间戳(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;
}

参考链接

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值