• C

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.
akohanAsked:
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

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

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
akohanAuthor Commented:

Everybody has come up with nice solution and also links.

Thanks!
0
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
C

From novice to tech pro — start learning today.

Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.