Interval calculation

O

Oswaldo Castro

Hi Folks,

I need to calculate the time interval between to strings that are
fields on a MySQL database. These strings are on the form "dd/mm/yyyy
hh:mi:ss" and the result should be in seconds.

I tried strptime() with no success. Can someone give me some code
examples?

Thanks in advance.

Oswaldo Castro
 
B

Barry Schwarz

Hi Folks,

I need to calculate the time interval between to strings that are
fields on a MySQL database. These strings are on the form "dd/mm/yyyy
hh:mi:ss" and the result should be in seconds.

I tried strptime() with no success. Can someone give me some code
examples?
Extract each data item from the first string, convert int, and store
in the appropriate field of a struct tm. When all the data in one
string has been stored in the struct, use mktime() to convert the time
to a time_t. Do the same for the second string. Use difftime to
return the difference in seconds.


<<Remove the del for email>>
 
A

Alex

Barry Schwarz said:
Extract each data item from the first string, convert int, and store
in the appropriate field of a struct tm. When all the data in one
string has been stored in the struct, use mktime() to convert the time
to a time_t. Do the same for the second string. Use difftime to
return the difference in seconds.

Fine, *if* the times are local time.
 
B

Barry Schwarz

Fine, *if* the times are local time.

I don't see that it would matter as long as both strings are in the
same "time zone." The number of seconds between two dates/times is a
constant independent of whether the data is in UTC or GMT or local or
even imaginary (except for the fun and games associated with leap
seconds).


<<Remove the del for email>>
 
A

Alex

Barry Schwarz said:
I don't see that it would matter as long as both strings are in the
same "time zone."

It depends on what you set tm_isdst to. To me, it is less than explicit what
the behaviour is unless tm_isdst is negative, in which case the times must
be local time. In this case, what matters is not that the strings are in the
same time zone as one another, but that they are in the same time zone as
what mktime() thinks applies.

That said, with tm_isdst = 0, on all implementations I have tried it, the
method you describe gives the right answers for GMT times - or (as I think
was your point) for any pair of times that have the same offset from GMT.

IMHO, there are a couple of things missing from the time support in the
standard library: "time_t mkgmtime(struct tm *)" (which completely ignores
the tm_isdst field) and "time_t addtime(time_t, double)" (I'm sure you can
guess).
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
474,141
Messages
2,570,817
Members
47,365
Latest member
BurtonMeec

Latest Threads

Top