get next working day


 
Thread Tools Search this Thread
Top Forums Programming get next working day
# 1  
Old 12-30-2008
get next working day

i want to get next working day from give date, that is other than Saturday or Sunday.
suppose given date is 26-DEC-2008 (i.e Friday) then i should get 29-DEC-2008 as next working day(i.e Monday). how code this in C. do we have any calendar API in C. please help me out.
thanks and regards
# 2  
Old 12-30-2008
man 2 time
man 3 localtime

Include <time.h>, get the current time (time(2)), increase once by 86400 (next day), feed it to localtime(3) and check if 0<struct tm.tm_wday<6 (Mon-Fri is 1-5). If it's not in that range, increase by 86400 until it is.
# 3  
Old 12-30-2008
thanks for help.
but i want in other way. i have function
computeBsnsDate(start date, no_of_days,next_bsns_date);

i want to pass startdate say "26-DEC-2008 and no_of_days as 1
so i have to get return date as "29-DEC-2008"

do i have to use getdate();

can ny1 tell how? please
thanks
# 4  
Old 12-30-2008
Feed you character array to strptime(3), the resulting struct tm to mktime, the resulting time_t to localtime, and test that result until struct tm.tm_wday indicates a workday. Then convert it to a character array and return that.
# 5  
Old 12-30-2008
Here is some code which does what you are looking to do
Code:
/*
**
**      USAGE: nextworkday [-d] date
**
**                       -d turn on debugging
**
**       INPUT:  Date as DD-MMM-YYYY
**
**    OUTPUT:  Prints next working day as DD-MMM-YYYY
**
**   EXAMPLE: nextworkday 30-DEC-2008
**
*/

#include <stdio.h>
#include <time.h>
#include <unistd.h>
#include <string.h>
#include <stdlib.h>

#define IFORMAT "%d-%b-%Y %H:%M:%S"
#define OFORMAT "%d-%b-%Y"

int
main(int argc, char* argv[])
{
   int c, debug = 0, errflg= 0;
   char buf[50];
   char date[80];
   char format[50];
   struct tm tm1;
   time_t t1;

   extern char *optarg;
   extern int optind, optopt;

   while ((c = getopt(argc, argv, "dh")) != -1) {
        switch(c) {
        case 'd':
            debug = 1;
            break;
        case 'h':
            errflg++;
            break;
        case '?':
            fprintf(stderr, "Unknown option: -%c\n", optopt);
            errflg++;
        }
   }
   if (errflg || (argc - optind != 1)) {
       fprintf(stderr, "Usage: nextworkday [-d] date\n");
       exit(2);
   }

   strcpy(date, argv[optind]);
    /* to ensure tm is populated with something decent */
   strcat(date, " 08:00:00");
   strcpy(format, IFORMAT);

   if (debug) {
       fprintf(stderr, "  DATE STR: %s\n", date);
   }

   if (!strptime(date, format, &tm1)) {
       fprintf(stderr, "strptime() error\n");
       exit(1);
   }

   if (debug) {
       fprintf(stderr, "BEFORE DoM: %d\n", tm1.tm_mday);
       fprintf(stderr, "     Month: %d\n", tm1.tm_mon);
       fprintf(stderr, "      Year: %d\n", tm1.tm_year);
       fprintf(stderr, "       DoW: %d\n", tm1.tm_wday);
   }

   if (tm1.tm_wday < 5)
      tm1.tm_mday = tm1.tm_mday + 1;
   else
      tm1.tm_mday = tm1.tm_mday + (8 - tm1.tm_wday);

   if (debug)
       fprintf(stderr, "   NEW DoM: %d\n", tm1.tm_mday);

   /* hack to fix up the time structure - see man page */
   if ((t1 = mktime(&tm1)) == -1) {
      fprintf(stderr, "mktime() error\n");
      exit(1);
   }

   if (debug) {
       fprintf(stderr, "AFTER  DoM: %d\n", tm1.tm_mday);
       fprintf(stderr, "     Month: %d\n", tm1.tm_mon);
       fprintf(stderr, "      Year: %d\n", tm1.tm_year);
       fprintf(stderr, "       DoW: %d\n", tm1.tm_wday);
   }

   if (strftime(buf,sizeof(buf), OFORMAT, &tm1) == 0) {
      fprintf(stderr, "strftime() error\n");
      exit(1);
   }

   printf("%s\n", buf);

   exit(0);

}

# 6  
Old 12-31-2008
thanks a lot Smilie
it solved my problem.
# 7  
Old 12-31-2008
fpmurphy gave you a very reasonable response based on your requirements.

I work in calendrics a lot, and the requirements you stated for the
definition of a "business day" were either very naive, or for a country
I have never done work for. In most places there are a lot of other
considerations as to what the definition of a "business day" is.

One reason is because the requirment doesn't take holidays and religious
observances into account. Holidays are never trivial, they are often
very local and often based on a distinctly non-Gregorian lunar
calculation. So there is no general method presented very often.

The other issue is that programmers think they "know" calendars
because they grew up with them, but may not know what calendars really do..
Even for the Gregorian calendar. Read the langinfo man page. Look at
the output of
Code:
 cal 9 1752

Do you know why September seems wrong?
And if you live in France that output of cal is wrong.

Holidays --
One way is parse a tztab-like file, then write code: maybe a "isholiday(struct
tm *)" function to parse the table the against a given date:

