常用时间函数及相互转换

1. 获取系统Unix时间戳

/*
*******************************************************************************
* @description     : 获取系统Unix时间戳
* @return          : Unix时间戳  ms
 *******************************************************************************
 */
long long GetTimeUnix(void)
{
    struct timeval tv;
    gettimeofday(&tv, NULL);
    return ((long long)tv.tv_sec*1000 + (long long)tv.tv_usec/1000);
}

2. 获取系统北京时间

/*
 *******************************************************************************
 * @description     : 获取系统北京时间
 * @return          : 时间结构
 *******************************************************************************
 */
struct tm *GetTimeSys(void) 
{ 
    struct timeval  tv; 
    struct timezone tz; 
    struct tm *p;
    
    gettimeofday(&tv, &tz); 
    p = localtime(&tv.tv_sec);
    // printf("time_now:%.2d%.2d%.2d.%.6ld\n", p->tm_hour, p->tm_min, p->tm_sec, tv.tv_usec); 

    return p;
}

3. 获取系统北京日期+时间

/*
 *******************************************************************************
 * @description     : 获取系统北京日期+时间
 * @return          : 时间结构
 *******************************************************************************
 */
struct tm *GetDateTime(char *psDateTime)
{
    time_t nSeconds;
    struct tm * pTM;
    
    time(&nSeconds);
    pTM = localtime(&nSeconds);

    /* 系统日期和时间,格式: yyyymmddHHMMSS */
    if(psDateTime != NULL)
        sprintf(psDateTime, "%04d-%02d-%02d %02d:%02d:%02d",
            pTM->tm_year + 1900, pTM->tm_mon + 1, pTM->tm_mday,
            pTM->tm_hour, pTM->tm_min, pTM->tm_sec);
            
    return pTM;
}

4. 获取自系统开机以来的毫秒数(tick)

/*
 *******************************************************************************
 * @description     : 获取自系统开机以来的毫秒数(tick)  
 *                    理论上比jiffies大5分钟(300000ms)
 * @return          : 系统tick  ms
 *******************************************************************************
 */
unsigned long GetTickCount(void)
{  
    struct timespec ts;  
  
    clock_gettime(CLOCK_MONOTONIC, &ts);  

    return (ts.tv_sec*1000 + ts.tv_nsec/1000000);  //ms
    // return (ts.tv_sec*1000000 + ts.tv_nsec/1000);  //us
}

5. UTC时间转换为任意时区时间

/* 定义如下数据结构 */
typedef struct  
{										    
 	unsigned int year;	 //年份
	unsigned char month; //月份
	unsigned char date;	 //日期
	unsigned char hour;  //小时
	unsigned char min; 	 //分钟
	unsigned char sec; 	 //秒钟
}*p_nmea_time, s_nmea_time;

/*
 *******************************************************************************
 * @description        : UTC时间转换为任意时区时间
 * @param - utc_time   : UTC时间
 * @param - timezone   : 时区,如果是转换为北京时间,timezone传8即可
 * @param - local_time : 转换后时间
 *******************************************************************************
 */
static void utc_to_local_time(p_nmea_time utc_time, int8_t timezone, p_nmea_time local_time)
{
    int year, month, day, hour;
	int lastday = 0;			//last day of this month 本月天数
	int lastlastday = 0;		//last day of last month 上个月天数

	year	 = utc_time->year;	//utc time
	month  	 = utc_time->month;
	day 	 = utc_time->date;
	hour 	 = utc_time->hour + timezone; 
	
	//1月大,2月小,3月大,4月小,5月大,6月小,7月大,8月大,9月小,10月大,11月小,12月大
	if(month==1 || month==3 || month==5 || month==7 || month==8 || month==10 || month==12)
	{
		lastday = 31;//本月天数
		lastlastday = 30;//这里应该补上上个月的天数
		
		if(month == 3)
		{
			if((year%400 == 0)||(year%4 == 0 && year%100 != 0))//if this is lunar year
				lastlastday = 29;
			else
				lastlastday = 28;
		}
		
		if(month == 8 || month == 1)//这里应该是8月和1月,因为8月和1月的上一个月(7月和12月)的天数是31天的
			lastlastday = 31;
	}
	else if(month == 4 || month == 6 || month == 9 || month == 11)
	{
		lastday = 30;
		lastlastday = 31;
	}
	else
	{
		lastlastday = 31;
		
		if((year%400 == 0)||(year%4 == 0 && year%100 != 0))
			lastday = 29;
		else
			lastday = 28;
	}

	if(hour >= 24)// if >24, day+1
	{					
		hour -= 24;
		day += 1; 
		if(day > lastday)// next month, day-lastday of this month
		{ 		
			day -= lastday;
			month += 1;
			if(month > 12)// next year, month-12
			{
				month -= 12;
				year += 1;
			}
		}
	}
	
	if(hour < 0)// if <0, day-1
	{
		hour += 24;
		day -= 1;
		if(day < 1)// month-1, day=last day of last month
		{
			day = lastlastday;
			month -= 1;
			if(month < 1)// last year, month=12
			{
				month = 12;
				year -= 1;
			}
		}
	}
	
  	// transfer value to local_time
	local_time->year  = year;
	local_time->month = month;
	local_time->date  = day;
	local_time->hour  = hour;
	local_time->min	  = utc_time->min;
	local_time->sec	  = utc_time->sec;
}

版权声明:本文为qq_34446736原创文章,遵循CC 4.0 BY-SA版权协议,转载请附上原文出处链接和本声明。