解析时间戳字符串 [英] Parse timestamp string

查看:75
本文介绍了解析时间戳字符串的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述




我正在寻找有关如何根据ISO 8601规范解析时间戳字符串

的建议。


对于那些不熟悉标准的人,这里有一个例子:


2003-09-09T23:00:00Z

它的紧凑形式也可以没有缺点和

冒号或时区信息。


解析器应​​该很小并且快速且相当容易地将
集成到C.如果可能的话,我不想链接到

额外的库。

到目前为止,我一直认为在lex / yacc(flex / bison)中做这件事但是在考虑完之后

,我不认为它们是正确的

这个工作的工具。


正则表达式正是我需要的但我不想要

a整个正则表达式引擎计算运行时的模式

没有必要。必须有一个更便宜的方式。


我已经在互联网上搜索了一种正则表达式

编译器/生成器会生成C我特定的代码

正则表达式但遗憾的是我找不到类似于我想要的东西。


当然,我可以通过乱搞

string.h中的字符串函数来解决这个问题,但是如果我能避免这样做的话,我会这样做。

所以。 />

任何建议都表示赞赏。


谢谢


Reiner

Hi,

I''m looking for advice on how to parse a timestamp string
according to the ISO 8601 specification.

For those unfamiliar with the standard, here''s an example:

2003-09-09T23:00:00Z

A compact form of it can as well be without the minuses and
the colons or with timezone information.

The parser should be small and fast and fairly easy to
integrate into C. And if possible, I don''t want to link to an
additional library.

I thought so far about doing it in lex/yacc (flex/bison) but
after thinking it through, I don''t consider them to be the right
tools for the job.

Regexes would be exactly what I need but I don''t want to have
a whole regex engine that calculates the pattern at runtime when
it''s not necessary. There must be a cheaper way of doing it.

I''ve already searched the internet for a kind of regex
compiler/generator that would generate C code for my specific
regex but unfortunately I didn''t find anything similar to what I
want.

Of course, I can do it the hard way by messing around with
the string functions in string.h but if I can avoid that I''ll do
so.

Any suggestions are appreciated.

Thanks

Reiner

推荐答案

Reiner Merz写道:
Reiner Merz wrote:


我正在寻找有关如何解析时间戳字符串的建议
根据ISO 8601规范。

对于那些不熟悉标准的人,这里有一个例子:

2003-09-09T23:00:00Z

当然,我可以通过搞乱来做到这一点用string.h中的字符串函数但是如果我可以避免那么我会这样做。
Hi,

I''m looking for advice on how to parse a timestamp string
according to the ISO 8601 specification.

For those unfamiliar with the standard, here''s an example:

2003-09-09T23:00:00Z

Of course, I can do it the hard way by messing around with
the string functions in string.h but if I can avoid that I''ll do
so.



我真的不认为这是这样的很重要的,并且给了你的b $ b b要求,呃,我剪断了,我认为这是要走的路。


现在,取决于应用程序你想到这篇文章

代码,我非常愿意为你开发合理的b
费用。如果您有兴趣,请随时与我联系。


-

Bertrand Mollinier Toublet

目前正在寻找就业机会旧金山湾区
http://www.bmt.dnsalias.org/就业


I do not really think this is such a big deal and given your
requirements, that, er, I snipped, I think this is the way to go.

Now, depending on the applications that you have in mind for this piece
of code, I am more than willing to develop that for you for a reasonable
fee. Feel free to contact me directly if you are interested.

--
Bertrand Mollinier Toublet
Currently looking for employment in the San Francisco Bay Area
http://www.bmt.dnsalias.org/employment


2003年9月9日15:23:52 -0700, lo ****** @ gmx.de (Reiner Merz)写道:
On 9 Sep 2003 15:23:52 -0700, lo******@gmx.de (Reiner Merz) wrote:


我我正在寻找有关如何根据ISO 8601规范解析时间戳字符串的建议。

对于那些不熟悉标准的人,这里有一个例子:

2003-09-09T23:00:00Z

它的紧凑形式也可以没有缺点和冒号或时区信息。
<解析器应该小而快,并且很容易集成到C.如果可能的话,我不想要链接到
附加库。
Hi,

I''m looking for advice on how to parse a timestamp string
according to the ISO 8601 specification.

For those unfamiliar with the standard, here''s an example:

2003-09-09T23:00:00Z

A compact form of it can as well be without the minuses and
the colons or with timezone information.

