Solved

C program to determine tomorrows date.

Posted on 1997-12-31
15
685 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
Master Your Team's Linux and Cloud Stack!

The average business loses $13.5M per year to ineffective training (per 1,000 employees). Keep ahead of the competition and combine in-person quality with online cost and flexibility by training with Linux Academy.

 

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

Windows Server 2016: All you need to know

Learn about Hyper-V features that increase functionality and usability of Microsoft Windows Server 2016. Also, throughout this eBook, you’ll find some basic PowerShell examples that will help you leverage the scripts in your environments!

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
how to communicate between two pc's through vb.net code ? 4 153
port mapped I/o query 10 186
Why this code doesn't work? 8 101
TCP/IP Socket - connection close results in data lost 14 134
This tutorial is posted by Aaron Wojnowski, administrator at SDKExpert.net.  To view more iPhone tutorials, visit www.sdkexpert.net. This is a very simple tutorial on finding the user's current location easily. In this tutorial, you will learn ho…
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…
Video by: Grant
The goal of this video is to provide viewers with basic examples to understand and use while-loops in the C programming language.
The goal of this video is to provide viewers with basic examples to understand how to create, access, and change arrays in the C programming language.

772 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