ReactOS 0.4.16-dev-981-g80eb313
strtime.cpp
Go to the documentation of this file.
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//
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
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>
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}
#define EINVAL
Definition: acclib.h:90
#define ERANGE
Definition: acclib.h:92
#define __cdecl
Definition: accygwin.h:79
#define _RESET_STRING(_String, _Size)
_wstrtime
result_buffer_count char *const _In_ int const _In_ bool const _In_ unsigned const _In_ STRFLT const _In_ bool const _Inout_ __crt_cached_ptd_host &ptd throw()
Definition: cvt.cpp:119
static DOUBLE local_time(DOUBLE time, DateInstance *date)
Definition: date.c:351
VOID WINAPI GetLocalTime(OUT LPSYSTEMTIME lpSystemTime)
Definition: time.c:286
GLuint buffer
Definition: glext.h:5915
#define _VALIDATE_RETURN_ERRCODE(expr, errorcode)
#define _Out_writes_z_(s)
Definition: no_sal2.h:180
#define _Success_(c)
Definition: no_sal2.h:84
#define _In_
Definition: no_sal2.h:158
#define _In_range_(l, h)
Definition: no_sal2.h:368
#define _Ret_z_
Definition: no_sal2.h:306
_strtime
Definition: time.h:313
errno_t __cdecl _strtime_s(char *const buffer, size_t const size_in_chars)
Definition: strtime.cpp:53
errno_t __cdecl _wstrtime_s(wchar_t *const buffer, size_t const size_in_chars)
Definition: strtime.cpp:58
Definition: ps.c:97
int errno_t
Definition: corecrt.h:615
#define const
Definition: zconf.h:233