venkysmarty
venkysmarty

Reputation: 11431

convertint date and time string to just integers in C++

How to convert

std::string strdate = "2012-06-25 05:32:06.963";

To some thing like this

std::string strintdate = "20120625053206963" // basically i removed -, :, space and .

I think I should use strtok or string functions, but I am not able to do it, can any one please help me here with sampel code.

so that i convert it to unsigned __int64 by using

// crt_strtoui64.c
#include <stdio.h>

unsigned __int64 atoui64(const char *szUnsignedInt) {
   return _strtoui64(szUnsignedInt, NULL, 10);
}

int main() {
   unsigned __int64 u = atoui64("18446744073709551615");
   printf( "u = %I64u\n", u );
}

Upvotes: 0

Views: 4412

Answers (6)

babun
babun

Reputation: 11

std::string strdate = "2012-06-25 05:32:06.963";
std::string result ="";
for(std::string::iterator itr = strdate.begin(); itr != strdate.end(); itr++)
{
    if(itr[0] >= '0' &&  itr[0] <= '9')
    {
        result.push_back(itr[0]);
    }
}

Upvotes: 0

Steve Jessop
Steve Jessop

Reputation: 279225

bool nondigit(char c) {
    return c < '0' || c > '9';
}

std::string strdate = "2012-06-25 05:32:06.963";
strdate.erase(
    std::remove_if(strdate.begin(), strdate.end(), nondigit),
    strdate.end()
);

std::istringstream ss(strdate);
unsigned __int64 result;
if (ss >> result) {
    // success
} else {
    // handle failure
}

Btw, your representation as a 64-bit int might be a bit fragile. Make sure that the date/time 2012-06-25 05:32:06 is input as 2012-06-25 05:32:06.000, otherwise the integer you get out of the end is smaller than expected (and hence might be confused for a date/time in the year 2AD).

Upvotes: 3

Rook
Rook

Reputation: 6145

I wouldn't use strtok. Here's a fairly simple method that just uses std::string member functions:

std::string strdate = "2012-06-25 05:32:06.963";
size_t pos = strdate.find_first_not_of("1234567890");
while (pos != std::string::npos)
{
    size_t endpos = strdate.find_first_of("1234567890", pos);
    strdate.erase(pos, endpos - pos);
    pos = strdate.find_first_not_of("1234567890");
}

This is not a super efficient approach, but it will work.

A perhaps more efficient approach might use a stringstream...

std::string strdate = "2012-06-25 05:32:06.963";

std::stringstream out;

for (auto i = strdate.begin(); i != strdate.end(); i++)
    if (std::isdigit(*i)) out << *i;

strdate = out.str();

I make no promises about time or space complexity, but I suspect that using string::erase multiple times might involve a little more memory shuffling.

Upvotes: 0

hmjd
hmjd

Reputation: 121961

If your compiler supports C++11 features:

#include <iostream>
#include <algorithm>
#include <string>

int main()
{
    std::string s("2012-06-25 05:32:06.963");
    s.erase(std::remove_if(s.begin(),
                           s.end(),
                           [](const char a_c) { return !isdigit(a_c); }),
            s.end());
    std::cout << s << "\n";
    return 0;
}

Upvotes: 2

Nim
Nim

Reputation: 33655

Here you go:

bool not_digit (int c) { return !std::isdigit(c); }

std::string date="2012-06-25 05:32:06.963";
// construct a new string
std::string intdate(date.begin(), std::remove_if(date.begin(), date.end(), not_digit));

Upvotes: 0

thommie
thommie

Reputation: 448

use string replace to replace the unwanted chars with no chars http://www.cplusplus.com/reference/string/string/replace/

Upvotes: 0

Related Questions