ISO8601和UTC 时间,由秒数转化日期时间,日期到秒数

本文提供了一系列时间操作的方法,包括从秒数转换为日期时间、获取系统ISO8601时间、获取时区等实用函数,并详细介绍了如何在不同操作系统环境下进行时间处理。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

ISO8601和UTC 时间,由秒数转化日期时间,日期到秒数

ISO8601时间格式:2018-6-5T17:46:50Z

UTC时间格式:  2018-06-05T03:46:50+08:00 等同于2018-6-5T11:46:50Z

UTC+时区= localtime;

其中"T"用来分割日期和时间,时间后面跟着的"-07:00"表示西七区,"+08:00"表示东八区。

时区默认是0时区,可以用"Z"表示,也可以不写。
对于我国,要使用"+08:00",表示东八区。

 



#include <time.h>
#include <string.h>
#ifdef WIN32
#include <windows.h>
#define  snprintf sprintf_s
#else
#include <sys/time.h>
#endif


typedef struct dataTime_t
{
    uint16_t wYear;
    uint16_t wMonth;
    uint16_t wDayOfWeek;
    uint16_t wDay;
    uint16_t wHour;
    uint16_t wMinute;
    uint16_t wSecond;
    uint16_t wMilliseconds;
}dataTime_t;


 /*
*获取系统ISO8601时间,字符串表示
*/
 int  GetDataTime_ISO8601(char * data, int size)
{
	time_t time_utc;
	struct tm tm_local;
	time(&time_utc);
#ifdef WIN32
	localtime_s(&tm_local, &time_utc);
#else
	localtime_r(&time_utc, &tm_local);
#endif
	int success = 0;
	success = snprintf(data, size, "%04d-%02d-%02dT%02d:%02d:%02dZ",
		tm_local.tm_year + 1900,
		tm_local.tm_mon + 1,
		tm_local.tm_mday,
		tm_local.tm_hour,
		tm_local.tm_min,
		tm_local.tm_sec);

	return success;
}
/*
*获取时区
*/
 int  GetTimeZone(int &nTimeZone)
{
	struct tm tm_local;
	time_t time_utc;
	// Get the UTC time
	time(&time_utc);    //当前时间的秒数
	// Get the local time
	// Use localtime_r for threads safe
#ifdef WIN32
	localtime_s(&tm_local, &time_utc);
#else
	localtime_r(&time_utc, &tm_local);
#endif
	time_t time_local;
	// Change tm to time_t
	time_local = mktime(&tm_local);
	struct tm tm_gmt;
	// Change it to GMT tm
#ifdef WIN32
	gmtime_s(&tm_gmt, &time_utc);
#else
        gmtime_r(&time_utc, &tm_gmt);;
#endif  

	int time_zone = tm_local.tm_hour - tm_gmt.tm_hour;
	if (time_zone < -12) {
		time_zone += 24;
	}else
	if (time_zone > 12) {
		time_zone -= 24;
	}
	nTimeZone = time_zone;
	return time_zone;
}

/*
*ISO8601或者UTC格式的时间,将字符串时间转换为 dataTime_t对象
*/

int GetDataTime_ISO8601_UTC_DataTime(const char * lpStringdata, int nStringSize, dateTime_t *lpDataTime)
{
	int lsuccess = 0;
	//2018-06-01T08:32:56.000Z   /2018-06-01T08:32:56Z  
    int lTimeZone = 0;
    if (lpStringdata == NULL || nStringSize < 20 ) return -1;
    
    char * lpSource = (char *)lpStringdata;

    char lpdate[6] = { 0 };
    strncpy(lpdate, lpSource, 4);
    lpdate[4] = '\0';
    lpDataTime->wYear = atoi(lpdate);

    char *llpSource = lpSource + 5;
    if (llpSource==NULL)  return -1;
    strncpy(lpdate, llpSource, 2);
    lpdate[2] = '\0';
    lpDataTime->wMonth = atoi(lpdate);

    llpSource = llpSource + 3;
    if (llpSource == NULL)  return -1;
    strncpy(lpdate, llpSource, 2);
    lpdate[2] = '\0';
    lpDataTime->wDay = atoi(lpdate);

    llpSource = llpSource + 3;
    if (llpSource == NULL)  return -1;
    strncpy(lpdate, llpSource, 2);
    lpdate[2] = '\0';
    lpDataTime->wHour = atoi(lpdate);

    llpSource = llpSource + 3;
    if (llpSource == NULL)  return -1;
    strncpy(lpdate, llpSource, 2);
    lpdate[2] = '\0';
    lpDataTime->wMinute = atoi(lpdate);


    llpSource = llpSource + 3;
    if (llpSource == NULL)  return -1;
    strncpy(lpdate, llpSource, 2);
    lpdate[2] = '\0';
    lpDataTime->wSecond = atoi(lpdate);

/*
* 2018-06-01T08:32:56Z
* 2018-06-01T08:32:56+08:00
* 2018-06-01T08:32:56.000Z
* 2018-06-01T08:32:56.000+08:00
*/
    llpSource = llpSource + 2;
    if (llpSource == NULL)  return -1;
    strncpy(lpdate, llpSource, 1);
    lpdate[1] = '\0';

    if (strcmp(lpdate, ".") == 0)
    {
        char *lpbuffer = llpSource + 1;
        if (lpbuffer == NULL)  return -1;
        char buffer[4] = { 0 };
        strncpy(buffer, lpbuffer, 3);
        buffer[3] = '\0';
        lpDataTime->wMilliseconds = atoi(buffer);

        char * lptime = lpbuffer + 3;
        char buf[6] = { 0 };
        if (lptime == NULL) return -1;
        strncpy(buf, lptime, 1);
        buf[1] = '\0';
        if (strcmp(buf, "+") == 0)
        {
            lptime = lptime + 1;
            if (lptime == NULL)  return -1;
            strncpy(buf, lptime, 2);
            buf[2] = '\0';
            lTimeZone = atoi(buf);
        }
    }
    if (strcmp(lpdate, "+") == 0)
    {
        llpSource = llpSource + 1;
        if (llpSource == NULL)  return -1;
        strncpy(lpdate, llpSource, 2);
        lpdate[2] = '\0';
        lTimeZone = atoi(lpdate);
    }

    printf("time : %4d/%d/%d %2d:%d:%d.%3d + %d:00", lpDataTime->wYear, lpDataTime->wMonth, lpDataTime->wDay, lpDataTime->wHour, lpDataTime->wMinute, lpDataTime->wSecond,lpDataTime->wMilliseconds,lTimeZone);

/
 
	return lsuccess;
}

