Nathaniel McCallum: Introducing libiso8601

Four years ago I needed a library for parsing ISO 8601 dates in C. After I
wrote most of it, we ended up going in a different direction. This code has
sat on my computer since then. But no more!

This week I polished it up and pushed it to GitHub. The library is fully
tested (with >98% code coverage) and handles not only all the ISO 8601
standard formats but many common non-standard variations as well.

Here’s an example of how to use it:

#include <iso8601.h>
#include <assert.h>
#include <string.h>

int main() {
    iso8601_time time = {};
    char str[128] = {};

    iso8601_parse("2010-02-14T13:14:23.123456Z", &time);

    assert(time.year == 2010);
    assert(time.month == 2);
    assert(time.day == 14);
    assert(time.hour == 13);
    assert(time.minute == 14);
    assert(time.second == 23);
    assert(time.usecond == 123456);

    iso8601_unparse(&time, ISO8601_FLAG_NONE, 4, ISO8601_FORMAT_WEEKDATE,
                    ISO8601_TRUNCATE_DAY, sizeof(str), str);

    assert(strcmp(str, "2010-W06-7") == 0);
    return 0;
}

I’d love to get some review of the API before I release the first version. So
if you’re into telling people how bad their code is, please wander this way!


Source From: fedoraplanet.org.
Original article title: Nathaniel McCallum: Introducing libiso8601.
This full article can be read at: Nathaniel McCallum: Introducing libiso8601.

Advertisement


Random Article You May Like

Leave a Reply

Your email address will not be published. Required fields are marked *

*
*