How can I creat a time class?

Discussion in 'C++' started by learning_C++, Sep 22, 2004.

  1. learning_C++

    learning_C++ Guest

    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);
    }
     
    learning_C++, Sep 22, 2004
    #1
    1. Advertising

  2. learning_C++ wrote:
    > 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.
     
    Victor Bazarov, Sep 22, 2004
    #2
    1. Advertising

  3. learning_C++

    osmium Guest

    learning_C++ writes:

    > 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>
     
    osmium, Sep 22, 2004
    #3
  4. learning_C++

    Default User Guest

    learning_C++ wrote:

    > 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.



    What do you mean, "does not work"? Doesn't compile? Compiles but
    crashes when it runs? Runs fine but gives wrong answers?

    Read the following:

    http://www.parashift.com/c -faq-lite/how-to-post.html#faq-5.8




    Brian Rodenborn
     
    Default User, Sep 22, 2004
    #4
  5. learning_C++

    Evan Carew Guest

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

    Learning,

    learning_C++ wrote:
    > 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-----
     
    Evan Carew, Sep 23, 2004
    #5
    1. Advertising

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

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. CM
    Replies:
    3
    Views:
    1,719
  2. John

    Can creat asp.net apps

    John, Nov 6, 2003, in forum: ASP .Net
    Replies:
    4
    Views:
    400
    Daniel Rimmelzwaan
    Nov 7, 2003
  3. learning_C++
    Replies:
    2
    Views:
    588
    John Harrison
    Sep 23, 2004
  4. Jerry Lee

    How we can creat this?

    Jerry Lee, Sep 20, 2005, in forum: ASP .Net Web Controls
    Replies:
    1
    Views:
    192
    Tasos Vogiatzoglou
    Sep 21, 2005
  5. Khalid Al-Ghamdi
    Replies:
    3
    Views:
    161
    Terry Reedy
    Nov 12, 2012
Loading...

Share This Page