我正在从事GPS项目,为此,我想在每次接收gps数据后添加时间戳(UTC)。如何在C中将IST转换为UTC?我将5:30小时减去tv.tv_sec,但这不是正确的方法。是否有从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;
}

最佳答案

您可能需要查看tzset()联机帮助页。它记录了一个全局变量timezone,类似于tzset()被调用后,它类似于本地时间(根据系统设置)与GMT的时区偏移量。

关于c - 如何在C(Ubuntu)中将IST时间转换为UTC时间?,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/29819791/

10-13 03:45