srand()

J

Jeremy Holdstadt

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
 
L

Lew Pitcher

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

Jeremy said:
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-----
 
G

Gordon Burditt

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
 
K

Kenneth Brody

Jeremy said:
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?
 
J

Jeremy Holdstadt

Kenneth said:
Jeremy said:
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
 
D

Default User

Jeremy said:
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
 
M

Martin Ambuhl

Jeremy said:
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
 
E

Eric Sosman

Martin said:
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()?
 
Z

ZAPPLE

Eric Sosman said:
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
 
M

Martin Ambuhl

ZAPPLE said:
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
 
K

Keith Thompson

Martin Ambuhl said:
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>
 
Z

ZAPPLE

Martin Ambuhl said:
ZAPPLE said:
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.
 
M

Martin Ambuhl

ZAPPLE said:
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.
 
Z

ZAPPLE

Martin Ambuhl said:
ZAPPLE said:
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
 

Ask a Question

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

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,744
Messages
2,569,483
Members
44,903
Latest member
orderPeak8CBDGummies

Latest Threads

Top