Solved

system dates

Posted on 2000-04-20
4
223 Views
Last Modified: 2010-04-15
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...
0
Comment
Question by:abeveridge
4 Comments
 
LVL 1

Expert Comment

by:MT_MU
ID: 2736742
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 ) );

}
0
 

Accepted Solution

by:
kishore_joshi earned 75 total points
ID: 2737061
check out this simple example to get the system date and time.....


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

int main(void)
{
  time_t t;

  t = time(NULL);

  printf("Current date is %s", ctime(&t));

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

  stime(&t);
  printf("\nNew date is %s", ctime(&t));

  return 0;
}
0
 
LVL 84

Expert Comment

by:ozo
ID: 2737073
/* Back up to same time previous day */
Assuming no daylight savings switch
0
 

Expert Comment

by:Dunbuje
ID: 2825050
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;
}

0

Featured Post

Master Your Team's Linux and Cloud Stack

Come see why top tech companies like Mailchimp and Media Temple use Linux Academy to build their employee training programs.

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 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…
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 and use switch statements in the C programming language.

856 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