描述
時間日期與time stamp轉(zhuǎn)換
參考
代碼
- Date.h
#ifndef __DATE_H__
#define __DATE_H__
/*******************************function********************/
/**function: UTC2Time4
* description: UTC to time
*/
time_t UTC2Time4(time_t tm_rect, int zone);
/**function: Time2UTC4
* description: time to UTC
*/
time_t Time2UTC4(time_t tm_rect, int zone);
#endif /* End of #ifndef __DATE_H__ */
- Date.c
#include<stdio.h>
#include<time.h>
#include "Date.h"
/*********************************** Code ************************************/
/**function: UTC2Time4
* description: UTC to time
*/
time_t UTC2Time4(time_t tm_rect, int zone)
{
time_t rect = tm_rect;
struct tm tblock;
struct tm *tm_now;
tm_now = &tblock;
rect = rect + (long)zone * 60 * 60;
if(DAYLIGHT_SAVING_TIME == GetCfgDSTFlag())
{
rect = Time2DST2(rect);
}
return rect;
}
/**function: Time2UTC4
* description: time to UTC
*/
time_t Time2UTC4(time_t tm_rect, int zone)
{
time_t rect = tm_rect;
struct tm tblock;
struct tm *tm_now;
tm_now = &tblock;
if(DAYLIGHT_SAVING_TIME == GetCfgDSTFlag())
{
rect = DST2Time2(rect);
}
rect = rect - (long)zone * 60 * 60;
return rect;
}
- 調(diào)用
time_t t;
t = time(NULL);
time_t rect;
int timezone = 8;
rect = Time2UTC4(t, timezone);
printf("rect:%lu, t:%lu\n", rect, t);
struct tm *tblock;
t = time(NULL);
tblock = localtime(&t);
rect = mktime(tblock);
printf("rect:%lu\n", rect);
rect = UTC2Time4(rect, timezone);
printf("rect:%lu\n", rect);