473,480 Members | 5,031 Online
Bytes | Software Development & Data Engineering Community
Create Post

Home Posts Topics Members FAQ

Converting seconds to (Days, Hours, Minutes, seconds)

Stu
Is there a simple function call within "C" that I can use to convert
number of seconds (keep in mind this may be a type longlong and has to
work on UNIX and NT) into Days, months, Hours, Minutes and seconds.

If anybody can provide me with an example I would be very grateful.

Thanks in advance for all that answer this post.

Nov 14 '05 #1
7 8990

"Stu" <be********@hotmail.com> wrote in message
news:11**********************@z14g2000cwz.googlegr oups.com...
Is there a simple function call within "C" that I can use to convert
number of seconds (keep in mind this may be a type longlong and has to
work on UNIX and NT) into Days, months, Hours, Minutes and seconds.
No.
If anybody can provide me with an example I would be very grateful.


Calculating a number of months, days, hours and minutes from a number
of seconds is rather trivial, so I won't bother.

If you intend to calculate a date/time pair from a number os seconds since
some predefined point (the famous UNIX EPOCH for instance) things will be a
lot trickier, since you will have to take leapyears into account too. In
that case i'd suggest using the systems functions for that and write a
wrapper that will (depending on some compiler definition) call the
appropriate function out of your system libs.
Nov 14 '05 #2
Stu wrote:

Is there a simple function call within "C" that I can use to convert
number of seconds (keep in mind this may be a type longlong and has to
work on UNIX and NT) into Days, months, Hours, Minutes and seconds.

If anybody can provide me with an example I would be very grateful.

Thanks in advance for all that answer this post.


The following code ignores months, pending your definition thereof.
(30 days? 365/12.0 days? 28 days? 27.whatever days?)

typedef unsigned long TIMEINT; /* change to long long if you wish */

void ConvSeconds(TIMEINT *d,
TIMEINT *hr,
TIMEINT *min,
TIMEINT *sec,
TIMEINT s)
{
*d = s / 86400;
s %= 86400;
*hr = s / 3600;
s /= 3600;
*min = s / 60;
*sec = s % 60;
}
Nov 14 '05 #3
On 22 Feb 2005 07:46:11 -0800, in comp.lang.c , "Stu"
<be********@hotmail.com> wrote:
Is there a simple function call within "C" that I can use to convert
number of seconds (keep in mind this may be a type longlong and has to
work on UNIX and NT) into Days, months, Hours, Minutes and seconds.
I have a feeling that your "time in seconds" is a time_t object maybe....
Note that this is not guaranteed to be a measure of seconds.
If anybody can provide me with an example I would be very grateful.


localtime() and gmtime() will do this for time_t objects.

--
Mark McIntyre
CLC FAQ <http://www.eskimo.com/~scs/C-faq/top.html>
CLC readme: <http://www.ungerhu.com/jxh/clc.welcome.txt>

----== Posted via Newsfeeds.Com - Unlimited-Uncensored-Secure Usenet News==----
http://www.newsfeeds.com The #1 Newsgroup Service in the World! 120,000+ Newsgroups
----= East and West-Coast Server Farms - Total Privacy via Encryption =----
Nov 14 '05 #4
"dandelion" <da*******@meadow.net> wrote:
"Stu" <be********@hotmail.com> wrote in message
news:11**********************@z14g2000cwz.googlegr oups.com...
Is there a simple function call within "C" that I can use to convert
number of seconds (keep in mind this may be a type longlong and has to
work on UNIX and NT) into Days, months, Hours, Minutes and seconds.


No.
If anybody can provide me with an example I would be very grateful.


Calculating a number of months, days, hours and minutes from a number
of seconds is rather trivial, so I won't bother.


Actually, calculating months is not that trivial. The problem is, what
months? 30 days, 31, 28, 30.5, 365.2425/12?

Richard
Nov 14 '05 #5

"Richard Bos" <rl*@hoekstra-uitgeverij.nl> wrote in message
news:42***************@news.individual.net...
"dandelion" <da*******@meadow.net> wrote:
"Stu" <be********@hotmail.com> wrote in message
news:11**********************@z14g2000cwz.googlegr oups.com...
Is there a simple function call within "C" that I can use to convert
number of seconds (keep in mind this may be a type longlong and has to
work on UNIX and NT) into Days, months, Hours, Minutes and seconds.


No.
If anybody can provide me with an example I would be very grateful.


Calculating a number of months, days, hours and minutes from a number
of seconds is rather trivial, so I won't bother.


Actually, calculating months is not that trivial. The problem is, what
months? 30 days, 31, 28, 30.5, 365.2425/12?


I'd just create a table holding the days_in_month. Something like

days_in_month[] =
{
31, /* January */
28,
31,
30,

/* etc. */

31 /* December */
};

Then, you need 'leapyear' info for February, which isn't that hard to
calculate. IIRC it's a leapyear if year % 4 == 0 && year % 100 != 0.

But ideas on what's trivial and what's not can be very different. I agree to
that.

Nov 14 '05 #6
dandelion wrote:
"Richard Bos" <rl*@hoekstra-uitgeverij.nl> wrote in message
"dandelion" <da*******@meadow.net> wrote:
"Stu" <be********@hotmail.com> wrote in message

Is there a simple function call within "C" that I can use to
convert number of seconds (keep in mind this may be a type
longlong and has to work on UNIX and NT) into Days, months,
Hours, Minutes and seconds.

