Solved

Get Last Wokring Day

Posted on 1998-08-12
5
212 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

Netscaler Common Configuration How To guides

If you use NetScaler you will want to see these guides. The NetScaler How To Guides show administrators how to get NetScaler up and configured by providing instructions for common scenarios and some not so common ones.

Question has a verified solution.

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

Written by John Humphreys C++ Threading and the POSIX Library This article will cover the basic information that you need to know in order to make use of the POSIX threading library available for C and C++ on UNIX and most Linux systems.   [s…
Container Orchestration platforms empower organizations to scale their apps at an exceptional rate. This is the reason numerous innovation-driven companies are moving apps to an appropriated datacenter wide platform that empowers them to scale at a …
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 additional member functions of the vector class. Specifically, the capacity and swap member functions will be introduced.

773 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