0

我们需要使用gettime()C 来获取当前时间。我正在尝试打印当前时间,但出现错误:

错误:“t”的存储大小未知

发生。我不知道如何解决这个问题。这是代码:

#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;
}
4

3 回答 3

3

目前尚不清楚您是否想获得时间,或者只是打印它。

对于第二种情况,很少有遗留方法可以提供格式化时间 ( asctime, ctime)。但这些可能不符合您的需求。

更灵活的选项是strftime基于 time/localtime_r 的数据使用。strftime 支持 GNU 日期可用的许多转义符(%Y,...)。

#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) ;
}
于 2019-11-20T12:17:52.300 回答
1

获取本地时间的标准 C 函数很简单time,包含在头文件 time.h 中

示例取自这里

/* 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;
}

有关不同时间格式的更多信息:

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

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

于 2017-08-28T11:40:11.433 回答
0

好的,您正在尝试使用 DOS.H 库,因此 time.h 和 ctime 库不适用于此特定问题,因为 DOS.H 是专有 C 库,您不能在任何其他 C(C++ 或C#),请在阅读完这篇文章后阅读库,这样你就可以清楚地看到我在说什么,所以在 DOS.H 库中有一个 STRUCT,用于保存所有时间变量,这是小时、分钟、秒,所以我们要做的第一件事就是声明一个允许我们保存此类数据的变量:

结构时间 tm;

一旦你这样做了,你就可以使用库中的 gettime() 函数,这可以将 que 值保存在我们可以访问的地方:

获取时间(&tm);

最后打印 o 使用这些数据随心所欲地执行您需要获取结构的每个寄存器的操作:

printf("系统时间为: %d : %d : %d\n",tm.ti_hour, tm.ti_min, tm.ti_sec);

检查此代码:

#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;
}
于 2020-04-27T04:49:49.283 回答