【问题标题】:Convert time_t to tm in a particular timezome在特定时区将 time_t 转换为 tm
【发布时间】:2016-05-23 20:32:32
【问题描述】:

localtime_r 函数将time_t 时间戳转换为当前 时区中的struct tm

如何将 UTC time_t 转换为不同时区的 struct tm,例如到澳大利亚悉尼的时间,考虑到夏令时和其他任何事情,并且不影响在程序的其他部分使用localtime

【问题讨论】:

  • 这个问题是关于时间转换的线程安全的吗?
  • 这似乎可以满足您的需求:stackoverflow.com/questions/1173578/…
  • 我认为标准的 C 函数在这里对你没有帮助。据我所知,库函数只是为您提供当前时间,操作系统处理时区和夏令时。因此,您可能需要使用特定于操作系统的函数或外部库。
  • 是否可以使用setenv改变TZ的值,调用localtime,然后将TZ设置回原来的值?
  • @NateEldredge 不是线程安全的?

标签: c time


【解决方案1】:

NetBSD 的 C 运行时库包含一些处理任意时区的函数(参见 NetBSD 6.1.5 - man page for ctime (netbsd section 3)。要在其他项目中获得类似的函数,您可以使用 IANA 的 Time Zone Database 的代码,特别是它的 localtime.cprivate.htzfile.h 文件。

【讨论】:

    猜你喜欢
    • 2015-05-06
    • 1970-01-01
    • 2014-02-02
    • 2018-06-04
    • 2019-04-12
    • 2022-11-12
    • 2022-01-24
    • 1970-01-01
    • 2021-07-21
    相关资源
    最近更新 更多