473,399 Members | 3,888 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,399 software developers and data experts.

DATE to string

jwf
I am writing a NON MFC C++ application (Plug-in for a 3rd party DB system).
The application is designed to synchronise data between MS Outlook and the DB
system. I am using smart pointers for the development which is fine and
sysnchronisation back and forth is working as expected. The problem I am
having is with the DATE type (implemented using an 8-byte floating-point
number. Days are represented by whole number increments starting with 30
December 1899, midnight as time zero. Hour values are expressed as the
absolute value of the fractional part of the number. ). MS Outlook uses the
DATE type for e-mail dates, appointment dates etc e.g
spMailItem->GetReceivedTime() which I need to convert to a string in a
readable format e.g "25/12/2006 10:29:00" I just cant seem to get this to
work. I also need to go the other way (string to DATE). Can anyone provide a
simple code example to do this for me as I have looked everywhere and cannot
find a NON MFC example that works.

Thanks.
Sep 2 '06 #1
11 5223
Hi jwf!
The problem I am
having is with the DATE type (implemented using an 8-byte floating-point
number. Days are represented by whole number increments starting with 30
December 1899, midnight as time zero. Hour values are expressed as the
absolute value of the fractional part of the number. ).
First convert DATE to SYSTEMTIME:
- VariantTimeToSystemTime
then you can convert it to any string you want.

The other way:
SystemTimeToVariantTime

--
Greetings
Jochen

My blog about Win32 and .NET
http://blog.kalmbachnet.de/
Sep 2 '06 #2

"jwf" <jw*@newsgroups.nospamwrote in message
news:00**********************************@microsof t.com...
>I am writing a NON MFC C++ application (Plug-in for a 3rd party DB system).
The application is designed to synchronise data between MS Outlook and the
DB
system. I am using smart pointers for the development which is fine and
sysnchronisation back and forth is working as expected. The problem I am
having is with the DATE type (implemented using an 8-byte floating-point
number. Days are represented by whole number increments starting with 30
December 1899, midnight as time zero. Hour values are expressed as the
absolute value of the fractional part of the number. ). MS Outlook uses
the
DATE type for e-mail dates, appointment dates etc e.g
spMailItem->GetReceivedTime() which I need to convert to a string in a
readable format e.g "25/12/2006 10:29:00" I just cant seem to get this to
work. I also need to go the other way (string to DATE). Can anyone
provide a
simple code example to do this for me as I have looked everywhere and
cannot
find a NON MFC example that works.
This is a simple one :)

VarFormatDateTime() from Oleaut32.dll
It takes a VARIANT* but the variant is vt = VT_DATE; just copy your date
value to the date member.

Secondly, if you want to pass a mask, you'd take VarFormat()...
VARIANT v={0};
v.vt = VT_DATE;
v.date = yourDate;
CComBSTR thFmt;
HRESULT hr = VarFormatDateTime(&v, 0, 0, &thFmt);

But I never use this function, I use a combination of VarFormat() and use
GetLocaleInfoW to get the date / time mask. That mask is passed to VarFormat
and there you go!

Thanks.
Sep 2 '06 #3

"Egbert Nierop (MVP for IIS)" <eg***********@nospam.invalidwrote in
message news:em*************@TK2MSFTNGP03.phx.gbl...

Of course, you should convert this into a function.

FYI

LCID lcid = GetThreadLocale();
SYSTEMTIME st={0};
CComBSTR fmt;
int lenReq = GetLocaleInfoW(lcid, LOCALE_SSHORTDATE, NULL, 0);
if (lenReq 0)
{
fmt.SetLength(lenReq - 1);
GetLocaleInfoW(lcid, LOCALE_SSHORTDATE, fmt, lenReq);
}

lenReq = GetLocaleInfoW(lcid, LOCALE_STIMEFORMAT, NULL, 0);
if (lenReq 0)
{
text.SetLength(lenReq - 1);
GetLocaleInfoW(lcid, LOCALE_STIMEFORMAT, text, lenReq);
fmt.Append(L' ');
fmt += text; //now fmt contains eg mm-dd-yyyy hh:mm
}

