使用这些函数获取当前时间并按需对其转换、调整及存储。 当前时间为系统时间。
_ftime
和 localtime
例程使用 TZ
环境变量。 如果未设置 TZ
,运行时库会尝试使用由操作系统指定的时区信息。 如果此信息不可用,则这些函数将使用默认值 PST8PDT。 有关 TZ
的详细信息,请参阅_tzset
;另请参阅_daylight
、timezone
和 _tzname
。
时间例程
函数 | 使用 |
---|---|
asctime 、、_wasctime asctime_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 后缀的函数版本更安全。 |
.- . | 计算两个时间之差。 |
_ftime 、、_ftime32 _ftime64 、_ftime_s 、_ftime64_s _ftime32_s |
将当前系统时间存储在 struct _timeb 或 struct __timeb64 类型的变量中。这些带有 _s 后缀的函数版本更安全。 |
.- . | 打开文件后设置修改时间 |
gmtime 、、_gmtime64 _gmtime32 、gmtime_s 、_gmtime32_s 、 _gmtime64_s |
将时间从类型 time_t 转换为 struct tm ,或从类型 __time64_t 转换为 struct tm 。 这些具有 _s 后缀的函数版本更安全。 |
localtime 、、_localtime64 _localtime32 、localtime_s 、_localtime32_s 、 _localtime64_s |
使用本地更正将时间从类型 time_t 转换为 struct tm 或从类型 __time64_t 转换为 struct tm 。 这些具有 _s 后缀的函数版本更安全。 |
.- . | 将时间转换为格林威治标准时间中的日历值。 |
.- . | 将时间转换为日历值。 |
_strdate 、、_wstrdate _strdate_s 、、 _wstrdate_s |
以字符串形式返回当前系统日期。 这些具有 _s 后缀的函数版本更安全。 |
strftime 、 wcsftime 、 、 _strftime_l 、 _wcsftime_l |
将日期时间字符串设置为可在全球范围内使用的格式。 |
_strtime 、、_wstrtime _strtime_s 、、 _wstrtime_s |
以字符串形式返回当前系统时间。 这些具有 _s 后缀的函数版本更安全。 |
.- . | 以 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_t
为 long 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
的版本。 要了解详细信息,请参阅标准类型以及文档中对各时间函数的注释。