Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Date/Time parsing in C++

While doing the data/time parsing in c++ (converting a string in any format to a date), i found the following useful methods

1) strptime() - here the %d, %m etc can have either 1 or 2 characters. The function will take care of that. As a result of this it will enforce that we use a separator between two conversion specifiers. Ex: Its not valid to give %d%m it has be to %d/%m or any other separator. Also this does not support timezones.

2) Boost date IO - here the %d, %m has to have 2 characters. Now, the input string i get is not guaranteed to have this. As a result, its not possible to use this successfully. However, this does seem to support timezone, but not sure. because it says for inputs it does support timezone

So i am planning to use both in conjunction to determine the date. But i would like to get one where i can take into account the timezone as well. But none seems to be supporting that.

Does anybody have a suggestion? Rgds, AJ

like image 907
AMM Avatar asked Oct 26 '25 17:10

AMM


1 Answers

@AJ: Added another answer so the code gets formatted

#include <stdio.h>
#include <sys/time.h>
#include <time.h>

int
main(void)
{
        struct tm tm[1] = {{0}};

        strptime("Tue, 19 Feb 2008 20:47:53 +0530", "%a, %d %b %Y %H:%M:%S %z", tm);
        fprintf(stdout, "off %ld\n", tm->tm_gmtoff);
        return 0;
}

And a run looks like (glibc 2.10.1):

freundt@clyde:pts/28:~/temp> ./test
off 19800
like image 108
hroptatyr Avatar answered Oct 28 '25 08:10

hroptatyr