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 10 14953
Reiner Merz wrote: 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.
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
On 9 Sep 2003 15:23:52 -0700, lo******@gmx.de (Reiner Merz) wrote: 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 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 wrote: 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.
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/
Al Bowers wrote:
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"
IIRC ISO8601 requires the T and the Z. You can only remove the - and the
:. And they must be all absent or all present, ie "200309-09T23:00:00Z"
is *NOT* acceptable.
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);
The big problem with a scanf implementation is that it does not tell you
where an illegal string fails.
--
Michel Bardiaux
Peaktime Belgium S.A. Bd. du Souverain, 191 B-1160 Bruxelles
Tel : +32 2 790.29.41
Thank you all for your contributions.
Unfortunately, this is not what I want but I must admit, I
haven't made myself clear enough.
Your parsing methods are all simple and working for complete
timestamp strings. But sscanf() is just too tolerant in my C
compiler (gcc) to be useful to me.
For example:
If I parse the string
03-09-09T23:00:00Z
"%4d" doesn't return an error on the year because it's not 4
digits long, which I really need.
So, I need a precise scanner function with the parser telling
me exacly where an error occured.
Thanks,
Reiner lo******@gmx.de (Reiner Merz) wrote: Thank you all for your contributions.
Unfortunately, this is not what I want but I must admit, I haven't made myself clear enough.
Your parsing methods are all simple and working for complete timestamp strings. But sscanf() is just too tolerant in my C compiler (gcc) to be useful to me.
<SNIP>
Hmm, well, it seems you are in trouble now.
Let me put it together:
You don't want to:
- link against an external library
- use lex/yacc (which, IMHO, are well suited to produce a
scanner/parser; that's what they're made for)
- use any other third party universal regex driven parsing engine
- "mess around" with C's string functions
- use sscanf (for good reasons, IMO)
IMHO you have to drop at least one of these restrictions to accomplish
your mission. Choose the solution that looks least worse to you.
My two-cent suggestion: use (f)lex/yacc(bison) - this way your
scanner/parser will be easier to maintain than any hand crafted
solution.
Good luck to you!
Regards
Irrwahn
--
The generation of random numbers is too important to be left to chance.
On 19 Sep 2003 03:29:06 -0700, in comp.lang.c , lo******@gmx.de
(Reiner Merz) wrote: If I parse the string
03-09-09T23:00:00Z
"%4d" doesn't return an error on the year because it's not 4 digits long, which I really need.
So, I need a precise scanner function with the parser telling me exacly where an error occured.
read the string char by char. Decide what you have as you receive it.
eg
read 2 chars
if next char is not numeric, then add 2000 to first two to form year
etc
--
Mark McIntyre
CLC FAQ <http://www.eskimo.com/~scs/C-faq/top.html>
CLC readme: <http://www.angelfire.com/ms3/bchambless0/welcome_to_clc.html>
Hi Irrwahn, You don't want to: - use lex/yacc (which, IMHO, are well suited to produce a scanner/parser; that's what they're made for)
You must have got me wrong there.
I did try to use lex/yacc because I thought they were made
for these kind of problems, but I didn't succeed. But my
inability to do it could be due to the fact that I've only
rarely used lex/yacc and when I used it, I used it for simple
configuration files. So, you could say, I'm not really
experienced.
My problem was mainly the scanner. I wanted to define my tokens
YEAR as four digits
MONTH as two digits
DAY as two digits
HOUR as two digits
MINUTE as two digits
SECOND as two digits
So, how can the scanner possibly distinguish between MONTH,
DAY, HOUR, MINUTE and SECOND? The flex manual says it picks the
longest possible match and if there are equally long matches it
picks the first defined token.
Ok, I could define a FOURDIGIT and a TWODIGIT token and then
assign the right values in bison. I guess that would work for
2003-09-09T23:00:00Z
but for
20030909T230000Z (which is a legal date time according to the
ISO specification)
I would imagine, lex gives me
FOURDIGIT 2003
FOURDIGIT 0909
FOURDIGIT 2300
TWODIGIT 00
and that is not really what I want.
But maybe I didn't understand the way lex/yacc work.
Cheers,
Reiner lo******@gmx.de (Reiner Merz) wrote: Hi Irrwahn,
You don't want to: - use lex/yacc (which, IMHO, are well suited to produce a scanner/parser; that's what they're made for) You must have got me wrong there.
Possibly ... :) I did try to use lex/yacc because I thought they were made for these kind of problems, but I didn't succeed. But my inability to do it could be due to the fact that I've only rarely used lex/yacc and when I used it, I used it for simple configuration files. So, you could say, I'm not really experienced.
My problem was mainly the scanner. I wanted to define my tokens
YEAR as four digits MONTH as two digits DAY as two digits HOUR as two digits MINUTE as two digits SECOND as two digits
So, how can the scanner possibly distinguish between MONTH, DAY, HOUR, MINUTE and SECOND? The flex manual says it picks the longest possible match and if there are equally long matches it picks the first defined token.
Ok, I could define a FOURDIGIT and a TWODIGIT token and then assign the right values in bison. I guess that would work for
2003-09-09T23:00:00Z
but for
20030909T230000Z (which is a legal date time according to the ISO specification)
Blame me, I was only thinking about the hyphen-separated format.
(And, besides, IMO the latter format is sheer braindead.) I would imagine, lex gives me
FOURDIGIT 2003 FOURDIGIT 0909 FOURDIGIT 2300 TWODIGIT 00
and that is not really what I want.
Well, obviously not, and furthermore, I cannot imagine a way to tell lex
how to disambiguate this and tokenize appropriately; but I am far from
being a lex expert. But maybe I didn't understand the way lex/yacc work.
Possibly better than I do.
However, to get back to your original problem, maybe the easiest
solution is to handcraft a scanner, traversing the string and use the
is*() functions from ctype.h, a la:
- read four consecutive digits, interprete as year
- gulp an optional hyphen
- read two consecutive digits, interprete as month
- gulp an optional hyphen
- read two consecutive digits, interprete as day
- gulp the mandatory 'T'
- read two consecutive digits, interprete as hour
- gulp an optional colon
- ... etc.
Not really a nice thing to do I admit, and you have to provide a lot of
error checking; but, after all, you know exactly the way your code
works.
Sorry, I'm unable to provide a better solution, maybe someone else can.
Good luck to you!
Regards
Irrwahn
--
My other computer is a abacus.
Reiner Merz <lo******@gmx.de> wrote: Hi Irrwahn,
You don't want to: - use lex/yacc (which, IMHO, are well suited to produce a scanner/parser; that's what they're made for)
You must have got me wrong there.
I did try to use lex/yacc because I thought they were made for these kind of problems, but I didn't succeed. But my inability to do it could be due to the fact that I've only rarely used lex/yacc and when I used it, I used it for simple configuration files. So, you could say, I'm not really experienced.
My problem was mainly the scanner. I wanted to define my tokens
YEAR as four digits MONTH as two digits DAY as two digits HOUR as two digits MINUTE as two digits SECOND as two digits
So, how can the scanner possibly distinguish between MONTH, DAY, HOUR, MINUTE and SECOND? The flex manual says it picks the longest possible match and if there are equally long matches it picks the first defined token.
Ok, I could define a FOURDIGIT and a TWODIGIT token and then assign the right values in bison. I guess that would work for
2003-09-09T23:00:00Z
but for
20030909T230000Z (which is a legal date time according to the ISO specification)
I would imagine, lex gives me
FOURDIGIT 2003 FOURDIGIT 0909 FOURDIGIT 2300 TWODIGIT 00
and that is not really what I want.
comp.compilers is probably a much better place to ask lex/yacc
questions. However, I can tell you that the solution is to just
define a single DIGIT token, then use yacc to recognise four DIGIT
tokens followed by an optional DASH token etc etc...
- Kevin. This thread has been closed and replies have been disabled. Please start a new discussion. Similar topics
by: N |
last post by:
Hi,
I would like to parse out each value that is seperated
by a comma in a field and use that value to join to another table.
What would be the easiest way to do so without having to
write a...
|
by: Jeannie |
last post by:
Hello group!
I'm in Europe, traveling with my laptop, and I don't any compilers
other than Borland C++ 5.5. available. I also don't have any manuals
or help files available. Sadly, more...
|
by: Danny |
last post by:
HI again
Is there a nifty function in access that will:
1. return the amount of occurances of a small string within a larger
string?
this<br>is<br>a<br>test
would return 3 for <br>
|
by: linzhenhua1205 |
last post by:
I want to parse a string like C program parse the command line into
argc & argv.
I hope don't use the array the allocate a fix memory first, and don't
use the memory allocate function like malloc....
|
by: Charles Law |
last post by:
I have a string similar to the following:
" MyString 40 "Hello world" all "
It contains white space that may be spaces or tabs, or a combination, and I
want to produce an array...
|
by: David Garamond |
last post by:
When a timestamp string input contains a timezone abbreviation (CDT,
PST, etc), which timezone offset is used? The input date's or today
date's? The result on my computer suggests the latter.
#...
|
by: Phil Mc |
last post by:
OK this should be bread and butter, easy to do, but I seem to be going
around in circles and not getting any answer to achieving this simple
task.
I have numbers in string format (they are...
|
by: Dan Somdahl |
last post by:
Hi, I am new to ASP but have what should be a fairly simple task that I
can't figure out.
I need to parse a string from a single, semi-colon delimited, 60 character
field (el_text) in a recordset...
|
by: tekt9t9 |
last post by:
Hello
I want to parse a string which holds a integer value to an int. The Interger.parseint() throws an exception. e.g
If i try to execute the following code:
i =...
|
by: Charles Arthur |
last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
|
by: ryjfgjl |
last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
|
by: ryjfgjl |
last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
|
by: BarryA |
last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
|
by: Sonnysonu |
last post by:
This is the data of csv file
1 2 3
1 2 3
1 2 3
1 2 3
2 3
2 3
3
the lengths should be different i have to store the data by column-wise with in the specific length.
suppose the i have to...
|
by: marktang |
last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
|
by: Oralloy |
last post by:
Hello folks,
I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>".
The problem is that using the GNU compilers,...
|
by: jinu1996 |
last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
|
by: Hystou |
last post by:
Overview:
Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
| |