Discussion:
How to get a CTime from a string
(too old to reply)
msnews.microsoft.com
2004-02-12 22:14:38 UTC
Permalink
Hi all
I have a CString that has a time formated like this "01/30/2004 01:41:53
PM". Can anyone tell me an easy way to create a CTime object from this
string without needing to parse it manually.
Thanks
Ali R.
2004-02-12 23:04:58 UTC
Permalink
If you don't mind using the COleDateTime object instead, it has a
ParseDateTime method.

Ali R.
Post by msnews.microsoft.com
Hi all
I have a CString that has a time formated like this "01/30/2004 01:41:53
PM". Can anyone tell me an easy way to create a CTime object from this
string without needing to parse it manually.
Thanks
Tim
2004-02-13 00:49:07 UTC
Permalink
Hi,

CTime is problematic: the year range is highly restricitve, it has / had
bugs (VC6 - don't care any more, stopped using in years ago) - it has
apparently not been fixed in VS.Net either. If you are east of GMT then the
bugs show.

Best advice? Don't ever use it (for dates). It is after all called CTime not
CDateTime.

If you want to bind database stuff use TIMESTAMP_STRUCT or COleDateTime.

- Tim
Post by msnews.microsoft.com
Hi all
I have a CString that has a time formated like this "01/30/2004 01:41:53
PM". Can anyone tell me an easy way to create a CTime object from this
string without needing to parse it manually.
Thanks
null
2004-02-13 15:01:20 UTC
Permalink
Post by Tim
Hi,
CTime is problematic: the year range is highly restricitve, it has / had
bugs (VC6 - don't care any more, stopped using in years ago) - it has
apparently not been fixed in VS.Net either.
COleDatTime also had its problems due to being based on a floating point
value.

Anyway, the .NET CTime and other related classes use 64 bit integers
internally so there is no
longer a restrictive year range (that's in the new MFC DLL (MFC70.DLL)).

From:

http://msdn.microsoft.com/msdnnews/2001/sept/vcnet/vcnet.asp

64-bit Time and Date Functions
Your code may be safe from the Year 2000 bug, but did you know that the time
and date functions in the standard C runtime library will fail after
19:14:07 on January 18, 2038? The new 64-bit time and date functions can be
used to represent a date until midnight of December 31, 3000.
CTime and CTimeSpan have new methods to use 64-bit time/date values,
including CTimeSpan::GetDays64, CTimeSpan::GetTotalHours64,
CTimeSpan::GetHours64, CTimeSpan::Serialize64, CTime::GetTime64, and
CTime::Serialize64.
All these methods use the __int64 native compiler type. C runtime library
functions have also been updated to support 64-bit time/date: _ctime64,
_wctime64, _ftime64, _futime64, _gmtime64, _localtime64, _time64, _utime64,
_wutime64; _findfirst64, _wfindfirst64, _findnext64, _wfindnext64; and
_stat64, _wstat64.
Gary Chang
2004-02-13 05:17:02 UTC
Permalink
Hi,

Thanks for posting in the community.

According to Tim and Ali's suggestion, I think the COleDateTime may be a
good choice for you.

However, if you want to persist with the CTime, you can build a CTime
object from a string via a COleDateTime helper object:
..
LPCTSTR strTime = "01/30/2004 01:41:53 PM";
COleDateTime oleTime;
SYSTEMTIME st;

bool ret = oleTime.ParseDateTime(strTime);
ret = oleTime.GetAsSystemTime(st);

CTime cTime(st);
..


Thanks!

Best regards,

Gary Chang
Microsoft Online Partner Support

Get Secure! - www.microsoft.com/security
This posting is provided "AS IS" with no warranties, and confers no rights.
--------------------
Tom Serface
2004-02-13 19:15:24 UTC
Permalink
Here is a function I use for a very specific date format. I guess this is
parsing it manually, but it might give you some ideas. You could modify
this for whatever format you use or make it more general if you'd like:

//
// Converts a char string US date (mm/dd/yy) to a CTime.
//
CTime CTimeFromUSDate(LPCTSTR cUSDate)
{
int month = 1, day = 1, year = 1970;
if(_tcscmp(cUSDate,NULL_DATE) != 0 && cUSDate != NULL && strlen(cUSDate) !=
0) {
_stscanf(cUSDate,"%d/%d/%d",&month, &day, &year);
if(year < 1900) {
if(year >= 70)
year += 1900;
else
year += 2000;
}
}
return CTime(year, month, day, 0, 0, 0);
}


Tom
Post by msnews.microsoft.com
Hi all
I have a CString that has a time formated like this "01/30/2004 01:41:53
PM". Can anyone tell me an easy way to create a CTime object from this
string without needing to parse it manually.
Thanks
Continue reading on narkive:
Loading...