nmea輸出的gnrmc資料中,時間資訊是格林威治時間,和我們東八區的時間相差八小時,要將其轉換為東八區的時間,可以用以下方法:
#include
"math.h"
#include
"stdbool.h"
#include
"stdint.h"
#include
"stdio.h"
#include
"stdlib.h"
#include
"string.h"
#include
"time.h"
#include
// 引數舉例 t.exe 2020-02-28 18
intmain
(int argc,
char
*ar**)
int year, month, mday;
int hour;
sscanf
(ar**[1]
,"%04d-%02d-%02d"
,&year,
&month,
&mday)
;sscanf
(ar**[2]
,"%02d"
,&hour)
;printf
("input: %02d-%02d-%02d %02d:00:00 \r\n"
, year, month, mday, hour)
;char timestring[32]
;struct tm *ptm;
struct tm now;
now.tm_year = year -
1900
; now.tm_mon = month -1;
now.tm_mday = mday;
now.tm_hour = hour;
now.tm_min =0;
now.tm_sec =0;
time_t nowsec =
mktime
(&now)
;printf
("secs : %d\r\n"
, nowsec)
; ptm =
localtime
(&nowsec)
;strftime
(timestring,
sizeof
(timestring)
,"%y-%m-%d %h:%m:%s"
, ptm)
;printf
("utc+0: %s\r\n"
, timestring)
; nowsec +
=3600*8
;//加上8小時的總秒數
ptm =
localtime
((time_t*
)&nowsec);(
void
)ptm;
strftime
(timestring,
sizeof
(timestring)
,"%y-%m-%d %h:%m:%s"
, ptm)
;printf
("utc+8: %s\r\n"
, timestring)
;return0;
}
C語言 UTC時間轉換為北京時間
最近使用gps模組獲取解析gprmc資料的時候,獲取到的是utc時間,需要轉換為北京時間,網上看了一些文章,發現存在一些問題,於是自己完善了一下,並做了注釋,方便理解。typedef struct p nmea time,s nmea time utc時間轉換為任意時區時間,如果是轉換為北京時間,t...
string 轉換為 32位時間time t
2008 12 11t13 20 09.74 tm tmtmp zeromemory tmtmp,sizeof tm tmtmp.tm year ttoi lpstr tmtmp.tm year tmtmp.tm year 1970 70 tmtmp.tm mon ttoi lpstr 5 tmtm...
將10位和13位時間戳轉換為時間
請求帶有時間戳請注意,yy mm dd hh mm ss是12小時制格式。yy mm dd hh mm ss是24小時制格式。差別巨大 將10 or 13 位時間戳轉為時間字串 convert the number 1407449951 1407499055617 to date time form...