/*
* 得到系统时间
*/

int GetLocalTime(dateTime_t *lpDateTime)
{
	int32_t lsuccess = 0;
#ifdef WIN32

   SYSTEMTIME *lpSystemTime = (SYSTEMTIME *)lpDataTime;
   GetLocalTime(lpSystemTime);
#else
	struct timeval     nowTime = { 0 };
	lsuccess = gettimeofday(&nowTime, 0);
	time_t time = (time_t)nowTime.tv_sec;
	struct tm ntm, *ptm = &ntm; 

        localtime_r(&time, &ntm);

	lpDataTime->wYear = ptm->tm_year + 1900;
	lpDataTime->wMonth = ptm->tm_mon + 1;
	lpDataTime->wDayOfWeek = ptm->tm_wday;
	lpDataTime->wDay = ptm->tm_mday;
	lpDataTime->wHour = ptm->tm_hour;
	lpDataTime->wMinute = ptm->tm_min;
	lpDataTime->wSecond = ptm->tm_sec;
	lpDataTime->wMilliseconds = nowTime.tv_usec / 1000;
#endif
	return lsuccess;
}

/*
* 设置系统时间
*/

int  SetLocalTime(dateTime_t *lpDateTime)
{
    int32_t lsuccess = 0;
#ifdef WIN32
    SYSTEMTIME *lpSystemTime = (SYSTEMTIME *)lpDataTime;
    lsuccess = SetLocalTime(lpSystemTime);
#else
    struct tm  _tm = { 0 };
    _tm.tm_sec = lpDataTime->wSecond;
    _tm.tm_min = lpDataTime->wMinute;
    _tm.tm_hour = lpDataTime->wHour;
    _tm.tm_mday = lpDataTime->wDay;
    _tm.tm_mon = lpDataTime->wMonth -1;
    _tm.tm_year = lpDataTime->wYear - 1900;

    struct timeval tv;
    tv.tv_sec = mktime(&_tm);
    tv.tv_usec = 0;
    if (settimeofday(&tv, (struct timezone *) 0) < 0)
    {
        printf("Set system datetime error!\n");
        return -1;
    }
#endif

    return lsuccess;
}

