时间管理Time Management

使用这些函数获取当前时间并按需对其转换、调整及存储。Use these functions to get the current time and convert, adjust, and store it as necessary. 当前时间为系统时间。The current time is the system time.

_ftimelocaltime 例程使用 TZ 环境变量。The _ftime and localtime routines use the TZ environment variable. 如果未设置 TZ ,则运行时库将尝试使用由操作系统指定的时区信息。If TZ is not set, the run-time library attempts to use the time-zone information specified by the operating system. 如果此信息不可用,则这些函数将使用默认值 PST8PDT。If this information is unavailable, these functions use the default value of PST8PDT. 有关 TZ的详细信息,请参阅 _tzset;另请参阅 _daylight、timezone 和 _tznameFor more information on TZ, see _tzset; also see _daylight, timezone, and _tzname.

时间例程Time Routines

函数Function 使用Use
asctime, _wasctime, asctime_s, _wasctime_sasctime, _wasctime, asctime_s, _wasctime_s 将时间从类型 struct tm 转换为字符串。Convert time from type struct tm to character string. 这些具有 _s 后缀的函数版本更安全。The versions of these functions with the _s suffix are more secure.
clockclock 返回进程已用的时钟时间。Return elapsed wall-clock time for process.
ctime、_ctime32、_ctime64、_wctime、_wctime32、_wctime64ctime_s、_ctime32_s、_ctime64_s、_wctime_s、_wctime32_s、_wctime64_sctime, _ctime32, _ctime64, _wctime, _wctime32, _wctime64, _ctime_s, _ctime32_s, _ctime64_s, _wctime_s, _wctime32_s, _wctime64_s 将时间从类型 time_t__time32_t__time64_t 转换为字符串。Convert time from type time_t, __time32_t or __time64_t to character string. 这些具有 _s 后缀的函数版本更安全。The versions of these functions with the _s suffix are more secure.
difftime、_difftime32、_difftime64difftime, _difftime32, _difftime64 计算两个时间之差。Compute difference between two times.
_ftime, _ftime32, _ftime64_ftime_s, _ftime32_s, _ftime64_s_ftime, _ftime32, _ftime64,_ftime_s, _ftime32_s, _ftime64_s 将当前系统时间存储在 struct _timebstruct __timeb64 类型的变量中。这些带有 _s 后缀的函数版本更安全。Store current system time in variable of type struct _timeb or type struct __timeb64 The versions of these functions with the _s suffix are more secure.
_futime、_futime32、_futime64_futime, _futime32, _futime64 打开文件后设置修改时间Set modification time on open file
gmtime、_gmtime32、_gmtime64gmtime_s、_gmtime32_s、_gmtime64_sgmtime, _gmtime32, _gmtime64, gmtime_s, _gmtime32_s, _gmtime64_s 将时间从类型 time_t 转换为 struct tm 或从类型 __time64_t 转换为 struct tm。这些具有 _s 后缀的函数版本更安全。Convert time from type time_t to struct tm or from type __time64_t to struct tm.The versions of these functions with the _s suffix are more secure.
localtime、_localtime32、_localtime64localtime_s、_localtime32_s、_localtime64_slocaltime, _localtime32, _localtime64, localtime_s, _localtime32_s, _localtime64_s 使用本地更正将时间从类型 time_t 转换为 struct tm 或从类型 __time64_t 转换为 struct tmConvert time from type time_t to struct tm or from type __time64_t to struct tm with local correction. 这些具有 _s 后缀的函数版本更安全。The versions of these functions with the _s suffix are more secure.
_mkgmtime、_mkgmtime32、_mkgmtime64_mkgmtime, _mkgmtime32, _mkgmtime64 将时间转换为格林威治标准时间中的日历值。Convert time to calendar value in Greenwich Mean Time.
mktime、_mktime32、_mktime64mktime, _mktime32, _mktime64 将时间转换为日历值。Convert time to calendar value.
_strdate, _wstrdate, _strdate_s, _wstrdate_s_strdate, _wstrdate, _strdate_s, _wstrdate_s 以字符串形式返回当前系统日期。Return current system date as string. 这些具有 _s 后缀的函数版本更安全。The versions of these functions with the _s suffix are more secure.
strftime、wcsftime、_strftime_l、_wcsftime_lstrftime, wcsftime, _strftime_l, _wcsftime_l 将日期时间字符串设置为可在全球范围内使用的格式。Format date-and-time string for international use.
_strtime, _wstrtime_strtime_s、_wstrtime_s_strtime, _wstrtime, _strtime_s, _wstrtime_s 以字符串形式返回当前系统时间。Return current system time as string. 这些具有 _s 后缀的函数版本更安全。The versions of these functions with the _s suffix are more secure.
time、_time32、_time64time, _time32, _time64 time_t__time32_t 类型或 __time64_t类型获取当前系统时间。Get current system time as type time_t, __time32_t or as type __time64_t.
_tzset_tzset 根据环境时间变量 TZ设置外部时间变量。Set external time variables from environment time variable TZ.
_utime、_utime32、_utime64、_wutime、_wutime32、_wutime64_utime, _utime32, _utime64, _wutime, _wutime32, _wutime64 使用当前时间或存储在结构中的时间值设置指定文件的修改时间。Set modification time for specified file using either current time or time value stored in structure.

备注

在 Microsoft C/C++ 的所有版本(除 Microsoft C/C++ 7.0 版)和 Visual C++ 的所有版本中,时间函数将当前时间返回为自 1970 年 1 月 1 日午夜以来过去的秒数。In all versions of Microsoft C/C++ except Microsoft C/C++ version 7.0, and in all versions of Visual C++, the time function returns the current time as the number of seconds elapsed since midnight on January 1, 1970. 在 Microsoft C/C++ 7.0 版中, time 将当前时间返回为自 1899 年 12 月 31 日午夜以来过去的秒数。In Microsoft C/C++ version 7.0, time returned the current time as the number of seconds elapsed since midnight on December 31, 1899.

备注

在 Visual C++ 2005 之前的 Visual C++ 和 Microsoft C/C++ 的版本中,time_tlong int(32 位),因此无法用于 2038 年 1 月 19 日 3:14:07 UTC 之后的日期。In versions of Visual C++ and Microsoft C/C++ before Visual C++ 2005, time_t was a long int (32 bits) and hence could not be used for dates past 3:14:07 January 19, 2038, 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的版本。time_t is now equivalent to __time64_t by default, but defining _USE_32BIT_TIME_T changes time_t to __time32_t and forces many time functions to call versions that take the 32-bit time_t. 有关详细信息,请参阅标准类型以及文档中对各时间函数的注释。For more information, see Standard Types and comments in the documentation for the individual time functions.

另请参阅See Also

按类别分的运行时例程Run-Time Routines by Category