6_11:strftime 打印系统时间

来源:互联网 发布:mac能装搜狗输入法吗 编辑:程序博客网 时间:2024/05/16 06:14

一.源代码:

<bldc:/home/tingbinz/apue.3e/SBSCODE/6>R*_*G:cat -n 6_11.c

     1  #include <stdio.h>
     2  #include <stdlib.h>
     3  #include <time.h>
     4
     5  int main()
     6  {
     7          time_t t;
     8          struct tm *tmp;
     9          char buf1[16];
    10          char buf2[64];
    11
    12          time(&t);
    13          tmp = localtime(&t);
    14
    15          if (strftime(buf1,16,"time and date: %r, %a %b %d, %Y", tmp) == 0)
    16                  printf("buffer length 16 is too small\n");
    17          else
    18                  printf("%s\n",buf1);
    19          if (strftime(buf2,64,"time and date: %r, %a %b %d, %Y", tmp) == 0)
    20                  printf("buffer length 64 is too small\n");
    21          else
    22                  printf("%s\n",buf2);
    23
    24          exit(0);

    25  }


二、运行结果:

<bldc:/home/tingbinz/apue.3e/SBSCODE/6>R*_*G:./what_is_the_time
buffer length 16 is too small
time and date: 12:03:11 AM, Wed Sep 30, 2015

0 0