• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 237
  • Last Modified:

passing timing parameters

Hello group,

In part of my code, I have to call a function which in there I should pass parametrs like       Year, month, day and hour. So, basically, user will call the exectuable file like:

     $ ./prog  2008 11  3  0  

which in fact is year 2008, month = Nov, first day of month and hour = 0 a.m.  or TODAY!
note that 0 = 12 a.m.

so now in order to make it more user-friendly I'm thinking of passing some pre-defined tokens like

    $ ./prog  today  
    $ ./prog  yesterday
    or  
    $ ./prog week
 
Question:
1) Is it ok to create a CASE block for these tokens?
2) how should I handle today or TODAY or tOdAY ?

Regards.
0
akohan
Asked:
akohan
3 Solutions
 
sunnycoderCommented:
>1) Is it ok to create a CASE block for these tokens?
case does not work on strings. .. you would need a bunch of strcmp .. probably if with strcmp (invoked based on argc value would be good)

if (argc == 2)
{
   if (strcmp(...))
   {
      ...
   }
    else if ((strcmp...))
   {
     ...
   }
}

>2) how should I handle today or TODAY or tOdAY ?
You can use a case insensitive comparison
http://linux.die.net/man/3/strncasecmp

Alternatively you can use toupper tolower to convert to a particular case and then compare
http://linux.die.net/man/3/toupper
0
 
SreejithGCommented:
1. Yes, It is ok to use CASE
2. Always Convert the input tokens (text) to upper case or lower case and  then compare
0
 
ozoCommented:
switch( strcasecmp(argv[1],"today")?strcasecmp(argv[1],"yesterday")?0:1:2 ){
    case 2: t=time(); break;
    case 1: t=time()-24*60*60; break; // if your locale has daylight saving time there may be two hours per year when this doesn't work
}
0
 
akohanAuthor Commented:

Everybody has come up with nice solution and also links.

Thanks!
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!

Tackle projects and never again get stuck behind a technical roadblock.
Join Now