Still celebrating National IT Professionals Day with 3 months of free Premium Membership. Use Code ITDAY17

x
?
Solved

C program to determine tomorrows date.

Posted on 1997-12-31
15
Medium Priority
?
811 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 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
Technology Partners: 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!

 

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

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!

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

An Outlet in Cocoa is a persistent reference to a GUI control; it connects a property (a variable) to a control.  For example, it is common to create an Outlet for the text field GUI control and change the text that appears in this field via that Ou…
This is a short and sweet, but (hopefully) to the point article. There seems to be some fundamental misunderstanding about the function prototype for the "main" function in C and C++, more specifically what type this function should return. I see so…
The goal of this video is to provide viewers with basic examples to understand opening and writing to files in the C programming language.
Video by: Grant
The goal of this video is to provide viewers with basic examples to understand and use nested-loops in the C programming language.

670 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