Time Conversion

closed account (G26pX9L8)
Hello all,

I want to convert a time string to a char. I tried many things and some works but thats all not what I want. I want to get the current time in seconds and convert that into a char. I need a char so I can use the strcat() function who requeries a char. Any suggestions?
Last edited on
You mean a char* I think.

When you say "convert a time string" do you mean that you already have the time in a std::string variable?

Can you show us your code so far so we can try to work out exactly what you are trying to achieve?
I think he uses something like asctime which returns the tm structure as an formatted cz... why don“t you assign your char* (since it is '\0'-terminated) to an string and execute the find-function on it, to find the ' ' between the single parts, in it?...

Btw... if you want the current time in seconds (since 1970) there is some function like time() :P...
Last edited on
closed account (G26pX9L8)
Lets make it clear

I already use the time() function. And that is what I want, I want to convert the time() into a char* but how? I haven't any code so far. Can somebody write a peice of code for me?
So you want to convert the number returned by std::time() into a string representation of that number?

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
#include <iostream>
#include <sstream>
#include <string>
#include <ctime>

int main()
{
	time_t t = std::time(0);

	std::ostringstream oss;
	oss << t;
	std::string s = oss.str();

	std::cout << s;

	return 0;
}
You know that this will only return the number of seconds elapsed since 1970!?...
closed account (G26pX9L8)
@Incubbus
Thats what I need but in a char
You can obtain a const char* from a std::string using this:

1
2
3
std::string s;

strcat(buff, s.c_str()); // function c_str() returns a char* from a string 


However it may be useful to concatenate strings using the + operator rather than using the strcat() function:

 
std::string filename = s + ".dat";
closed account (G26pX9L8)
Ok tnx all problem fixed
Topic archived. No new replies allowed.