473,799 Members | 3,161 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

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

Sep 14 '06 #1
5 2669
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);
Sep 14 '06 #2
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

Sep 14 '06 #3
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.
Sep 14 '06 #4
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
Sep 15 '06 #5
"Marcus" <mc*******@wall a.comwrote in message
news:11******** *************@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.
Sep 15 '06 #6

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

2
1377
by: Christopher T King | last post by:
Speakign of rounding errors, why is round() defined to round away from zero? I've always been quite fond of the mathematical definition of rounding of floor(n+.5). As a contrived example of why the latter is IMHO better, I present the following: for x in : print int(round(x)) This prints -10, -9, ..., -2, -1, 1, 2, ..., 9, 10 (it skips zero), probably not what you'd expect. I'm not sure how often a case like this
6
3424
by: DaveK | last post by:
I am upgrading an MS Access 97 application which uses the format function to round decimal numbers to two places e.g. format(dblValue, "standard") Having done an initial study of this function I can see no obvious pattern in how it does rounding, it is certainly not bankers rounding. I need to replicate the rounding algorithm used in the new application so that the data will be consistent, so if anyone knows the rounding
13
1736
by: dhildebrandt | last post by:
I have a query that uses the Round function to change decimals into whole numbers. For 4 years straight the thing has always rounded the numbers in exactly the same way so that whenever I update my graphs, the percentages for the historical data are always exactly what they were the last time I ran the query. Now, for the first time my percentages are all just slightly off and I have not changed the query. The only thing I can think of...
11
6659
by: cj | last post by:
Lets assume all calculations are done with decimal data types so things are as precise as possible. When it comes to the final rounding to cut a check to pay dividends for example in VB rounding seems to be done like this 3.435 = 3.44 3.445 = 3.44 Dim decNbr1 As Decimal
6
2252
by: shaqattack1992-newsgroups | last post by:
Hello Everyone, I'm using the following on a form in my database: =(-Int(-Sum((IIf(=Yes,((*)+()), (((*)+)*1.06))*100))))/100 In this case, I want to calculate a total for an order. If the LineTaxExempt field is checked (meaning tax exempt), the total is
206
13333
by: md | last post by:
Hi Does any body know, how to round a double value with a specific number of digits after the decimal points? A function like this: RoundMyDouble (double &value, short numberOfPrecisions) It then updates the value with numberOfPrecisions after the decimal
20
5017
by: jacob navia | last post by:
Hi "How can I round a number to x decimal places" ? This question keeps appearing. I would propose the following solution #include <float.h> #include <math.h>
30
29698
by: bdsatish | last post by:
The built-in function round( ) will always "round up", that is 1.5 is rounded to 2.0 and 2.5 is rounded to 3.0. If I want to round to the nearest even, that is my_round(1.5) = 2 # As expected my_round(2.5) = 2 # Not 3, which is an odd num I'm interested in rounding numbers of the form "x.5" depending upon whether x is odd or even. Any idea about how to implement it ?
10
4385
by: kinannawaz | last post by:
Can any one help me what will be the time complexity in worst case i.e Big Oh of the following algo I need to resolve it i have worked on it but am not able to come up with the final solution .the below algorithm is going to draw a circle any help or suggestion regarding this algo will be hidhly appreciatable circle (radius) for i =10 to -10 for j =-10 to 10 d = sqrt (i+j) if radius -0.5 < d < radius + 0.5 puetpixel (i,j,1)
0
9541
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
10484
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
0
10251
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
0
9072
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
1
7565
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
6805
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
5463
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
1
4141
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
2
3759
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.