473,778 Members | 1,958 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 3121
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
5104
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
7929
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
2452
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
2017
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 of switching to a
5
3990
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 anyone know why?? Thanks in advance.
2
1387
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 links pertaining to the creation of web applications with Python, which I'm only just getting...
0
1812
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
40155
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 = document.createElement('INPUT');
1
1296
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
9629
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 usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
9470
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
10298
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
10127
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...
1
10069
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 most users, this new feature is actually very convenient. If you want to control the update process,...
0
9923
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 choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
1
7475
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
5370
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...
3
2865
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 effective websites that not only look great but also perform exceptionally well. In this comprehensive...

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.