srand()

Discussion in 'C Programming' started by Jeremy Holdstadt, Apr 26, 2004.

  1. This seems to be a C question to me. If it is not, I
    apologize.

    This command: awk 'BEGIN {srand();print srand()}'
    will give the number of seconds since the epoch, present time.

    Can any of you tell me how to get the number of seconds
    since the epoch for an arbitrary post-epoch date like
    Jan 25 1980 13:34 GMT?

    How to modify that command to do this?

    Thanks a lot!

    Jeremy

    --
    I think, therefore I am. I think.
    Jeremy Holdstadt, Apr 26, 2004
    #1
    1. Advertising

  2. Jeremy Holdstadt

    Lew Pitcher Guest

    -----BEGIN PGP SIGNED MESSAGE-----
    Hash: SHA1

    Jeremy Holdstadt wrote:

    > This seems to be a C question to me. If it is not, I
    > apologize.
    >
    > This command: awk 'BEGIN {srand();print srand()}'
    > will give the number of seconds since the epoch, present time.


    This is off-topic for comp.lang.c

    Awk is not C, nor are awk builtin functions C. And, as you read in
    comp.unix.shell, this trick depends on how awk's builtin srand is implemented.
    The first srand() seeds itself from the current clock, which is measured in
    seconds since the Unix epoch, and the second srand() returns the value used as a
    seed to the first srand(), thus providing the Unix epoch time to print.

    FWIW, Unix epoch time doesn't have anything to do with C either.

    > Can any of you tell me how to get the number of seconds
    > since the epoch for an arbitrary post-epoch date like
    > Jan 25 1980 13:34 GMT?


    Some topicality here.

    You want the time.h header and the asctime() or ctime() functions.

    > How to modify that command to do this?


    That's also off topic for comp.lang.c

    - --
    Lew Pitcher
    IT Consultant, Enterprise Application Architecture,
    Enterprise Technology Solutions, TD Bank Financial Group

    (Opinions expressed are my own, not my employers')
    -----BEGIN PGP SIGNATURE-----
    Version: GnuPG v1.2.4 (MingW32)

    iD8DBQFAjUtpagVFX4UWr64RAm4tAKDeP/TisblwE8xDYyPQbXMQVo3QMACfQ50K
    DDTtuG78ZZQR1mmzKlHPXHw=
    =aepT
    -----END PGP SIGNATURE-----
    Lew Pitcher, Apr 26, 2004
    #2
    1. Advertising

  3. >This command: awk 'BEGIN {srand();print srand()}'
    >will give the number of seconds since the epoch, present time.


    Not according to the manual page for awk on my system. srand() in
    awk returns the previous seed. srand() in C returns void. Neither
    of these have anything to do with the current time.

    >Can any of you tell me how to get the number of seconds
    >since the epoch for an arbitrary post-epoch date like
    >Jan 25 1980 13:34 GMT?


    There is no guarantee in C that a time_t represents a number of
    <any time unit> since <epoch>. *IF* your platform does happen to
    conform to the POSIX requirements for a time_t, then the value in
    a time_t is what you want.

    Filling in a struct tm (parsing the text string of a date to get a
    filled-in struct tm is left as an exercise for the reader) and
    calling mktime() on it would give you a time_t value for that time
    as the return value of mktime(). (See, there actually is a little
    C relevance for this question.)

    Gordon L. Burditt
    Gordon Burditt, Apr 26, 2004
    #3
  4. Jeremy Holdstadt wrote:
    >
    > This seems to be a C question to me. If it is not, I
    > apologize.
    >
    > This command: awk 'BEGIN {srand();print srand()}'

    ^^^
    [...]

    Perhaps comp.lang.awk can answer this?

    --
    +-------------------------+--------------------+-----------------------------+
    | Kenneth J. Brody | www.hvcomputer.com | |
    | kenbrody at spamcop.net | www.fptech.com | #include <std_disclaimer.h> |
    +-------------------------+--------------------+-----------------------------+
    Kenneth Brody, Apr 26, 2004
    #4
  5. Kenneth Brody wrote:
    >
    >
    > Jeremy Holdstadt wrote:
    >>
    >> This seems to be a C question to me. If it is not, I
    >> apologize.
    >>
    >> This command: awk 'BEGIN {srand();print srand()}'

    > ^^^
    > [...]
    >
    > Perhaps comp.lang.awk can answer this?
    >


    Well <sheepish grin> I didn't know there *was* an entire newsgroup
    devoted to awk.

    Thanks Ken. And to Gordon and Lew too.

    Jeremy

    --
    I think, therefore I am. I think.
    Jeremy Holdstadt, Apr 26, 2004
    #5
  6. Jeremy Holdstadt

    Default User Guest

    Jeremy Holdstadt wrote:
    >
    > This seems to be a C question to me. If it is not, I
    > apologize.
    >
    > This command: awk 'BEGIN {srand();print srand()}'
    > will give the number of seconds since the epoch, present time.
    >
    > Can any of you tell me how to get the number of seconds
    > since the epoch for an arbitrary post-epoch date like
    > Jan 25 1980 13:34 GMT?
    >
    > How to modify that command to do this?



    You'll need a couple of calls to mktime() to get the time_t values for
    the two dates, your "epoch" (that isn't defined in the C standard) and
    the other date. Then use difftime() to get the difference in seconds
    between those two.



    Brian Rodenborn
    Default User, Apr 26, 2004
    #6
  7. Jeremy Holdstadt wrote:

    > This seems to be a C question to me. If it is not, I
    > apologize.
    >
    > This command: awk 'BEGIN {srand();print srand()}'
    > will give the number of seconds since the epoch, present time.
    >
    > Can any of you tell me how to get the number of seconds
    > since the epoch for an arbitrary post-epoch date like
    > Jan 25 1980 13:34 GMT?
    >
    > How to modify that command to do this?


    No, because awk and its argument at not C. But see if this gives you a
    place to start:

    #include <stdio.h>
    #include <stdlib.h>
    #include <time.h>

    int main(void)
    {
    time_t now, then;
    struct tm epoch = /* Jan 25 1980 13:34 */ {.tm_min = 34,.tm_hour =
    13,.tm_mday = 25,.tm_mon = 0,.tm_year = 80,.tm_isdst = -1
    };
    then = mktime(&epoch);
    now = time(0);
    if (now == (time_t) - 1) {
    fprintf(stderr, "time not available.\n");
    exit(EXIT_FAILURE);
    }
    printf("%.0f seconds since epoch\n", difftime(now, then));
    return 0;
    }


    765340521 seconds since epoch
    Martin Ambuhl, Apr 26, 2004
    #7
  8. Jeremy Holdstadt

    Eric Sosman Guest

    Martin Ambuhl wrote:
    >
    > Jeremy Holdstadt wrote:
    >
    > > This seems to be a C question to me. If it is not, I
    > > apologize.
    > >
    > > This command: awk 'BEGIN {srand();print srand()}'
    > > will give the number of seconds since the epoch, present time.
    > >
    > > Can any of you tell me how to get the number of seconds
    > > since the epoch for an arbitrary post-epoch date like
    > > Jan 25 1980 13:34 GMT?
    > >
    > > How to modify that command to do this?

    >
    > No, because awk and its argument at not C. But see if this gives you a
    > place to start:
    >
    > #include <stdio.h>
    > #include <stdlib.h>
    > #include <time.h>
    >
    > int main(void)
    > {
    > time_t now, then;
    > struct tm epoch = /* Jan 25 1980 13:34 */ {.tm_min = 34,.tm_hour =
    > 13,.tm_mday = 25,.tm_mon = 0,.tm_year = 80,.tm_isdst = -1
    > };
    > then = mktime(&epoch);
    > now = time(0);
    > if (now == (time_t) - 1) {
    > fprintf(stderr, "time not available.\n");
    > exit(EXIT_FAILURE);
    > }
    > printf("%.0f seconds since epoch\n", difftime(now, then));
    > return 0;
    > }
    >
    > 765340521 seconds since epoch


    Note that mktime() interprets its input as being in the
    local time zone, not necessarily UTC. Thus, there's likely
    to be a fairly substantial error in the printed result ...

    I can't think of any really clean way to solve this
    problem. One could apply both gmtime() and localtime()
    to `then' to figure out the zone offset at the time of
    the epoch, then adjust the members of `epoch' accordingly
    and call mktime() again to get a corrected `then'. This
    sort of lying to the library always makes me uneasy, though,
    and I'd worry about mktime() trying to apply daylight savings
    rules to DS-less UTC ... It'd probably be better to use the
    deduced offset to adjust the difftime() result.

    By the way, it's laudable that you checked for failure
    of time(), but why did you not similarly check mktime()?

    --
    Eric Sosman, Apr 26, 2004
    #8
  9. Jeremy Holdstadt

    ZAPPLE Guest

    Eric Sosman <> wrote in message news:<>...
    > Martin Ambuhl wrote:
    > >
    > > Jeremy Holdstadt wrote:
    > >
    > > > This seems to be a C question to me. If it is not, I
    > > > apologize.
    > > >
    > > > This command: awk 'BEGIN {srand();print srand()}'
    > > > will give the number of seconds since the epoch, present time.
    > > >
    > > > Can any of you tell me how to get the number of seconds
    > > > since the epoch for an arbitrary post-epoch date like
    > > > Jan 25 1980 13:34 GMT?
    > > >
    > > > How to modify that command to do this?

    > >
    > > No, because awk and its argument at not C. But see if this gives you a
    > > place to start:
    > >
    > > #include <stdio.h>
    > > #include <stdlib.h>
    > > #include <time.h>
    > >
    > > int main(void)
    > > {
    > > time_t now, then;
    > > struct tm epoch = /* Jan 25 1980 13:34 */ {.tm_min = 34,.tm_hour =
    > > 13,.tm_mday = 25,.tm_mon = 0,.tm_year = 80,.tm_isdst = -1
    > > };
    > > then = mktime(&epoch);
    > > now = time(0);
    > > if (now == (time_t) - 1) {
    > > fprintf(stderr, "time not available.\n");
    > > exit(EXIT_FAILURE);
    > > }
    > > printf("%.0f seconds since epoch\n", difftime(now, then));
    > > return 0;
    > > }
    > >
    > > 765340521 seconds since epoch

    >
    > Note that mktime() interprets its input as being in the
    > local time zone, not necessarily UTC. Thus, there's likely
    > to be a fairly substantial error in the printed result ...
    >
    > I can't think of any really clean way to solve this
    > problem. One could apply both gmtime() and localtime()
    > to `then' to figure out the zone offset at the time of
    > the epoch, then adjust the members of `epoch' accordingly
    > and call mktime() again to get a corrected `then'. This
    > sort of lying to the library always makes me uneasy, though,
    > and I'd worry about mktime() trying to apply daylight savings
    > rules to DS-less UTC ... It'd probably be better to use the
    > deduced offset to adjust the difftime() result.
    >
    > By the way, it's laudable that you checked for failure
    > of time(), but why did you not similarly check mktime()?


    Hello,
    Great information. C a great Programming language. There are a number
    of things that I dont know in C. Just now I am coming to know about
    time_t, struct tm, daylight savings in C, difftime etc..
    Thanks a lot for posting such usefull information. When I tried
    running it in my computer, it showed 765426211 seconds.
    Is there any way of finding the date in the past from the given
    seconds. That is if we give 765426211 the out put should be 13:34 jan
    25 1980.
    ZAPPLE
    ZAPPLE, Apr 27, 2004
    #9
  10. ZAPPLE wrote:

    > Hello,
    > Great information. C a great Programming language. There are a number
    > of things that I dont know in C. Just now I am coming to know about
    > time_t, struct tm, daylight savings in C, difftime etc..
    > Thanks a lot for posting such usefull information. When I tried
    > running it in my computer, it showed 765426211 seconds.
    > Is there any way of finding the date in the past from the given
    > seconds. That is if we give 765426211 the out put should be 13:34 jan
    > 25 1980.


    Here's an example going the other way, counting up. You can work out
    the other way, I'm sure. You may want to delete the block beginning
    with if (!try && INT_MAX != SHRT_MAX) , since it might output a _lot_ of
    lines. I have removed some 46720 lines of output from my run.
    Redirecting output to a file will keep it from zipping past.

    #include <stdio.h>
    #include <stdlib.h>
    #include <time.h>
    #include <limits.h>

    int main(void)
    {
    struct tm epoch = /* Jan 25 1980 13:34 */ {.tm_sec = 0,.tm_min =
    34,.tm_hour =
    13,.tm_mday = 25,.tm_mon = 0,.tm_year = 80,.tm_isdst = -1
    };
    struct tm newtime; /* so we can leave epoch alone */
    time_t then, now;
    long addend = 765426211L, taddend, tmp;
    int try = 0;
    newtime = epoch; /* copying old value */
    taddend = addend;
    tmp = taddend + newtime.tm_sec;
    printf("I'm going to try to add %ld seconds to the time\n"
    "represented by %s"
    "The struct member tm_sec is an int, so it is possible that\n"
    "this cannot be done at once.\n"
    "On my machine, INT_MAX is %d,\n"
    "so I will need %ld (or more) tries. Your milage may
    vary.\n\n",
    taddend, asctime(&epoch), INT_MAX,
    tmp / INT_MAX + (tmp % INT_MAX != 0));
    while (taddend + newtime.tm_sec > INT_MAX) {
    tmp = INT_MAX - newtime.tm_sec;
    printf(" ... partial update %d, adding %ld\n", ++try, tmp);
    newtime.tm_sec = INT_MAX;
    taddend -= tmp;
    if (mktime(&newtime) == (time_t) - 1) {
    printf("Oops! mktime failed, quitting ...\n");
    exit(EXIT_FAILURE);
    }
    printf(" Time has been updated to %s", asctime(&newtime));
    }
    if (!taddend)
    printf("Done!\n");
    else {
    printf("Final addition (%ld)\n", taddend);
    newtime.tm_sec += taddend;
    if (mktime(&newtime) == (time_t) - 1) {
    printf("Oops! mktime failed, quitting ...\n");
    exit(EXIT_FAILURE);
    }
    printf(" Time has been updated to %s", asctime(&newtime));
    then = mktime(&epoch);
    now = mktime(&newtime);
    printf("Check: the difference is %.0f seconds\n\n",
    difftime(now, then));
    }
    if (!try && INT_MAX != SHRT_MAX) {
    newtime = epoch; /* copying old value */
    taddend = addend;
    tmp = taddend + newtime.tm_sec;
    printf("That didn't show any intermediate steps.\n"
    "Let's pretend INT_MAX is actually %d.\n"
    "Then I will need %ld (or more) tries. "
    "Your milage may vary.\n\n",
    SHRT_MAX, tmp / SHRT_MAX + (tmp % SHRT_MAX != 0));
    while (taddend + newtime.tm_sec > SHRT_MAX) {
    tmp = SHRT_MAX - newtime.tm_sec;
    printf(" ... partial update %d, adding %ld\n", ++try, tmp);
    newtime.tm_sec = SHRT_MAX;
    taddend -= tmp;
    if (mktime(&newtime) == (time_t) - 1) {
    printf("Oops! mktime failed, quitting ...\n");
    exit(EXIT_FAILURE);
    }
    printf(" Time has been updated to %s", asctime(&newtime));
    }
    if (!taddend)
    printf("Done!\n");
    else {
    printf("Final addition (%ld)\n", taddend);
    newtime.tm_sec += taddend;
    if (mktime(&newtime) == (time_t) - 1) {
    printf("Oops! mktime failed, quitting ...\n");
    exit(EXIT_FAILURE);
    }
    printf(" Time has been updated to %s", asctime(&newtime));
    then = mktime(&epoch);
    now = mktime(&newtime);
    printf("Check: the difference is %.0f seconds\n\n",
    difftime(now, then));
    }
    }
    return 0;
    }




    I'm going to try to add 765426211 seconds to the time
    represented by Sun Jan 25 13:34:00 1980
    The struct member tm_sec is an int, so it is possible that
    this cannot be done at once.
    On my machine, INT_MAX is 2147483647,
    so I will need 1 (or more) tries. Your milage may vary.

    Final addition (765426211)
    Time has been updated to Tue Apr 27 15:57:31 2004
    Check: the difference is 765426211 seconds

    That didn't show any intermediate steps.
    Let's pretend INT_MAX is actually 32767.
    Then I will need 23360 (or more) tries. Your milage may vary.

    ... partial update 1, adding 32767
    Time has been updated to Fri Jan 25 22:40:07 1980
    ... partial update 2, adding 32760
    Time has been updated to Sat Jan 26 07:46:07 1980
    [23360 updates deleted]
    ... partial update 23363, adding 32760
    Time has been updated to Tue Apr 27 00:52:07 2004
    ... partial update 23364, adding 32760
    Time has been updated to Tue Apr 27 09:58:07 2004
    Final addition (21564)
    Time has been updated to Tue Apr 27 15:57:31 2004
    Check: the difference is 765426211 seconds
    Martin Ambuhl, Apr 27, 2004
    #10
  11. Martin Ambuhl <> writes:
    [...]
    > Here's an example going the other way, counting up. You can work out
    > the other way, I'm sure. You may want to delete the block beginning
    > with if (!try && INT_MAX != SHRT_MAX) , since it might output a _lot_
    > of lines. I have removed some 46720 lines of output from my
    > run. Redirecting output to a file will keep it from zipping past.
    >
    > #include <stdio.h>
    > #include <stdlib.h>
    > #include <time.h>
    > #include <limits.h>
    >
    > int main(void)
    > {
    > struct tm epoch = /* Jan 25 1980 13:34 */ {.tm_sec = 0,.tm_min =
    > 34,.tm_hour =
    > 13,.tm_mday = 25,.tm_mon = 0,.tm_year = 80,.tm_isdst = -1
    > };


    Note that this construct is new in C99. If you happen to be using a
    pre-C99 compiler, you might have to do something like this:

    struct tm epoch;
    ...
    epoch.tm_sec = 0;
    epoch.tm_min = 34;
    epoch.tm_hour = 13;
    epoch.tm_mday = 25;
    epoch.tm_mon = 0;
    epoch.tm_year = 80;
    epoch.tm_isdst = -1;

    The approach Martin suggests here is necessary if you want to perform
    this kind of operation portably. In portable code, you can't make any
    assumptions about the representation of type time_t (except that it's
    an arithmetic type capable of representing times).

    <OT>

    On many system, including all Unix and Unix-like systems, a time_t is
    an integer representing the number of seconds since the epoch
    (1970-01-01 00:00:00 GMT). If (and only if) you're not concerned
    about portability beyond such systems, you can get away with
    performing arithmetic directly on time_t values and assuming that
    time_t is an integer type counting seconds. Of course, you code will
    break badly if it's ever ported to a system with a different
    representation for time_t.

    </OT>

    --
    Keith Thompson (The_Other_Keith) <http://www.ghoti.net/~kst>
    San Diego Supercomputer Center <*> <http://users.sdsc.edu/~kst>
    Schroedinger does Shakespeare: "To be *and* not to be"
    Keith Thompson, Apr 27, 2004
    #11
  12. Jeremy Holdstadt

    ZAPPLE Guest

    Martin Ambuhl <> wrote in message news:<c6melg$dmfmo$-berlin.de>...
    > ZAPPLE wrote:
    >
    > > Hello,
    > > Great information. C a great Programming language. There are a number
    > > of things that I dont know in C. Just now I am coming to know about
    > > time_t, struct tm, daylight savings in C, difftime etc..
    > > Thanks a lot for posting such usefull information. When I tried
    > > running it in my computer, it showed 765426211 seconds.
    > > Is there any way of finding the date in the past from the given
    > > seconds. That is if we give 765426211 the out put should be 13:34 jan
    > > 25 1980.

    >
    > Here's an example going the other way, counting up. You can work out
    > the other way, I'm sure. You may want to delete the block beginning
    > with if (!try && INT_MAX != SHRT_MAX) , since it might output a _lot_ of
    > lines. I have removed some 46720 lines of output from my run.
    > Redirecting output to a file will keep it from zipping past.
    >
    > #include <stdio.h>
    > #include <stdlib.h>
    > #include <time.h>
    > #include <limits.h>
    >
    > int main(void)
    > {
    > struct tm epoch = /* Jan 25 1980 13:34 */ {.tm_sec = 0,.tm_min =
    > 34,.tm_hour =
    > 13,.tm_mday = 25,.tm_mon = 0,.tm_year = 80,.tm_isdst = -1
    > };
    > struct tm newtime; /* so we can leave epoch alone */
    > time_t then, now;
    > long addend = 765426211L, taddend, tmp;
    > int try = 0;
    > newtime = epoch; /* copying old value */
    > taddend = addend;
    > tmp = taddend + newtime.tm_sec;
    > printf("I'm going to try to add %ld seconds to the time\n"
    > "represented by %s"
    > "The struct member tm_sec is an int, so it is possible that\n"
    > "this cannot be done at once.\n"
    > "On my machine, INT_MAX is %d,\n"
    > "so I will need %ld (or more) tries. Your milage may
    > vary.\n\n",
    > taddend, asctime(&epoch), INT_MAX,
    > tmp / INT_MAX + (tmp % INT_MAX != 0));
    > while (taddend + newtime.tm_sec > INT_MAX) {
    > tmp = INT_MAX - newtime.tm_sec;
    > printf(" ... partial update %d, adding %ld\n", ++try, tmp);
    > newtime.tm_sec = INT_MAX;
    > taddend -= tmp;
    > if (mktime(&newtime) == (time_t) - 1) {
    > printf("Oops! mktime failed, quitting ...\n");
    > exit(EXIT_FAILURE);
    > }
    > printf(" Time has been updated to %s", asctime(&newtime));
    > }
    > if (!taddend)
    > printf("Done!\n");
    > else {
    > printf("Final addition (%ld)\n", taddend);
    > newtime.tm_sec += taddend;
    > if (mktime(&newtime) == (time_t) - 1) {
    > printf("Oops! mktime failed, quitting ...\n");
    > exit(EXIT_FAILURE);
    > }
    > printf(" Time has been updated to %s", asctime(&newtime));
    > then = mktime(&epoch);
    > now = mktime(&newtime);
    > printf("Check: the difference is %.0f seconds\n\n",
    > difftime(now, then));
    > }
    > if (!try && INT_MAX != SHRT_MAX) {
    > newtime = epoch; /* copying old value */
    > taddend = addend;
    > tmp = taddend + newtime.tm_sec;
    > printf("That didn't show any intermediate steps.\n"
    > "Let's pretend INT_MAX is actually %d.\n"
    > "Then I will need %ld (or more) tries. "
    > "Your milage may vary.\n\n",
    > SHRT_MAX, tmp / SHRT_MAX + (tmp % SHRT_MAX != 0));
    > while (taddend + newtime.tm_sec > SHRT_MAX) {
    > tmp = SHRT_MAX - newtime.tm_sec;
    > printf(" ... partial update %d, adding %ld\n", ++try, tmp);
    > newtime.tm_sec = SHRT_MAX;
    > taddend -= tmp;
    > if (mktime(&newtime) == (time_t) - 1) {
    > printf("Oops! mktime failed, quitting ...\n");
    > exit(EXIT_FAILURE);
    > }
    > printf(" Time has been updated to %s", asctime(&newtime));
    > }
    > if (!taddend)
    > printf("Done!\n");
    > else {
    > printf("Final addition (%ld)\n", taddend);
    > newtime.tm_sec += taddend;
    > if (mktime(&newtime) == (time_t) - 1) {
    > printf("Oops! mktime failed, quitting ...\n");
    > exit(EXIT_FAILURE);
    > }
    > printf(" Time has been updated to %s", asctime(&newtime));
    > then = mktime(&epoch);
    > now = mktime(&newtime);
    > printf("Check: the difference is %.0f seconds\n\n",
    > difftime(now, then));
    > }
    > }
    > return 0;
    > }
    >
    >
    >
    >
    > I'm going to try to add 765426211 seconds to the time
    > represented by Sun Jan 25 13:34:00 1980
    > The struct member tm_sec is an int, so it is possible that
    > this cannot be done at once.
    > On my machine, INT_MAX is 2147483647,
    > so I will need 1 (or more) tries. Your milage may vary.
    >
    > Final addition (765426211)
    > Time has been updated to Tue Apr 27 15:57:31 2004
    > Check: the difference is 765426211 seconds
    >
    > That didn't show any intermediate steps.
    > Let's pretend INT_MAX is actually 32767.
    > Then I will need 23360 (or more) tries. Your milage may vary.
    >
    > ... partial update 1, adding 32767
    > Time has been updated to Fri Jan 25 22:40:07 1980
    > ... partial update 2, adding 32760
    > Time has been updated to Sat Jan 26 07:46:07 1980
    > [23360 updates deleted]
    > ... partial update 23363, adding 32760
    > Time has been updated to Tue Apr 27 00:52:07 2004
    > ... partial update 23364, adding 32760
    > Time has been updated to Tue Apr 27 09:58:07 2004
    > Final addition (21564)
    > Time has been updated to Tue Apr 27 15:57:31 2004
    > Check: the difference is 765426211 seconds


    Hi,
    This works fine for some cases, now I tried to understand this
    program, I have a doubt,
    Let me assume the int_max be 30, and the at the first stage seconds be
    zero,and I want to add 300 seconds to the original time, OK
    so when it first goes inside the while loop, the taddend+new.tm_sec =
    300 which is greater than 30, OK, goes inside the loop.
    Now the temp value is 30 - 300 = -270, Is that right?
    new.tm_sec = INT_max (30) OK.
    now what is taddend-=taddend_tmp; here it becomes 300=300-(-270) so,
    570.
    when it goes inside the loop again, the value of taddend+new.tm_sec
    will still increase from 300 to 570 and it should go on increasing
    leading to infinite loop ,
    My question is when will that decrease?
    Please reply.
    Thanks a lot.
    ZAPPLE - MY Computer is Clever then me.
    ZAPPLE, Apr 30, 2004
    #12
  13. ZAPPLE wrote:

    > Martin Ambuhl <> wrote in message news:<c6melg$dmfmo$-berlin.de>...


    >> while (taddend + newtime.tm_sec > INT_MAX) {
    >> tmp = INT_MAX - newtime.tm_sec;

    [...]
    > This works fine for some cases, now I tried to understand this
    > program, I have a doubt,
    > Let me assume the int_max be 30, and the at the first stage seconds be

    1) it is INT_MAX, and is at least 32,767
    2) newtime.tm_sec is an int

    > zero,and I want to add 300 seconds to the original time, OK
    > so when it first goes inside the while loop, the taddend+new.tm_sec =
    > 300 which is greater than 30, OK, goes inside the loop.
    > Now the temp value is 30 - 300 = -270, Is that right?


    No, the value of tmp is INT_MAX - newtime.tm_sec
    newtime.tm_sec must be in the range of an int, so is never greater than
    INT_MAX, and
    after the application of mktime, newtime.tm_sec must be in the range
    [0,60].
    INT_MAX - newtime.tm_sec can never be less than 0 unless
    newtime.tm_sec > INT_MAX, which, because newtime.tm_sec is an int cannot
    happen.
    Martin Ambuhl, Apr 30, 2004
    #13
  14. Jeremy Holdstadt

    ZAPPLE Guest

    Martin Ambuhl <> wrote in message news:<c6ufa4$fnqtr$-berlin.de>...
    > ZAPPLE wrote:
    >
    > > Martin Ambuhl <> wrote in message news:<c6melg$dmfmo$-berlin.de>...

    >
    > >> while (taddend + newtime.tm_sec > INT_MAX) {
    > >> tmp = INT_MAX - newtime.tm_sec;

    > [...]
    > > This works fine for some cases, now I tried to understand this
    > > program, I have a doubt,
    > > Let me assume the int_max be 30, and the at the first stage seconds be

    > 1) it is INT_MAX, and is at least 32,767
    > 2) newtime.tm_sec is an int
    >
    > > zero,and I want to add 300 seconds to the original time, OK
    > > so when it first goes inside the while loop, the taddend+new.tm_sec =
    > > 300 which is greater than 30, OK, goes inside the loop.
    > > Now the temp value is 30 - 300 = -270, Is that right?

    >
    > No, the value of tmp is INT_MAX - newtime.tm_sec
    > newtime.tm_sec must be in the range of an int, so is never greater than
    > INT_MAX, and
    > after the application of mktime, newtime.tm_sec must be in the range
    > [0,60].
    > INT_MAX - newtime.tm_sec can never be less than 0 unless
    > newtime.tm_sec > INT_MAX, which, because newtime.tm_sec is an int cannot
    > happen.


    Hi Martin,
    Yes you are right. There was something wrong in my caclulation.
    Thanks
    ZAPPLE
    ZAPPLE, May 1, 2004
    #14
    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. Graeme

    srand() question

    Graeme, Jul 29, 2003, in forum: C++
    Replies:
    6
    Views:
    704
    Karl Heinz Buchegger
    Jul 29, 2003
  2. August1
    Replies:
    4
    Views:
    7,725
    August1
    Dec 8, 2003
  3. David Shaw

    Why isn't srand working?

    David Shaw, Mar 3, 2004, in forum: C++
    Replies:
    14
    Views:
    670
    CBFalconer
    Mar 5, 2004
  4. August1
    Replies:
    0
    Views:
    7,504
    August1
    May 16, 2004
  5. Arijit Das

    srand versus srandom - srand with random() safe?

    Arijit Das, Oct 17, 2011, in forum: C Programming
    Replies:
    12
    Views:
    3,007
    Nick Keighley
    Oct 18, 2011
Loading...

Share This Page