时间管理

使用这些函数获取当前时间并按需对其转换、调整及存储。 当前时间为系统时间。

_ftimelocaltime 例程使用 TZ 环境变量。 如果未设置 TZ,运行时库会尝试使用由操作系统指定的时区信息。 如果此信息不可用,则这些函数将使用默认值 PST8PDT。 有关 TZ 的详细信息,请参阅_tzset;另请参阅_daylighttimezone_tzname

时间例程

函数 使用
asctime_wasctimeasctime_s_wasctime_s 将时间从类型 struct tm 转换为字符串。 这些具有 _s 后缀的函数版本更安全。
clock 返回进程已用的时钟时间。
ctime_ctime32_ctime64_wctime_wctime32_wctime64_ctime_s_ctime32_s_ctime64_s_wctime_s_wctime32_s_wctime64_s 将时间从类型 time_t__time32_t__time64_t 转换为字符串。 这些具有 _s 后缀的函数版本更安全。
difftime_difftime32_difftime64 计算两个时间之差。
_ftime_ftime32_ftime64_ftime_s_ftime32_s、_ftime64_s 将当前系统时间存储在 struct _timebstruct __timeb64 类型的变量中。这些带有 _s 后缀的函数版本更安全。
_futime_futime32_futime64 打开文件后设置修改时间
gmtime_gmtime32_gmtime64gmtime_s_gmtime32_s_gmtime64_s 将时间从类型 time_t 转换为 struct tm,或从类型 __time64_t 转换为 struct tm。 这些具有 _s 后缀的函数版本更安全。
localtime_localtime32_localtime64localtime_s_localtime32_s_localtime64_s 使用本地更正将时间从类型 time_t 转换为 struct tm 或从类型 __time64_t 转换为 struct tm。 这些具有 _s 后缀的函数版本更安全。
_mkgmtime_mkgmtime32_mkgmtime64 将时间转换为格林威治标准时间中的日历值。
mktime_mktime32_mktime64 将时间转换为日历值。
_strdate_wstrdate_strdate_s_wstrdate_s 以字符串形式返回当前系统日期。 这些具有 _s 后缀的函数版本更安全。
strftimewcsftime_strftime_l_wcsftime_l 将日期时间字符串设置为可在全球范围内使用的格式。
_strtime_wstrtime_strtime_s_wstrtime_s 以字符串形式返回当前系统时间。 这些具有 _s 后缀的函数版本更安全。
time_time32_time64 time_t__time32_t 类型或 __time64_t类型获取当前系统时间。
_tzset 根据环境时间变量 TZ设置外部时间变量。
_utime_utime32_utime64_wutime_wutime32_wutime64 使用当前时间或存储在结构中的时间值设置指定文件的修改时间。

注意

在 Microsoft C/C++ 的所有版本(除 Microsoft C/C++ 7.0 版)和 Visual C++ 的所有版本中,时间函数将当前时间返回为自 1970 年 1 月 1 日午夜以来过去的秒数。 在 Microsoft C/C++ 7.0 版中, time 将当前时间返回为自 1899 年 12 月 31 日午夜以来过去的秒数。

注意

在 Visual Studio 2005 之前的 Visual C++ 和 Microsoft C/C++ 版本中,time_tlong int(32 位),因此无法用于 2038 年 1 月 19 日 3:14:07 UTC 之后的日期。 现在,time_t 默认等于 __time64_t ,但定义 _USE_32BIT_TIME_T 会将 time_t 改为 __time32_t and forces many time functions 改为 call versions that take the 32-bit time_t的版本。 要了解详细信息,请参阅标准类型以及文档中对各时间函数的注释。

另请参阅

按类别分的通用 C 运行时例程