integer to time

Discussion in 'C++' started by Marcus, Apr 11, 2005.

  1. Marcus

    Marcus Guest

    I was wondering if someone could help explain how an integer could be
    transformed into a time format. For instance I have an integer 62446
    which should read 17:xx:xx or hhmmss (The colon seperators are not
    necessary, as I don't mind it staying in an integer format... but I'd
    just like to be able to recognize the time when printing). I know to
    divide the entire number by 60*60 to get the hour, but I'm unsure how
    to derive the rest of the numbers. If anyone could give me an example
    of a formula for this and/or explain how the integer is derived from
    the time format, it would really go a long way in helping me
    understand.

    Any help is greatly appreicated.

    Regards,
    Marcus
     
    Marcus, Apr 11, 2005
    #1
    1. Advertising

  2. Marcus wrote:
    > I was wondering if someone could help explain how an integer could be
    > transformed into a time format. For instance I have an integer 62446
    > which should read 17:xx:xx or hhmmss (The colon seperators are not
    > necessary, as I don't mind it staying in an integer format... but I'd
    > just like to be able to recognize the time when printing). I know to
    > divide the entire number by 60*60 to get the hour, but I'm unsure how
    > to derive the rest of the numbers. If anyone could give me an example
    > of a formula for this and/or explain how the integer is derived from
    > the time format, it would really go a long way in helping me
    > understand.


    So, your number is what, a number of seconds and you need to extract
    the hours, the minutes, and the seconds from it? If I tell you that I
    have 1234 cents, how do you determine how many dollars I have? Let's
    make it a bit more curious. If I tell you my height is 65 inches, what
    is my height in feet/inches? Try doing the same thing with your number.

    Hint: use / and % operators

    V
     
    Victor Bazarov, Apr 11, 2005
    #2
    1. Advertising

  3. Marcus

    rossum Guest

    On 11 Apr 2005 14:27:53 -0700, "Marcus" <> wrote:

    >I was wondering if someone could help explain how an integer could be
    >transformed into a time format. For instance I have an integer 62446
    >which should read 17:xx:xx or hhmmss (The colon seperators are not
    >necessary, as I don't mind it staying in an integer format... but I'd
    >just like to be able to recognize the time when printing). I know to
    >divide the entire number by 60*60 to get the hour, but I'm unsure how
    >to derive the rest of the numbers. If anyone could give me an example
    >of a formula for this and/or explain how the integer is derived from
    >the time format, it would really go a long way in helping me
    >understand.
    >
    >Any help is greatly appreicated.
    >
    >Regards,
    >Marcus


    As you say 60*60 gives hours, I assume that your original number,
    62446, is to be treated as that many seconds.

    Rather than start with the hours, it is probably better to start with
    the minutes. Dividing by 60 will give the number of minutes, with
    some seconds left over. In your example 62446 seconds gives 1040
    minutes with 46 seconds left over: (60 * 1040) + 46 = 62446.

    Now take the 1040 minutes and divide by 60 to see how many hours there
    are. This gives 17 hours with 20 minutes left over: (60 * 17) + 20 =
    1040.

    So 62446 seconds = 17 hours, 20 minutes and 46 seconds or 17:20:46.

    HTH

    rossum


    The ultimate truth is that there is no ultimate truth
     
    rossum, Apr 11, 2005
    #3
  4. Marcus

    Marcus Guest

    Thanks Rossum and Victor. Ok, the math part I now get. I was trying to
    make it more complicated than it actually was... I was imagining some
    exotic formating, 67000 seconds makes a lot more sense. :)

    I can't figure out if there's a a way to write that as a single
    expression, or if it's necessary to break it up into two parts? More
    specifically, I'm not sure how to pass the remainder in c++... Victor
    hinted at the % operator, which looks like it divides for the
    remainder, but I'm not sure how to use it. Trying to find some tuts on
    this right now.

    Anyways, thanks for the help.

    Regards, Marcus
     
    Marcus, Apr 11, 2005
    #4
  5. Marcus

    codigo Guest

    "Marcus" <> wrote in message
    news:...
    > I was wondering if someone could help explain how an integer could be
    > transformed into a time format. For instance I have an integer 62446
    > which should read 17:xx:xx or hhmmss (The colon seperators are not
    > necessary, as I don't mind it staying in an integer format... but I'd
    > just like to be able to recognize the time when printing). I know to
    > divide the entire number by 60*60 to get the hour, but I'm unsure how
    > to derive the rest of the numbers. If anyone could give me an example
    > of a formula for this and/or explain how the integer is derived from
    > the time format, it would really go a long way in helping me
    > understand.
    >
    > Any help is greatly appreicated.
    >
    > Regards,
    > Marcus
    >


    Should be simple arithmetic.

    24 hours * 60 min * 60 sec = 86,400 total seconds per day

    so... integer is in range

    from 0 to 86,399 is 86,400 values

    ------

    #include <iostream>
    using std::cout;

    const int hour = 3600;
    const int min = 60;

    class Time
    {
    int m_t; // member seconds
    public:
    Time(int t) : m_t(t)
    {
    }
    ~Time()
    {
    }

    void getTime() const
    {
    int n_input = m_t;
    int n_hour_result = n_input / hour; // hours
    n_input -= n_hour_result * hour;
    int n_min_result = n_input / min; // minutes residue
    n_input -= n_min_result * min; // seconds residue

    cout << n_hour_result;
    cout << "::";
    cout << n_min_result;
    cout << "::";
    cout << n_input;
    cout << "\n";

    } // getTime()

    }; // Time

    int main()
    {
    Time t(86399); // 23:59:59
    t.getTime();

    Time tt(3661); // 1:1:1 am
    tt.getTime();

    return 0;
    }
    -----------

    Fix the cout formatting for single digits and you might
    want to throw an exception if 0 > m_t > 86399.
     
    codigo, Apr 12, 2005
    #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. =?Utf-8?B?Sm9l?=

    CType(x,Integer) vs. Integer.Parse(x)

    =?Utf-8?B?Sm9l?=, Feb 6, 2006, in forum: ASP .Net
    Replies:
    7
    Views:
    5,974
    =?Utf-8?B?RGF2aWQgQW50b24=?=
    Feb 7, 2006
  2. =?ISO-8859-1?Q?Thomas_Gagn=E9?=

    No Math.min(Integer, Integer)?

    =?ISO-8859-1?Q?Thomas_Gagn=E9?=, Jul 29, 2003, in forum: Java
    Replies:
    0
    Views:
    534
    =?ISO-8859-1?Q?Thomas_Gagn=E9?=
    Jul 29, 2003
  3. Sebastian Stelzer

    How do I add an Integer to another Integer?

    Sebastian Stelzer, Oct 14, 2004, in forum: Java
    Replies:
    2
    Views:
    518
    Yu SONG
    Oct 15, 2004
  4. Sebastian Stelzer

    How do I add an Integer to another Integer?

    Sebastian Stelzer, Oct 14, 2004, in forum: Java
    Replies:
    6
    Views:
    45,784
    JavaBean2010
    Apr 7, 2010
  5. flamesrock
    Replies:
    8
    Views:
    517
    Hendrik van Rooyen
    Nov 24, 2006
Loading...

Share This Page