Convert time_t from localtime zone to UTC

前端 未结 3 1722
眼角桃花
眼角桃花 2021-01-02 03:57

I have a time_t that represents the time in seconds since epoch. Those seconds refer to the local time.

I want to convert them to UTC.

Is there

3条回答
  •  爱一瞬间的悲伤
    2021-01-02 04:07

    If you are okay with using Abseil's time library, one other way to do this is:

    auto civil_second =
      absl::LocalTimeZone().At(absl::FromTimeT()).cs;
    
    time_t time_in_utc = absl::ToTimeT(absl::FromCivil(civil_second, absl::UTCTimeZone()));
    

    (Maybe there is a simpler set of calls in the library to do this, but I have not explored further. :))

提交回复
热议问题