好用的strftime时间转换

上一篇 / 下一篇  2009-08-26 13:31:50

摘自man手册

SYNOPSIS
       #include <time.h>

       size_t strftime(char *s, size_t max, const char *format,
                           const struct tm *tm);

DESCRIPTION
       The  strftime()  function  formats  the  broken-down  time tm according to the format specification format and places the
       result in the character array s of size max.

Example

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

       int
       main(int argc, char *argv[])
       {
           char outstr[200];
           time_t t;
           struct tm *tmp;

           t = time(NULL);
           tmp = localtime(&t);
           if (tmp == NULL) {
               perror("localtime");
               exit(EXIT_FAILURE);
           }

           if (strftime(outstr, sizeof(outstr), argv[1], tmp) == 0) {
               fprintf(stderr, "strftime returned 0");
               exit(EXIT_FAILURE);
           }

           printf("Result string is \"%s\"\n", outstr);
           exit(EXIT_SUCCESS);
       }


TAG:

 

评分:0

我来说两句

日历

« 2024-03-24  
     12
3456789
10111213141516
17181920212223
24252627282930
31      

数据统计

  • 访问量: 10506
  • 日志数: 3
  • 建立时间: 2009-08-21
  • 更新时间: 2009-10-26

RSS订阅

Open Toolbar