VARIANT v={0};
v.vt = VT_DATE;
v.date = [yourdatehere]
HRESULT hr = VarFormatDateTime(&v, 0, 0, &fmt);

Sep 2 '06 #4
jwf


"Jochen Kalmbach [MVP]" wrote:
Hi jwf!
The problem I am
having is with the DATE type (implemented using an 8-byte floating-point
number. Days are represented by whole number increments starting with 30
December 1899, midnight as time zero. Hour values are expressed as the
absolute value of the fractional part of the number. ).

First convert DATE to SYSTEMTIME:
- VariantTimeToSystemTime
then you can convert it to any string you want.

The other way:
SystemTimeToVariantTime

--
Greetings
Jochen

My blog about Win32 and .NET
http://blog.kalmbachnet.de/
Thanks for this Jochen your post was very useful. The full method I used to
get from DATE to string is below (for anyone interested):

// Variables
DATE MailDateTime;

LPSYSTEMTIME SysTime;

string FormattedDateTime;

std::ostringstream oss_date;

//Get mail received time from outlook to DATE
MailDateTime = spMailItem->GetReceivedTime();

//Convert DATE to LPSYSTEMTIME
VariantTimeToSystemTime(MailDateTime, SysTime);

//Build string stream from date parts for readable format day/month/year
oss_date << SysTime->wDay << "/" << SysTime->wMonth << "/" << SysTime->wYear;

//assign string stream to string
FormattedDateTime.assign(oss_date.str());


Sep 2 '06 #5
Ray
jwf wrote:
Thanks for this Jochen your post was very useful. The full method I used to
get from DATE to string is below (for anyone interested):

// Variables
DATE MailDateTime;

LPSYSTEMTIME SysTime;

string FormattedDateTime;

std::ostringstream oss_date;

//Get mail received time from outlook to DATE
MailDateTime = spMailItem->GetReceivedTime();

//Convert DATE to LPSYSTEMTIME
VariantTimeToSystemTime(MailDateTime, SysTime);

//Build string stream from date parts for readable format day/month/year
oss_date << SysTime->wDay << "/" << SysTime->wMonth << "/" << SysTime->wYear;

//assign string stream to string
FormattedDateTime.assign(oss_date.str());
Hi jwf,

When did you allocate memory for LPSYSTEMTIME? As it is now I think your
code will crash when you call VariantTimeToSystemTime. LPSYSTEMTIME is a
pointer to SYSTEMTIME structure, so you need to have a SYSTEMTIME
allocated to receive the value when you call VariantTimeToSystemTime, e.g.:

SYSTEMTIME st;
LPSYSTEMTIME SysTime = &st;

Ray
Sep 3 '06 #6
jwf

"Ray" wrote:
Hi jwf,

When did you allocate memory for LPSYSTEMTIME? As it is now I think your
code will crash when you call VariantTimeToSystemTime. LPSYSTEMTIME is a
pointer to SYSTEMTIME structure, so you need to have a SYSTEMTIME
allocated to receive the value when you call VariantTimeToSystemTime, e.g.:

SYSTEMTIME st;
LPSYSTEMTIME SysTime = &st;

Ray

Hi Ray

Thanks for this but it doesn't seem necessary to do this.. The code in my
post seems to run without problems. You will have to excuse my C++ is very
rusty been mainly coding in VB.NET the last 4 years. Can you see any issue
with this?

Thanks

>
Sep 3 '06 #7
Hi jwf!
>When did you allocate memory for LPSYSTEMTIME? As it is now I think your
code will crash when you call VariantTimeToSystemTime. LPSYSTEMTIME is a
pointer to SYSTEMTIME structure, so you need to have a SYSTEMTIME
allocated to receive the value when you call VariantTimeToSystemTime, e.g.:

