1 //
2 // strdate.cpp
3 //
4 // Copyright (c) Microsoft Corporation. All rights reserved.
5 //
6 // The strdate() family of functions, which return the current data as a string.
7 //
8 #include <corecrt_internal_securecrt.h>
9 #include <corecrt_internal_time.h>
10
11
12 // Returns the current date as a string of the form "MM/DD/YY". These functions
13 // return an error code on failure, or zero on success. The buffer must be at
14 // least nine characters in size.
15 template <typename Character>
16 static errno_t __cdecl common_strdate_s(
_Out_writes_z_(size_in_chars)17 _Out_writes_z_(size_in_chars) Character* const buffer,
18 _In_ _In_range_(>=, 9) size_t const size_in_chars
19 ) throw()
20 {
21 _VALIDATE_RETURN_ERRCODE(buffer != nullptr && size_in_chars > 0, EINVAL);
22 _RESET_STRING(buffer, size_in_chars);
23 _VALIDATE_RETURN_ERRCODE(size_in_chars >= 9, ERANGE);
24
25 SYSTEMTIME local_time;
26 GetLocalTime(&local_time);
27
28 int const month = local_time.wMonth;
29 int const day = local_time.wDay;
30 int const year = local_time.wYear % 100;
31
32 static Character const zero_char = static_cast<Character>('0');
33
34 #pragma warning(disable:__WARNING_POTENTIAL_BUFFER_OVERFLOW_HIGH_PRIORITY) // 26015
35
36 // Store the components of the date into the string in MM/DD/YY form:
37 buffer[0] = static_cast<Character>(month / 10 + zero_char); // Tens of month
38 buffer[1] = static_cast<Character>(month % 10 + zero_char); // Units of month
39 buffer[2] = static_cast<Character>('/');
40 buffer[3] = static_cast<Character>(day / 10 + zero_char); // Tens of day
41 buffer[4] = static_cast<Character>(day % 10 + zero_char); // Units of day
42 buffer[5] = static_cast<Character>('/');
43 buffer[6] = static_cast<Character>(year / 10 + zero_char); // Tens of year
44 buffer[7] = static_cast<Character>(year % 10 + zero_char); // Units of year
45 buffer[8] = static_cast<Character>('\0');
46
47 return 0;
48 }
49
_strdate_s(char * const buffer,size_t const size_in_chars)50 extern "C" errno_t __cdecl _strdate_s(char* const buffer, size_t const size_in_chars)
51 {
52 return common_strdate_s(buffer, size_in_chars);
53 }
54
_wstrdate_s(wchar_t * const buffer,size_t const size_in_chars)55 extern "C" errno_t __cdecl _wstrdate_s(wchar_t* const buffer, size_t const size_in_chars)
56 {
57 return common_strdate_s(buffer, size_in_chars);
58 }
59
60
61
62 // Returns the current date as a string of the form "MM/DD/YY". These functions
63 // assume that the provided result buffer is at least nine characters in length.
64 // Each returns the buffer on success, or null on failure.
65 template <typename Character>
66 _Success_(return != 0)
67 static Character* __cdecl common_strdate(_Out_writes_z_(9) Character* const buffer) throw()
68 {
69 errno_t const status = common_strdate_s(buffer, 9);
70 if (status != 0)
71 return nullptr;
72
73 return buffer;
74 }
75
_strdate(char * const buffer)76 extern "C" char* __cdecl _strdate(char* const buffer)
77 {
78 return common_strdate(buffer);
79 }
80
_wstrdate(wchar_t * const buffer)81 extern "C" wchar_t* __cdecl _wstrdate(wchar_t* const buffer)
82 {
83 return common_strdate(buffer);
84 }
85