imestamp from localtime mktime
Hi,
i have to kind of strings (char*)
1) hh:mm:ss
2)YYYY-MM-TThh:mm:ss.000Z (eg.:"2008-09-26T01:51:42.000Z")
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28
|
long Timestamp(char* CurrentTime)
{
std::string str = CurrentTime;
time_t t;
struct tm * DateTime;
t = time(NULL);
DateTime = localtime(&t);
if(str.length() == 8)
{
DateTime->tm_hour = atoi(str.substr(0,2).c_str());
DateTime->tm_min = atoi(str.substr(3,2).c_str());
DateTime->tm_sec = atoi(str.substr(6,2).c_str());
return (long)mktime(DateTime);
}
else if(str.length() == 24)
{
DateTime->tm_year = atoi(str.substr(0,4).c_str()) - 1900;
DateTime->tm_mon = atoi( str.substr(5,2).c_str()) -1;
DateTime->tm_mday = atoi(str.substr(8,2).c_str());
DateTime->tm_hour = atoi(str.substr(11,2).c_str());
DateTime->tm_min = atoi(str.substr(14,2).c_str());
DateTime->tm_sec = atoi(str.substr(17,2).c_str());
return (long)mktime(DateTime);
}
else return (long)t; //timestamp from current time
}
|
this code works with the input. My question is this the best way or is this code nonsens.
Return: timestamp from selected or current time.
Greetz
Topic archived. No new replies allowed.