## time functions

### time functions

where can I get source code for some functions that will handle time
arithmetic.  For example:

subtract(1.50, 0.60) returns 0.50

I would need these plus divide, percent, multiply.

Yes, I know this is all very simple, but remember the old maxim: "where
possible steal code".

Thanks

--
joe mc cool
The more you say the less the better.
========================================================================

Benburb                            compuserve: 100117,2613
N. Ireland                         voice     : (044) 1861 548074
BT71 7LN                           fax       : (044) 1861 549860
========================================================================

### time functions

Quote:> where can I get source code for some functions that will handle time
> arithmetic.  For example:

> 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,
second).

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.

--

You need to initialize 't' before you use it. You got lucky in one case
and 't' happened to contain a reasonable value. In the other case, you
didn't get lucky.

DS