How to get current time in C using gettime()?

22.6k views Asked by At

We're required to use gettime() to get the current time in C. I'm trying to print the current time but the error:

error: storage size of 't' isn't known

occurs. I don't know how to solve this. Here is the code:

#include<stdio.h>
#include<dos.h>

int main(){

   struct time t;

   gettime(&t);

   printf("%d:%d:%d", t.ti_hour,t.ti_min,t.ti_sec);

   getch();
   return 0;
}
3

There are 3 answers

0
Clonk On

The standard C function to get the local time is simply time, included in the header time.h

Example taken from here.

/* time example */
#include <stdio.h>      /* printf */
#include <time.h>       /* time_t, struct tm, difftime, time, mktime */

int main ()
{
  time_t timer;
  struct tm y2k = {0};
  double seconds;

  y2k.tm_hour = 0;   y2k.tm_min = 0; y2k.tm_sec = 0;
  y2k.tm_year = 100; y2k.tm_mon = 0; y2k.tm_mday = 1;

  time(&timer);  /* get current time; same as: timer = time(NULL)  */

  seconds = difftime(timer,mktime(&y2k));

  printf ("%.f seconds since January 1, 2000 in the current timezone", seconds);

  return 0;
}

More info on the different time format :

http://www.cplusplus.com/reference/ctime/mktime/

http://www.cplusplus.com/reference/ctime/localtime/

0
user13415071 On

Ok, you're trying to use the DOS.H library, so the time.h and ctime library does not apply to this particular issue due to DOS.H is an exclusive C library you cannot use it in any other C (C++ or C#), please read the library after you read this post so you can see clearly what am i talking about, so within the DOS.H library is an STRUCT that you use for save all the variables of time, this is hour, minutes, seconds, so the first thing that we have to do is to declare a variable that allow us save this type of data:

struct time tm;

Once you do that you can use the gettime() function wich is on the library to, this to save que values in a place where we can get access to:

gettime(&tm);

And finally to print o do wherever you want with this data you need to get each register of the structure:

printf("System time is: %d : %d : %d\n",tm.ti_hour, tm.ti_min, tm.ti_sec);

Check this code:

#include<stdio.h>
#include<dos.h>
#include<conio.h>

int main()
{
struct date fecha;
struct time hora;
union REGS regs;

getdate(&fecha);
printf("La fecha del sistema es: %d / %d / %d\n",fecha.da_day,fecha.da_mon,fecha.da_year);

regs.x.cx = 0x004c;
regs.x.dx = 0x4b40;
regs.h.ah = 0x86; /* 004c4b40h = 5000000 microsegundos */

int86(0x15,&regs,&regs); /* Interrupcion 15h suspension de sistema  */

gettime(&hora);
printf("la hora del sistema es: %d : %d : %d\n",hora.ti_hour,hora.ti_min,hora.ti_sec);

getche();
clrscr();
return 0;
}
0
dash-o On

It's not clear if you want to get the time, or just print it.

For the second case, there are few legacy methods that will provide formatted time (asctime, ctime). But those may not fit your needs.

The more flexible option is to use strftime based on data from time/localtime_r. The strftime support many of the escapes (%Y, ...) that are available with GNU date.

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

void main(void)
{

   time_t now = time(NULL) ;
   struct tm tm_now ;
   localtime_r(&now, &tm_now) ;
   char buff[100] ;
   strftime(buff, sizeof(buff), "%Y-%m-%d, time is %H:%M", &tm_now) ;
   printf("Time is '%s'\n", buff) ;
}