使用这些函数获取当前时间并按需对其转换、调整及存储。 当前时间为系统时间。
_ftime 和 localtime 例程使用 TZ 环境变量。 如果未设置 TZ,运行时库会尝试使用由操作系统指定的时区信息。 如果此信息不可用,则这些函数将使用默认值 PST8PDT。 有关 TZ 的详细信息,请参阅_tzset;另请参阅_daylight、timezone 和 _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 后缀的函数版本更安全。 |
| .- . | 计算两个时间之差。 |
_ftime、、_ftime32_ftime64、_ftime_s、_ftime64_s _ftime32_s |
将当前系统时间存储在 struct _timeb 或 struct __timeb64 类型的变量中。这些带有 _s 后缀的函数版本更安全。 |
| .- . | 打开文件后设置修改时间 |
gmtime、、_gmtime32_gmtime64、gmtime_s、_gmtime32_s、 _gmtime64_s |
将时间从类型 time_t 转换为 struct tm,或从类型 __time64_t 转换为 struct tm。 这些具有 _s 后缀的函数版本更安全。 |
localtime、、_localtime32_localtime64、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的版本。 要了解详细信息,请参阅标准类型以及文档中对各时间函数的注释。