The parser should be small and fast and fairly easy to
integrate into C. And if possible, I don''t want to link to an
additional library.




C已包含包含模式匹配的库。您给出的

示例可以解析为:


sscanf(字符串,%d-%d-%dT%d:%d:% d%c",/ * ... * /);


您需要查看返回代码以确定

匹配是否成功或没有。


先从一个较长的模式开始,然后尝试每个模式

,直到找到成功的模式。


尼克。



C already includes libraries that include pattern matching. The
example you give can be parsed with:

sscanf( string, "%d-%d-%dT%d:%d:%d%c", /* ... */ );

You need to look at the return code to determine whether the
match was successful or not.

Start with one of the longer patterns first and try each pattern
until you find the one that succeeds.

Nick.





Reiner Merz写道:


Reiner Merz wrote:


我正在寻找有关如何根据ISO 8601规范解析时间戳字符串的建议。

对于那些不熟悉的人标准,这是一个例子:

2003-09-09T23:00:00Z

它的紧凑形式也可以没有弊端和/>冒号或时区信息。

解析器应该小而快,并且很容易集成到C中。如果可能的话,我不想链接到一个额外的图书馆。

我想到目前为止在lex / yacc(flex / bison)中做这件事但是
之后通过思考,我不认为它们是适合这项工作的正确工具。

正则表达式正是我所需要的,但我不想拥有
一个完整的正则表达式引擎,它在运行时计算模式,而不需要它。必须有一个更便宜的方法。

我已经在互联网上搜索了一种正则表达式编译器/生成器,它将为我的特定生成C代码
正则表达式,但不幸的是我没有发现任何类似于我想要的东西。

当然,我可以通过搞乱
字符串函数来做到这一点。 string.h但是如果我可以避免那么我会这样做。

任何建议都表示赞赏。
Hi,

I''m looking for advice on how to parse a timestamp string
according to the ISO 8601 specification.

For those unfamiliar with the standard, here''s an example:

2003-09-09T23:00:00Z

A compact form of it can as well be without the minuses and
the colons or with timezone information.

The parser should be small and fast and fairly easy to
integrate into C. And if possible, I don''t want to link to an
additional library.

I thought so far about doing it in lex/yacc (flex/bison) but
after thinking it through, I don''t consider them to be the right
tools for the job.

Regexes would be exactly what I need but I don''t want to have
a whole regex engine that calculates the pattern at runtime when
it''s not necessary. There must be a cheaper way of doing it.

I''ve already searched the internet for a kind of regex
compiler/generator that would generate C code for my specific
regex but unfortunately I didn''t find anything similar to what I
want.

Of course, I can do it the hard way by messing around with
the string functions in string.h but if I can avoid that I''ll do
so.

Any suggestions are appreciated.




你可以使用

a例程从时间戳中删除可能的非数字,例如


char * str =" 2003-09-09T23:00:00Z" ,buf [32];

int i,j;


for(i = 0,j = 0; str [i]!=''\\ \\ 0''; i ++)

if(str [i]> =''0''&& str [i]< =''9'')

buf [j ++] = str [i];

buf [j] =''\ 0'';


buf然后be20030909230000

Th en函数sscanf使用格式字符串解析

"%4u%2u%2u%2u%u%2u"


包括< stdio.h> ;

无符号年龄,星期一,小时,分钟,秒

sscanf(buf,"%4u%2u%2u%2u%u% 2U",&安培;年,&安培;星期一,和天,&安培; HR,&安培;分钟,& sec);


-

Al Bowers

美国佛罗里达州坦帕市

mailto: xa*@abowers.combase.com (删除x)
http://www.geocities.com/abowers822/



You may remove the possible non-digits from the timestamp with
a routine like

char *str = "2003-09-09T23:00:00Z ", buf[32];
int i,j;

for(i = 0,j = 0;str[i] != ''\0'';i++)
if(str[i] >= ''0'' && str[i] <= ''9'')
buf[j++] = str[i];
buf[j] = ''\0'';

buf would then be "20030909230000"
Then function sscanf to parse using the format string
"%4u%2u%2u%2u%u%2u"

include <stdio.h>
unsigned yr, mon, day, hr, min, sec

sscanf(buf,"%4u%2u%2u%2u%u%2u",&yr,&mon,&day,&hr,& min,&sec);

--
Al Bowers
Tampa, Fl USA
mailto: xa*@abowers.combase.com (remove the x)
http://www.geocities.com/abowers822/


这篇关于解析时间戳字符串的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