Re: CTime getting skewed due to regional settings
"PRMARJORAM" <PRMARJORAM@discussions.microsoft.com> ha scritto nel messaggio
news:EB8890C7-EB85-4B0A-8898-C86899935BB2@microsoft.com...
I am writing to a DateTime field but via a dynamically constructed SQL
statement:
[...]
String DateTime::ToDBString()const
{
struct tm timeinfo;
char timebuf[26];
memset(timebuf,0,26);
errno_t errNo = localtime_s(&timeinfo, &mRawTime);
assert(errNo == 0 );
strftime(timebuf,26,"%Y-%m-%d ",&timeinfo);
return timebuf;
}
as opposed to previously using the ToString function which used the
following
strftime(timebuf,26,"%x",&timeinfo);
[...]
Hope this makes sense?
OK, so you are sending date values to SQL Server using a string like this:
YYYY-MM-DD
e.g.
2008-07-14
which is ISO 8601 representation for dates.
SQL Server accepts that, and that format is locale/language independent. So
it is fine.
You can see the table shown here, too:
"Date and time formats for input"
http://www.karaszi.com/SQLServer/info_datetime.asp
If you use Oracle, I'm not sure, but I think that you should use a syntax
like this to pass dates to SQL:
to_date( <date string>, <date format> )
e.g.
to_date( '2008-07-14', 'yyyy-mm-dd' )
or (no spaces):
to_date( '20080714', 'yyyymmdd' )
So, you may want to define a custom function for that
<code>
CString ToOracleDate( time_t * rawTime )
{
// Convert from time_t raw time to struct tm
struct tm timeInfo;
errno_t errNo = localtime_s( &timeInfo, rawTime );
// Format date as yyyymmdd
CString date;
date.Format( _T("%04d%02d%02d"), // yyyymmdd
timeInfo.tm_year + 1900,
timeInfo.tm_mon + 1,
timeInfo.tm_mday
);
// Use Oracle SQL syntax (to_date)
CString oracleDate;
oracleDate.Format( _T("to_date( '%s', 'yyyymmdd' )"), (LPCTSTR)date);
return oracleDate;
}
</code>
to be used like this:
<code>
time_t rawTime;
time( &rawTime );
CString oracleDate = ToOracleDate( &rawTime );
// print for test:
_tprintf( _T("%s\n"), (LPCTSTR)oracleDate);
</code>
The output is like this:
to_date( '20080714', 'yyyymmdd' )
which should be OK for Oracle.
HTH,
Giovanni