Редактиране

Споделяне чрез


_strdate_s, _wstrdate_s

Copy the current system date to a buffer. These functions 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 size
);
errno_t _wstrdate_s(
   wchar_t *buffer,
   size_t size
);
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

buffer
A pointer to a buffer to put the formatted date string.

size
Size of the buffer in character units.

Return value

Zero if successful. The return value is an error code if there's a failure. Error codes are defined in ERRNO.H; see table below for the exact errors generated by this function. For more information on error codes, see errno.

Error conditions

buffer size Return Contents of buffer
NULL (any) EINVAL Not modified
Not NULL (pointing to valid buffer) 0 EINVAL Not modified
Not NULL (pointing to valid buffer) 0 < size < 9 EINVAL Empty string
Not NULL (pointing to valid buffer) size >= 9 0 Current date formatted as specified in the remarks

Security issues

If you pass in an invalid, non-NULL value for buffer, it results in an access violation if the size parameter is greater than nine.

Passing a value for size greater than the actual size of buffer results in a buffer overrun.

Remarks

These functions provide more secure versions of _strdate and _wstrdate. The _strdate_s function copies the current system date to the buffer pointed to by buffer. It's formatted mm/dd/yy, where mm is the two-digit month, dd is the two-digit day, and yy is the last two digits of the year. For example, the string 12/05/99 represents December 5, 1999. The buffer must be at least nine characters long.

_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.

When buffer is a NULL pointer, or size is fewer than nine characters, the invalid parameter handler is invoked. It's described in Parameter validation. If execution is allowed to continue, these functions return -1. They set errno to EINVAL if the buffer is NULL or if size is less than or equal to 0. Or, they set errno to ERANGE if size is less than 9.

In C++, use of these functions is simplified by template overloads. The overloads can infer buffer length automatically, which eliminates the need to specify a size argument. And, they can automatically replace non-secure functions with their newer, more secure counterparts. For more information, see Secure template overloads.

The debug library versions of these functions first fill the buffer with 0xFE. To disable this behavior, use _CrtSetDebugFillThreshold.

By default, this function's global state is scoped to the application. To change this behavior, see Global state in the CRT.

Generic-text routine mapping:

TCHAR.H routine _UNICODE and _MBCS not defined _MBCS defined _UNICODE defined
_tstrdate_s _strdate_s _strdate_s _wstrdate_s

Requirements

Routine Required header
_strdate <time.h>
_wstrdate <time.h> or <wchar.h>
_strdate_s <time.h>

Example

See the example for time.

See also

Time management
asctime_s, _wasctime_s
ctime_s, _ctime32_s, _ctime64_s, _wctime_s, _wctime32_s, _wctime64_s
gmtime_s, _gmtime32_s, _gmtime64_s
localtime_s, _localtime32_s, _localtime64_s
mktime, _mktime32, _mktime64
time, _time32, _time64
_tzset