Sponsored Content
Full Discussion: get next working day
Top Forums Programming get next working day Post 302272405 by fpmurphy on Tuesday 30th of December 2008 01:37:34 PM
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);

}

 

10 More Discussions You Might Find Interesting

1. 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

2. 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

3. 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

4. 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

5. 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

6. 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

7. 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

8. 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

9. 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

10. 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
GETOPT(P)						     POSIX Programmer's Manual							 GETOPT(P)

NAME
getopt, optarg, opterr, optind, optopt - command option parsing SYNOPSIS
#include <unistd.h> int getopt(int argc, char * const argv[], const char *optstring); extern char *optarg; extern int optind, opterr, optopt; DESCRIPTION
The getopt() function is a command-line parser that shall follow Utility Syntax Guidelines 3, 4, 5, 6, 7, 9, and 10 in the Base Definitions volume of IEEE Std 1003.1-2001, Section 12.2, Utility Syntax Guidelines. The parameters argc and argv are the argument count and argument array as passed to main() (see exec() ). The argument optstring is a string of recognized option characters; if a character is followed by a colon, the option takes an argument. All option characters allowed by Utility Syntax Guideline 3 are allowed in optstring. The implementation may accept other characters as an extension. The variable optind is the index of the next element of the argv[] vector to be processed. It shall be initialized to 1 by the system, and getopt() shall update it when it finishes with each element of argv[]. When an element of argv[] contains multiple option characters, it is unspecified how getopt() determines which options have already been processed. The getopt() function shall return the next option character (if one is found) from argv that matches a character in optstring, if there is one that matches. If the option takes an argument, getopt() shall set the variable optarg to point to the option-argument as follows: 1. If the option was the last character in the string pointed to by an element of argv, then optarg shall contain the next element of argv, and optind shall be incremented by 2. If the resulting value of optind is greater than argc, this indicates a missing option- argument, and getopt() shall return an error indication. 2. Otherwise, optarg shall point to the string following the option character in that element of argv, and optind shall be incremented by 1. If, when getopt() is called: argv[optind] is a null pointer* argv[optind] is not the character - argv[optind] points to the string "-" getopt() shall return -1 without changing optind. If: argv[optind] points to the string "--" getopt() shall return -1 after incrementing optind. If getopt() encounters an option character that is not contained in optstring, it shall return the question-mark ( '?' ) character. If it detects a missing option-argument, it shall return the colon character ( ':' ) if the first character of optstring was a colon, or a ques- tion-mark character ( '?' ) otherwise. In either case, getopt() shall set the variable optopt to the option character that caused the error. If the application has not set the variable opterr to 0 and the first character of optstring is not a colon, getopt() shall also print a diagnostic message to stderr in the format specified for the getopts utility. The getopt() function need not be reentrant. A function that is not required to be reentrant is not required to be thread-safe. RETURN VALUE
The getopt() function shall return the next option character specified on the command line. A colon ( ':' ) shall be returned if getopt() detects a missing argument and the first character of optstring was a colon ( ':' ). A question mark ( '?' ) shall be returned if getopt() encounters an option character not in optstring or detects a missing argument and the first character of optstring was not a colon ( ':' ). Otherwise, getopt() shall return -1 when all command line options are parsed. ERRORS
No errors are defined. The following sections are informative. EXAMPLES
Parsing Command Line Options The following code fragment shows how you might process the arguments for a utility that can take the mutually-exclusive options a and b and the options f and o, both of which require arguments: #include <unistd.h> int main(int argc, char *argv[ ]) { int c; int bflg, aflg, errflg; char *ifile; char *ofile; extern char *optarg; extern int optind, optopt; . . . while ((c = getopt(argc, argv, ":abf:o:")) != -1) { switch(c) { case 'a': if (bflg) errflg++; else aflg++; break; case 'b': if (aflg) errflg++; else { bflg++; bproc(); } break; case 'f': ifile = optarg; break; case 'o': ofile = optarg; break; case ':': /* -f or -o without operand */ fprintf(stderr, "Option -%c requires an operand ", optopt); errflg++; break; case '?': fprintf(stderr, "Unrecognized option: -%c ", optopt); errflg++; } } if (errflg) { fprintf(stderr, "usage: . . . "); exit(2); } for ( ; optind < argc; optind++) { if (access(argv[optind], R_OK)) { . . . } This code accepts any of the following as equivalent: cmd -ao arg path path cmd -a -o arg path path cmd -o arg -a path path cmd -a -o arg -- path path cmd -a -oarg path path cmd -aoarg path path Checking Options and Arguments The following example parses a set of command line options and prints messages to standard output for each option and argument that it encounters. #include <unistd.h> #include <stdio.h> ... int c; char *filename; extern char *optarg; extern int optind, optopt, opterr; ... while ((c = getopt(argc, argv, ":abf:")) != -1) { switch(c) { case 'a': printf("a is set "); break; case 'b': printf("b is set "); break; case 'f': filename = optarg; printf("filename is %s ", filename); break; case ':': printf("-%c without filename ", optopt); break; case '?': printf("unknown arg %c ", optopt); break; } } Selecting Options from the Command Line The following example selects the type of database routines the user wants to use based on the Options argument. #include <unistd.h> #include <string.h> ... char *Options = "hdbtl"; ... int dbtype, i; char c; char *st; ... dbtype = 0; while ((c = getopt(argc, argv, Options)) != -1) { if ((st = strchr(Options, c)) != NULL) { dbtype = st - Options; break; } } APPLICATION USAGE
The getopt() function is only required to support option characters included in Utility Syntax Guideline 3. Many historical implementations of getopt() support other characters as options. This is an allowed extension, but applications that use extensions are not maximally por- table. Note that support for multi-byte option characters is only possible when such characters can be represented as type int. RATIONALE
The optopt variable represents historical practice and allows the application to obtain the identity of the invalid option. The description has been written to make it clear that getopt(), like the getopts utility, deals with option-arguments whether separated from the option by <blank>s or not. Note that the requirements on getopt() and getopts are more stringent than the Utility Syntax Guide- lines. The getopt() function shall return -1, rather than EOF, so that <stdio.h> is not required. The special significance of a colon as the first character of optstring makes getopt() consistent with the getopts utility. It allows an application to make a distinction between a missing argument and an incorrect option letter without having to examine the option letter. It is true that a missing argument can only be detected in one case, but that is a case that has to be considered. FUTURE DIRECTIONS
None. SEE ALSO
exec() , the Base Definitions volume of IEEE Std 1003.1-2001, <unistd.h>, the Shell and Utilities volume of IEEE Std 1003.1-2001 COPYRIGHT
Portions of this text are reprinted and reproduced in electronic form from IEEE Std 1003.1, 2003 Edition, Standard for Information Technol- ogy -- Portable Operating System Interface (POSIX), The Open Group Base Specifications Issue 6, Copyright (C) 2001-2003 by the Institute of Electrical and Electronics Engineers, Inc and The Open Group. In the event of any discrepancy between this version and the original IEEE and The Open Group Standard, the original IEEE and The Open Group Standard is the referee document. The original Standard can be obtained online at http://www.opengroup.org/unix/online.html . IEEE
/The Open Group 2003 GETOPT(P)
All times are GMT -4. The time now is 06:41 AM.
Unix & Linux Forums Content Copyright 1993-2022. All Rights Reserved.
Privacy Policy