Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

C Library for Parsing Date Time [closed]

Tags:

c

date

parsing

Is one aware of a date parsing function for c. I am looking for something like:

time = parse_time("9/10/2009");
printf("%d\n", time->date);
time2 = parse_time("Monday September 10th 2009")    
time2 = parse_time("Monday September 10th 2009 12:30 AM")

Thank you

like image 794
adk Avatar asked Sep 12 '09 03:09

adk


2 Answers

There are two fairly common approaches in C:

  1. Use strptime() with an array of supported formats you accept.

  2. Bang head against table a lot, and then either give up or use another language which has a usable library already (like perl or python).

like image 150
James Antill Avatar answered Oct 08 '22 20:10

James Antill


If the format is consistent you can use scanf family functions

#include<stdio.h>

int main()
{
    char *data = "Tue, 13 Dec 2011 16:08:21 GMT";
    int h, m, s, d, Y;
    char M[4];
    sscanf(data, "%*[a-zA-Z,] %d %s %d %d:%d%:%d", &d, M, &Y, &h, &m, &s);
    return 0;
}
like image 37
Shiplu Mokaddim Avatar answered Oct 08 '22 21:10

Shiplu Mokaddim