[Webinar] Streamline your web hosting managementRegister Today

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 876
  • Last Modified:

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.
0
swintmicro
Asked:
swintmicro
  • 5
  • 5
  • 2
  • +3
1 Solution
 
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 new generation of project management tools

With monday.com’s project management tool, you can see what everyone on your team is working in a single glance. Its intuitive dashboards are customizable, so you can create systems that work for you.

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

Featured Post

Managing Security & Risk at the Speed of Business

Gartner Research VP, Neil McDonald & AlgoSec CTO, Prof. Avishai Wool, discuss the business-driven approach to automated security policy management, its benefits and how to align security policy management with business processes to address today's security challenges.

  • 5
  • 5
  • 2
  • +3
Tackle projects and never again get stuck behind a technical roadblock.
Join Now