How to convert string to datetime in C ++

I have a result set (from a function) based on time. But the datetime value is in a string format (for example, "21: 5 January 23, 11"). I want to convert "21: 5 January 23, 11" to datetime. How can I do this in C ++? I just want to filter the entries for today. So I need to get the current date from "21: 5 January 23, 11".

Edit:

I can get the current date and time using SYSTEMTIME st; GetSystemTime (& th);

Is there a way to convert "21: 5 January 23, 11" to the above format?

+3
source share
4 answers
#include <ctime>
#include <iomanip>
#include <iostream>
#include <sstream>

// Converts UTC time string to a time_t value.
std::time_t getEpochTime(const std::wstring& dateTime)
{
   // Let consider we are getting all the input in
   // this format: '2014-07-25T20:17:22Z' (T denotes
   // start of Time part, Z denotes UTC zone).
   // A better approach would be to pass in the format as well.
   static const std::wstring dateTimeFormat{ L"%Y-%m-%dT%H:%M:%SZ" };

   // Create a stream which we will use to parse the string,
   // which we provide to constructor of stream to fill the buffer.
   std::wistringstream ss{ dateTime };

   // Create a tm object to store the parsed date and time.
   std::tm dt;

   // Now we read from buffer using get_time manipulator
   // and formatting the input appropriately.
   ss >> std::get_time(&dt, dateTimeFormat.c_str());

   // Convert the tm structure to time_t value and return.
   return std::mktime(&dt);
}
+6
source

The most common C ++ way is to use Boost.DateTime .

+1
source

If you only need to check if the two lines have the same date or not, and if it is guaranteed that the lines are in the same format, then there is no need to convert them to a date. You just need to compare the substrings after the first space character. If they are the same, then the dates are the same. Here is a sample code:

using namespace std;

string getCurrentDate()
{
    //Enumeration of the months in the year
    const char* months[] = {"Jan", "Feb", "Mar", "Apr", "May", "Jun", "Jul", "Aug", "Sep", "Oct", "Nov", "Dec"};

    //Get the current system date time
    SYSTEMTIME st; 
    GetSystemTime(&st);

    //Construct the string in the format "21:5 Jan 23, 11"
    ostringstream ss;
    ss<<st.wHour<<":"<<st.wMinute<<
        " "<<months[st.wMonth-1]<<
        " "<<st.wDay<<", "<<st.wYear%1000;

    //Extract the string from the stream
    return ss.str();

}

string getDateString(const string& s)
{
    //Extract the date part from the string "21:5 Jan 23, 11"

    //Look for the first space character in the string
    string date;
    size_t indx = s.find_first_of(' ');
    if(indx != string::npos) //If found
    {
        //Copy the date part
        date = s.substr(indx + 1);
    }
    return date;
}
bool isCurrentDate(const string& s1)
{
    //Get the date part from the passed string
    string d1 = getDateString(s1);

    //Get the date part from the current date
    string d2 = getDateString(getCurrentDate());

    //Check whether they match
    return ! d1.empty() && ! d2.empty() && d1 == d2;
}

int main( void )
{
    bool s = isCurrentDate("21:5 Jan 23, 11");
    bool s1 = isCurrentDate("21:5 Jan 25, 11");
    return 0;
}
0
source

Source: https://habr.com/ru/post/1787442/


All Articles