Solved

Get Last Wokring Day

Posted on 1998-08-12
5
224 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
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

Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
C Language combined operators 28 138
computer science syllabus 3 118
VS2015 Redefinition errors 4 107
MFC COM Server not showing  form 4 27
This article shows you how to optimize memory allocations in C++ using placement new. Applicable especially to usecases dealing with creation of large number of objects. A brief on problem: Lets take example problem for simplicity: - I have a G…
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 how to user default arguments when defining functions. This method of defining functions will be contrasted with the non-default-argument of defining functions.

710 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