std::localtime

定义于头文件 <ctime>
std::tm* localtime( const std::time_t *time );

转换作为 std::time_t 值的从纪元起时间到以本地时间表达的日历时。

目录

参数

time - 指向要转换的 time_t 对象的指针

返回值

成功时为指向内部静态 std::tm 对象的指针,否则为 NULL 。该结构体可能在 std::gmtimestd::localtimestd::ctime 之间共享,而且可能在每次调用时被覆盖。

注意

此函数可能不是线程安全的。

POSIX 要求若此函数因参数过大而失败,则设置 errnoEOVERFLOW

POSIX 指定此函数如同以调用 tzset 确定时区信息,该函数调用环境变量 TZ

示例

#include <iostream>
#include <iomanip>
#include <ctime>
#include <stdlib.h> // 定义 putenv 于 POSIX
 
int main()
{
    std::time_t t = std::time(nullptr);
    std::cout << "UTC:       " << std::put_time(std::gmtime(&t), "%c %Z") << '\n';
    std::cout << "local:     " << std::put_time(std::localtime(&t), "%c %Z") << '\n';
    // POSIX 限定:
    std::string tz = "TZ=Asia/Singapore";
    putenv(tz.data());
    std::cout << "Singapore: " << std::put_time(std::localtime(&t), "%c %Z") << '\n';
}

输出:

UTC:       Fri Sep 15 14:16:29 2017 GMT
local:     Fri Sep 15 14:16:29 2017 UTC
Singapore: Fri Sep 15 22:16:29 2017 SGT

参阅

转换纪元起时间为表示为协调世界时的日历时间
(函数)
localtimeC 文档

版本历史

  • (当前 | 先前 2017年10月11日 (三) 05:58Fruderica讨论 | 贡献. . (1,801字节) (-253). . (撤销)
  • 当前 | 先前 2014年10月26日 (日) 16:39P12bot讨论 | 贡献 . . (2,054字节) (0). . (Fix some translations) (撤销)
  • 当前 | 先前 2013年7月2日 (二) 07:20P12bot讨论 | 贡献 . . (2,054字节) (-40). . (Use {{lc}}. Update links. Various fixes.) (撤销)
  • 当前 | 先前 2012年11月2日 (五) 15:48P12bot讨论 | 贡献 . . (2,094字节) (+150). . (r2.7.3) (机器人添加:de, en, es, it, pt, ru 去除:zh) (撤销)
  • 当前 | 先前 2012年10月26日 (五) 09:50P12讨论 | 贡献 . . (1,944字节) (0). . (1个修订: Translate from the English version) (撤销)
  • 当前 | 先前 2012年10月26日 (五) 06:00TranslationBot讨论 | 贡献. . (1,944字节) (-47). . (Translated from the English version using Google Translate) (撤销)
  • 当前 | 先前 2012年10月25日 (四) 13:14P12讨论 | 贡献 . . (1,991字节) (0). . (1个修订: Translate from the English version) (撤销)
  • 当前 | 先前 2012年10月25日 (四) 12:00TranslationBot讨论 | 贡献. . (1,991字节) (+960). . (Translated from the English version using Google Translate) (撤销)
  • 当前 | 先前 2012年5月4日 (五) 11:16P12bot讨论 | 贡献 . . (1,031字节) (+91). . (r2.7.3) (机器人添加:en:cpp/chrono/c/localtime, fr:cpp/chrono/c/localtime, ja:cpp/chrono/c/localtime (撤销)
  • 当前 | 先前 2012年5月1日 (二) 22:31P12bot讨论 | 贡献 . . (940字节) (+14). . (替换文字 - 「<code terminal>」替换为「<syntaxhighlight lang="text">」) (撤销)
  • 当前 | 先前 2012年5月1日 (二) 03:36P12讨论 | 贡献 . . (926字节) (0). . (1个修订: Import from Dokuwiki) (撤销)
  • 当前 | 先前) 2012年5月1日 (二) 03:36P12讨论 | 贡献. . (926字节) (+926). . (Import from dokuwiki)