Time in C++

Discussion in 'C++' started by gabitoju@gmail.com, Oct 21, 2006.

  1. Guest

    I have to hours like this 20:30:15 (HH:MM:SS) loeaded in two
    std:string, and I wan't to compare them. I looked at ctime but I didn't
    found, first, somethig to convert from string to time and then some
    function to compare them.
    How can I do this?

    Thanks
    , Oct 21, 2006
    #1
    1. Advertising

  2. Daniel T. Guest

    wrote:

    > I have to hours like this 20:30:15 (HH:MM:SS) loeaded in two
    > std:string, and I wan't to compare them. I looked at ctime but I didn't
    > found, first, somethig to convert from string to time and then some
    > function to compare them.
    > How can I do this?


    There are a couple of ways you can do this. One would be to wrap the
    string in a "time" class that understands the format of times. If this
    type of data was used extensively in a program, I would probably go this
    rout. Another would be to implement a times_equal and times_less that
    takes two strings as parameters. I'm going to assume the latter in this
    case.

    Take the code below and add to it where it says "insert code here" until
    it will compile without errors or warnings and when run, will display
    "Works so far!" on the screen.

    If you have any problems, post what you have so far and I'll help you
    through them. Once you get it working, post what you did and I'll help
    you with the next step.

    ----- begin code -----
    #include <cassert>
    #include <iostream>
    #include <string>

    using namespace std;

    bool times_equal( const string& lhs, const string& rhs ) {
    // insert code here
    }

    bool times_less( const string& lhs, const string& rhs ) {
    // insert code here
    }

    int main() {
    string a = "20:30:15";
    assert( times_equal( a, a ) );
    cout << "Works so far!\n";
    };

    --
    There are two things that simply cannot be doubted, logic and perception.
    Doubt those, and you no longer have anyone to discuss your doubts with,
    nor any ability to discuss them.
    Daniel T., Oct 21, 2006
    #2
    1. Advertising

  3. David Harmon Guest

    On 21 Oct 2006 14:58:41 -0700 in comp.lang.c++,
    wrote,
    >I have to hours like this 20:30:15 (HH:MM:SS) loeaded in two
    >std:string, and I wan't to compare them. I looked at ctime but I didn't
    >found, first, somethig to convert from string to time and then some
    >function to compare them.


    You may have to do your own parsing from string to three integers,
    with the usual istringstream, strtol(), sscanf(), your choice.

    The rest you can do with mktime(), difftime() etc. from the old C
    library. The usual answer to that is the same in C++ as it is in C,
    and is covered in Steve Summit's C FAQ. It is always good to check
    the FAQ before posting. You can get the FAQ at:
    http://www.eskimo.com/~scs/C-faq/top.html
    David Harmon, Oct 22, 2006
    #3
  4. Bo Yang Guest

    :
    > I have to hours like this 20:30:15 (HH:MM:SS) loeaded in two
    > std:string, and I wan't to compare them. I looked at ctime but I didn't
    > found, first, somethig to convert from string to time and then some
    > function to compare them.
    > How can I do this?
    >
    > Thanks
    >

    Oh , I think boost date time library
    will help you !

    You can take some example in the following url:
    http://www.boost.org/boost/boost_1_33_1/doc/html/date_time/examples.html
    Bo Yang, Oct 22, 2006
    #4
  5. wrote:
    > I have to hours like this 20:30:15 (HH:MM:SS) loeaded in two
    > std:string, and I wan't to compare them. I looked at ctime but I didn't
    > found, first, somethig to convert from string to time and then some
    > function to compare them.
    > How can I do this?


    Wait a second, why not just compare strings? If they are
    in hh:mm:ss format, than a lexical comparison works just
    as you want it to, doesn't it?

    string time1="20:30:15";
    string time2="21:15:20";

    cout << (time1 > time2) << " " << (time1 < time2) << endl;

    yields:

    false true

    HTH,
    - J.
    Jacek Dziedzic, Oct 22, 2006
    #5
  6. Jacek Dziedzic wrote:
    > wrote:
    > > I have to hours like this 20:30:15 (HH:MM:SS) loeaded in two
    > > std:string, and I wan't to compare them. I looked at ctime but I didn't
    > > found, first, somethig to convert from string to time and then some
    > > function to compare them.
    > > How can I do this?

    >
    > Wait a second, why not just compare strings? If they are
    > in hh:mm:ss format, than a lexical comparison works just
    > as you want it to, doesn't it?
    >
    > string time1="20:30:15";
    > string time2="21:15:20";
    >
    > cout << (time1 > time2) << " " << (time1 < time2) << endl;
    >
    > yields:
    >
    > false true
    >


    Just what I was about to say. Also works for ISO formatted dates
    (YYYY-MM-DD).

    You do need to make sure that times are zero padded though. 1:23:45
    won't work, but 01:23:45 will. You should also assert that the
    seperators are the same.


    K
    =?iso-8859-1?q?Kirit_S=E6lensminde?=, Oct 23, 2006
    #6
  7. Guest

    I'm analyzing web server logs, so the formate is hh:mm:ss. (time1 >
    time2) works fine. But I having a class to wrap time is something that
    I will do because I have to do some calculations like if (time1 - time
    2 = 15(min) ). I also check out boost library and It's great, but to
    big for what I'm doing.

    Thank you all guys.

    Kirit Sælensminde wrote:
    > Jacek Dziedzic wrote:
    > > wrote:
    > > > I have to hours like this 20:30:15 (HH:MM:SS) loeaded in two
    > > > std:string, and I wan't to compare them. I looked at ctime but I didn't
    > > > found, first, somethig to convert from string to time and then some
    > > > function to compare them.
    > > > How can I do this?

    > >
    > > Wait a second, why not just compare strings? If they are
    > > in hh:mm:ss format, than a lexical comparison works just
    > > as you want it to, doesn't it?
    > >
    > > string time1="20:30:15";
    > > string time2="21:15:20";
    > >
    > > cout << (time1 > time2) << " " << (time1 < time2) << endl;
    > >
    > > yields:
    > >
    > > false true
    > >

    >
    > Just what I was about to say. Also works for ISO formatted dates
    > (YYYY-MM-DD).
    >
    > You do need to make sure that times are zero padded though. 1:23:45
    > won't work, but 01:23:45 will. You should also assert that the
    > seperators are the same.
    >
    >
    > K
    , Oct 23, 2006
    #7

  8. > Kirit Sælensminde wrote:
    > > Jacek Dziedzic wrote:
    > > > wrote:
    > > > > I have to hours like this 20:30:15 (HH:MM:SS) loeaded in two
    > > > > std:string, and I wan't to compare them. I looked at ctime but I didn't
    > > > > found, first, somethig to convert from string to time and then some
    > > > > function to compare them.
    > > > > How can I do this?
    > > >
    > > > Wait a second, why not just compare strings? If they are
    > > > in hh:mm:ss format, than a lexical comparison works just
    > > > as you want it to, doesn't it?
    > > >
    > > > string time1="20:30:15";
    > > > string time2="21:15:20";
    > > >
    > > > cout << (time1 > time2) << " " << (time1 < time2) << endl;
    > > >
    > > > yields:
    > > >
    > > > false true
    > > >

    > >
    > > Just what I was about to say. Also works for ISO formatted dates
    > > (YYYY-MM-DD).
    > >
    > > You do need to make sure that times are zero padded though. 1:23:45
    > > won't work, but 01:23:45 will. You should also assert that the
    > > seperators are the same.
    > >
    > >
    > > K


    wrote:
    > I'm analyzing web server logs, so the formate is hh:mm:ss. (time1 >
    > time2) works fine. But I having a class to wrap time is something that
    > I will do because I have to do some calculations like if (time1 - time
    > 2 = 15(min) ). I also check out boost library and It's great, but to
    > big for what I'm doing.
    >
    > Thank you all guys.
    >


    Top posting is generally frowned upon around here. I've moved your
    response to the end.

    You can only do that calculation if you use the date together with the
    time. If you're not too worried about the precision (i.e. you don't
    have to be accurate to less than a couple of seconds per year) then the
    calculations aren't too difficult to write by hand given a lookup table
    of the days per month and a leap year formula. Things get trickier if
    you want to deal with old dates or time differences that are very
    accurate over long periods.


    K
    =?iso-8859-1?q?Kirit_S=E6lensminde?=, Oct 24, 2006
    #8
    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. engsol
    Replies:
    2
    Views:
    946
    Dan Bishop
    Jan 26, 2004
  2. Replies:
    8
    Views:
    475
    Magnus Lycka
    Aug 5, 2005
  3. Peter Hansen
    Replies:
    0
    Views:
    712
    Peter Hansen
    Feb 22, 2006
  4. Peter Hansen
    Replies:
    0
    Views:
    604
    Peter Hansen
    Feb 22, 2006
  5. flamesrock
    Replies:
    8
    Views:
    449
    Hendrik van Rooyen
    Nov 24, 2006
Loading...

Share This Page