C code for date time format validation. specified format is “YYYYMMDDHHMMSSmmmmmm”

I need to validate a date-time value for "YYYYMMDDHHMMSSmmmmmm" format. Actually what I am hoping is I need to find out a already tested working C code to validate a date time value for above format. Further, I have a date-time value like 201304011031000000. I need a function to verify whether this is a valid date or not.[isdatetime()]

Below are the each parts of formats.

YYYY : Year
MM : Month
DD : Day
HH : hour
MM : Minutes
SS : seconds
mmmmmm: micro-seconds

Answers


If you're on a POSIX system, this looks like something that should be handled with strptime(), but the 'milliseconds' (or microseconds) part is not handled by strptime() or any other standard conversion function I know of.

Assuming the question is asking for microseconds, you could use a variation on the theme provided by:

#include <stdio.h>
#include <string.h>
#include <time.h>

int main(void)
{
    const char  datetime[] = "20130417221633012345"; // YYYYMMDDHHMMSSFFFFFF
    struct tm   time_val;
    unsigned    microsecs;
    const char *end = strptime(datetime, "%Y%m%d%H%M%S", &time_val);
    if (end != 0)
    {
        int nbytes;
        if (strlen(end) == 6 && sscanf(end, "%6u%n", &microsecs, &nbytes) == 1 &&
            nbytes == 6)
        {
            char buffer[32];
            strftime(buffer, sizeof(buffer), "%Y-%m-%d %H:%M:%S", &time_val);
            printf("%s = %s.%.6u\n", datetime, buffer, microsecs);
        }
    }
    return 0;
}

Revised requirement
#include <stdio.h>
#include <string.h>
#include <time.h>

int isdatetime(const char *datetime)
{
    // datetime format is YYYYMMDDHHMMSSFFFFFF
    struct tm   time_val;
    unsigned    microsecs;
    int         nbytes;
    const char *end = strptime(datetime, "%Y%m%d%H%M%S", &time_val);

    if (end != 0 && strlen(end) == 6 &&
        sscanf(end, "%6u%n", &microsecs, &nbytes) == 1 && nbytes == 6)
        return 1;   // Valid
    return 0;       // Invalid
}

Need Your Help

Non-type template parameters in function and in inheritance

c++ variadic-templates variadic-functions

I'm following this great tutorial. The author heavily uses variadic templates and I came to a point where I'm stuck, can't understand. Can you help me?

Getting a second row of xlabels in matlab graph?

matlab

How do I get a row of labels underneath my xticklabel?

About UNIX Resources Network

Original, collect and organize Developers related documents, information and materials, contains jQuery, Html, CSS, MySQL, .NET, ASP.NET, SQL, objective-c, iPhone, Ruby on Rails, C, SQL Server, Ruby, Arrays, Regex, ASP.NET MVC, WPF, XML, Ajax, DataBase, and so on.