RTC时钟换算函数

来源:互联网 发布:java命令行运行有包名 编辑:程序博客网 时间:2024/05/18 02:55

时间日期转换成32位数

typedef struct CTime_tagtm{  // 日期时间    u8  tm_sec;    // seconds after the minute - [0,59]    u8  tm_min;    // minutes after the hour - [0,59]    u8  tm_hour;   // hours since midnight - [0,23]              u8  tm_wday;   // days since Sunday - [0,6]    u8  tm_mday;   // day of the month - [1,31]    u8  tm_mon;    // months since January - [0,11]     u16 tm_year;   // Year (2000, 2068)} CTime_tm;typedef u32 CTime_time_t;// date/time in unix secs past 1-Jan-2000.#define CTime_time_t_MAX      _UI32_MAX// maximum CTime_time_t value

// 获得秒单位,同时获取星期几。CTime_time_t CTime_mktime(CTime_tm * ptm){u32 days;if ((ptm->tm_year < 2000) || (ptm->tm_year >= 2068) || (ptm->tm_mon > 11) || (ptm->tm_mday > 31) || (ptm->tm_mday == 0) || (ptm->tm_hour > 23) || (ptm->tm_min > 59) || (ptm->tm_sec > 59))return 0;days = (ptm->tm_year-2000)*365L + (ptm->tm_year-1997)/4;// 获得到前一年末的总天数。days += CTime_DaysofMonths[ptm->tm_mon];// 加上当年之前月的总天数if ((ptm->tm_year-2000) % 4 == 0) // 当年为闰年{if (ptm->tm_mon >= 2)days++;}days += ptm->tm_mday - 1;// 加上当月之前的天数// 2000年1月1日星期六,为6ptm->tm_wday = (u8) ((days+6) % 7); // 获得星期几return days*(3600L*24L) + (ptm->tm_hour)*3600L + (ptm->tm_min)*60 + ptm->tm_sec;}
eg:

CTime_tm tmNow;
SStmRtc_GetCurrentTimeAndDate(&tmNow);
TRACE6('K', 'K', "time=%d:%d:%d--%d:%d:%d", tmNow.tm_hour, tmNow.tm_min, tmNow.tm_sec, tmNow.tm_mday, tmNow.tm_mon, tmNow.tm_year);
u32 time;
time = CTime_mktime(&tmNow);


32位数转换成时间日期

// 获得标准时间void CTime_gmtime(CTime_tm * ptm, CTime_time_t tt){u32 days,days2;u32 year,i;days = tt / (3600L*24L);//一天,单位:秒tt  %= (3600L*24L);ptm->tm_hour = (u8) (tt / 3600L);//小时tt %= 3600L;ptm->tm_min = (u8) (tt / 60L);//分钟ptm->tm_sec = (u8) (tt % 60L);//秒// 2000年1月1日星期六,为6ptm->tm_wday = (u8) ((days+6) % 7); // 获得星期几days2 = days;year = days / 365;days %= 365;if (year){i = (year+3) / 4;/*if (days >= i)days -= i;else{year -= 1;days = days + 366 - i;}*/if (days >= i) {}elseyear -= 1;}days = days2 - ((year)*365L + (year+3)/4);ptm->tm_year = (u16)(2000 + year);if (((year % 4) == 0) && (days >= 59)) // 当年为闰年{if (days == 59){ptm->tm_mon = 1;ptm->tm_mday = 29;return;}else days--;}for (i = 11; ; --i){if (days >= CTime_DaysofMonths[i])break;if (i == 0) break;}ptm->tm_mon = (u8)i;ptm->tm_mday = (u8) (days-CTime_DaysofMonths[i] + 1);}

// 获得当天已经过去的秒数u32 CTime_GetDaySeconds(CTime_time_t tt){return (tt % (3600L*24L));}// 获得标准时间void CTime_gmtime(CTime_tm * ptm, CTime_time_t tt){u32 days,days2;u32 year,i;days = tt / (3600L*24L);//一天,单位:秒tt  %= (3600L*24L);ptm->tm_hour = (u8) (tt / 3600L);//小时tt %= 3600L;ptm->tm_min = (u8) (tt / 60L);//分钟ptm->tm_sec = (u8) (tt % 60L);//秒// 2000年1月1日星期六,为6ptm->tm_wday = (u8) ((days+6) % 7); // 获得星期几days2 = days;year = days / 365;days %= 365;if (year){i = (year+3) / 4;/*if (days >= i)days -= i;else{year -= 1;days = days + 366 - i;}*/if (days >= i) {}elseyear -= 1;}days = days2 - ((year)*365L + (year+3)/4);ptm->tm_year = (u16)(2000 + year);if (((year % 4) == 0) && (days >= 59)) // 当年为闰年{if (days == 59){ptm->tm_mon = 1;ptm->tm_mday = 29;return;}else days--;}for (i = 11; ; --i){if (days >= CTime_DaysofMonths[i])break;if (i == 0) break;}ptm->tm_mon = (u8)i;ptm->tm_mday = (u8) (days-CTime_DaysofMonths[i] + 1);}// 获得星期几 days since Sunday - [0,6]u32 CTime_GetWeekDay(CTime_time_t tt){u32 days;days = tt / (3600L*24L);// 2000年1月1日星期六,为6return ((days+6) % 7); // 获得星期几}



eg:

CTime_tm  tm;
u32 tt = GetDwordFromPtr(param->timedata);
CTime_gmtime(&tm, tt);




原创粉丝点击