> where can I get source code for some functions that will handle time
> arithmetic. For example:
> add(1.50, 1.50) returns 2.40.
> subtract(1.50, 0.60) returns 0.50
I strongly recommend against trying to use floating point values to
represent hours and minutes in this way; they are totally unsuitable.
Better is to use one of the standard representations for time: time_t
(according to POSIX, an arithmetic type containing seconds since the epoch
1970-01-01 00:00 UTC) or struct tm (a struct containing a broken-down
representation of a time, including year, month, day of month, hour, minute,
The ISO C standard provides the difftime() function, which takes two time_t
values and returns the difference between them in seconds, as a double. As
for addition, the simplest solution is usually to add values to the relevant
field or fields of a struct tm, then use mktime() to renormalise that
struct. mktime() also returns a time_t corresponding to that time; to
convert in the other direction (from time_t to struct tm) you can use
localtime() (or sometimes gmtime()).
Quote:> I would need these plus divide, percent, multiply.
What on earth does it mean to multiply or divide one time by another?
Quote:> Yes, I know this is all very simple, but remember the old maxim: "where
> possible steal code".
Actually, it's distinctly non-trivial to get time calculations correct; this
is another reason for reusing working code.