C++ Converting a time string to seconds from the epoch

前端 未结 10 607
無奈伤痛
無奈伤痛 2020-12-01 14:53

I have a string with the following format:

2010-11-04T23:23:01Z

The Z indicates that the time is UTC.
I would rather store t

10条回答
  •  囚心锁ツ
    2020-12-01 15:04

    Problem here is that mktime uses local time not UTC time.

    How about just computing the time difference between UTC and local time, then adding it to the value returned by mktime?

    time_t local = time(NULL),
           utc   = mktime(gmtime(&local));
    int    diff  = utc - local;
    

提交回复
热议问题