/*
* 设置系统时间
*/
 int SetLocalTimeAddTZ(dateTime_t *lpDateTime, int ltimezone)
{
    int32_t  lsuccess = 0;
    int llSecond = ltimezone * 3600;

    struct tm _tm = { 0 };
    _tm.tm_sec = lpDataTime->wSecond;
    _tm.tm_min = lpDataTime->wMinute;
    _tm.tm_hour = lpDataTime->wHour;
    _tm.tm_mday = lpDataTime->wDay;
    _tm.tm_mon = lpDataTime->wMonth -1;
    _tm.tm_year = lpDataTime->wYear - 1900;

    time_t timeSeconds = mktime(&_tm);
    time_t CountSecond = timeSeconds + llSecond;

#ifdef WIN32

    struct tm timer, *time = &timer;
     localtime_s(time,&CountSecond);
    lpDataTime->wYear = time->tm_year + 1900;
    lpDataTime->wMonth = time->tm_mon +1;
    lpDataTime->wDayOfWeek = time->tm_wday;
    lpDataTime->wDay = time->tm_mday;
    lpDataTime->wHour = time->tm_hour;
    lpDataTime->wMinute = time->tm_min;
    lpDataTime->wSecond = time->tm_sec;

    SYSTEMTIME *lpSystemTime = (SYSTEMTIME *)lpDataTime;
    avx_success = SetLocalTime(lpSystemTime);

    printf("Time+TZ: %4d/%d/%d  %2d:%d:%d\n", lpSystemTime->wYear, lpSystemTime->wMonth, lpSystemTime->wDay,
        lpSystemTime->wHour, lpSystemTime->wMinute, lpSystemTime->wSecond);
#else

    struct timeval tv;
    tv.tv_sec = CountSecond;
    tv.tv_usec = 0;

    if (settimeofday(&tv, (struct timezone *) 0) < 0)
    {
        printf("Set system datatime error!\n");
        return -1;
    }
#endif
    return lsuccess;
}
/*
* function :get current time 
* out   : Seconds/Millisecond
*/
int64_t SKY_GetUTCTime()
{
    struct timeval tv;
    gettimeofday(&tv, NULL);
    return (int64_t)tv.tv_sec * 1000000 + tv.tv_usec;
}
/*
* input :Seconds/Millisecond
* out   : date struct(yyyy/mm/dd hh/mm/ss)
*/
int  SKY_GetDataTimeFromUTCTime(int64_t llUTCTime,dataTime_t *lpDataTime)
{
    struct tm timer, *time = &timer;
    int64_t  llUTCTimeSecond = llUTCTime / 1000000;
    int64_t  llUTCTimeMicros = llUTCTime % 1000000;
#ifdef _WIN32
    localtime_s(time,(time_t*)&llUTCTimeSecond);
#else
    localtime_r((time_t*)&llUTCTimeSecond,time);
#endif
    lpDataTime->wYear = time->tm_year + 1900;
    lpDataTime->wMonth = time->tm_mon + 1;
    lpDataTime->wDayOfWeek = time->tm_wday;
    lpDataTime->wDay = time->tm_mday;
    lpDataTime->wHour = time->tm_hour;
    lpDataTime->wMinute = time->tm_min;
    lpDataTime->wSecond = time->tm_sec;
    lpDataTime->wMilliseconds = llUTCTimeMicros / 1000;
    return 0;
}
/*
* input :date struct(yyyy/mm/dd hh/mm/ss)
* out   : Seconds/Millisecond
*/
int64_t SKY_GetUTCTimeFromDataTime(dataTime_t *lpDataTime)
{
    struct tm  _tm = { 0 };
    _tm.tm_sec = lpDataTime->wSecond;
    _tm.tm_min = lpDataTime->wMinute;
    _tm.tm_hour = lpDataTime->wHour;
    _tm.tm_mday = lpDataTime->wDay;
    _tm.tm_mon = lpDataTime->wMonth - 1;
    _tm.tm_year = lpDataTime->wYear - 1900;
//  time_t  llUTCTimeSecond = mktime(&_tm);    //此处在不同平台可能会出现异常
    int64_t llUTCTimeSecond = mktime(&_tm); 
    int64_t llUTCTimeMicros = lpDataTime->wMilliseconds * 1000;
    int64_t llUTCTime = llUTCTimeSecond * 1000000 + llUTCTimeMicros;
    return  llUTCTime;
} 

在项目过程中涉及到时间操作相关的细节问题,现在将常用的方法整理出来,方便大家借鉴,同时发现问题,请积极提出。
 

智能网联汽车的安全员高级考试涉及多个方面的专业知识,包括但不限于自动驾驶技术原理、车辆传感器融合、网络安全防护以及法律法规等内容。以下是针对该主题的一些核心知识解析: ### 关于智能网联车安全员高级考试的核心内容 #### 1. 自动驾驶分级标准 国际自动机工程师学会(SAE International)定义了六个级别的自动驾驶等级,从L0到L5[^1]。其中,L3及以上级别需要安全员具备更高的应急处理能力。 #### 2. 车辆感知系统的组成与功能 智能网联车通常配备多种传感器,如激光雷达、毫米波雷达、摄像头和超声波传感器等。这些设备协同工作以实现环境感知、障碍物检测等功能[^2]。 #### 3. 数据通信与网络安全 智能网联车依赖V2X(Vehicle-to-Everything)技术进行数据交换,在此过程中需防范潜在的网络攻击风险,例如中间人攻击或恶意软件入侵[^3]。 #### 4. 法律法规要求 不同国家和地区对于无人驾驶测试及运营有着严格的规定,考生应熟悉当地交通法典中有关自动化驾驶部分的具体条款[^4]。 ```python # 示例代码:模拟简单决策逻辑 def decide_action(sensor_data): if sensor_data['obstacle'] and not sensor_data['emergency']: return 'slow_down' elif sensor_data['pedestrian_crossing']: return 'stop_and_yield' else: return 'continue_driving' example_input = {'obstacle': True, 'emergency': False, 'pedestrian_crossing': False} action = decide_action(example_input) print(f"Action to take: {action}") ``` 需要注意的是,“同学”作为特定平台上的学习资源名称,并不提供官方认证的标准答案集;建议通过正规渠道获取教材并参加培训课程来准备此类资格认证考试
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值