代码之家  ›  专栏  ›  技术社区  ›  adk

用于分析日期时间的C库[关闭]

  •  6
  • adk  · 技术社区  · 15 年前

    是否知道C的日期解析函数。我正在查找类似以下内容的函数:

    time = parse_time("9/10/2009");
    printf("%d\n", time->date);
    time2 = parse_time("Monday September 10th 2009")    
    time2 = parse_time("Monday September 10th 2009 12:30 AM")
    

    谢谢你

    9 回复  |  直到 8 年前
        1
  •  2
  •   Alex Martelli    15 年前

    这个 Julian Library 做了很多你问的事情——特别是看 parsing 作品。不过,我认为这并不完全符合您的要求(即 Monday 我相信,会把它扔出去转一圈;-)。

        2
  •  11
  •   James Antill    15 年前

    在C语言中有两种相当常见的方法:

    1. 将strptime()与您接受的受支持格式数组一起使用。

    2. 经常用头撞击表,然后放弃或者使用已经有可用库的另一种语言(如Perl或Python)。

        3
  •  5
  •   Shiplu Mokaddim    13 年前

    如果格式一致,则可以使用 scanf 家庭职能

    #include<stdio.h>
    
    int main()
    {
        char *data = "Tue, 13 Dec 2011 16:08:21 GMT";
        int h, m, s, d, Y;
        char M[4];
        sscanf(data, "%*[a-zA-Z,] %d %s %d %d:%d%:%d", &d, M, &Y, &h, &m, &s);
        return 0;
    }
    
        4
  •  3
  •   Jonathan Leffler    8 年前

    不幸的是,标准库中唯一的东西是 getdate() 由POSIX定义,而不是C标准。它将处理许多时间格式,但您需要事先知道格式,而不仅仅是向函数传递一个通用字符串。

    它也不支持VisualC++,如果这对你来说是个问题。然而,GNUCruntime支持这个例程。

        5
  •  2
  •   Mark Lodato    14 年前

    Git有一个便携式的 date parsing library ,根据GPLV2发布。你可以用它。我想你想要 approxidate_careful() .

        6
  •  1
  •   Andrew Stone    11 年前

    我参加聚会有点晚了,但根据马克·洛达托的帖子,我把这个放在了一起: git's approxidate in library form .在Linux/Mac上测试。

        7
  •  0
  •   blak3r    15 年前

    及时。你有史崔蒂姆:

    // Scan values from buf string into tptr struct. On success it returns pointer
    // to the character following the last character parsed. Otherwise it returns null.
    char * strptime(const char* buf, const char* format, struct tm* tptr)
    

    与之相反的是

    // Format tm into a date/time string.
    size t strftime(char* s, size t n, const char* format, const struct tm* tptr)
    

    Click here for the complete Reference 论维基百科

        8
  •  0
  •   Felix Dombek    9 年前

    在窗户里,有 VarDateFromStr 如果这样使用,它可以自动解析许多格式:

    LPCWSTR dateString = L"
    DATE result;
    HRESULT hr = ::VarDateFromStr(dateString,
                                  LOCALE_ALL,
                                  0,
                                  &result);
    
    if (FAILED(hr))
    {
        /* handle error */
        /* DISP_E_TYPEMISMATCH means that it didn't recognize the format. */
    }
    

    它通常可以识别数字格式,但也可以解析“2009年9月10日上午12:30”,无需 星期一 在我的德国电脑上没有 ,但这可能取决于区域设置。这些词必须是本地语言,例如,英语系统需要“june”,德语系统需要“juni”。

        9
  •  -1
  •   Jani    9 年前

    这个 notmuch mail project 具有用于日期字符串的gplv2+分析器。它支持各种用户友好格式的绝对和相对日期,尽管相对日期仅指过去。代码在 parse-time-string subdirectory of the notmuch source tree .