Time in C++

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • gabitoju@gmail.com

    #1

    Time in C++

    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

  • Daniel T.

    #2
    Re: Time in C++

    gabitoju@gmail. com 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.

    Comment

    • David Harmon

      #3
      Re: Time in C++

      On 21 Oct 2006 14:58:41 -0700 in comp.lang.c++, gabitoju@gmail. com
      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:


      Comment

      • Bo Yang

        #4
        Re: Time in C++

        gabitoju@gmail. com :
        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:

        Comment

        • Jacek Dziedzic

          #5
          Re: Time in C++

          gabitoju@gmail. com 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.

          Comment

          • Kirit Sælensminde

            #6
            Re: Time in C++


            Jacek Dziedzic wrote:
            gabitoju@gmail. com 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

            Comment

            • gabitoju@gmail.com

              #7
              Re: Time in C++

              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:
              gabitoju@gmail. com 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

              Comment

              • Kirit Sælensminde

                #8
                Re: Time in C++


                Kirit Sælensminde wrote:
                Jacek Dziedzic wrote:
                gabitoju@gmail. com 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
                gabitoju@gmail. com 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

                Comment

                Working...