473,563 Members | 2,897 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

adding time segments

Hi just wondering if anyone had any ideas on how to do this in C#.

I am trying to get the total time of several start and stop time data
entries. For example the input data looks like
starttime endtime
3:00pm 4:00pm
6:00pm 7:00pm.
6:30pm 7:00pm
3:00pm 3:30pm
It would be easy but I do not want to count any overlapped times. For
example for the data above I would want a sum of 2 hours, not counting the
6:30-7:00 segment and the 3:00-3:30 segment. Most of the data is more
complexed than the example above, more data entries with several different
overlaps. I currently have a loop and am comparing the current entry with
the previous entries. This works ok until I get to the 3:00-3:30 segment as
it does not know to not count it since it is only comparing it with the
6:30-7:00 segment.
thanks!

--
Paul G
Software engineer.
Oct 9 '07 #1
8 1624
Hi Paul,

Using DateTime for the times, you can easily test if one span is within the boundaries of another and then just merge the two

On Tue, 09 Oct 2007 19:28:01 +0200, Paul <Pa**@discussio ns.microsoft.co mwrote:
Hi just wondering if anyone had any ideas on how to do this in C#.

I am trying to get the total time of several start and stop time data
entries. For example the input data looks like
starttime endtime
3:00pm 4:00pm
6:00pm 7:00pm.
6:30pm 7:00pm
3:00pm 3:30pm
It would be easy but I do not want to count any overlapped times. For
example for the data above I would want a sum of 2 hours, not counting the
6:30-7:00 segment and the 3:00-3:30 segment. Most of the data is more
complexed than the example above, more data entries with several different
overlaps. I currently have a loop and am comparing the current entry with
the previous entries. This works ok until I get to the 3:00-3:30 segment as
it does not know to not count it since it is only comparing it with the
6:30-7:00 segment.
thanks!


