12

I've been programming in VB.NET for most of my very programming career. I have a C++ project provided to me, which I need to make a few modifications, and I am feeling hopelessly lost and confused.

It is a Visual Studio 2008 MFC project in C++.

an output variable has been defined:

char szout[900];

This line below, is used to append values to the output variable before output:

strcpy(szout, "TextHere")

So one of the many examples from searching, that i have tried, was to include at the top:

#include <windows.h>

And then for my code:

SYSTEMTIME st;
GetSystemTime(&st);
char myDate[20] = st;
CT2CA outputDate(myDate);
strcat(szout, outputDate);

For some reason the variables appended on to szout must be of type CT2CA, which i'm not really sure what this is either.

But then I get the following errors on the second and third line (char myDate...etc...) and (CT2CA output....etc....)

error C2440: 'initializing' : cannot convert from 'SYSTEMTIME' to 'char [20]'

error C2664: 'ATL::CW2AEX<>::CW2AEX(LPCWSTR) throw(...)' : cannot convert parameter 1 from 'char [20]' to 'LPCWSTR'

So I'll clarify, I am a complete novice with this, and would appreciate any and all help.

Thank you,

raym0nd
  • 3,172
  • 7
  • 36
  • 73
Adam
  • 187
  • 1
  • 2
  • 14

2 Answers2

28

If you are using MFC, why not:

// uses printf() format specifications for time
CString t = CTime::GetCurrentTime().Format("%H:%M");

// Or, if you have OLE Support
CString t = COleDateTime::GetCurrentTime().Format("%H:%M");
Chad
  • 18,706
  • 4
  • 46
  • 63
  • Project builds without error if i do not include either line. Including the first option gives this error: error C2664: 'CString ATL::CTime::Format(LPCTSTR) const' : cannot convert parameter 1 from 'const char [6]' to 'LPCTSTR' ------ including the second option gives this error: error C2664: 'CString ATL::COleDateTime::Format(DWORD,LCID) const' : cannot convert parameter 1 from 'const char [6]' to 'DWORD' – Adam Aug 02 '11 at 21:09
  • Also, if instead i put .Format(0, "%H:%M"); , it gets rid of the DWORD conversion error..but then generates a new error for parameter 2, that it cant be converted to LCID. – Adam Aug 02 '11 at 21:18
  • Sorry for so many comments =\... if i remove all parameters from the .Format method, then it compiles okay. But i would still like to be able to do the formatting as you had shown. – Adam Aug 02 '11 at 21:21
  • 4
    @Adam `LPCTSTR` translates to mean *"Long Pointer to a Constant STRing of Tchars"*, which means you're in Windows Unicode land. Changing it to `.Format(L"%H:%M")` might just fix it for you, but I myself have never really understood what best practices are for this sort of thing. This might shed some light: http://www.codeproject.com/KB/string/cppstringguide2.aspx – HostileFork says dont trust SE Aug 02 '11 at 21:41
  • 1
    I'll throw in that Joel Spolsky thinks this is the minimum every programmer should know about Unicode: http://www.joelonsoftware.com/articles/Unicode.html – HostileFork says dont trust SE Aug 02 '11 at 21:42
  • thank you for answer Chad, and the help Hostile Fork :) *nice name BTW*. I read the article by Joel Spolsky, very inlightening, thank you. – Adam Aug 03 '11 at 13:53
  • CT2CA isn't a type, it's an MFC [string conversion macro](http://msdn.microsoft.com/en-us/library/vstudio/87zae4a3.aspx#atl70stringconversionclassesmacros). – jake Aug 07 '13 at 20:10
  • This will get the time in the local time zone format. How do I get the UTC time? – Damian Jan 18 '17 at 12:16
  • @Damian After calling `GetCurrentTime()`, convert it using https://msdn.microsoft.com/en-us/library/38wh24td.aspx#coledatetime__getassystemtime then https://msdn.microsoft.com/en-us/library/windows/desktop/ms725485(v=vs.85).aspx – Chad Jan 18 '17 at 14:37
7

In MFC the following code is for current date in MMDDYYYY format.

CTime t = CTime::GetCurrentTime();
CString s = t.Format("%m%d%Y");
Pabitra Dash
  • 1,461
  • 2
  • 21
  • 28