C code to get local time offset in minutes relative to UTC?

后端 未结 5 2335
不思量自难忘°
不思量自难忘° 2021-02-20 07:40

I didn\'t find a trivial way to get the time offset in minutes between the local time and the UTC time.

At first I intended to use tzset() but it doesn\'t p

5条回答
  •  醉梦人生
    2021-02-20 08:03

    Does your system's strftime() function support the %z and %Z specifiers? On FreeBSD,

     %Z    is replaced by the time zone name.
    
     %z    is replaced by the time zone offset from UTC; a leading plus sign
           stands for east of UTC, a minus sign for west of UTC, hours and
           minutes follow with two digits each and no delimiter between them
           (common form for RFC 822 date headers).
    

    and I can use this to print this:

    $ date +"%Z: %z"
    CEST: +0200
    

    ISO C99 has this in 7.23.3.5 The strftime function:

    %z     is replaced by the offset from UTC in the ISO 8601 format
           ‘‘−0430’’ (meaning 4 hours 30 minutes behind UTC, west of Greenwich),
           or by no characters if no time zone is determinable. [tm_isdst]
    %Z     is replaced by the locale’s time zone name or abbreviation, or by no
           characters if no time zone is determinable. [tm_isdst]
    

提交回复
热议问题