可以将文章内容翻译成中文,广告屏蔽插件可能会导致该功能失效(如失效,请关闭广告屏蔽插件后再试):
问题:
Want to improve this question? Update the question so it's on-topic for Stack Overflow.
Closed 4 years ago.
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:
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:
There are two fairly common approaches in C:
Use strptime() with an array of supported formats you accept.
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).
回答3:
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;
}
回答4:
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.
回答5:
Git has a portable date parsing library, released under GPLv2. You may be able to use that. I think you want approxidate_careful()
.
回答6:
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.
回答7:
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
回答8:
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.
回答9:
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.