About the output of ctime: format in Unicode and Its Solution

Source: Internet
Author: User

Old programs often use the format function for output when processing ctime formatting.
Under normal circumstances, there will be no problem. However, the bug was recently changed, and a special case was encountered in the old control to find its defects.

[Details]
The OCX written in MFC contains the following simple code:
Ctime T = ctime: getcurrenttime ();
Cstring stime = T. Format (_ T ("last calculation time: % Y, % m, % d % H: % m: % s "));
In ANSI, the above Code runs normally, but in UNICODE: the output is an empty string and it is not normal.
[Try again]
Remove the Chinese character from the formatted string and change it to English. Everything is normal.
[View Source Code]
View the source code of ctime: Format and find the basic function called:
_ Tcsftime (szbuffer, _ countof (szbuffer), pformat, ptmtemp)
See the type of szbuffer and pformat: tchar. Unicode is supported.
[Test again]
Write an ocx file and use the Unicode version for testing. The output is an empty string.

[Analyze the cause carefully]
Let's take a look at the Unicode version of _ tcsftime and conduct an experiment on it:
Struct tm t = {0, 0, 12, 25, 11, 93 };
Wchar_t wsdest [255];
Wchar_t wsformat [] = l "last calculation time: % Y, % m, % d % H: % m: % s ";
Size_t L = wcsftime (wsdest, 255, wsformat, & T );
Wsdest [l] = l'/0 ';
: Messageboxw (null, wsdest, l "test", mb_ OK );
Output: empty string.

Convert the format string to English:
Wchar_t wsformat [] = l "last time: % Y, % m, % d % H: % m: % s ";
Output: Last time:, 25 12:00:00

[Conclusion]
The Unicode version of ctime: format does not support formatting strings in Chinese. Hey.

[Solution]
Abandon the low-level junk stuff ctime and replace it with new weapons.
I wrote another Formatting Function. to be compatible with the previous one, I still accept the ctime input.

Cstring cbaseutil: formatdatetime (const ctime & T)
{
Cstring sret (_ T (""));
If (! T. getlocaltm () return sret; // t is not initialized and illegal.
// Or assert (T. getlocaltm ())

// For the format of formatted strings, see the help of varformat.
// For example, the latest calculation time is yyyy, mm, dd hh: NN: Ss.
Cstring sformat = sformat. loadstring (Format String );
Coledatetime DT (T. getyear (), T. getmonth (), T. getday (),
T. gethour (), T. getminute (), T. getsecond ());

Colevariant VAR (DT );
BSTR bstrout = sret. allocsysstring ();
# Ifdef _ Unicode
Varformat (VAR, sformat. getbuffer (0), 0, 0, & bstrout );
# Else
Uses_conversion;
Varformat (VAR, a2w (sformat. getbuffer (0), 0, 0, & bstrout );
# Endif
Sret = bstrout;
: Sysfreestring (bstrout );
Return sret;
}

Okay, the above method is OK. If you don't need ctime for the parameter, it would be best because ctime
The range is also relatively narrow. You can use coledatetime directly.

Stone on-10-21

 

This article from the csdn blog, reproduced please indicate the source: http://blog.csdn.net/tiger119/archive/2005/10/22/513526.aspx

 

======================================

Supplement

In fact, the format of coledatetime also has the same problem, because

_ Localeupdate _ loc_update (plocinfo );

Return _ wcsftime_l_stat (
Wstring,
Maxsize,
Wformat,
Timeptr,
_ Loc_update.getlocalet ());

//////

Then

Static size_t _ cdecl _ wcsftime_l_stat

// If (_ errcheck_einval_erange (_ wcstombs_s_l (null, format, FLEN * 2, wformat, FLEN * 2-1, plocinfo ))! = 0)
//{
// Goto done;
//}

This sentence will be incorrect.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.