SYSTEMTIME st;
LPSYSTEMTIME SysTime = &st;

Ray

Hi Ray

Thanks for this but it doesn't seem necessary to do this..
Of yourse: This *IS* necessary!!!

You have just a pointer to nirvana...

You *must not* use LPSYSTEMTIME, instead use SYSTETIME and pass a
pointer to this struct:
SYSTEMTIME st;
VariantTimeToSystemTime(dbl, &st);

Greetings
Jochen
Sep 4 '06 #8
jwf

"Jochen Kalmbach [MVP]" wrote:
Hi jwf!
When did you allocate memory for LPSYSTEMTIME? As it is now I think your
code will crash when you call VariantTimeToSystemTime. LPSYSTEMTIME is a
pointer to SYSTEMTIME structure, so you need to have a SYSTEMTIME
allocated to receive the value when you call VariantTimeToSystemTime, e.g.:

SYSTEMTIME st;
LPSYSTEMTIME SysTime = &st;

Ray

Hi Ray

Thanks for this but it doesn't seem necessary to do this..

Of yourse: This *IS* necessary!!!

You have just a pointer to nirvana...

You *must not* use LPSYSTEMTIME, instead use SYSTETIME and pass a
pointer to this struct:
SYSTEMTIME st;
VariantTimeToSystemTime(dbl, &st);

Greetings
Jochen
Understood. I have updated code sample appropriately (hopefully there are no
further issues):

// Variables
DATE MailDateTime;

SYSTEMTIME SysTime;

string FormattedDateTime;

std::ostringstream oss_date;
//Get mail received time from outlook to DATE
MailDateTime = spMailItem->GetReceivedTime();

//Convert DATE to LPSYSTEMTIME
VariantTimeToSystemTime(MailDateTime, &SysTime);

//Build string stream from date parts for readable format day/month/year
oss_date << SysTime.wDay << "/" << SysTime.wMonth << "/" << SysTime.wYear <<
" " << SysTime.wHour << ":" << SysTime.wMinute << ":" << SysTime.wSecond;

//assign string stream to string
FormattedDateTime.assign(oss_date.str());

Thanks again
Sep 4 '06 #9
Ray
jwf wrote:
>>Thanks for this but it doesn't seem necessary to do this..
Of yourse: This *IS* necessary!!!
Hi jwf,

The fastest way to catch this kind of error is by running a Debug build
(as opposed to Release). In debug configuration, uninitialized pointers
like LPSYSTEMTIME are initialized to a value that'll give you illegal
access exception or something along that line when you try to
dereference it.

Like Jochen mentioned, your pointer points to nirvana, that is, nobody
knows where it points to :) (that is, you were *lucky* that your snippet
ran without problems!)

As a rule, watch out for those pointers that are redefined in Windows
SDK not to look like pointers anymore, e.g.: LPSYSTEMTIME, LPRECT,
LPVARIANT...

(why would anybody bother to come up with LPSYSTEMTIME where SYSTEMTIME*
would have just done as well is beyond me. Don't they like asterisks or
something? Or they hate double asterisks so one could say LPRECT*
instead of RECT**? Perhaps Raymond Chen has a blog about this... hmmm...)
>>
You have just a pointer to nirvana...

You *must not* use LPSYSTEMTIME, instead use SYSTETIME and pass a
pointer to this struct:
SYSTEMTIME st;
VariantTimeToSystemTime(dbl, &st);

Greetings
Jochen

Understood. I have updated code sample appropriately (hopefully there are no
further issues):

// Variables
DATE MailDateTime;

SYSTEMTIME SysTime;

string FormattedDateTime;

std::ostringstream oss_date;
//Get mail received time from outlook to DATE
MailDateTime = spMailItem->GetReceivedTime();

//Convert DATE to LPSYSTEMTIME
VariantTimeToSystemTime(MailDateTime, &SysTime);

