Easy way to convert a struct tm (expressed in UTC) to time_t type

前端 未结 9 924
臣服心动
臣服心动 2020-11-29 05:44

How do I do the above? There is mktime function but that treats the input as expressed in local time but how do i perform the conversion if my input tm variable happens to b

9条回答
  •  無奈伤痛
    2020-11-29 06:45

    The following implementation of timegm(1) works swimmingly on Android, and probably works on other Unix variants as well:

    time_t timegm( struct tm *tm ) {
      time_t t = mktime( tm );
      return t + localtime( &t )->tm_gmtoff;
    }
    

提交回复
热议问题