问题描述
我在寻找一个普通的C对口 <一个href=\"http://$c$c.google.com/p/datejs/wiki/APIDocumentation#parse\"><$c$c>date.parse()$c$c>.
I'm looking for a plain C counterpart for date.js date.parse()
.
这就是东西一周前或昨天作为输入的理解。英语只是确定。
That is, something that understands "week ago" or "yesterday" as input. English-only is OK.
的注:图书馆不应根据GPL许可,所以Git的 date.c
或解析器为GNU 日期-d
不会做。顺便说一句,如果你想知道我为什么不坐下来和code这一点,去看看提到库的源... 的
Note: a library should not be licensed under GPL, so Git's date.c
or parser for GNU date -d
wouldn't do. BTW, if you wonder why wouldn't I just sit down and code this, go and look at the source of mentioned libraries...
推荐答案
下面的解决方案是不是你问什么的,但我希望,尽管不是一个普通的C回答将覆盖您的需求。重新发明轮子是不是很长的路要走让我们使用date.js用C用的SpiderMonkey,Mozilla的JavaScript引擎运行它。
The following solution is not exactly what you've asked for but I hope that despite not being a plain C answer it will cover your needs. Reinventing the wheel isn't a way to go so let's use date.js in C by running it with SpiderMonkey, the Mozilla JavaScript engine.
下面是我如何做的。我已经开始与下载date.js并将其转换为一个为const char *
名为 code
中定义 date.js.h
。
Here's how I did it. I've begun with downloading date.js and translating it into a const char*
named code
defined in date.js.h
.
( \
echo 'const char *code =' ; \
curl https://datejs.googlecode.com/files/date.js | \
sed -e 's/\\/\\\\/g; s/"/\\"/g; s/^/"/; s/\r\?$/\\n"/'; \
echo ';' \
) > date.js.h
然后我把<一个href=\"https://developer.mozilla.org/index.php?title=En/SpiderMonkey/JSAPI_User_Guide&revision=158\"相对=nofollow> JSAPI的的Hello,World!的作为一个起点。
Then I took the JSAPI's Hello, World! as a starting point.
#include "jsapi.h"
#include "date.js.h"
static JSClass global_class = { "global", JSCLASS_GLOBAL_FLAGS,
JS_PropertyStub, JS_PropertyStub, JS_PropertyStub, JS_StrictPropertyStub,
JS_EnumerateStub, JS_ResolveStub, JS_ConvertStub, JS_FinalizeStub,
JSCLASS_NO_OPTIONAL_MEMBERS };
void reportError(JSContext *cx, const char *message, JSErrorReport *report) {
fprintf(stderr, "%s:%u:%s\n",
report->filename ? report->filename : "<no filename>",
(unsigned int) report->lineno, message);
}
int main(int argc, const char *argv[]) {
JSRuntime *rt;
JSContext *cx;
JSObject *global;
rt = JS_NewRuntime(8L * 1024L * 1024L);
if (rt == NULL) return 1;
cx = JS_NewContext(rt, 8192);
if (cx == NULL) return 1;
JS_SetOptions(cx, JSOPTION_VAROBJFIX | JSOPTION_JIT | JSOPTION_METHODJIT);
JS_SetVersion(cx, JSVERSION_LATEST);
JS_SetErrorReporter(cx, reportError);
global = JS_NewCompartmentAndGlobalObject(cx, &global_class, NULL);
if (global == NULL) return 1;
if (!JS_InitStandardClasses(cx, global)) return 1;
/* Here's where the interesting stuff is starting to take place.
* Begin by evaluating sources of date.js */
jsval out;
if (!JS_EvaluateScript(cx, global, code, strlen(code), "code", 1, &out))
return 1;
/* Now create a call to Date.parse and evaluate it. The return value should
* be a timestamp of a given date. If no errors occur convert the timestamp
* to a double and print it. */
const int buflen = 1024;
char parse[buflen + 1];
snprintf(parse, buflen, "Date.parse(\"%s\").getTime();", argv[1]);
if (!JS_EvaluateScript(cx, global, parse, strlen(parse), "parse", 1, &out))
return 1;
double val;
JS_ValueToNumber(cx, out, &val);
printf("%i\n", (int) (val / 1000));
/* Finally, clean everything up. */
JS_DestroyContext(cx);
JS_DestroyRuntime(rt);
JS_ShutDown();
return 0;
}
下面是它的工作原理在实践中。
Here's how it works in practice.
$ time ./parse "week ago"
1331938800
0.01user 0.00system 0:00.02elapsed 92%CPU (0avgtext+0avgdata 6168maxresident)k
0inputs+0outputs (0major+1651minor)pagefaults 0swaps
$ time ./parse yesterday
1332457200
0.01user 0.00system 0:00.02elapsed 84%CPU (0avgtext+0avgdata 6168maxresident)k
0inputs+0outputs (0major+1653minor)pagefaults 0swaps
正如你可以看到它的非常快,你可以通过重复使用的所有后续调用最初创建的背景下 Date.parse
显著提高其性能。
许可问题说起,date.js可在MIT的条款和SpiderMonkey的下MPL 1.1,2.0 GPL或LGPL 2.1可用。链接它动态地满足非GPL的要求。
Speaking of licensing issues, date.js is available under terms of MIT and SpiderMonkey is available under MPL 1.1, GPL 2.0 or LGPL 2.1. Linking it dynamically satisfies the non-GPL requirement.
TL; DR: git的克隆https://gist.github.com/2180739.git&放大器;&安培; CD&2180739功放;&安培;让&功放;&安培;昨天./parse
这篇关于C库来解析大致日期的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!