Date string to time_t

Feb 23, 2010 at 7:26pm
Hello,

How can I convert a date string such as dd.mm.yyyy or mm/dd/yyyy to time_t? I've spent many, many hours trying all sorts of stuff and googling, but I have been unable to find or program a function that does this. The tm struct needs way too much information. I simply want to be able know the difference between a given date and 1.1.1970 in seconds.

The boost date-time library seemed to offer some functions for this kind of stuff, but I got some linker errors that I was unable to fix.

Thanks in advance
Feb 23, 2010 at 7:46pm
If you got linker errors, you probably aren't linking the Boost libraries. In any case, if you want to use the tm struct, you can probably just pass 0s for the very precise times as you don't really need that much precision.
Feb 23, 2010 at 8:11pm
That's right, I wasn't linking some libraries but I couldn't find out how I could do that either (it required lots of files/stuff I'm completely unfamiliar with and the instructions were confusing to me). So I guessed there probably is an easier way of doing this that I haven't found out.

The tm struct was the first thing I tried and I indeed did put zeros for the hours, minutes etc., but the fields where the problems arise are the more bizarre ones such as "Days since sunday" and "Days since January 1". I would have to do some calculations to get these fields and I would have to take for example leap years into account, so again I was thinking that there should be a better way.
Feb 25, 2010 at 4:54am
Convert the two dates to Julian days then subtract and convert to seconds (by * 86400) with:
1
2
3
4
5
6
7
8
9
10
11
12
double julday(double d, int m, int y) 
{
int A,C,D;
double JD,B;
A=int(y/100);
if(y<500)B=0;
else B=2-A+int(A/4);
C=int(365.25*(y+4716));
D=int(30.6001*(m+1));
JD=C+D+B+d-1524.5;
return JD;
}

This takes care of leap years. Notionally if a time is not indicated seconds will be noon to noon.
Topic archived. No new replies allowed.