Quote:
# The first six fields specify the first minute in which a holiday
# specified in the seventh field, applies. The fields are
# separated by spaces or tabs. The first six are integer patterns that
# specify the minute (0-59), hour (0-23), day of the month (1-31), month
# of the year (1-12), year (1970-2038), and day of the week (0-6, with
# 0=Sunday). The minute, hour, and month of the year must contain a
# number in the (respective) range indicated above. The day of the
# month, year, and day of the week can contain a number as above or two
# numbers separated by a minus (indicating an inclusive range). Either
# the day of the month or the day of the week field must be a range, the
# other must be simple number.
#
#
# Holiday table - partial table example
MST7MDT
#minute
# hour
# DOM
# month
# year
# DOW
0 0 25 12 1990-2038 1-5 christmas
0 0 24 12 1990-2038 1-5 christmas eve
0 0 26 12 1990-2038 1 christmas on sunday
0 0 23 12 1990-2038 5 christmas eve on saturday
0 0 4 7 1990-2038 1-5 july 4
0 0 3 7 1990-2038 5 july 4 on saturday
0 0 5 7 1990-2038 1 july 4 on sunday
0 0 25-31 5 1990-2038 1 Memorial day always on the last monday of may
0 0 15-21 1 1990-2038 1 MLK day 3rd Monday in April
0 0 1-7 9 1990-2038 1 Labor day 1st Monday in Sept
0 0 E-2 0 0 0 Good Friday two days before easter
The E-2 entry is a paschal lunar easter algorithm for western Easter
(as opposed to Orthodox Easter) - the entry above returns two days
before Easter. Lunar holidays and very important religious observances
usually require special algorithms.

The table also deals with moving holidays onto work days, like observing
July 4th (a US holiday) on July 5 or on July 3 because July 4 is on a weekend.
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

KSH script Not working (calculate days since 1/1/2000 given day 4444)

I am unable to get this KSH script to work. Can someone help. I've been told this should work with KSH93. Which I think I have on Solaris 10. If I do a grep -i version /usr/dt/bin/dtksh I get @(#)Version M-12/28/93d @(#)Version 12/28/93 @(#)Version M-12/28/93 This is correct for... (5 Replies)
Discussion started by: thibodc
5 Replies

2. Shell Programming and Scripting

Find Month first Working Day

Hi, I would like to calculate 1st working/Business day of each month. Exp: 1st -Oct-2011 is Saturday--- Non Business Day So the Next Working Day would be 3-Oct-2011 I need a shell script to calculate the month first business date. (3 Replies)
Discussion started by: koti_rama
3 Replies

3. UNIX for Dummies Questions & Answers

Running Script via Crontab on 2nd Working day each month

Hello Guys, I have a questions regarding running a shell script every second working day each month. I have no clue how solve this problem :wall:. Important is that it has to be the second working (Mo-Fr). Example: If 1st and 2nd Days of month are Sat and Sun the script must run on 4th day... (5 Replies)
Discussion started by: Hollo
5 Replies

4. Shell Programming and Scripting

Calculating 12th working day

I have a business requirement in my project where I need to calculate the 12th working day of every month. Can any please tell me the solution to my problem. Thanks in advance (7 Replies)
Discussion started by: ami_smart
7 Replies

5. Shell Programming and Scripting

How to find the first working day of month ?

Hi, How to find the first working day of month ? My requirement is, I need to call the function only if today is first working day of month. I could find out one function which finds last working day in month in this forum. Can anyone pls let me know for first working day. Thanks. for... (10 Replies)
Discussion started by: vnimavat
10 Replies

6. Shell Programming and Scripting

Script to find previous month last day minus one day timestamp

Hi All, I need to find the previous month last day minus one day, using shell script. Can you guys help me to do this. My Requirment is as below: Input for me will be 2000909(YYYYMM) I need the previous months last day minus 1 day timestamp. That is i need 2000908 months last day minus ... (3 Replies)
Discussion started by: girish.raos
3 Replies

7. Shell Programming and Scripting

last working day of previous month

Hi, I want a script(ksh) to see if today is the last working day(Mon-Fri) of the month. If it is the last working day I need to print current date, else I need the last working day of previous month. Thanks in advance. (1 Reply)
Discussion started by: rspk_praveen
1 Replies

8. Shell Programming and Scripting

Get Last working day of the month

Hi I need a script to get "Last working day of the month". I will pass the month and year as parameters and i need to get the last working date. Ex for June 2008 the last working day is 30th its monday. for August 2008 the last working day is 29th and it is Friday. ie the last working... (6 Replies)
Discussion started by: manmarirama
6 Replies

9. Shell Programming and Scripting

set Working day in ksh

Hello guys it´s a pleasure to type with the unix community...I´m new in shell script and I need to insert into a #!/ksh a statment that will check if a file that I´ll receive from another script is arriving in the first working day of each month: let´s say that I´ll reveive the following files... (1 Reply)
Discussion started by: Rafael.Buria
1 Replies

10. Shell Programming and Scripting

Write a shell script to find whether the first day of the month is a working day

Hi , I am relatively new to unix... Can u pls help me out to find out if the first day of the month is a working day ie from (Monday to Friday)...using Date and If clause in Korn shell.. This is very urgent. Thanks for ur help... (7 Replies)
Discussion started by: phani
7 Replies
Login or Register to Ask a Question