• C

C program to determine tomorrows date.

I am new to C and I need a program to tell tomorrows date.
For example I do it in Perl5 like this:

#!/bin/perl
#
@tomorrow=localtime(time+(60*60*24)) ;

if ($tomorrow[3] == 1)
{
        print "Today is the last day of the month.\n" ;
}

but I need this in C, how can I duplicate this functionality
exactly in a C program.  I am on a UNIX platform.
swintmicroAsked:
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.

yonatCommented:
Try doing a man on localtime and gmtime.
0
swintmicroAuthor Commented:
I did and it didn't help, see I don't know much about C or the writing of a C program, neither of the man pages you listed contained an in-depth tutorial on C, so they don't solve this problem.  Right now I need this program and don't have time to learn C just to write it.
0
ozoCommented:
#include <time.h>
struct tm* tomorrow;
time_t t;
t = time(0)+24*60*60;
tomorrow=localtime(&t);
if( tomorrow->tm_mday == 1 ){
  print "Today is the last day of the month.\n";
}
/* fortunately, daylight savings time seldom switches
 on the first or last day of the month, so this should be safe,
 but in general, there may be 2 hours during the year when
 (localtime(time+(60*60*24)))[3] won't be the day after today */
0
The Ultimate Tool Kit for Technolgy Solution Provi

Broken down into practical pointers and step-by-step instructions, the IT Service Excellence Tool Kit delivers expert advice for technology solution providers. Get your free copy for valuable how-to assets including sample agreements, checklists, flowcharts, and more!

swintmicroAuthor Commented:
Thank you for helping, however I couldn't get this to compile.
I am using GCC 2721 on Solaris 2.5.1 and when I compile (gcc time.c) I get:

time.c:4: conflicting types for `t'
time.c:3: previous declaration of `t'
time.c:4: initializer element is not constant
time.c:4: warning: data definition has no type or storage class
time.c:5: conflicting types for `tomorrow'
time.c:2: previous declaration of `tomorrow'
time.c:5: warning: passing arg 1 of `localtime' from incompatible pointer type
time.c:5: warning: initialization makes integer from pointer without a cast
time.c:5: initializer element is not constant
time.c:5: warning: data definition has no type or storage class
time.c:6: parse error before `if'

Any help would be appreciated.
0
ozoCommented:
Did you have a
 main(){
 }
around your program?
(also, Perl's
  print "Today is the last day of the month.\n";
should become
  printf("Today is the last day of the month.\n");
in C)

0
LucHoltkampCommented:
The following compiles and runs flawlessly with BC5:
/* Begin of File*/
#include <time.h>
#include <stdio.h>
void main(void)
{
   time_t t = time(0) + 24 * 60 * 60;
   if(localtime(&t)->tm_mday == 1)
     printf("Today is the last day of the month.\n");
}
/* End of file */

See if you can find out how time_t is defined or typedefed (in time.h)
0
_ZaphodCommented:
If you don't know C at all and wish to code in C, I recommend buying a book or going to C classes. Learning from people using the internet won't get you far.
0
ozoCommented:
I also seem to have spoken too soon in saying that daylight savings
time switches didn't seem to be close enough to the beginning of the month
to give you a problem.
On my system, this method will give the wrong answer at
23:01 Sat 31-Mar-2001
and at
00:59 Sun 31-Oct-1999
0
swintmicroAuthor Commented:
With the help of ozo's program and an associate of mine we got it to compile.  I am new to C and I did try to write this on my own,
but it turned out to be that I needed it fairly quickly, I could have eventually figured it out, but eventually was WAY too late. Thanks ozo.  
0
LucHoltkampCommented:
I agree with _Zaphod. If you need to do more C/C++ get a good learning book. It's great fun ;)
.luc.
0
ozoCommented:
/* this version should be more reliable near daylight savings time changes */
time_t t = time(0);
t += (36-localtime(&t)->tm_hour)*60*60;
if( localtime(&t)->tm_mday == 1 ){
  printf("Today is the last day of the month.\n");
}
0
alexoCommented:
how about closing the question then?

0
swintmicroAuthor Commented:
no one has submitted an "answer" yet
0
ozoCommented:
Do you still want an answer?  It sounded like you were saying the answer came too late.

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
swintmicroAuthor Commented:
No, your answer helped alot
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.