【问题标题】:How to get current time in C using gettime()?如何使用 gettime() 在 C 中获取当前时间?
【发布时间】:2017-08-28 07:43:42
【问题描述】:

我们需要使用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;
}

【问题讨论】:

  • 嗯,gettime 是 Javascript 的东西,而不是 C 的东西。如果有人或某事告诉您在您的特定系统或平台上有一个名为 gettime 的 C 函数,请询问他们如何使用它。
  • gettime 既不是标准 C 也不是 posix,您需要知道要包含的标头以及要链接的库(如果需要)。
  • 此外,struct time t; 不是标准 C,因为有 no time 结构,而是 tm struct
  • IMO 的主要问题是要求使用旧 DOS 时代的旧、过时和过时的功能。我希望学校一般停止使用旧的 Turbo C 和 Turbo C++ 和 DOS,而改为教授现代 C 和 C++。甚至可能是这些语言的旧标准版本。
  • @JoseG。 Turbo C 编译器使用它自己的库来支持这些函数。但它不是当前 C 标准的一部分。结果,它的代码块(我假设使用 gcc 作为编译器)不支持这些库函数。如果您的讲师要求您仅使用这些功能,您可以继续使用 Turbo C。但如果您希望使用标准 C,您可以查看time。该链接还包含有关如何使用它的示例。这将适用于代码块。

标签: c timestamp gettime


【解决方案1】:

不清楚是要获取时间,还是直接打印出来。

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

更灵活的选择是基于 time/localtime_r 的数据使用strftime。 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) ;
}

【讨论】:

    【解决方案2】:

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

    示例取自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;
    }
    

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

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

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

    【讨论】:

      【解决方案3】:

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

      结构时间tm;

      完成此操作后,您可以使用库中的 gettime() 函数来将 que 值保存在我们可以访问的地方:

      gettime(&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;
      }
      

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 2012-05-09
        • 2013-12-25
        • 1970-01-01
        • 2014-03-12
        • 2016-07-30
        • 1970-01-01
        • 2017-08-02
        相关资源
        最近更新 更多