How can I creat a time class?

L

learning_C++

I hope to make a time class or a function. So I can make use it to
show time where I want. But it does not work.
Please help me!
Thanks,

class Time {
public:
string get_current_time();
};

string Time::get_current_time(){
string timestr;
time_t rawtime;
struct tm * timeinfo;
time ( &rawtime );
timeinfo = localtime ( &rawtime );
timestr=asctime(timeinfo);
}

or I try to make a function like this:
string get_current_time(){
string timestr;
time_t rawtime;
struct tm * timeinfo;
time_t time ( &rawtime );
timeinfo = localtime ( &rawtime );
timestr=asctime(timeinfo);
}

it still does not work.

string timestr;
time_t rawtime;
struct tm * timeinfo;
time_t time ( &rawtime );
timeinfo = localtime ( &rawtime );
timestr=asctime(timeinfo);
}
 
V

Victor Bazarov

learning_C++ said:
I hope to make a time class or a function. So I can make use it to
show time where I want. But it does not work.

Please explain what you mean by "does not work". Read the FAQ 5.8.
 
O

osmium

learning_C++ said:
I hope to make a time class or a function. So I can make use it to
show time where I want. But it does not work.
Please help me!
Thanks,

class Time {
public:
string get_current_time();
};

string Time::get_current_time(){
string timestr;
time_t rawtime;
struct tm * timeinfo;
time ( &rawtime );
timeinfo = localtime ( &rawtime );
timestr=asctime(timeinfo);
}

The first thing I note is that the function promises to return a string.
But where is the code that casues that to happen?
<snip>
 
E

Evan Carew

-----BEGIN PGP SIGNED MESSAGE-----
Hash: SHA1

Learning,

learning_C++ said:
I hope to make a time class or a function. So I can make use it to
show time where I want. But it does not work.
Please help me!
Thanks,

One shortcut would be to use Boost's gregorian::date class. For an in
depth desctiption, see http://boost.org/libs/date_time/doc/class_date.html

where you create a data class, get local date, then cast it to a string.

Evan
-----BEGIN PGP SIGNATURE-----
Version: GnuPG v1.0.6 (GNU/Linux)
Comment: Using GnuPG with Mozilla - http://enigmail.mozdev.org

iD8DBQFBUiU1oo/Prlj9GScRAjs1AJwO+0q5dPF41JoM6HDWu0V7DoIR9wCbB6pq
axKRqEol1GYQo89Qqh2KtC4=
=U9Y3
-----END PGP SIGNATURE-----
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,995
Messages
2,570,230
Members
46,818
Latest member
Brigette36

Latest Threads

Top