bog frog
02-07-2006, 09:22 PM
Hello, I'm trying to convert a date string to it's corresponding system time in seconds (in a long) but my calculation is about 18 hours off. I'm at a loss right now for what could be wrong - any help would be appreciated..
The input format is: YYYYMMDDHHMMSS
When I compare this value to my current time GetTime() function, they are about 18 hours when they should only be an hour at most.
long CMyApp::GetTime( CString sTime ) {
struct tm t;
time_t t_of_day;
static char time_buffer[10];
char *pEnd;
// populate tmobject from param
t.tm_year = atoi( sTime.Mid( 0, 4 ) ) - 1900;
t.tm_mon = atoi( sTime.Mid( 4, 2 ) ) - 1;
t.tm_mday = atoi( sTime.Mid( 6, 2 ) );
t.tm_hour = atoi( sTime.Mid( 8, 2 ) );
t.tm_min = atoi( sTime.Mid( 10, 2 ) );
t.tm_sec = atoi( sTime.Mid( 12, 2 ) );
t.tm_isdst = 0;
// convert to time_t
t_of_day = mktime(&t);
// needs time_t
sprintf( time_buffer, "ud", t_of_day ); // percent sign normally before ud, this board stripped it out
// return as long
return strtol( time_buffer, &pEnd, 0 );
}
long CMyApp::GetTime() {
static char time_buffer[10];
time_t now;
char *pEnd;
now = time ( NULL );
sprintf( time_buffer, "ud", now ); // percent sign normally before ud, this board stripped it out
return strtol( time_buffer, &pEnd, 0 );
}
The input format is: YYYYMMDDHHMMSS
When I compare this value to my current time GetTime() function, they are about 18 hours when they should only be an hour at most.
long CMyApp::GetTime( CString sTime ) {
struct tm t;
time_t t_of_day;
static char time_buffer[10];
char *pEnd;
// populate tmobject from param
t.tm_year = atoi( sTime.Mid( 0, 4 ) ) - 1900;
t.tm_mon = atoi( sTime.Mid( 4, 2 ) ) - 1;
t.tm_mday = atoi( sTime.Mid( 6, 2 ) );
t.tm_hour = atoi( sTime.Mid( 8, 2 ) );
t.tm_min = atoi( sTime.Mid( 10, 2 ) );
t.tm_sec = atoi( sTime.Mid( 12, 2 ) );
t.tm_isdst = 0;
// convert to time_t
t_of_day = mktime(&t);
// needs time_t
sprintf( time_buffer, "ud", t_of_day ); // percent sign normally before ud, this board stripped it out
// return as long
return strtol( time_buffer, &pEnd, 0 );
}
long CMyApp::GetTime() {
static char time_buffer[10];
time_t now;
char *pEnd;
now = time ( NULL );
sprintf( time_buffer, "ud", now ); // percent sign normally before ud, this board stripped it out
return strtol( time_buffer, &pEnd, 0 );
}