strptime

STRPTIME(3)                                                                                            Linux Programmer's Manual                                                                                           STRPTIME(3)



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.  Here
       format is a character string that consists of field descriptors and text characters, reminiscent of scanf(3).  Each field descriptor consists of a % character followed by another character that specifies the replacement for
       the field descriptor.  All other characters in the format string must have a matching character in the input string, except for whitespace, which matches zero or more whitespace characters in the input string.  There should
       be whitespace or other alphanumeric characters between any two field descriptors.

       The strptime() function processes the input string from left to right.  Each of the three possible input elements (whitespace, literal, or format) are handled one after the other.  If the input cannot be matched to the for‐
       mat string the function stops.  The remainder of the format and input strings are not processed.

       The  supported  input  field descriptors are listed below.  In case a text string (such as a weekday or month name) is to be matched, the comparison is case insensitive.  In case a number is to be matched, leading zeros are
       permitted but not required.

       %%     The % character.

       %a or %A
              The weekday name according to the current locale, in abbreviated form or the full name.

       %b or %B or %h
              The month name according to the current locale, in abbreviated form or the full name.

       %c     The date and time representation for the current locale.

       %C     The century number (0-99).

       %d or %e
              The day of month (1-31).

       %D     Equivalent to %m/%d/%y.  (This is the American style date, very confusing to non-Americans, especially since %d/%m/%y is widely used in Europe.  The ISO 8601 standard format is %Y-%m-%d.)

       %H     The hour (0-23).

       %I     The hour on a 12-hour clock (1-12).

       %j     The day number in the year (1-366).

       %m     The month number (1-12).
       

使用前要加强宏定义和头文件
#define _XOPEN_SOURCE /* See feature_test_macros(7) */
#include <time.h>

#ifndef  _XOPEN_SOURCE
#define  _XOPEN_SOURCE
#endif // ! _XOPEN_SOURCE


#include <iostream>
#include <time.h>
using namespace  std;
time_t timeTest(const char* timeData,const char* format)
{
    time_t tt;
    strptime(timeData, &tt);
    return std::mktime(&tt);
}


int main()
{
    auto t=timeTest("20200429", "%y%m%D");
    cout << "time str 20200429 is " << t << endl;   
    return 0;
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值