473,573 Members | 5,078 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

calculate "working minutes" between 2 dates

Hello,
I am hoping someone else has thought about a date time calculation i need to
perform.

I would like to be able to calculate the number of "working minutes" between
2 dates, given my working week definition.

Lets say I have a working week definition of Monday through Friday, 9 am to
5 pm.
Date1 = January 1st, 2005 at 8 am
Date2 = February 14th, 2005 at 10 am.
How many "working minutes" between the 2 dates?

How would you code this?

My approach is rather brute force. I break the calculation into 3 parts:
1. Calculate the working minutes in a full week (5 days * 8 hr * 60
minutes= 2400 minutes)
2. Minutes prior to first full week. I basically go day by day and do
date math.
3. Number of full weeks * result from step 1 (2400 most of the time)
4. Minutes after last full week. I basically go day by day and do date
math.

The sum of step 2 + 3 + 4 gives me my answer. I am doing this calculation
alot and it has proven to be a bottleneck.

Any suggestions on making it faster? Please let me know if I can provide
more details. Thanks for all replies in advance!

Thanks,
Dan


Nov 17 '05 #1
3 3102
I can't see an obvious alternative to your method.

Are you in a situation where the same date pairs will be passed over
and over, so caching previous answers might have a beneficial effect,
or are the date pairs passed randomly distributed?

If caching would help you, you could create a hash table that contains
previous results... the only thing you would have to come up with would
be an efficient way of getting a key from the two dates to use as a
hash key.

If it's highly likely that the same date pair will be passed many times
in sequence, you could even just have a super-cheap "one off" cache:
the last pair of dates and the answer, and before starting the
calculation check to see if the caller is just re-asking the previous
question.

Nov 17 '05 #2
Good idea.

The application is a discrete event simulation where I am scheduling future
events. Thinking about this, i think many times the first date will be the
same for many consecutive calls to the method, so caching will be great.

Thank you for this valuable tip.

Dan
"Bruce Wood" <br*******@cana da.com> wrote in message
news:11******** *************@z 14g2000cwz.goog legroups.com...
I can't see an obvious alternative to your method.

Are you in a situation where the same date pairs will be passed over
and over, so caching previous answers might have a beneficial effect,
or are the date pairs passed randomly distributed?

If caching would help you, you could create a hash table that contains
previous results... the only thing you would have to come up with would
be an efficient way of getting a key from the two dates to use as a
hash key.

If it's highly likely that the same date pair will be passed many times
in sequence, you could even just have a super-cheap "one off" cache:
the last pair of dates and the answer, and before starting the
calculation check to see if the caller is just re-asking the previous
question.

Nov 17 '05 #3
If the start date is normally the same, then you have two options.

1. Come up with a key scheme for start / end date combinations, and
hash the answers as you compute them. This is the most general scheme,
but its value varies with the cost of computing the hash key: the more
expensive the key, the less effective the cache. Try this:

public struct StartEndDateTim e
{
private static Hashtable _cache = new Hashtable();

private DateTime _startDate;
private DateTime _endDate;

public StartEndDateTim e(DateTime startDate, DateTime endDate)
{
this._startDate = startDate;
this._endDate= endDate;
}

public DateTime StartDate
{
get { return this._startDate ; }
}

public DateTime EndDate
{
get { return this._endDate; }
}

public override int GetHashCode()
{
return this._startDate .GetHashCode() +
this._endDate.G etHashCode();
}

public override bool Equals(object obj)
{
if (obj is DateTime)
{
return Equals((DateTim e)obj);
}
else
{
return false;
}
}

public bool Equals(DateTime otherDate)
{
return this._startDate .Equals(otherDa te._startDate) &&
this._endDate.E quals(otherDate ._endDate);
}

public long GetWorkingDayMi nutes()
{
long result;
obj cacheResult = StartEndDateTim e._cache[this];
if (cacheResult != null)
{
result = (long)cacheResu lt;
}
else
{
... do your math here ...
StartEndDateTim e._cache[this] = result;
}
return result;
}
}

