Solved

Get Last Wokring Day

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

How to run any project with ease

Manage projects of all sizes how you want. Great for personal to-do lists, project milestones, team priorities and launch plans.
- Combine task lists, docs, spreadsheets, and chat in one
- View and edit from mobile/offline
- Cut down on emails

Join & Write a Comment

C++ Properties One feature missing from standard C++ that you will find in many other Object Oriented Programming languages is something called a Property (http://www.experts-exchange.com/Programming/Languages/CPP/A_3912-Object-Properties-in-C.ht…
Basic understanding on "OO- Object Orientation" is needed for designing a logical solution to solve a problem. Basic OOAD is a prerequisite for a coder to ensure that they follow the basic design of OO. This would help developers to understand the b…
The goal of the video will be to teach the user the difference and consequence of passing data by value vs passing data by reference in C++. An example of passing data by value as well as an example of passing data by reference will be be given. Bot…
The viewer will learn how to clear a vector as well as how to detect empty vectors in C++.

707 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

Need Help in Real-Time?

Connect with top rated Experts

16 Experts available now in Live!

Get 1:1 Help Now