ReactOS 0.4.16-dev-732-g2d1144a
strftime.cpp
Go to the documentation of this file.
1//
2// strftime.cpp
3//
4// Copyright (c) Microsoft Corporation. All rights reserved.
5//
6// The strftime family of functions, which format time data into a string, and
7// related functionality.
8//
10#include <locale.h>
11
12//-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
13//
14// Day and Month Name and Time Locale Information Fetching Functions
15//
16//-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
17extern "C" char* __cdecl _Getdays_l(_locale_t const locale)
18{
19 _LocaleUpdate locale_update(locale);
20 __crt_lc_time_data const* const time_data = locale_update.GetLocaleT()->locinfo->lc_time_curr;
21
22 size_t length = 0;
23 for (size_t n = 0; n < 7; ++n)
24 {
25 length += strlen(time_data->wday_abbr[n]) + strlen(time_data->wday[n]) + 2;
26 }
27
28 __crt_unique_heap_ptr<char> buffer(_malloc_crt_t(char, length + 1));
29 if (buffer.get() == nullptr)
30 return nullptr;
31
32
33 char* it = buffer.get();
34 for (size_t n = 0; n < 7; ++n)
35 {
36 *it++ = ':';
37 _ERRCHECK(strcpy_s(it, (length + 1) - (it - buffer.get()), time_data->wday_abbr[n]));
38 it += strlen(it);
39 *it++ = ':';
40 _ERRCHECK(strcpy_s(it, (length + 1) - (it - buffer.get()), time_data->wday[n]));
41 it += strlen(it);
42 }
43 *it++ = '\0';
44
45 return buffer.detach();
46}
47
48extern "C" char* __cdecl _Getdays()
49{
50 return _Getdays_l(nullptr);
51}
52
53
54
55extern "C" char* __cdecl _Getmonths_l(_locale_t const locale)
56{
57 _LocaleUpdate locale_update(locale);
58 __crt_lc_time_data const* time_data = locale_update.GetLocaleT()->locinfo->lc_time_curr;
59
60 size_t length = 0;
61 for (size_t n = 0; n < 12; ++n)
62 {
63 length += strlen(time_data->month_abbr[n]) + strlen(time_data->month[n]) + 2;
64 }
65
66 __crt_unique_heap_ptr<char> buffer(_malloc_crt_t(char, length + 1));
67 if (buffer.get() == nullptr)
68 return nullptr;
69
70 char* it = buffer.get();
71 for (size_t n = 0; n < 12; ++n)
72 {
73 *it++ = ':';
74 _ERRCHECK(strcpy_s(it, (length + 1) - (it - buffer.get()), time_data->month_abbr[n]));
75 it += strlen(it);
76 *it++ = ':';
77 _ERRCHECK(strcpy_s(it, (length + 1) - (it - buffer.get()), time_data->month[n]));
78 it += strlen(it);
79 }
80 *it++ = '\0';
81
82 return buffer.detach();
83}
84
85
86
87extern "C" char* __cdecl _Getmonths()
88{
89 return _Getmonths_l(nullptr);
90}
91
92extern "C" void* __cdecl _W_Gettnames();
93
94extern "C" void* __cdecl _Gettnames()
95{
96 return _W_Gettnames();
97}
98
99
100
101//-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
102//
103// The strftime family of functions
104//
105//-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
106// These functions format a time as a string using a given locale. They place
107// characters into the user's output buffer, expanding time format directives as
108// described in the provided control string. The lc_time_arg and locale are
109// used for locale data.
110//
111// If the total number of characters that need to be written (including the null
112// terminator) is less than the max_size, then the number of characters written
113// (not including the null terminator) is returned. Otherwise, zero is returned.
114//
115// These functions simply delegate to the corresponding wide string functions
116// (e.g. _wcsftime).
117_Success_(return > 0)
118extern "C" size_t __cdecl _Strftime_l (
119 _Out_writes_z_(maxsize) char * const string,
125 )
126{
127 _LocaleUpdate locale_update(locale);
128 unsigned int const lc_time_cp = locale_update.GetLocaleT()->locinfo->lc_time_cp;
129
130 _VALIDATE_RETURN(string != nullptr, EINVAL, 0)
132 *string = '\0';
133
134 _VALIDATE_RETURN(format != nullptr, EINVAL, 0)
135 _VALIDATE_RETURN(timeptr != nullptr, EINVAL, 0)
136
138
140
141 if (cvt1 != 0) {
142 return 0;
143 }
144
145 // Allocate a new wide-char output string with the same size as the char*
146 // string one passed in as argument
147 __crt_unique_heap_ptr<wchar_t> const wstring(_malloc_crt_t(wchar_t, maxsize));
148 if (wstring.get() == nullptr)
149 {
150 // malloc should set the errno, if any
151 return 0;
152 }
153
156 {
157 return 0;
158 }
159
162
163 if (cvt2 != 0) {
164 return 0;
165 }
166
167 return copy_back.size();
168}
169
170extern "C" size_t __cdecl _Strftime(
171 char* const string,
172 size_t const max_size,
173 char const* const format,
174 tm const* const timeptr,
175 void* const lc_time_arg
176 )
177{
178 return _Strftime_l(string, max_size, format, timeptr, lc_time_arg, nullptr);
179}
180
181extern "C" size_t __cdecl _strftime_l(
182 char* const string,
183 size_t const max_size,
184 char const* const format,
185 tm const* const timeptr,
186 _locale_t const locale
187 )
188{
189 return _Strftime_l(string, max_size, format, timeptr, nullptr, locale);
190}
191extern "C" size_t __cdecl strftime(
192 char* const string,
193 size_t const max_size,
194 char const* const format,
195 tm const* const timeptr
196 )
197{
198 return _Strftime_l(string, max_size, format, timeptr, nullptr, nullptr);
199}
#define EINVAL
Definition: acclib.h:90
ACPI_SIZE strlen(const char *String)
Definition: utclib.c:269
#define __cdecl
Definition: accygwin.h:79
static INT max_size
Definition: history.c:51
Definition: terminate.cpp:24
Definition: _locale.h:75
#define _ERRCHECK(e)
#define _VALIDATE_RETURN(expr, errorcode, retexpr)
errno_t __acrt_mbs_to_wcs_cp(char const *const null_terminated_input_string, __crt_win32_buffer< wchar_t, ResizePolicy > &win32_buffer, unsigned int const code_page)
errno_t __acrt_wcs_to_mbs_cp(wchar_t const *const null_terminated_input_string, __crt_win32_buffer< char, ResizePolicy > &win32_buffer, unsigned int const code_page)
GLdouble n
Definition: glext.h:7729
GLuint buffer
Definition: glext.h:5915
GLuint GLsizei GLsizei * length
Definition: glext.h:6040
if(dx< 0)
Definition: linetemp.h:194
#define strcpy_s(d, l, s)
Definition: utility.h:200
#define _Out_writes_z_(s)
Definition: no_sal2.h:180
#define _Success_(c)
Definition: no_sal2.h:84
#define _In_z_
Definition: no_sal2.h:164
#define _In_
Definition: no_sal2.h:158
#define _In_opt_
Definition: no_sal2.h:212
size_t __cdecl _strftime_l(char *const string, size_t const max_size, char const *const format, tm const *const timeptr, _locale_t const locale)
Definition: strftime.cpp:181
char *__cdecl _Getdays()
Definition: strftime.cpp:48
_In_ size_t const _In_z_ const char *const _In_ const tm *const timeptr
Definition: strftime.cpp:122
errno_t const cvt2
Definition: strftime.cpp:161
void *__cdecl _Gettnames()
Definition: strftime.cpp:94
char *__cdecl _Getmonths()
Definition: strftime.cpp:87
char *__cdecl _Getmonths_l(_locale_t const locale)
Definition: strftime.cpp:55
errno_t const cvt1
Definition: strftime.cpp:139
__crt_unique_heap_ptr< wchar_t > const wstring(_malloc_crt_t(wchar_t, maxsize))
size_t const wcsftime_result
Definition: strftime.cpp:154
_In_ size_t const maxsize
Definition: strftime.cpp:120
char *__cdecl _Getdays_l(_locale_t const locale)
Definition: strftime.cpp:17
size_t __cdecl _Strftime(char *const string, size_t const max_size, char const *const format, tm const *const timeptr, void *const lc_time_arg)
Definition: strftime.cpp:170
__crt_no_alloc_win32_buffer< char > copy_back(string, maxsize)
__crt_internal_win32_buffer< wchar_t > wformat
Definition: strftime.cpp:137
void *__cdecl _W_Gettnames()
Definition: wcsftime.cpp:83
size_t __cdecl strftime(char *const string, size_t const max_size, char const *const format, tm const *const timeptr)
Definition: strftime.cpp:191
unsigned int const lc_time_cp
Definition: strftime.cpp:128
_In_ size_t const _In_z_ const char *const _In_ const tm *const _In_ void *const lc_time_arg
Definition: strftime.cpp:123
Definition: format.c:58
Definition: time.h:68
int errno_t
Definition: corecrt.h:615
size_t __cdecl _Wcsftime_l(wchar_t *const string, size_t const max_size, wchar_t const *const format, tm const *const timeptr, void *const lc_time_arg, _locale_t const locale)
Definition: wcsftime.cpp:1049
#define const
Definition: zconf.h:233