# Converting integer to Broken Time in C? - Linux

This is a discussion on Converting integer to Broken Time in C? - Linux ; Is there a standard integer to time function? For example, given an integer value of 361 seconds, it returns a struct of time in hours, minutes and seconds. Here, 6 min and 1 second. -- Wayne T. Watson (Watson Adventures, ...

# Thread: Converting integer to Broken Time in C?

1. ## Converting integer to Broken Time in C?

Is there a standard integer to time function? For example, given an integer value of
361 seconds, it returns a struct of time in hours, minutes and seconds. Here, 6 min
and 1 second.
--
(121.015 Deg. W, 39.262 Deg. N) GMT-8 hr std. time)
Obz Site: 39Â° 15' 7" N, 121Â° 2' 32" W, 2700 feet

Web Page:

2. ## Re: Converting integer to Broken Time in C?

W. Watson wrote:
> Is there a standard integer to time function? For example, given an
> integer value of 361 seconds, it returns a struct of time in hours,
> minutes and seconds. Here, 6 min and 1 second.

int v = 361;
struct { int h, m, s; } t;
t.s = v % 60;
t.m = (v / 60) % 60;
t.h = v / 60 / 60;

The localtime() and gmtime() functions don't do quite
what you want: They convert a time_t to a struct tm, but
the value represents a time stamp ("2004-12-31 23:59:59")
rather than a duration ("seven minutes"). You could (ab)use
them for your purpose, but you might run into oddities like
time zone corrections and/or daylight savings adjustments if
your durations got large enough. Besides, what you want to
do is so simple that dragging out all the machinery
would be canaricide by cannon.

--
Eric Sosman
esosman@acm-dot-org.invalid