Solved

C program to determine tomorrows date.

Posted on 1997-12-31
15
650 Views
Last Modified: 2010-05-18
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.
0
Comment
Question by:swintmicro
  • 5
  • 5
  • 2
  • +3
15 Comments
 
LVL 5

Expert Comment

by:yonat
ID: 1256866
Try doing a man on localtime and gmtime.
0
 

Author Comment

by:swintmicro
ID: 1256867
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
 
LVL 84

Expert Comment

by:ozo
ID: 1256868
#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
 

Author Comment

by:swintmicro
ID: 1256869
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
 
LVL 84

Expert Comment

by:ozo
ID: 1256870
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
 
LVL 3

Expert Comment

by:LucHoltkamp
ID: 1256871
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
 
LVL 1

Expert Comment

by:_Zaphod
ID: 1256872
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
How your wiki can always stay up-to-date

Quip doubles as a “living” wiki and a project management tool that evolves with your organization. As you finish projects in Quip, the work remains, easily accessible to all team members, new and old.
- Increase transparency
- Onboard new hires faster
- Access from mobile/offline

 
LVL 84

Expert Comment

by:ozo
ID: 1256873
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
 

Author Comment

by:swintmicro
ID: 1256874
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
 
LVL 3

Expert Comment

by:LucHoltkamp
ID: 1256875
I agree with _Zaphod. If you need to do more C/C++ get a good learning book. It's great fun ;)
.luc.
0
 
LVL 84

Expert Comment

by:ozo
ID: 1256876
/* 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
 
LVL 11

Expert Comment

by:alexo
ID: 1256877
how about closing the question then?

0
 

Author Comment

by:swintmicro
ID: 1256878
no one has submitted an "answer" yet
0
 
LVL 84

Accepted Solution

by:
ozo earned 200 total points
ID: 1256879
Do you still want an answer?  It sounded like you were saying the answer came too late.

0
 

Author Comment

by:swintmicro
ID: 1256880
No, your answer helped alot
0

Featured Post

IT, Stop Being Called Into Every Meeting

Highfive is so simple that setting up every meeting room takes just minutes and every employee will be able to start or join a call from any room with ease. Never be called into a meeting just to get it started again. This is how video conferencing should work!

Join & Write a Comment

Summary: This tutorial covers some basics of pointer, pointer arithmetic and function pointer. What is a pointer: A pointer is a variable which holds an address. This address might be address of another variable/address of devices/address of fu…
Windows programmers of the C/C++ variety, how many of you realise that since Window 9x Microsoft has been lying to you about what constitutes Unicode (http://en.wikipedia.org/wiki/Unicode)? They will have you believe that Unicode requires you to use…
The goal of this video is to provide viewers with basic examples to understand and use pointers in the C programming language.
The goal of this video is to provide viewers with basic examples to understand opening and writing to files in the C programming language.

758 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

17 Experts available now in Live!

Get 1:1 Help Now