//Build string stream from date parts for readable format day/month/year
oss_date << SysTime.wDay << "/" << SysTime.wMonth << "/" << SysTime.wYear <<
" " << SysTime.wHour << ":" << SysTime.wMinute << ":" << SysTime.wSecond;

//assign string stream to string
FormattedDateTime.assign(oss_date.str());

Thanks again
Sep 4 '06 #10
(why would anybody bother to come up with LPSYSTEMTIME where SYSTEMTIME*
would have just done as well is beyond me. Don't they like asterisks or
Long long ago, when we all used 16-bit compilers, SYSTEMTIME* wouldn't be
good enough (see the SS, BS, and DS CPU registers -- yuck!).
You'd have:

typedef SYSTEMTIME *PSYSTEMTIME; // not usable as argument to a DLL
typedef SYSTEMTIME __far *LPSYSTEMTIME;

I think I got the __far in the right place, it means the pointer can
reference memory in a different segment . But it's easy to get confused
between:
(a) __far SYSTEMTIME*
(b) SYSTEMTIME __far*
(c) SYSTEMTIME* __far

so the typedefs were a lifesaver.

Somehow the Windows API team never broke the habit, although when all
pointers became 32-bit in the flat memory model, the PXYZ (non-far)
datatypes jsut disappeared.
Sep 6 '06 #11
Ray
Ben Voigt wrote:
>(why would anybody bother to come up with LPSYSTEMTIME where SYSTEMTIME*
would have just done as well is beyond me. Don't they like asterisks or

Long long ago, when we all used 16-bit compilers, SYSTEMTIME* wouldn't be
good enough (see the SS, BS, and DS CPU registers -- yuck!).
<snip>

Thanks Ben... yeah, I guess I was lucky to start with Visual C++... 4.0
if memory serves--so I never really programmed Win16 and started right
away with 32-bit platforms. Well, lucky, or spoiled :)
Sep 7 '06 #12

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

2
by: androtech | last post by:
Hello, I'm looking for a function that returns a date range for a specified week number of the year. I'm not able to find functions like this anywhere. Any pointers/help would be much...
4
by: Robert Scarborough | last post by:
I have a Table in a Typed Dataset which contains a Date field called EventDate. I've ensured that the field is defined as Date as opposed to DateTime in the Typed Dataset. When I generate an...
15
by: Paul J. Ettl | last post by:
Two questions: I use var date1 = new Date(); to get todays date. But how can I get yesterdays date? Furthermore I use
12
by: DC Gringo | last post by:
How can I convert this pubLatest to a date with format "m/d/yyyy"? Dim pubLatest As New Date pubLatest = Me.SqlSelectCommand1.Parameters("@pubLatest").Value -- _____ DC G
2
by: Keith | last post by:
Good Afternoon, New to .Net. I am trying to pass date/time values to a MS Access query depending on what value is selected from a dropdown list box (January, February, etc). I have declared...
12
by: Assimalyst | last post by:
Hi, I have a working script that converts a dd/mm/yyyy text box date entry to yyyy/mm/dd and compares it to the current date, giving an error through an asp.net custom validator, it is as...
44
by: user | last post by:
Hi, Let's say I have 2 dates in the b/m format: Date 1 and date 2 How do I check whether Date2 is later than Date 1? Date1. 21-Nov-2006 09:00:00 PM
21
by: Darin | last post by:
I have an applicatoin that works 100% perfect when running on a machine setup for English (United States), but when I change it to Spanish (Mexico), the dates start giving me fits. THe reason is...
7
by: creative1 | last post by:
Hello everyone. I am experiencing a strange problem that I can't fix on my own. I think I need expert's suggestions for this. The problem is: I want to print account statement (or any other...
5
Stang02GT
by: Stang02GT | last post by:
I have been asked to validate a date on our web-page so that people cannot enter dates like 14/1/08 or 2/30/06. I have found code that will do exactly what i need it to do, but i am not sure how to...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.