No.

If anybody can provide me with an example I would be very
grateful.

Calculating a number of months, days, hours and minutes from
a number of seconds is rather trivial, so I won't bother.
Actually, calculating months is not that trivial. The problem
is, what months? 30 days, 31, 28, 30.5, 365.2425/12?


I'd just create a table holding the days_in_month. Something like

.... snip ...
Then, you need 'leapyear' info for February, which isn't that
hard to calculate. IIRC it's a leapyear if year % 4 == 0 &&
year % 100 != 0.


Here is a routine I used to use to compute yr, mo, day from the
CP/M date stamp, which corresponded to days since 1 Jan 1978. To
use it you need to make a preliminary conversion of seconds since
something into days since something. Dealing with quad years of
1461 days avoids a plethora of silly errors. No need to worry
further about leap year until 2100.

PROCEDURE drtodate(thedate : integer; VAR yr, mo, day : integer);
(* 1 Jan 1978 corresponds to Digital Research date = 1 *)
(* BUG - cannot handle negative values for dates > 2067 *)

VAR
i, y1 : integer;
dayspermonth : ARRAY[1..12] OF 1..31;

BEGIN (* drtodate *)
FOR i := 1 TO 12 DO dayspermonth[i] := 31;
dayspermonth[4] := 30; dayspermonth[6] := 30;
dayspermonth[9] := 30; dayspermonth[11] := 30;
IF thedate > 731 THEN BEGIN (* avoid overflows *)
yr := 1980; thedate := thedate - 731; END
ELSE BEGIN
thedate := thedate + 730; yr := 1976; END;
(* 0..365=y0; 366..730=y1; 731..1095=y2; 1096..1460=y3 *)
i := thedate DIV 1461; thedate := thedate MOD 1461;
y1 := (thedate-1) DIV 365; yr := yr + y1 + 4*i;
IF y1 = 0 THEN (* leap year *) dayspermonth[2] := 29
ELSE BEGIN
thedate := thedate - 1; (* 366 -> 365 -> 1 Jan *)
dayspermonth[2] := 28; END;
day := thedate - 365*y1 + 1; mo := 1;
WHILE day > dayspermonth[mo] DO BEGIN
day := day - dayspermonth[mo];
mo := succ(mo); END;
END; (* drtodate *)

--
"If you want to post a followup via groups.google.com, don't use
the broken "Reply" link at the bottom of the article. Click on
"show options" at the top of the article, then click on the
"Reply" at the bottom of the article headers." - Keith Thompson
Nov 14 '05 #7
On Thu, 24 Feb 2005 12:18:24 +0100, "dandelion" <da*******@meadow.net>
wrote:
<snip>
Then, you need 'leapyear' info for February, which isn't that hard to
calculate. IIRC it's a leapyear if year % 4 == 0 && year % 100 != 0.

Officially || year % 400 == 0. Or equivalently but more symmetrically
year % 4 == 0 && !(year % 100 == 0 && !(year % 400 == 0 )) .

2000 was a leap year. The next effect of the 400 term is 2400, and
it's not unthinkable that we will have a very different calendar by
then. In fact if you want to repeat the Y2K type of mistake you can
just do year % 4 and assume (or try to require) your application won't
be used for dates beyond 2099 or before 1901.
- David.Thompson1 at worldnet.att.net
Nov 14 '05 #8

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

6
46061
by: Kription | last post by:
Hello, I am having a nightmare of a time getting seconds converted to hour-minute-seconds I have a third party application that outputs only in seconds since so if someone has been logged...
5
16875
by: Masahiro Ito | last post by:
I have column of data that have elapsed time in minutes. For example: 1:15 1:47 23:12 I like to be able to use sql server to do simple math functions. Does anyone have a simple...
6
26397
by: Harlin Seritt | last post by:
I would like to take milliseconds and convert it to a more human-readable format like: 4 days 20 hours 10 minutes 35 seconds Is there something in the time module that can do this? I havent...
2
10816
by: Harlin Seritt | last post by:
How can I take a time given in milliseconds (I am doing this for an uptime script) and convert it to human-friendly time i.e. "4 days, 2 hours, 25 minutes, 10 seonds."? Is there a function from the...
1
16963
by: markdp | last post by:
I'm trying to write a program that takes a user inputted number of seconds and converts it into hours, minutes and seconds. So for instance, the program prompts the user for seconds, lets say 1000...
3
2919
by: Bert Murphy | last post by:
Im trying to write code that would have seconds for the input,convert the seconds to the hours:minutes:seconds,accumulate the hours:minutes:seconds to a maximum of 999Hours:59Minutes:59Seconds as...
2
12264
by: mar10 | last post by:
i have TIME WORKED stored as minutes - I'd like to convert this to days,hours,minutes. I'm currently converting to hours and minutes but have not had success adding the days portion to the...
6
6556
by: alexs | last post by:
hi, I'm moving a mysql database over to using db2 V9.5 The database is used by our radius server to store network accounting information from our switches. One parameter is the length of a...
5
12777
by: =?Utf-8?B?U2hlbGRvbg==?= | last post by:
Hello - I have about ten integer values that each need to be converted to days, hours, minutes and seconds. I can't seem to find a formula for same that works. These integer values come from a...
0
7041
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
7080
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
1
6736
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
1
4772
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...
0
4478
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and...
0
2994
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The...
0
2980
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
561
muto222
php
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
0
178
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.