ReactOS 0.4.16-dev-852-gcfcc8d8
asctime.cpp
Go to the documentation of this file.
1//
2// asctime.cpp
3//
4// Copyright (c) Microsoft Corporation. All rights reserved.
5//
6// The asctime() family of functions, which convert a tm struct into a string.
7//
10
11#define _ASCBUFSIZE 26
12
13
14
15//-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
16//
17// asctime_s and _wasctime_s
18//
19//-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
20template <typename Character>
22 _Out_writes_(2) Character* p,
23 int const value,
24 bool const zero_fill
25 ) throw()
26{
27 if (value >= 10 || zero_fill)
28 {
29 *p++ = static_cast<Character>('0' + value / 10);
30 }
31 else
32 {
33 *p++ = ' ';
34 }
35
36 *p++ = static_cast<Character>('0' + value % 10);
37 return p;
38}
39
40
41
42// Converts a time structure (tm) into an ASCII string. The string is always
43// exactly 26 characters long, in the form Tue May 1 02:34:55 1984\n\0. The
44// buffer 'size_in_chars' must be at least 26. The string is generated into the
45// buffer. On success, zero is returned and the buffer contains the time string;
46// on failure, an error code is returned and the contents of the buffer are
47// indeterminate.
48template <typename Character>
49_Success_(return == 0)
50static errno_t __cdecl common_asctime_s(
52 _In_range_(>=, _ASCBUFSIZE) size_t const size_in_chars,
53 tm const* const tm_value
55{
57 buffer != nullptr && size_in_chars > 0,
58 EINVAL
59 )
60
61 _RESET_STRING(buffer, size_in_chars);
62
64 _VALIDATE_RETURN_ERRCODE(tm_value != nullptr, EINVAL)
65 _VALIDATE_RETURN_ERRCODE(tm_value->tm_year >= 0, EINVAL)
66
67 // Month, hour, minute, and second are zero-based
68 _VALIDATE_RETURN_ERRCODE(tm_value->tm_mon >= 0 && tm_value->tm_mon <= 11, EINVAL)
69 _VALIDATE_RETURN_ERRCODE(tm_value->tm_hour >= 0 && tm_value->tm_hour <= 23, EINVAL)
70 _VALIDATE_RETURN_ERRCODE(tm_value->tm_min >= 0 && tm_value->tm_min <= 59, EINVAL)
71 _VALIDATE_RETURN_ERRCODE(tm_value->tm_sec >= 0 && tm_value->tm_sec <= 60, EINVAL) // including leap second
72 _VALIDATE_RETURN_ERRCODE(tm_value->tm_wday >= 0 && tm_value->tm_wday <= 6, EINVAL)
73
74 _VALIDATE_RETURN_ERRCODE(__crt_time_is_day_valid(tm_value->tm_year, tm_value->tm_mon, tm_value->tm_mday), EINVAL)
75
76 Character* buffer_it = buffer;
77
78 // Copy the day name into the buffer:
79 char const* const day_first = __dnames + tm_value->tm_wday * 3;
80 char const* const day_last = day_first + 3;
81 for (char const* day_it = day_first; day_it != day_last; ++day_it)
82 *buffer_it++ = static_cast<Character>(*day_it);
83
84 *buffer_it++ = static_cast<Character>(' ');
85
86 // Copy the month name into the buffer:
87 char const* const month_first = __mnames + tm_value->tm_mon * 3;
88 char const* const month_last = month_first + 3;
89 for (char const* month_it = month_first; month_it != month_last; ++month_it)
90 *buffer_it++ = static_cast<Character>(*month_it);
91
92 *buffer_it++ = static_cast<Character>(' ');
93
94 // Copy the day of the month (1 - 31) into the buffer:
95 buffer_it = common_asctime_s_write_value(buffer_it, tm_value->tm_mday, false);
96 *buffer_it++ = static_cast<Character>(' ');
97
98 // Copy the time into the buffer in HH:MM:SS form:
99 buffer_it = common_asctime_s_write_value(buffer_it, tm_value->tm_hour, true);
100 *buffer_it++ = static_cast<Character>(':');
101 buffer_it = common_asctime_s_write_value(buffer_it, tm_value->tm_min, true);
102 *buffer_it++ = static_cast<Character>(':');
103 buffer_it = common_asctime_s_write_value(buffer_it, tm_value->tm_sec, true);
104 *buffer_it++ = static_cast<Character>(' ');
105
106 // Copy the four-digit year into the buffer:
107 buffer_it = common_asctime_s_write_value(buffer_it, __crt_get_century(tm_value->tm_year), true);
108 buffer_it = common_asctime_s_write_value(buffer_it, __crt_get_2digit_year(tm_value->tm_year), true);
109
110 // And that's it...
111 *buffer_it++ = static_cast<Character>('\n');
112 *buffer_it++ = static_cast<Character>('\0');
113
114 return 0;
115}
116
118 char* const buffer,
119 size_t const size_in_chars,
120 tm const* const tm_value
121 )
122{
123 return common_asctime_s(buffer, size_in_chars, tm_value);
124}
125
127 wchar_t* const buffer,
128 size_t const size_in_chars,
129 tm const* const tm_value
130 )
131{
132 return common_asctime_s(buffer, size_in_chars, tm_value);
133}
134
135
136
137//-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
138//
139// asctime and _wasctime
140//
141//-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
142// Utility functions used by common_asctime to get the per-thread actime buffer.
143static char** common_asctime_get_ptd_buffer(char) throw()
144{
146 if (ptd == nullptr)
147 return nullptr;
148
149 return &ptd->_asctime_buffer;
150}
151
152static wchar_t** common_asctime_get_ptd_buffer(wchar_t) throw()
153{
155 if (ptd == nullptr)
156 return nullptr;
157
158 return &ptd->_wasctime_buffer;
159}
160
161// Converts a time structure (tm) into an ASCII string. The string is always
162// exactly 26 characters long, in the form Tue May 1 02:34:55 1984\n\0.
163// The return value is a pointer to a per-thread buffer containing the
164// generated time string.
165template <typename Character>
166_Success_(return != 0)
168static Character* __cdecl common_asctime(tm const* const tm_value) throw()
169{
170 static Character static_buffer[_ASCBUFSIZE];
171
172 Character** ptd_buffer_address = common_asctime_get_ptd_buffer(Character());
173 if (ptd_buffer_address != nullptr && *ptd_buffer_address == nullptr)
174 {
175 *ptd_buffer_address = _calloc_crt_t(Character, _ASCBUFSIZE).detach();
176 }
177
178 Character* const buffer = ptd_buffer_address != nullptr && *ptd_buffer_address != nullptr
179 ? *ptd_buffer_address
180 : static_buffer;
181
182 errno_t const status = common_asctime_s(buffer, _ASCBUFSIZE, tm_value);
183 if (status != 0)
184 return nullptr;
185
186 return buffer;
187}
188
189extern "C" char* __cdecl asctime(tm const* const tm_value)
190{
191 return common_asctime<char>(tm_value);
192}
193
194extern "C" wchar_t* __cdecl _wasctime(tm const* const tm_value)
195{
196 return common_asctime<wchar_t>(tm_value);
197}
#define EINVAL
Definition: acclib.h:90
#define __cdecl
Definition: accygwin.h:79
tm const *const tm_value throw()
Definition: asctime.cpp:54
#define _ASCBUFSIZE
Definition: asctime.cpp:11
errno_t __cdecl _wasctime_s(wchar_t *const buffer, size_t const size_in_chars, tm const *const tm_value)
Definition: asctime.cpp:126
errno_t __cdecl asctime_s(char *const buffer, size_t const size_in_chars, tm const *const tm_value)
Definition: asctime.cpp:117
wchar_t *__cdecl _wasctime(tm const *const tm_value)
Definition: asctime.cpp:194
char *__cdecl asctime(tm const *const tm_value)
Definition: asctime.cpp:189
static char ** common_asctime_get_ptd_buffer(char)
Definition: asctime.cpp:143
static Character *__cdecl common_asctime_s_write_value(_Out_writes_(2) Character *p, int const value, bool const zero_fill)
Definition: asctime.cpp:21
__acrt_ptd *__cdecl __acrt_getptd_noexit(void)
#define _RESET_STRING(_String, _Size)
char const __mnames[]
Definition: timeset.cpp:157
char const __dnames[]
Definition: timeset.cpp:151
bool __cdecl __crt_time_is_day_valid(int const yr, int const mo, int const dy)
int __crt_get_century(int const year)
int __crt_get_2digit_year(int const year)
_In_ size_t const _In_ int _In_ bool const _In_ unsigned const _In_ __acrt_rounding_mode const _Inout_ __crt_cached_ptd_host & ptd
Definition: cvt.cpp:355
GLuint buffer
Definition: glext.h:5915
GLfloat GLfloat p
Definition: glext.h:8902
#define _VALIDATE_RETURN_ERRCODE(expr, errorcode)
#define _Out_writes_z_(s)
Definition: no_sal2.h:180
#define _Post_readable_size_(s)
Definition: no_sal2.h:536
#define _Success_(c)
Definition: no_sal2.h:84
#define _Ret_writes_z_(s)
Definition: no_sal2.h:312
#define _Out_writes_(s)
Definition: no_sal2.h:176
#define _In_range_(l, h)
Definition: no_sal2.h:368
Definition: ps.c:97
Definition: time.h:68
int errno_t
Definition: corecrt.h:615
Definition: pdh_main.c:96
#define const
Definition: zconf.h:233