我正在開發GPS項目,爲此我想在每次接收GPS數據後添加時間戳(UTC)。我如何將IST轉換爲C中的UTC?我將tv.tv_sec減去5:30小時,但這不是正確的方法。是否有任何功能從IST獲取UTC?如何在C(Ubuntu)中將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;
}