_strdate_s、_wstrdate_s_strdate_s, _wstrdate_s

将当前系统日期复制到缓冲区。Copy the current system date to a buffer. CRT 中的安全性功能中所述,这些版本的 _strdate、_wstrdate 具有安全性增强功能。These are versions of _strdate, _wstrdate with security enhancements as described in Security Features in the CRT.

语法Syntax

errno_t _strdate_s(
   char *buffer,
   size_t numberOfElements
);
errno_t _wstrdate_s(
   wchar_t *buffer,
   size_t numberOfElements
);
template <size_t size>
errno_t _strdate_s(
   char (&buffer)[size]
); // C++ only
template <size_t size>
errno_t _wstrdate_s(
   wchar_t (&buffer)[size]
); // C++ only

参数Parameters

bufferbuffer
指向缓冲区的指针将使用格式化的日期字符串填充。A pointer to a buffer which will be filled in with the formatted date string.

numberOfElementsnumberOfElements
缓冲区的大小。Size of the buffer.

返回值Return Value

如果成功,则返回 0。Zero if successful. 如果失败,则返回值为错误代码。The return value is an error code if there is a failure. 错误代码在 ERRNO.H 中定义;有关此函数生成的确切错误,请参阅下表。Error codes are defined in ERRNO.H; see table below for the exact errors generated by this function. 有关错误代码的详细信息,请参阅 errnoFor more information on error codes, see errno.

错误条件Error Conditions

bufferbuffer numberOfElementsnumberOfElements 返回Return 内容缓冲区Contents of buffer
NULLNULL (任意数值)(any) EINVALEINVAL 未修改Not modified
NULL (指向有效的缓冲区)Not NULL (pointing to valid buffer) 00 EINVALEINVAL 未修改Not modified
NULL (指向有效的缓冲区)Not NULL (pointing to valid buffer) 0 < numberOfElements < 90 < numberOfElements < 9 EINVALEINVAL 空字符串Empty string
NULL (指向有效的缓冲区)Not NULL (pointing to valid buffer) numberOfElements > = 9numberOfElements >= 9 00 注解中指定的当前日期格式Current date formatted as specified in the remarks

安全性问题Security Issues

传入无效非NULL值的缓冲区将导致访问冲突,如果numberOfElements参数大于 9。Passing in an invalid non NULL value for the buffer will result in an access violation if the numberOfElements parameter is greater than 9.

将大小的值传递大于的实际大小缓冲区将导致缓冲区溢出。Passing values for size that is greater than the actual size of the buffer will result in buffer overrun.

备注Remarks

这些函数提供更多安全版本 _strdate_wstrdateThese functions provide more secure versions of _strdate and _wstrdate. _Strdate_s函数将当前系统日期复制到由指向的缓冲区缓冲区格式化mm/dd / yy,其中mm是两位数表示月dd是两位数表示日,和yy是一年中的最后两位数字。The _strdate_s function copies the current system date to the buffer pointed to by buffer, formatted mm/dd/yy, where mm is two digits representing the month, dd is two digits representing the day, and yy is the last two digits of the year. 例如,字符串12/05/99表示 1999 年 12 月 5 日。For example, the string 12/05/99 represents December 5, 1999. 缓冲区长度必须至少为 9 个字符。The buffer must be at least 9 characters long.

_wstrdate_s是宽字符版本 _strdate_s; 的自变量和返回值 _wstrdate_s都是宽字符字符串。_wstrdate_s is a wide-character version of _strdate_s; the argument and return value of _wstrdate_s are wide-character strings. 否则这些函数具有相同行为。These functions behave identically otherwise.

如果缓冲区NULL指针,或者,如果numberOfElements小于 9 个字符,将调用无效参数处理程序,如中所述参数验证If buffer is a NULL pointer, or if numberOfElements is less than 9 characters, the invalid parameter handler is invoked, as described in Parameter Validation. 如果允许执行继续,这些函数将返回-1 并设置errnoEINVAL如果缓冲区NULL或者如果numberOfElements是否小于或等于 0 或一组errnoERANGE如果numberOfElements小于 9。If execution is allowed to continue, these functions return -1 and set errno to EINVAL if the buffer is NULL or if numberOfElements is less than or equal to 0, or set errno to ERANGE if numberOfElements is less than 9.

在 C++ 中,使用这些函数由模板重载简化;重载可以自动推导出缓冲区长度 (不再需要指定大小自变量),并且它们可以自动用以更新、更安全的对应物替换旧的、不安全的函数。In C++, using these functions is simplified by template overloads; the overloads can infer buffer length automatically (eliminating the need to specify a size argument) and they can automatically replace older, non-secure functions with their newer, secure counterparts. 有关详细信息,请参阅 Secure Template OverloadsFor more information, see Secure Template Overloads.

一般文本例程映射:Generic-Text Routine Mapping:

TCHAR.H 例程TCHAR.H routine 未定义 _UNICODE 和 _MBCS_UNICODE & _MBCS not defined 已定义 _MBCS_MBCS defined 已定义 _UNICODE_UNICODE defined
_tstrdate_s_tstrdate_s _strdate_s_strdate_s _strdate_s_strdate_s _wstrdate_s_wstrdate_s

要求Requirements

例程所返回的值Routine 必需的标头Required header
_strdate_strdate <time.h><time.h>
_wstrdate_wstrdate <time.h> 或 <wchar.h><time.h> or <wchar.h>
_strdate_s_strdate_s <time.h><time.h>

示例Example

请参阅 time 的示例。See the example for time.

请参阅See also

时间管理Time Management
asctime_s、_wasctime_sasctime_s, _wasctime_s
ctime_s、_ctime32_s、_ctime64_s、_wctime_s、_wctime32_s、_wctime64_sctime_s, _ctime32_s, _ctime64_s, _wctime_s, _wctime32_s, _wctime64_s
gmtime_s、_gmtime32_s、_gmtime64_sgmtime_s, _gmtime32_s, _gmtime64_s
localtime_s、_localtime32_s、_localtime64_slocaltime_s, _localtime32_s, _localtime64_s
mktime、_mktime32、_mktime64mktime, _mktime32, _mktime64
time、_time32、_time64time, _time32, _time64
_tzset_tzset