473,287 Members | 1,834 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,287 software developers and data experts.

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 3076
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*******@canada.com> wrote in message
news:11*********************@z14g2000cwz.googlegro ups.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 StartEndDateTime
{
private static Hashtable _cache = new Hashtable();

private DateTime _startDate;
private DateTime _endDate;

public StartEndDateTime(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.GetHashCode();
}

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

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

public long GetWorkingDayMinutes()
{
long result;
obj cacheResult = StartEndDateTime._cache[this];
if (cacheResult != null)
{
result = (long)cacheResult;
}
else
{
... do your math here ...
StartEndDateTime._cache[this] = result;
}
return result;
}
}

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

StartEndDateTime period = new StartEndDateTime(startDate, endDate);
long periodMinutes = period.GetWorkingDayMinutes();

.... 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
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
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....
1
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...
8
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
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...
2
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...
0
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
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');...
1
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...
0
by: MeoLessi9 | last post by:
I have VirtualBox installed on Windows 11 and now I would like to install Kali on a virtual machine. However, on the official website, I see two options: "Installer images" and "Virtual machines"....
0
by: DolphinDB | last post by:
The formulas of 101 quantitative trading alphas used by WorldQuant were presented in the paper 101 Formulaic Alphas. However, some formulas are complex, leading to challenges in calculation. Take...
0
by: Aftab Ahmad | last post by:
Hello Experts! I have written a code in MS Access for a cmd called "WhatsApp Message" to open WhatsApp using that very code but the problem is that it gives a popup message everytime I clicked on...
0
by: Aftab Ahmad | last post by:
So, I have written a code for a cmd called "Send WhatsApp Message" to open and send WhatsApp messaage. The code is given below. Dim IE As Object Set IE =...
0
by: marcoviolo | last post by:
Dear all, I would like to implement on my worksheet an vlookup dynamic , that consider a change of pivot excel via win32com, from an external excel (without open it) and save the new file into a...
1
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
0
by: jfyes | last post by:
As a hardware engineer, after seeing that CEIWEI recently released a new tool for Modbus RTU Over TCP/UDP filtering and monitoring, I actively went to its official website to take a look. It turned...
0
by: ArrayDB | last post by:
The error message I've encountered is; ERROR:root:Error generating model response: exception: access violation writing 0x0000000000005140, which seems to be indicative of an access violation...
1
by: PapaRatzi | last post by:
Hello, I am teaching myself MS Access forms design and Visual Basic. I've created a table to capture a list of Top 30 singles and forms to capture new entries. The final step is a form (unbound)...

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.