.... then you can use this struct in your application like this:

StartEndDateTim e period = new StartEndDateTim e(startDate, endDate);
long periodMinutes = period.GetWorki ngDayMinutes();

.... and the fact that there's a cache involved is transparent. I would
try this first and then time it to see how performant it is.

2. At least cache the answer to your second part: minutes prior to
first full week, along with the start date from the previous request.
When you enter the routine, you can check the start date against the
previous start date and, if they're the same, use the previous "minutes
prior to first full week" and save yourself that calculation. You can
do the same with the end date. That saves you two of the three most
expensive calculations, leaving you only to calculate the full weeks
minutes each time (until the end date changes, then you have to do the
math again).

You could even come up with a hash table scheme for the end date ->
minutes after last full week answer, and see if the hashing is faster
than the mathematics.

Again, this way you still have to do some math, but you cut back
drastically on the amount you have to do. It's probably less performant
than a full start/end date -> final result caching scheme, but it will
be a big help, nonetheless.

Nov 17 '05 #4

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

Similar topics

8
5096
by: Brian Huether | last post by:
Whenever I use this, it doesn't seem to return anything. Any idea what is happening here? My webhost (ipowerweb) uses php 4.3.1. -brian
3
7912
by: www | last post by:
Hi there, I need to calculate working days for a given period, "Date from", "Date To". Plus I want to be able to insert Public Holidays to exclude aswell. Your help will be greatly appreciated. Cheers Charles
1
2439
by: Jason | last post by:
I am trying to send mail via SMTP, using VB.NET on XP. I keep getting this error: "Could not access 'CDO.Message' object." (I can send mail fine via VB6, using CDONTS.) I have added a reference to System.Web, and this is my code: Try WebMailMessage.From = Sender
8
2005
by: David Thielen | last post by:
Hi; ..NET 2.0 I have a situation where when the user selects an item in a drop down list, the code behind is called to update the values in another list on the page. This update can take 3 - 5 seconds. When it is complete, the same page is still displayed. What is the best way to tell the user the page is working? I don't like the idea...
5
3972
by: Jon via DotNetMonster.com | last post by:
<siteMapNode title="share price" description="Link to Netcall on the London Stock Exchange" url="http://www.yahoo.co.uk" role="" target="_blank" /> Hi all I'm trying to open the Yahoo web site in a new window using the above code in my asp.net web.sitemap. I'm expecting the target="_blank" bit to make it do this. but it isnt. Does...
2
1370
by: Michiel Sikma | last post by:
Hello everybody. I recently had a bit of data loss and, among some other minor things, lost my bookmarks. I once bookmarked this video tutorial which allegedly showed how to make a wiki in 10 minutes with some Python network framework. Does anybody know which one it might have been? I've been meaning to do it for some time now. Any other...
0
1796
by: WORKING IN FAITH | last post by:
three years I LOVE You Monica More options 1 message - Collapse all WORKING IN FAITH View profile More options Nov 13, 11:29 am three years I LOVE You Monica
12
40126
by: ssh | last post by:
function testfn(name) var tbody = document.getElementById('hellospace').getElementsByTagName('tbody'); var row = document.createElement('TR'); var cell1 = document.createElement('TD'); cell1.align="left"; cell1.style.height = "22"; cell1.style.width = "15"; cell1.valign = "middle"; cell1.className = "dwInfo"; var inp1 =...
1
1286
by: Claire | last post by:
Hi, either I'm going mad or is params not working anymore in VS 2005? I have function as follows public tableEquipment ReadEquipment(bool ReadImages, params Int64 RecIDs) { } if I call "tableEquipment equipment = myDatabase.ReadEquipment(false, RecID);"
0
8218
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...
1
7800
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For...
0
8091
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the...
0
6435
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...
0
5296
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...
0
3744
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
2228
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
1330
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
0
1049
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.