【问题标题】:C++: how to convert date string to intC++:如何将日期字符串转换为 int
【发布时间】:2011-04-22 09:28:15
【问题描述】:

您好,我需要一个将字符串时间转换为 int 的程序。在unix下支持所有这些格式:

 "Wed, 09 Feb 1994 22:23:32 GMT"       -- HTTP format
 "Thu Feb  3 17:03:55 GMT 1994"        -- ctime(3) format
 "Thu Feb  3 00:00:00 1994",           -- ANSI C asctime() format
 "Tuesday, 08-Feb-94 14:15:29 GMT"     -- old rfc850 HTTP format
 "Tuesday, 08-Feb-1994 14:15:29 GMT"   -- broken rfc850 HTTP format

 "03/Feb/1994:17:03:55 -0700"   -- common logfile format
 "09 Feb 1994 22:23:32 GMT"     -- HTTP format (no weekday)
 "08-Feb-94 14:15:29 GMT"       -- rfc850 format (no weekday)
 "08-Feb-1994 14:15:29 GMT"     -- broken rfc850 format (no weekday)

 "1994-02-03 14:15:29 -0100"    -- ISO 8601 format
 "1994-02-03 14:15:29"          -- zone is optional
 "1994-02-03"                   -- only date
 "1994-02-03T14:15:29"          -- Use T as separator
 "19940203T141529Z"             -- ISO 8601 compact format
 "19940203"                     -- only date

 "08-Feb-94"         -- old rfc850 HTTP format    (no weekday, no time)
 "08-Feb-1994"       -- broken rfc850 HTTP format (no weekday, no time)
 "09 Feb 1994"       -- proposed new HTTP format  (no weekday, no time)
 "03/Feb/1994"       -- common logfile format     (no time, no offset)

 "Feb  3  1994"      -- Unix 'ls -l' format
 "Feb  3 17:03"      -- Unix 'ls -l' format

 "11-15-96  03:52PM" -- Windows 'dir' format 

你能不能给点建议。

【问题讨论】:

    标签: c++ unix time


    【解决方案1】:

    试试time_get facet 类。公共职能

    get_time
    get_date
    get_weekday
    get_monthname
    get_year 
    

    返回您的日期数据并检查您要显示的日期格式,您可以相应地以该格式显示日期。

    【讨论】:

      【解决方案2】:

      我不知道有任何非增强解决方案。您可能不得不依靠istringstream 并自己动手。应该不会太难,只要确保为解析器准备好一组测试用例即可。

      【讨论】:

        【解决方案3】:

        我会仔细看看Boost.Date_TimeDate-time input/output 看起来像你想要的。

        【讨论】:

        • 哦,很遗憾我不能在我的项目中使用 boost。只是 C++ 和 stl
        • @Nikita:在这种情况下,您能否更详细地概述您的限制?这里没有人会为您编写例程,我们关于如何编写代码的最佳建议不会比您可以做的更好,所以最好的办法是推荐第三方库。如果您不能使用具有极无限制许可的免费第三方库,您可以使用什么?
        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 2023-04-07
        • 2011-04-29
        • 1970-01-01
        • 2022-07-06
        • 1970-01-01
        • 1970-01-01
        • 2020-05-24
        相关资源
        最近更新 更多