博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
ISO8601和UTC 时间,由秒数转化日期时间,日期到秒数
阅读量:4166 次
发布时间:2019-05-26

本文共 8662 字,大约阅读时间需要 28 分钟。

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 
#include
#ifdef WIN32#include
#define snprintf sprintf_s#else#include
#endiftypedef 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;} 

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

 

转载地址:http://dcqxi.baihongyu.com/

你可能感兴趣的文章
jarFile
查看>>
EJB与JAVA BEAN_J2EE的异步消息机制
查看>>
数学等于号是=那三个横杠是什么符
查看>>
HTTP协议详解
查看>>
java多线程中的join方法详解
查看>>
idea添加gradle模块报错The project is already registered
查看>>
在C++中如何实现模板函数的外部调用
查看>>
HTML5学习之——HTML 5 拖放
查看>>
HTML5学习之——HTML 5 Canvas vs. SVG
查看>>
HTML5学习之——HTML 5 应用程序缓存
查看>>
HTML5学习之——HTML 5 Web Workers
查看>>
HTML5学习之——HTML 5 Canvas
查看>>
HTML5学习之——HTML5 内联 SVG
查看>>
HTML5学习之——HTML 5 服务器发送事件
查看>>
SVG学习之——HTML 页面中的 SVG
查看>>
SVG 形状学习之——SVG圆形
查看>>
SVG 滤镜学习之——SVG 滤镜
查看>>
mysql中用命令行复制表结构的方法
查看>>
hbase shell出现ERROR: org.apache.hadoop.hbase.ipc.ServerNotRunningYetException
查看>>
让代码变得更优雅-Lombok
查看>>