--
Happy coding!
Morten Wennevik [C# MVP]
Oct 9 '07 #2
Hi thanks for the quick response. Guess I am not quite sure how to set up
the comparison since it seems like it might have to change dynamically. It
gets a bit complicated for example as shown below. I think I would have to
store all merges and then with each new time segment I would need to compare
it to all of the saved time segments, so not quite sure how to set this up in
a loop.

typical data

start time end time
1 2
4 5
8 9:30
10 11:20
1:15 1:45
4:15 4:45
7:45 8:15
9:30 10:10
--
Paul G
Software engineer.
"Morten Wennevik [C# MVP]" wrote:
Hi Paul,

Using DateTime for the times, you can easily test if one span is within the boundaries of another and then just merge the two

On Tue, 09 Oct 2007 19:28:01 +0200, Paul <Pa**@discussio ns.microsoft.co mwrote:
Hi just wondering if anyone had any ideas on how to do this in C#.

I am trying to get the total time of several start and stop time data
entries. For example the input data looks like
starttime endtime
3:00pm 4:00pm
6:00pm 7:00pm.
6:30pm 7:00pm
3:00pm 3:30pm
It would be easy but I do not want to count any overlapped times. For
example for the data above I would want a sum of 2 hours, not counting the
6:30-7:00 segment and the 3:00-3:30 segment. Most of the data is more
complexed than the example above, more data entries with several different
overlaps. I currently have a loop and am comparing the current entry with
the previous entries. This works ok until I get to the 3:00-3:30 segment as
it does not know to not count it since it is only comparing it with the
6:30-7:00 segment.
thanks!

--
Happy coding!
Morten Wennevik [C# MVP]
Oct 9 '07 #3
Paul wrote:
[...]
I currently have a loop and am comparing the current entry with
the previous entries. This works ok until I get to the 3:00-3:30 segment as
it does not know to not count it since it is only comparing it with the
6:30-7:00 segment.
Can we assume that all of the times being considered are either on the
same date, or include the date information?

It seems to me that the most straightforward implementation would be to
keep some kind of list of "DateTime with range" instances. In other
words, instances of a class that looks something like this:

class DateTimeRange
{
DateTime _dtStart;
DateTime _dtEnd;

public DateTimeRange(D ateTime dtStart, DateTime dtEnd)
{
_dtStart = dtStart;
_dtEnd = dtEnd;
}
}

You'd start with an empty list. Each time you add a new time range (eg
"starting at 3pm, ending at 4pm", you would scan the list, looking for a
place to insert it. If there's overlap, you would modify the existing
entry in your list, expanding it to include the new entry. You do this
for each entry in your list of time ranges. When you're done, you
simply enumerate the list and extract the final start/end time values
from the list.

You'll have a variety of cases to deal with. The overlap could involve
the new range ending within an existing range, beginning within an
existing range, being completely contained within an existing range,
completely containing an existing range, or even spanning two existing
ranges.

I think a general-purpose way of dealing with those cases would be to
implement the code so that you simply check for any kind of overlap, and
once you find overlap, you remove the existing range from the list,
merge it as needed with the new range, and then re-add the result to
your list, handling the newly created range just as you would an original.

Conceptually I think that implementing that recursively would be
easiest, but it's not actually required.

Hope that helps.

Pete
Oct 9 '07 #4
An alternative to this could be tracking a DateTime and a timespan --
using the DateTime as your anchor (start, end, or possibly you could
switch between them).

Chris.
Oct 9 '07 #5
Chris Shepherd wrote:
An alternative to this could be tracking a DateTime and a timespan --
using the DateTime as your anchor (start, end, or possibly you could
switch between them).
My newsreader is buggy, so I'm not sure if you're responding to my post
or not. In the thread view, it looks like you are, but I never really
know if there's no quoted text. :) But, assuming you are...

I personally think that using a DateTime as an anchor with TimeSpan for
length conceptually "feels" better. However, the original data is all
actual times, and I think that in reality the implementation is the same
except for what sort of conversions you need to do. Using only DateTime
values, given that the original data is also only using things that
essentially map to DateTime values likely will minimize the excess code
put in just to handle converting back and forth.

I definitely wouldn't bother making the DateTime anchor be able to be
the start or end, with a positive or negative TimeSpan. I've had to
write code like that before, in scenarios where it actually is
necessary, and it can be a pain. It's much nicer to be able to just
assume that the anchor is at the beginning of the range.

Of course, if the range is always just a start and end time, as I
proposed, the question of which end the anchor is on goes away. Perhaps
that's another good reason to stick with keeping the internal
representation the same as the input data. :)

Pete
Oct 9 '07 #6
Peter Duniho wrote:
My newsreader is buggy, so I'm not sure if you're responding to my post
or not. In the thread view, it looks like you are, but I never really
know if there's no quoted text. :) But, assuming you are...
Yup, it was. Apologies for not quoting your post.
I personally think that using a DateTime as an anchor with TimeSpan for
length conceptually "feels" better. However, the original data is all
actual times, and I think that in reality the implementation is the same
except for what sort of conversions you need to do. Using only DateTime
values, given that the original data is also only using things that
essentially map to DateTime values likely will minimize the excess code
put in just to handle converting back and forth.
True enough. I was just tossing the idea out there. It's not like you
can't just subtract start from end and get your TimeSpan anyways. :)
I definitely wouldn't bother making the DateTime anchor be able to be
the start or end, with a positive or negative TimeSpan. I've had to
write code like that before, in scenarios where it actually is
necessary, and it can be a pain. It's much nicer to be able to just
assume that the anchor is at the beginning of the range.
Agreed. As with the other stuff, I was throwing it out there in the
"brainstorm ing" mindset.

Chris.
Oct 9 '07 #7
Thanks for all of the responses. I came up with the following which I think
closely follows the suggestions, but without using a time interval.

I sort all of the data based on the start time. I then loop through the
data and if the current time merges with the previous time I keep a running
min and max to handle these merges. When a new record is read into the
algorithm, if a merge is not detected I then save off the start and stop
times. If a merge is detected I do not save off the start and stop times but
adjust the running min and max.
--
Paul G
Software engineer.
"Chris Shepherd" wrote:
Peter Duniho wrote:
My newsreader is buggy, so I'm not sure if you're responding to my post
or not. In the thread view, it looks like you are, but I never really
know if there's no quoted text. :) But, assuming you are...

Yup, it was. Apologies for not quoting your post.
I personally think that using a DateTime as an anchor with TimeSpan for
length conceptually "feels" better. However, the original data is all
actual times, and I think that in reality the implementation is the same
except for what sort of conversions you need to do. Using only DateTime
values, given that the original data is also only using things that
essentially map to DateTime values likely will minimize the excess code
put in just to handle converting back and forth.

True enough. I was just tossing the idea out there. It's not like you
can't just subtract start from end and get your TimeSpan anyways. :)
I definitely wouldn't bother making the DateTime anchor be able to be
the start or end, with a positive or negative TimeSpan. I've had to
write code like that before, in scenarios where it actually is
necessary, and it can be a pain. It's much nicer to be able to just
assume that the anchor is at the beginning of the range.

Agreed. As with the other stuff, I was throwing it out there in the
"brainstorm ing" mindset.

Chris.
Oct 9 '07 #8
On Tue, 09 Oct 2007 19:44:02 +0200, Paul <Pa**@discussio ns.microsoft.co mwrote:
Hi thanks for the quick response. Guess I am not quite sure how to set up
the comparison since it seems like it might have to change dynamically. It
gets a bit complicated for example as shown below. I think I would have to
store all merges and then with each new time segment I would need to compare
it to all of the saved time segments, so not quite sure how to set this up in
a loop.

typical data

start time end time
1 2
4 5
8 9:30
10 11:20
1:15 1:45
4:15 4:45
7:45 8:15
9:30 10:10
Try creating a class or struct holding two DateTime. Then create a list of all time spans using the struct/class to hold the times. If you sort if by start time it will be easier. Taking the first timespan in the list. Are there any other timespans having a start time earlier than this items end time. If so, merge them by adjusting the end time (might want to check if the end time is less than the first items end time). If no other items started in this timespan, store it as a unique timespan somewhere, do this as long as you have remaining items.

--
Happy coding!
Morten Wennevik [C# MVP]
Oct 9 '07 #9

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

Similar topics

12
9453
by: | last post by:
I've trolled the lists, FAQs, and Net as a whole, but can't find anything definitive to answer this. We're looking for real-time graph capability (bar, line, etc), so that we can display telemetry from a robot system. There are a bunch of packages out there, but many seem to provide only static graphs (e.g. for scientific, financial data,...
5
6509
by: cvisal | last post by:
Hi all Im working on productivity calculations (Time calculations) and need some help in coding. Database Tool:MS-Access 2003. The general operator punch-in time is 5:30 AM and the punch-out time is 2:00PM. The Break times are 1) 9:30 AM to 9:45 AM 2) 11:00AM to 11:30 AM 3) 12:30PM to 12:45 PM
0
1625
by: Nihar | last post by:
Hello all, I m just trying to add multiple nodes to an existing xml file. Here is my sample. <ADT> <IN1> <DG1> <GT1>
8
2615
by: karthik.infoguy | last post by:
Any one just let me know what are the different segments in a compiler and what data are stored in different segments????
5
1485
by: pavi | last post by:
Hi, I want to understand how the C's executable a.out is organised into segments. Which segments the varibles are put into according to their declarations. which Linux commands are used to check these details. Kindly anyone suggest any books or documents or link which contains details of the above. Regards, Praveen Kumar A.S
1
9645
by: phantom2850 | last post by:
hello friends, I recently attended 3 interviews, all three had similar questions... Where is const stored? what are diff segments? what is heap? where is it on RAM/ROM? what is stored in stack? what is relation b/w Segments and RAM/ROM/FLASH Can someone suggest me some tutorial for this?
7
2112
by: kr | last post by:
Hi All, Suppose I consider a sample program as given below:- #include<stdio.h> #include<stdlib.h> int i; int main() { char *test(int i); char *tmp = NULL;
26
2327
by: Ravindra.B | last post by:
I have declared a global variable which is array of pointers and allocated memory for each array variable by using malloc. Some thing similar to below... static char *arr; main() { int i;
0
1214
by: aaragon | last post by:
Hello everybody, I have an interesting problem for which I still don't have a solution. Imagine that you're working with points in two-dimesional space, so the point class should be (simplifying): class Point { double x_,y_;
0
7665
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main...
0
7583
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...
0
7888
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. ...
0
8106
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...
0
3643
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...
0
3626
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
2082
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
1
1200
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
0
924
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating...

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.