A simple example calculating the number of seconds since 01/01/1970

Discussion in 'C Programming' started by cyril, Aug 22, 2003.

  1. cyril

    cyril Guest

    I saw that is a recurrent message but i couln't succed in founding the
    solution ...

    I'm developping under Linux

    Does anyone have a code snippet that permit to get in a simple long
    var the number of sec since 01/01/1970.... I come from the Java world
    ;o)

    Thanks for all.

    PS : the code I try give me a negative value :
    tm is a tm struct
    currenttime and t_70 are some time_t struct;

    tm.tm_day = 1;
    tm.tm_year = 1970;
    t_70 = mktime(&tm); //normally initialized to 0

    currenttime = time(NULL);

    sec = difftime(t_70, currenttime);
     
    cyril, Aug 22, 2003
    #1
    1. Advertising

  2. tm.tm_year should be set to the number of years since 1900, not the
    actual year you want.

    so in effect, t_70 is after currenttime, which would give you a negative
    value.

    cyril wrote:
    > I saw that is a recurrent message but i couln't succed in founding the
    > solution ...
    >
    > I'm developping under Linux
    >
    > Does anyone have a code snippet that permit to get in a simple long
    > var the number of sec since 01/01/1970.... I come from the Java world
    > ;o)
    >
    > Thanks for all.
    >
    > PS : the code I try give me a negative value :
    > tm is a tm struct
    > currenttime and t_70 are some time_t struct;
    >
    > tm.tm_day = 1;
    > tm.tm_year = 1970;
    > t_70 = mktime(&tm); //normally initialized to 0
    >
    > currenttime = time(NULL);
    >
    > sec = difftime(t_70, currenttime);
     
    Corne' Cornelius, Aug 22, 2003
    #2
    1. Advertising

  3. cyril

    Martijn Guest

    cyril wrote:
    > Does anyone have a code snippet that permit to get in a simple long
    > var the number of sec since 01/01/1970.... I come from the Java world
    > ;o)


    time_t t = time(NULL);

    Depending on your implementation, time_t may be a typedef of long. If you
    _really_ need a long, go cast it.

    long l = (long)t;

    Good luck,

    --
    Martijn
    http://www.sereneconcepts.nl
     
    Martijn, Aug 23, 2003
    #3
  4. Martijn wrote:

    > cyril wrote:
    >> Does anyone have a code snippet that permit to get in a simple long
    >> var the number of sec since 01/01/1970.... I come from the Java world
    >> ;o)

    >
    > time_t t = time(NULL);
    >
    > Depending on your implementation, time_t may be a typedef of long. If you
    > _really_ need a long, go cast it.
    >
    > long l = (long)t;


    Bad advice. Even if time_t is a long on his implementation, the resulting
    code will not be portable. Indeed, it might not even work, since the
    Standard does not require time_t to represent the number of seconds since
    1/1/1970.

    The OP would do better to build a struct tm like this:

    struct tm firstjan1970 = {0};
    firstjan1970.tm_year = 70;
    firstjan1970.tm_mon = 0;
    firstjan1970.tm_mday = 1;

    and then call mktime to get a time_t.

    The next steps are to get the current time using time(), and then call
    difftime() to get the number of seconds difference as a double.


    --
    Richard Heathfield :
    "Usenet is a strange place." - Dennis M Ritchie, 29 July 1999.
    C FAQ: http://www.eskimo.com/~scs/C-faq/top.html
    K&R answers, C books, etc: http://users.powernet.co.uk/eton
     
    Richard Heathfield, Aug 23, 2003
    #4
  5. cyril

    cyril Guest

    Thanks to all your advices, i succed in what I wanna do.

    Thanks for all.

    Regards, Cyril.
     
    cyril, Aug 25, 2003
    #5
    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. vertigo

    days since 1 Jan 1970

    vertigo, Jul 7, 2004, in forum: Perl
    Replies:
    6
    Views:
    7,320
    Joe Smith
    Jul 8, 2004
  2. Summu82
    Replies:
    5
    Views:
    869
    Richard Heathfield
    Jun 7, 2006
  3. JC
    Replies:
    4
    Views:
    3,250
    Marcel Müller
    Jan 7, 2009
  4. Michael Tan
    Replies:
    32
    Views:
    997
    Ara.T.Howard
    Jul 21, 2005
  5. Tom
    Replies:
    7
    Views:
    409
    Dr John Stockton
    Jun 18, 2004
Loading...

Share This Page