Solved

Get Last Wokring Day

Posted on 1998-08-12
5
219 Views
Last Modified: 2010-04-01
Hi,
  I want to get last working day.  
  For example, today is "12-August-98" and I want to get "11-August-98".  Also, if today is "17-august-98", then the program should print out "14-August-98".
Thanks!!
jpchen@yahoo.com
0
Comment
Question by:jpchen
5 Comments
 
LVL 15

Expert Comment

by:Tommy Hui
ID: 1170112
If you have a library that converts the date into a julian date as well as returning the day of the week then you're all set. Assume TDate is such a class:

  TDate test("14-August-98");

  do
  {
    test = test.PrevDate();
  } while (!IsWorkingDate(test));


All TDate::PrevDate has to do is take the date and convert it to Julian date and subtract one.

IsWorkingDate() examines the day of the week the date is and returns TRUE if it is Monday through Friday, otherwise FALSE.
0
 
LVL 11

Expert Comment

by:alexo
ID: 1170113
0
 
LVL 2

Expert Comment

by:kinkajou
ID: 1170114
What are your expectations jpchen? Do you want someone to write the code and send you an exe or just some code commented here? What platform do you need this for?
0
 

Author Comment

by:jpchen
ID: 1170115
I don't need the code and I just need a hit and a easy way to do this.
Anyway, I found the solution by my self this morning.

Thanks all!!
jpchen

0
 
LVL 8

Accepted Solution

by:
Answers2000 earned 200 total points
ID: 1170116
OK.  Here's how to do it.  Assuming you're using standard C library.

(1) Convert the date you're starting with (in your example 12 Aug or 17 Aug) into time since some reference point.
Using standard time.h header, you can do this by filling in struct tm and calling mk_time (which will return a value of type time_t which is seconds since 1970).  Keep this value

(2) Work out day of week.
Using standard C library convert back to a struct tm by calling gmtime (this gives a day of week 0-6).

(3) Depending on day of week, subtract from value generated in stage (1).  
If day of week is Monday subtract (60*60*24*3) - i.e. 3 days going back to previous Friday
If day of week is Tues to Sat, subtract (60*60*24*1) - i.e. 1 day going back to previous day
If day of week is Sunday, subtract (60*60*24*2), i.e. 2 days going back to Friday

(4) Convert the value from (3) back into a date.  Again you can use gmtime

One last point, this doesn't deal with holidays which of course are different in each country anyway (hmmm we don't celebrate July 4 or Thanksgiving in the UK <g>).  The only way to deal with this is to include a data table in your program to take these into account.

Finally, a request - if possible -  Please encourage your friends at yahoo to index my site ASAP (i've already submitted a while ago) - it's http://www.ans2000.com
0

Featured Post

Free Tool: SSL Checker

Scans your site and returns information about your SSL implementation and certificate. Helpful for debugging and validating your SSL configuration.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Unlike C#, C++ doesn't have native support for sealing classes (so they cannot be sub-classed). At the cost of a virtual base class pointer it is possible to implement a pseudo sealing mechanism The trick is to virtually inherit from a base class…
Often, when implementing a feature, you won't know how certain events should be handled at the point where they occur and you'd rather defer to the user of your function or class. For example, a XML parser will extract a tag from the source code, wh…
The goal of the video will be to teach the user the concept of local variables and scope. An example of a locally defined variable will be given as well as an explanation of what scope is in C++. The local variable and concept of scope will be relat…
The viewer will learn how to user default arguments when defining functions. This method of defining functions will be contrasted with the non-default-argument of defining functions.

821 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question