【发布时间】:2015-04-23 09:50:30
【问题描述】:
我正在从事 GPS 项目,为此我想在每次接收 gps 数据后添加时间戳(UTC)。如何在 C 中将 IST 转换为 UTC?我从 tv.tv_sec 中减去 5:30 小时,但这不是正确的方法。有什么函数可以从 IST 获取 UTC 吗?
#define MAX_STRING_SIZE 50 /* for TimeStampsForServer */
int fnTimeStampsForGpsData ()
{
struct timeval tv;
struct tm *ptm;
char TimeString[MAX_STRING_SIZE];
long MicroSeconds;
/**
* Obtain the time of day, and convert it to a tm struct.
*/
gettimeofday (&tv, NULL);
tv.tv_sec -= 19800; /* Number of seconds (5:30) */
ptm = localtime (&tv.tv_sec);
/**
* Format the date and time, down to a single second.
*/
strftime (TimeString, sizeof (TimeString), "%Y-%m-%d %H:%M:%S", ptm);
/**
* Copying microseconds.
*/
MicroSeconds = tv.tv_usec;
/**
* Print the formatted time, in seconds, followed by a decimal point
* and the microseconds.
*/
printf ("%s.%06ld\n", TimeString, MicroSeconds);
return 0;
}
【问题讨论】: