• linux C用strftime strptime将字符串与struct tm互转


    参考摘自:https://bbs.csdn.net/topics/390674012 

    linux C中

    strftime将struct tm转换为字符串:

    NAME
           strftime - format date and time
    
    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.
    
    RETURN VALUE
           Provided that the result string, including the terminating null byte, does not exceed max bytes, strfu2010
           time() returns the number of bytes (excluding the terminating null byte) placed in the  array  s.   If
           the  length  of  the  result string (including the terminating null byte) would exceed max bytes, then
           strftime() returns 0, and the contents of the array are undefined.  (This behavior  applies  since  at
           least libc 4.4.4; very old versions of libc, such as libc 4.4.1, would return max if the array was too
           small.)
    
           Note that the return value 0 does not necessarily indicate an error.  For example, in many locales  %p
           yields an empty string.  An empty format string will likewise yield an empty string.
    
    ATTRIBUTES
           For an explanation of the terms used in this section, see attributes(7).

      

    strptime将字符串转换为struct tm;

    NAME
           strptime - convert a string representation of time to a time tm structure
    
    SYNOPSIS
           #define _XOPEN_SOURCE       /* See feature_test_macros(7) */
           #include <time.h>
    
           char *strptime(const char *s, const char *format, struct tm *tm);
    
    DESCRIPTION
           The  strptime()  function  is  the  converse function to strftime(3) and converts the character string
           pointed to by s to values which are stored in the tm structure pointed to  by  tm,  using  the  format
           specified  by  format.  
    
    RETURN VALUE
           The return value of the function is a pointer to the first character not processed  in  this  function
           call.  In case the input string contains more characters than required by the format string the return
           value points right after the last consumed input character.  In case the whole input  string  is  conu2010
           sumed the return value points to the null byte at the end of the string.  If strptime() fails to match
           all of the format string and therefore an error occurred the function returns NULL.
    
    ATTRIBUTES
           For an explanation of the terms used in this section, see attributes(7).

    实例1:strftime()

     1 #include <time.h>
     2 #include <stdio.h>
     3 #include <string.h>
     4 #include <stdlib.h>
     5 #include "time.h"
     6 
     7 static char local_time[32] = {0};
     8 
     9 char * my_get_time(void)
    10 {
    11         time_t t_time;
    12         struct tm *t_tm;
    13 
    14         t_time = time(NULL);
    15         t_tm = localtime(&t_time);
    16         if (t_tm == NULL)
    17                 return NULL;
    18 
    19         //if(strftime(local_time, sizeof(local_time), "%FT%T%z", t_tm) == 0) // 2020-01-08T09:28:54+0800
    20         //if(strftime(local_time, sizeof(local_time), "%FT%T", t_tm) == 0)   // 2020-01-08T09:30:27
    21         //if(strftime(local_time, sizeof(local_time), "%F", t_tm) == 0)      // 2020-01-08
    22         if(strftime(local_time, sizeof(local_time), "%T", t_tm) == 0)        // 09:32:40
    23                 return NULL;
    24     
    25         return local_time;
    26 }
    27 
    28 int main(int argc, char *argv[])
    29 {
    30         printf("%s
    ", my_get_time());
    31 
    32         return 0;
    33 }

    实例2:strptime()

    如下代码我是求两个时间的秒数

     1 #include <stdio.h>
     2 #include <stdlib.h>
     3 #include <string.h>
     4 #include <time.h>
     5 
     6 int main(int argc, char *argv[])
     7 {
     8         //char fmt[] = "%Y-%m-%d %H:%M:%S";
     9         char fmt[] = "%F %T";
    10         time_t ggtime,curtime;
    11         struct tm *tb = (struct tm*)malloc(sizeof(struct tm));
    12 
    13         strptime(argv[1], fmt, tb);
    14         ggtime = mktime(tb);
    15 
    16         curtime = time(NULL);
    17         printf("The pause used %f seconds.
    ",difftime(curtime,ggtime));
    18 
    19         return 0;
    20 }
    运行:
    [root@localhost 05]# ./main '2020-1-8 10:00:02' The pause used 25.000000 seconds.
  • 相关阅读:
    Mysql模糊查询 select count(*) from sys_invitation where from_id like '%1006%';
    java 结束程序进程 代码
    [解决问题]selenium.remote.UnreachableBrowserException 异常分析并解决问题
    【解决问题】failed: java.lang.RuntimeException: org.openqa.selenium.WebDriverException: Unexpected error launching Internet Explorer.
    linux下nginx部署以及配置详解
    Linux系统下安装jdk及环境配置(两种方法)
    linux服务器同时运行两个或多个tomcat
    linux配置环境jdk
    mysql修改后启动my.cnf报错Starting MySQL... ERROR! The server quit without updating PID file (/var/lib/mysql/localhost.localdomain.pid).
    mysql执行出错:Table 'k_user' is read only
  • 原文地址:https://www.cnblogs.com/LiuYanYGZ/p/14250018.html
Copyright © 2020-2023  润新知