convert CString to CTime - mfc

In smart device MFC application:
I have successfully converted CTime to CString. Now I want to convert it back, CString to CTime.
How can I do that?

Use COleDateTime::ParseDateTime(CString) and then convert it to CTime.

Check out the example mentioned in this MSDN doc page: COleDateTime::ParseDateTime

Related

Get Current Time in mm/dd/yyyy in C++

I am using the following code for setting the time in a Date Control in MFC using C++
CTime date;
date = date.GetCurrentTime();
this->m_headerDate.SetTime(&date);
This will get the Date and set it to the control in what ever format the user machine uses. But I want to set it to a format of ONLY mm/dd/yyyy.
There should be some way of doing this in MFC. Are there any utility functions for this?
Thanks,
Without MFC:
#include <iostream>
#include <ctime>
using namespace std;
int main() {
const int MAXLEN = 80;
char s[MAXLEN];
time_t t = time(0);
strftime(s, MAXLEN, "%m/%d/%Y", localtime(&t));
std::cout << s << '\n';
}
Compiled Code
With MFC:
This function formats a date as a date string for a specified locale. The function formats either a specified date or the local system date.
int GetDateFormat(
LCID Locale,
DWORD dwFlags,
CONST SYSTEMTIME* lpDate,
LPCTSTR lpFormat,
LPTSTR lpDateStr,
int cchDate
);
change LPCTSTR lpFormat to MM:dd:yyyy
If you're talking about getting a specific textual representation of a date/time, you can use strftime() to format a date in many different ways, including the one specified in your question.
You will need a variable of type time_t using the facilities in the ctime header. So you can either switch to using those times, or I believe CTime::GetTime( ) will give you one.
However, if you're talking about forcing a control to display it's date/time in a specific format, that's a property of the control itself. For example, CDateTimeCtrl provides a SetFormat() method which will modify how it displays its data.
Here I'm storing value in a CString variable
Try this code:
CString strTime;
CTime date;
date = GetCurrentTime();
strTime = date.Format(_T("%m/%d/%Y"));
I have found a way of doing this...not sure if this is the simplest way though
Since we already have a datetime control .All we can do is just use the Setformat Function of the DatetimeControl. PFB an example of this
CDateTimeCtrl m_DateTimeCtrl;
m_DateTimeCtrl.SetFormat(_T("MM/dd/yyyy"));
The above will set it up to the format of 01/14/2015 which is desired. Thanks to Paxdiablo, Himanshu and Irrational Person for the inputs. They did point me in right direction with different options.

Read special characters from file

For a few day I am trying to solve an ecoding issue in my C++ code.
I have the following content of a txt file (saved utf-8): "québécois". (and I have only read rights over the file).
I am reading it with ReadFile function in a std::string variable.
The first surpize is that the strContnet is not "québécois", but "québécois". (double encoded ??)
Why?
How cand I read it's content in a std::string variable in order to obtaint "québécois" (utf8 to ansi?)
Until now I have two ways to get somewere close to what I want but none satisfie me enought.
1. Convert from:
std::string to std::wstring (using MultiByteToWideChar)
create a CString from the wstring
use CT2CA to obtaint a new std::string
and finally use agin MultiByteToWideChar for a final std::wstring containint "québécois"
I thing the method is unsafe and not verry smart and I have a wstring not string.
I found this article:
How to convert from UTF-8 to ANSI using standard c++
And if I use utf8_to_string function twice I obtain what I want, but still I am not sure if it's the wright way.
Can anybody help me with a solution for this issue?

Converting a string of multibyte characters to widechar's gives unexpected results

I'm trying to read a web-page in UTF-8 encoding using WinInet library.
Here's some of my code:
HINTERNET hUrl = ::InternetOpenUrl(hInet, wurl.c_str(),NULL,NULL,NULL,NULL);
CHAR buffer[65536];
std::wstring full_content;
std::wstring read_content;
DWORD number_of_bytes_read=1;
while(number_of_bytes_read)
{
::InternetReadFile(hUrl, buffer, 65536, &number_of_bytes_read);
// ::InternetReadFileExW(hUrl, &buffersw, IRF_SYNC,NULL);
//((hUrl,buffer,65536,&number_of_bytes_read);
read_content.resize(number_of_bytes_read);
::MultiByteToWideChar(CP_ACP,MB_COMPOSITE,
&buffer[0],number_of_bytes_read,
&read_content[0],number_of_bytes_read);
full_content.append(read_content);
//readed_content.append(buffer,number_of_bytes_read);
}
I correctly see the english symbols, but instead of russian symbols I see a trash. What can it be?
Thanks in advance.
Your web page is UTF-8 and yet you decode it using ANSI code page (CP_ACP). Use CP_UTF8 instead
Change CP_ACP to CP_UTF8 and MB_COMPOSITE to 0
From the docs
For UTF-8 or code page 54936 (GB18030, starting with Windows Vista), dwFlags must be set to either 0 or MB_ERR_INVALID_CHARS. Otherwise, the function fails with ERROR_INVALID_FLAGS.
Do not convert at all. Keep it UTF-8 in memory. Convert to UTF-16 only when interacting with Windows API functions.
More info on this approach in http://utf8everywhere.org.

getting the local system date time and converting it to a string (MFC C++)

I have inherited some MFC C++ code (it's an ActiveX OCX control running on a Windows Mobile 6.5 device) and I need to acquire the system date and time and append it as part of an existing string which gets passed via the com port to another device.
I can get the system date and time, but I can not figure out how to convert that into a string so that I can append it (via strcat.)
I've found a number of different answers on Google and Bing for what at first glance seemed like such a simple problem... :( but I don't know enough MFC C++ to adapt any of it to my needs. Any help would be greatly appreciated.
CTime t = CTime::GetCurrentTime();
CString s = t.Format( "%A, %B %d, %Y" );
char * str = (LPCTSTR) s;
Note, I believe that str is only valid while s is in scope. Probably should copy it off somewhere if you need it to be around after s is destroyed. If you are passing it to strcat() you're probably OK.
In MFC the following code is for current date in MMDDYYYY format.
CTime t = CTime::GetCurrentTime();
CString strDate = t.Format("%m%d%Y");

MFC CString Format gives weird behaviour

I am having a weird problem with CString I have the following code which gives an unexpected result:
CString sourcePath = _T("C:\\some\\path\\file.ext");
CString log;
log.Format(_T("Path = %s"), sourcePath);
the result string shows Path = (null).
I am not sure what is going on under the hood there, I have tried casting the sourcePath to an LPCTSTR and got a valid pointer and when viewing the content of that memory address in the memory view window of MSVC everything seems valid.
this annoys me as I have tried the same method in previous projects and it worked pretty well.
Thanks.
P.S. Please, before people start commenting here asking about what is a CString, as it happened in an earlier post of mine, please check here first: MSDN CString
Edit: sorry about the slash thingy.. and yes the original code does have double slashes..
As your MSDN CString link also mentions, you cannot pass the CString itself to the Format function.
Use:
log.Format(_T("Path = %s"), sourcePath.GetString());
The string "C:\some\path\file.ext" should be "C:\some\path\file.ext" - otherwise you will read control characters (\s \p \f) instead.
Depending on the MFC version and whether your app is built for win32 or x64, you might have to cast the CString:
log.Format(_T("Path = %s"), (LPCTSTR)sourcePath);
There is a problem in your sourcePath initialization.
The character \ is a special character in C/C++. You should replace it with \ (double )
So the first line of code should be: CString sourcePath = _T("C:\\some\\path\\file.ext");
Hope this helps.