8

Is one aware of a date parsing function for c. I am looking for something like:

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")

Thank you

1
  • As mentioned by blak3r in a comment, your specification is ambiguous. In the first example, the date can be parsed as 10th september or as 9th october, depending on the country you are in. You have to specify a format.
    – bortzmeyer
    Commented Sep 13, 2009 at 17:03

9 Answers 9

12

There are two fairly common approaches in C:

  1. Use strptime() with an array of supported formats you accept.

  2. Bang head against table a lot, and then either give up or use another language which has a usable library already (like perl or python).

6

If the format is consistent you can use scanf family functions

#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;
}
3

Unfortunately, the only thing in the standard library is getdate(), defined by POSIX, not the C standard. It will handle many time formats, but you need to know the format in advance — not just pass a generic string to the function.

It's also not supported on Visual C++, if that's an issue for you. The GNU C runtime supports this routine, however.

1
  • is there anything that is not in the standard and poratble?
    – adk
    Commented Sep 12, 2009 at 3:29
2

The Julian Library does much of what you ask -- see in particular how its parsing works. However I don't think it quite stretches ALL the way to your requirements (that Monday, I believe, would throw it for a spin;-).

2

Git has a portable date parsing library, released under GPLv2. You may be able to use that. I think you want approxidate_careful().

1

I'm a bit late to the party, but building on what Mark Lodato posted, I threw this together: git's approxidate in library form. Tested on Linux/Mac.

0

In time.h you have strptime:

// 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)

which does the opposite of

// 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 on Wikipedia

3
  • What if I do not know the format?
    – adk
    Commented Sep 12, 2009 at 3:44
  • 1
    @adk hmm... well you need to give some instruction in order for it to know whether your feeding it a month versus a day or year... ya know? But, you could for a given input string try to parse it with multiple formats and keep trying it until the function doesn't return null.
    – blak3r
    Commented Sep 12, 2009 at 3:48
  • @Mitch Wheat - dunno about VC++ did you check ctime?
    – blak3r
    Commented Sep 12, 2009 at 3:54
0

In Windows, there is VarDateFromStr which can automatically parse many formats if used like this:

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. */
}

It will generally recognize numeric formats, but can also parse "September 10 2009 12:30 AM", without Monday and on my German computer without th, but that might be locale-dependent. The words must be in the local language, for example it will need "June" on English systems but "Juni" on German systems.

0

The notmuch mail project has a GPLv2+ parser for date strings. It supports absolute and relative dates in a variety of user friendly formats, although relative dates only refer to the past. The code is in the parse-time-string subdirectory of the notmuch source tree.

2
  • While this link may answer the question, it is better to include the essential parts of the answer here and provide the link for reference. Link-only answers can become invalid if the linked page changes. - From Review Commented Dec 29, 2015 at 17:14
  • @djhaskin987 Improved the answer slightly, but there isn't much more essential information that could be added beyond pointing at the definitive source of the code. How is this possibly worse than the two upvoted answers pointing at git's date parser?
    – Jani
    Commented Dec 29, 2015 at 17:56

Not the answer you're looking for? Browse other questions tagged or ask your own question.