Rounding time algo

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • Marcus

    Rounding time algo

    Hi, was wondering if anyone knows a good algo for rounding time to a
    desired interval.

    For instance, I have data set that is timestamped in second resolution
    and I need to modify that data to be timestamped for an x minute
    interval. So if time t = 93601, and x = 5 then t would be rounded up
    to 94000... if x were 1, t would equal 93700, etc. All time is integer
    based.

    Any help would be really appreciated.

    Marcus

  • Mark P

    #2
    Re: Rounding time algo

    Marcus wrote:
    Hi, was wondering if anyone knows a good algo for rounding time to a
    desired interval.
    >
    For instance, I have data set that is timestamped in second resolution
    and I need to modify that data to be timestamped for an x minute
    interval. So if time t = 93601, and x = 5 then t would be rounded up
    to 94000... if x were 1, t would equal 93700, etc. All time is integer
    based.
    >
    Any help would be really appreciated.
    >
    Marcus
    >
    I don't understand your math. If x = 5 and you want to round to 5
    minute intervals, then a value t in seconds should be a multiple of 5 *
    60 = 300 seconds. But 94000 is not divisible by 300. It looks like
    you're assuming 100 seconds per minute based on your rounded values.

    In any event, we can phrase this as a more general question: how do you
    round a value n to a multiple of m? Since it looks like you want to
    round up, you can do the following:

    int remainder = n % m;
    int rounded_n = n + (remainder ? (m - remainder) : 0);

    Comment

    • Marcus

      #3
      Re: Rounding time algo

      Mark P wrote:
      Marcus wrote:
      Hi, was wondering if anyone knows a good algo for rounding time to a
      desired interval.

      For instance, I have data set that is timestamped in second resolution
      and I need to modify that data to be timestamped for an x minute
      interval. So if time t = 93601, and x = 5 then t would be rounded up
      to 94000... if x were 1, t would equal 93700, etc. All time is integer
      based.

      Any help would be really appreciated.

      Marcus
      >
      I don't understand your math. If x = 5 and you want to round to 5
      minute intervals, then a value t in seconds should be a multiple of 5 *
      60 = 300 seconds. But 94000 is not divisible by 300. It looks like
      you're assuming 100 seconds per minute based on your rounded values.
      >
      In any event, we can phrase this as a more general question: how do you
      round a value n to a multiple of m? Since it looks like you want to
      round up, you can do the following:
      >
      int remainder = n % m;
      int rounded_n = n + (remainder ? (m - remainder) : 0);
      Hi Mark, thanks for the reply.

      Reading back over my post I can see where the confusion is. Time t is
      expressed as an integer, however it reflects an actual timestamp
      containing hours minutes and seconds, instead of an integer expressed
      in seconds. So the above 93601, can also be written 9:36:01.

      Not sure if that would change your approach. What exactly are your
      variables there, I'm not quite sure I understand? The other little
      problem with straight division are the hour increments every 60 min...
      that's why I was wondering if there might be a special approach to
      this.

      Marcus

      Comment

      • Mark P

        #4
        Re: Rounding time algo

        Marcus wrote:
        Mark P wrote:
        >Marcus wrote:
        >>Hi, was wondering if anyone knows a good algo for rounding time to a
        >>desired interval.
        >>>
        >>For instance, I have data set that is timestamped in second resolution
        >>and I need to modify that data to be timestamped for an x minute
        >>interval. So if time t = 93601, and x = 5 then t would be rounded up
        >>to 94000... if x were 1, t would equal 93700, etc. All time is integer
        >>based.
        >>>
        >>Any help would be really appreciated.
        >>>
        >>Marcus
        >>>
        >I don't understand your math. If x = 5 and you want to round to 5
        >minute intervals, then a value t in seconds should be a multiple of 5 *
        >60 = 300 seconds. But 94000 is not divisible by 300. It looks like
        >you're assuming 100 seconds per minute based on your rounded values.
        >>
        >In any event, we can phrase this as a more general question: how do you
        >round a value n to a multiple of m? Since it looks like you want to
        >round up, you can do the following:
        >>
        >int remainder = n % m;
        >int rounded_n = n + (remainder ? (m - remainder) : 0);
        >
        Hi Mark, thanks for the reply.
        >
        Reading back over my post I can see where the confusion is. Time t is
        expressed as an integer, however it reflects an actual timestamp
        containing hours minutes and seconds, instead of an integer expressed
        in seconds. So the above 93601, can also be written 9:36:01.
        >
        Not sure if that would change your approach. What exactly are your
        variables there, I'm not quite sure I understand? The other little
        problem with straight division are the hour increments every 60 min...
        that's why I was wondering if there might be a special approach to
        this.
        >
        Marcus
        >
        I explain what n and m are in the preceding text. remainder is a
        temporary variable. rounded_n is, as you might guess, the rounded value
        of n.

        One straightforward way to work with times is convert everything to
        seconds, perform the required arithmetic, and then convert everything
        back. I'll let you figure out the details of how to do this but you'll
        find the mod operator helpful for both directions of the conversion.

        Comment

        • Frederick Gotham

          #5
          Re: Rounding time algo

          Marcus posted:
          Hi, was wondering if anyone knows a good algo for rounding time to a
          desired interval.

          No, but if I exert my brain for approximately seven seconds, I'd probably
          come up with one. You should try it.

          For instance, I have data set that is timestamped in second resolution
          and I need to modify that data to be timestamped for an x minute
          interval. So if time t = 93601, and x = 5 then t would be rounded up
          to 94000... if x were 1, t would equal 93700, etc. All time is integer
          based.

          You shouldn't have mentioned time at all. All you want to do is round an
          integer up to the nearest specified integer.

          Any help would be really appreciated.

          template<class IntType>
          inline
          IntType RoundUp(IntType const x,IntType const factor)
          {
          return factor * (x/factor + !!(x%factor));
          }

          Or, if you would prefer a macro:

          #define ROUND_UP_RAW(x, factor) (factor * (x/factor + !!(x%factor)))
          #define ROUND_UP(x,fact or) ROUND_UP_RAW((x ),(factor))

          --

          Frederick Gotham

          Comment

          • Jim Langston

            #6
            Re: Rounding time algo

            "Marcus" <mcdesigns@wall a.comwrote in message
            news:1158213290 .304567.14730@m 73g2000cwd.goog legroups.com...
            Hi, was wondering if anyone knows a good algo for rounding time to a
            desired interval.
            >
            For instance, I have data set that is timestamped in second resolution
            and I need to modify that data to be timestamped for an x minute
            interval. So if time t = 93601, and x = 5 then t would be rounded up
            to 94000... if x were 1, t would equal 93700, etc. All time is integer
            based.
            >
            Any help would be really appreciated.
            >
            Marcus
            Rounding an integer is fairly simple. If you want to drop 2 places, then
            divide by 100 and mulitply by 100. Since integer division drops any
            fraction/remainder you get what you want.

            93601 / 100 == 936
            936 * 100 = 93600

            Now, we get into the complication of rounding. Say we wanted to loose 3
            places of presion.

            93601 / 1000 = 93
            93 * 1000 = 93000

            which is not what you want. The normal way to handle rounding off is to add
            some muliple of 5. Since we want to round the 100's to the nearest
            thousand, add 500.

            ( 93601 + 500 ) == 94101 / 1000 = 94
            94 * 1000 == 94000

            An actual function to do this is trivial.


            Comment

            Working...