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

Discussion in 'C Programming' started by Stu, Feb 22, 2005.

  1. Stu

    Stu Guest

    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.
     
    Stu, Feb 22, 2005
    #1
    1. Advertising

  2. Stu

    dandelion Guest

    "Stu" <> wrote in message
    news:...
    > 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.
     
    dandelion, Feb 22, 2005
    #2
    1. Advertising

  3. Stu

    infobahn Guest

    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;
    }
     
    infobahn, Feb 22, 2005
    #3
  4. On 22 Feb 2005 07:46:11 -0800, in comp.lang.c , "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.


    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 =----
     
    Mark McIntyre, Feb 22, 2005
    #4
  5. Stu

    Richard Bos Guest

    "dandelion" <> wrote:

    > "Stu" <> wrote in message
    > news:...
    > > 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
     
    Richard Bos, Feb 23, 2005
    #5
  6. Stu

    dandelion Guest

    "Richard Bos" <> wrote in message
    news:...
    > "dandelion" <> wrote:
    >
    > > "Stu" <> wrote in message
    > > news:...
    > > > 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.
     
    dandelion, Feb 24, 2005
    #6
  7. Stu

    CBFalconer Guest

    dandelion wrote:
    > "Richard Bos" <> wrote in message
    >> "dandelion" <> wrote:
    >>> "Stu" <> 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 := 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
     
    CBFalconer, Feb 24, 2005
    #7
  8. On Thu, 24 Feb 2005 12:18:24 +0100, "dandelion" <>
    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
     
    Dave Thompson, Mar 7, 2005
    #8
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. tdi1686
    Replies:
    2
    Views:
    376
    Kevin Spencer
    Aug 13, 2003
  2. 00_CP_D12
    Replies:
    3
    Views:
    8,972
    dilipv
    Feb 22, 2008
  3. Eric

    Time Format in datagrid (Hours, Minutes, Seconds)

    Eric, Aug 12, 2004, in forum: ASP .Net Datagrid Control
    Replies:
    1
    Views:
    307
    Alvin Bruney [MVP]
    Aug 12, 2004
  4. `p
    Replies:
    7
    Views:
    385
    Steve Litt
    Dec 14, 2005
  5. lemonhead71
    Replies:
    0
    Views:
    1,546
    lemonhead71
    Oct 3, 2012
Loading...

Share This Page