473,806 Members | 2,607 Online
Bytes | Software Development & Data Engineering Community
+ 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 9015

"Stu" <be********@hot mail.com> wrote in message
news:11******** **************@ z14g2000cwz.goo glegroups.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(TIM EINT *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********@hot mail.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*******@mead ow.net> wrote:
"Stu" <be********@hot mail.com> wrote in message
news:11******** **************@ z14g2000cwz.goo glegroups.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.in dividual.net...
"dandelion" <da*******@mead ow.net> wrote:
"Stu" <be********@hot mail.com> wrote in message
news:11******** **************@ z14g2000cwz.goo glegroups.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*******@mead ow.net> wrote:
"Stu" <be********@hot mail.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(thedat e : 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.c om, 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*******@mead ow.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.ne t
Nov 14 '05 #8

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

Similar topics

6
46120
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 in for 1 minute, the time value would be 60, if they were on for 5 minutes and 3 seconds the value would be 303
5
16895
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 function/formula that will convert into a decimal minutes form?
6
26595
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 been able to find anything that would do it. Thanks,
2
10842
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 time module that can do this? Thanks, Harlin Seritt
1
16988
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 seconds are inputted. The program will output "there are 0 hours, 16 minutes and 40 seconds in 1000 seconds." I've been trying to do some type of nested loop...but I'm having problems. Any help would be much appreciated! Thanks
3
2941
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 long as the user inputs more seconds.Then roller over to 0:0:0 and continue. Also need to be able to subtract from the accumulated value. Any help would be appreciated.
2
12355
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 formula below. Any direction is greatly appreciated. thanks
6
6597
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 network session in seconds. In mysql there is a sec_to_time function which i then used to store the session time as a char string
5
12889
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 Sql Server database that SUMS up the seconds. The only way SUM will work in SS is with an integer value, therefore, I could not use datetime in Sql Server.) I need to ultimately put the converted values into ten datetimepicker controls.
0
9719
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
9597
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
10369
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 tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
0
9187
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
1
7650
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 presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
5546
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 last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
0
5682
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
2
3851
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
3
3008
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 can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

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.