📜  C中的time.h头文件以及示例

📅  最后修改于: 2021-05-28 05:19:50             🧑  作者: Mango

time.h头文件包含用于获取和操作日期和时间信息的函数的定义。

  • 它描述了三种与时间相关的数据类型
    1. clock_t :clock_t将日期表示为整数,这是日历时间的一部分。
    2. time_t :time_t以整数形式表示时钟时间,它是日历时间的一部分。
    3. struct tm :struct tm保存日期和时间,其中包含:
      struct tm {
          // seconds,  range 0 to 59
          int tm_sec;
        
          // minutes, range 0 to 59
          int tm_min;
        
          // hours, range 0 to 23
          int tm_hour;
        
          // day of the month, range 1 to 31
          int tm_mday;
        
          // month, range 0 to 11
          int tm_mon;
        
          // The number of years since 1900
          int tm_year;
        
          // day of the week, range 0 to 6
          int tm_wday;
        
          // day in the year, range 0 to 365
          int tm_yday;
        
          // daylight saving time
          int tm_isdst;
      }
      
  • 它还包含CLOCKS_PER_SEC宏,该宏保存系统时钟每秒滴答的次数。
  • time.h中的预定义函数
    S.No Function Name Explanation
    1. asctime() This function returns the date and time in the format
    day month date hours:minutes:seconds year.
    Eg: Sat Jul 27 11:26:03 2019.
    asctime() function returns a string by taking struct tm variable as a parameter.
    2. clock() This function returns the processor time consumed by a program
    3. ctime() This function returns the date and time in the format
    day month hours:minutes:seconds year
    Eg: Sat Jul 27 11:26:03 2019
    time is printed based on the pointer returned by Calendar Time
    4. difftime() This function returns the difference between the times provided.
    5. gmtime() This function prints the UTC (Coordinated Universal Time) Time and date.
    Format for both gmtime() and asctime() is same
    6. mktime() This function returns the calendar-time equivalent using struct tm.
    7. time() This function returns the calendar-time equivalent using data-type time_t.
    8. strftime() This function helps to format the string returned by other time functions using different format specifiers
  • 例子:
    1. 程序以打印系统的日期和时间。
      #include 
      #include 
      int main(void)
      {
          struct tm* ptr;
          time_t lt;
          lt = time(NULL);
          ptr = localtime(<);
          printf("%s", asctime(ptr));
          return 0;
      }
      
      输出:
      Tue Aug  6 09:00:29 2019
      
    2. 程序打印系统的UTC(世界标准时间)。
      #include 
      #include 
      int main(void)
      {
          struct tm* ptr;
          time_t lt;
          lt = time(NULL);
          ptr = gmtime(<);
          printf("%s", asctime(ptr));
          return 0;
      }
      
      输出:
      Tue Aug  6 09:00:31 2019
      
    3. 程序计算将两个数字相加所花费的时间。
      注意:如果用户缓慢输入,则该时间也将总计执行时间。
      #include 
      #include 
      int main(void)
      {
          time_t start, end;
          start = time(NULL);
          int a, b;
          scanf("%d %d", &a, &b);
          printf("Sum of %d and %d is %d\n",
                 a, b, a + b);
          end = time(NULL);
          printf("Time taken to print sum is %.2f seconds",
                 difftime(end, start));
      }
      
      输出:
      Sum of 4196144 and 0 is 4196144
      Time taken to print sum is 0.00 seconds
      
    4. 程序来查找时钟滴答声。
      #include 
      #include 
      #include 
        
      int frequency_of_primes(int n)
      {
          // This function checks the number of
          // primes less than the given parameter
          int i, j;
          int freq = n - 1;
          for (i = 2; i <= n; ++i)
              for (j = sqrt(i); j > 1; --j)
                  if (i % j == 0) {
                      --freq;
                      break;
                  }
          return freq;
      }
        
      int main()
      {
          clock_t t;
          int f;
          t = clock();
          f = frequency_of_primes(9999);
          printf("The number of primes lower"
                 " than 10, 000 is: %d\n",
                 f);
          t = clock() - t;
          printf("No. of clicks %ld clicks (%f seconds).\n",
                 t, ((float)t) / CLOCKS_PER_SEC);
          return 0;
      }
      
      输出:
      The number of primes lower than 10, 000 is: 1229
      No. of clicks 2837 clicks (0.002837 seconds).
      
    5. 程序将时间打印为小时:由asctime()文件返回的分钟。
      #include 
      #include 
      int main()
      {
          time_t rawtime;
          struct tm* timeinfo;
        
          // Used to store the time
          // returned by localetime() function
          char buffer[80];
        
          time(&rawtime);
          timeinfo = localtime(&rawtime);
          strftime(buffer, 80,
                   "Time is %I:%M%p.",
                   timeinfo);
        
          // strftime() function stores the
          // current time as Hours : Minutes
          //%I %M and %p-> format specifier
          // of Hours minutes and am/pm respectively*/
        
          // prints the formatted time
          puts(buffer);
        
          return 0;
      }
      
      输出:
      Time is 09:00AM.
      

想要从精选的最佳视频中学习和练习问题,请查看《基础知识到高级C的C基础课程》。