Link to home
Start Free TrialLog in
Avatar of abeveridge
abeveridge

asked on

system dates

I am writting a C program and I need to display the current system date.  What is the syntax, what header file do I need etc...
Avatar of MT_MU
MT_MU

One way...

#include <time.h>
#include <stdio.h>
#include <sys/types.h>
#include <sys/timeb.h>
#include <string.h>

void main()
{
  time_t ltime;

  time( &ltime );
  printf( "Time in seconds since UTC  1/1/70:\t%ld\n", ltime );
  printf( "time and date:\t\t\t%s", ctime( &ltime ) );

}
ASKER CERTIFIED SOLUTION
Avatar of kishore_joshi
kishore_joshi

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of ozo
/* Back up to same time previous day */
Assuming no daylight savings switch
As Ozo and Kishori Joshi put it, the answer of your question can be put in this way. And you can even see the output of this program from the output file.


 #include <stdio.h>
 #include <time.h>
 #include <stdlib.h>
 #include <string.h>

long int        time();
char           *ctime();
  FILE *fp, *fopen();
int main(void)
{

  time_t clock;

  clock = time(NULL);
   printf("\n\n");
        fp = fopen("goandsee", "w");
        if((fp = fopen("goandsee", "w")) == NULL){
        printf("Error for writing output file.\n");
        exit(1);
               }

  fprintf(fp, "Current date is %s", ctime(&clock));

  clock -= 24L*60L*60L;  /* Back up to same time previous day */

  stime(&clock);
  fprintf(fp, "\n New date is %s", ctime(&clock));

  return 0;
}