c + + Foundation (6): matlab time conversion, ascii to char and time format conversion

Keywords: ascii MATLAB codec

Let the output start and end time, did not expect that matlab is output like this, feel egg pain incomparably in an instant

5.0000000e+01   4.8000000e+01   4.9000000e+01   5.6000000e+01   4.5000000e+01   4.9000000e+01   5.0000000e+01   4.5000000e+01   5.0000000e+01   4.8000000e+01   3.2000000e+01   4.9000000e+01   5.7000000e+01   5.8000000e+01   5.1000000e+01   5.7000000e+01   5.8000000e+01   4.8000000e+01   4.8000000e+01
5.0000000e+01   4.8000000e+01   4.9000000e+01   5.6000000e+01   4.5000000e+01   4.9000000e+01   5.0000000e+01   4.5000000e+01   5.0000000e+01   4.8000000e+01   3.2000000e+01   5.0000000e+01   4.8000000e+01   5.8000000e+01   4.9000000e+01   5.7000000e+01   5.8000000e+01   5.0000000e+01   5.2000000e+01

Fortunately, we have done file reading and character conversion before, and then we get such an array

[50, 48, 49, 56, 45, 49, 50, 45, 50, 48, 32, 49, 57, 58, 51, 57, 58, 48, 48]
[50, 48, 49, 56, 45, 49, 50, 45, 50, 48, 32, 50, 48, 58, 49, 57, 58, 50, 52]

What I get is the ascii number. What I need is 19:39:00 on December 20, 2018 and 20:19:24 on December 20, 2018
Turn the ascii code value of each bit of the char * array into a character, and then turn it strong

(char) ascii

Then we get the char * array and turn it into a string

std::string startTimeStr(startTimeArray);
std::string endTimeStr(endTimeArray);

Then string to time

// Time string to time (yyyy MM DD HH: mm: SS)
time_t StringToDatetime(std::string str)
{
    const char* cha = str.c_str();
    tm tm_;                                    // Define the tm structure.
    int year, month, day, hour, minute, second;// Each int temporary variable that defines time.
    sscanf(cha, "%d-%d-%d %d:%d:%d", &year, &month, &day, &hour, &minute, &second);// Convert the date time stored in string to int temporary variable.
    tm_.tm_year = year - 1900;                 // In, because the TM structure stores the time from 1900, tm_year is the int temporary variable minus 1900.
    tm_.tm_mon = month - 1;                    // Month, because the month storage range of the TM structure is 0-11, tm_mon is the int temporary variable minus 1.
    tm_.tm_mday = day;                         // Day.
    tm_.tm_hour = hour;                        // When.
    tm_.tm_min = minute;                       // Points.
    tm_.tm_sec = second;                       // Seconds.
    tm_.tm_isdst = 0;                          // Not daylight saving time.
    time_t t_ = mktime(&tm_);                  // Convert the tm structure to the time? Format.
    return t_;                                 // Return value. 
}

Here is the calculation time difference

time_t startTime = StringToDatetime(startTimeStr);
time_t endTime = StringToDatetime(endTimeStr);
double diffTime = difftime(endTime, startTime); // Return seconds

Here, read the code line by line through getLine, and then use strtok to split the string according to the space to get the desired result

Original text: Thinkin Liu Blog: IT old five

ps: in the actual application scenario, it is often to get the data in the text, and it is also necessary to convert the data to double string, and distinguish different data according to the lines, which can be processed in the omission of the code
c + + Foundation (1): string to wstring and file copy
c + + Foundation (2): string replacement and file path truncation
c + + Foundation (3): array auto increment processing and string to int,double
c + + Foundation (4): int to string and array default
c + + Foundation (5): read the file line by line and divide the data according to the characters

Posted by Anidazen on Sat, 07 Dec 2019 00:23:01 -0800