Reactos
at listview 88 lines 3.3 kB view raw
1// 2// strtime.cpp 3// 4// Copyright (c) Microsoft Corporation. All rights reserved. 5// 6// The strtime family of functions, which return the current time as a string. 7// 8#include <corecrt_internal_securecrt.h> 9#include <corecrt_internal_time.h> 10 11#pragma warning(disable:__WARNING_HIGH_PRIORITY_OVERFLOW_POSTCONDITION) // 26045 Potential postcondition violation that could result in overflow 12#pragma warning(disable:__WARNING_RETURNING_BAD_RESULT) // 28196 The requirement that 'return!=0' is not satisfied. (The expression does not evaluate to true.) 13 14// Returns the current time as a string of the form "HH:MM:SS". These functions 15// return an error code on failure, or zero on success. The buffer must be at 16// least nine characters in size. 17template <typename Character> 18_Success_(buffer != 0 && size_in_chars >= 9) 19static errno_t __cdecl common_strtime_s( 20 _Out_writes_z_(size_in_chars) Character* const buffer, 21 _In_ _In_range_(>=, 9) size_t const size_in_chars 22 ) throw() 23{ 24 _VALIDATE_RETURN_ERRCODE(buffer != nullptr && size_in_chars > 0, EINVAL); 25 _RESET_STRING(buffer, size_in_chars); 26 _VALIDATE_RETURN_ERRCODE(size_in_chars >= 9, ERANGE); 27 28 SYSTEMTIME local_time; 29 GetLocalTime(&local_time); 30 31 int const hours = local_time.wHour; 32 int const minutes = local_time.wMinute; 33 int const seconds = local_time.wSecond; 34 35 static Character const zero_char = static_cast<Character>('0'); 36 37#pragma warning(disable:__WARNING_INCORRECT_VALIDATION) // 26014 Prefast doesn't notice the validation. 38 39 // Store the components of the date into the string in HH:MM:SS form: 40 buffer[0] = static_cast<Character>(hours / 10 + zero_char); // Tens of hour 41 buffer[1] = static_cast<Character>(hours % 10 + zero_char); // Units of hour 42 buffer[2] = static_cast<Character>(':'); 43 buffer[3] = static_cast<Character>(minutes / 10 + zero_char); // Tens of minute 44 buffer[4] = static_cast<Character>(minutes % 10 + zero_char); // Units of minute 45 buffer[5] = static_cast<Character>(':'); 46 buffer[6] = static_cast<Character>(seconds / 10 + zero_char); // Tens of second 47 buffer[7] = static_cast<Character>(seconds % 10 + zero_char); // Units of second 48 buffer[8] = static_cast<Character>('\0'); 49 50 return 0; 51} 52 53extern "C" errno_t __cdecl _strtime_s(char* const buffer, size_t const size_in_chars) 54{ 55 return common_strtime_s(buffer, size_in_chars); 56} 57 58extern "C" errno_t __cdecl _wstrtime_s(wchar_t* const buffer, size_t const size_in_chars) 59{ 60 return common_strtime_s(buffer, size_in_chars); 61} 62 63 64 65// Returns the current time as a string of the form "HH:MM:SS". These functions 66// assume that the provided result buffer is at least nine characters in length. 67// Each returns the buffer on success, or null on failure. 68template <typename Character> 69_Success_(buffer != 0) 70_Ret_z_ 71static Character* __cdecl common_strtime(_Out_writes_z_(9) Character* const buffer) throw() 72{ 73 errno_t const status = common_strtime_s(buffer, 9); 74 if (status != 0) 75 return nullptr; 76 77 return buffer; 78} 79 80extern "C" char* __cdecl _strtime(char* const buffer) 81{ 82 return common_strtime(buffer); 83} 84 85extern "C" wchar_t* __cdecl _wstrtime(wchar_t* const buffer) 86{ 87 return common_strtime(buffer); 88}