Obtaining number of seconds since Jan 1 1970.

J

JC

Is there some portable way to obtain the number of seconds since the
start of January 1st, 1970? I don't have a copy of the C++ standard in
front of me, but C99 states:

[7.23.2.4/2] "The time function determines the current calendar time.
The encoding of the value is unspecified."

I'm assuming C++ didn't introduce any additional restrictions.

And, if there is no portable way, is there some way to check what the
encoding of time() is on the current platform, so I can at least have
some run-time assertions fail if my assumptions are incorrect?

Thanks,
Jason
 
P

Pascal J. Bourguignon

JC said:
Is there some portable way to obtain the number of seconds since the
start of January 1st, 1970? I don't have a copy of the C++ standard in
front of me, but C99 states:

[7.23.2.4/2] "The time function determines the current calendar time.
The encoding of the value is unspecified."

I'm assuming C++ didn't introduce any additional restrictions.

And, if there is no portable way, is there some way to check what the
encoding of time() is on the current platform, so I can at least have
some run-time assertions fail if my assumptions are incorrect?

You have two problems:

1- find what is the current time.
2- compute the number of seconds between the current time and 1/1/1970.

The second step can be implemented portably.

The first one cannot, unless you use the universal device:
std::cout<<"What time is it? enter YYYY MM DD HH MM SS"<<std::endl;
std::cin>>ye>>mo>>da>>ho>>mi>>ss;
and there you go to step 2 entirely portably.


Otherwise, it seems to me you can use gmtime to convert from the
unspecified time format time_t to the struct tm specified
decomposition...
 
J

JC

JC said:
Is there some portable way to obtain the number of seconds since the
start of January 1st, 1970? I don't have a copy of the C++ standard in
front of me, but C99 states:
[snip]
Otherwise, it seems to me you can use gmtime to convert from the
unspecified time format time_t to the struct tm specified
decomposition...

That's a good idea. Actually, if I pack a struct tm with the desired
epoch, I can use mktime() to convert it, use time() to get the current
time, and difftime() to get the number of seconds.

Thanks again,
Jason
 
J

James Kanze

On Jan 7, 12:12 pm, (e-mail address removed) (Pascal J. Bourguignon)
wrote:
JC said:
Is there some portable way to obtain the number of seconds
since the start of January 1st, 1970? I don't have a copy
of the C++ standard in front of me, but C99 states:
[snip]
Otherwise, it seems to me you can use gmtime to convert from
the unspecified time format time_t to the struct tm
specified decomposition...
That's a good idea. Actually, if I pack a struct tm with the
desired epoch, I can use mktime() to convert it, use time() to
get the current time, and difftime() to get the number of
seconds.

Supposing, of course, that what you choose as the epoch is
representable in a time_t. (I don't think that there's much
risk for 1 Jan., 1970. But if you choose some earlier time, you
might run into problems.)
 
M

Marcel Müller

Hi,
[7.23.2.4/2] "The time function determines the current calendar time.
The encoding of the value is unspecified."

I'm assuming C++ didn't introduce any additional restrictions.

I bet, too.

But *any* implementation of time I ever heard about immediately returns
the number of seconds since 01.01.1970 00:00. And since many
applications rely on that, it is really unlikely that a compiler/runtime
that does not satisfy this condition becomes common.
So, how portable should your application become?

And, if there is no portable way, is there some way to check what the
encoding of time() is on the current platform, so I can at least have
some run-time assertions fail if my assumptions are incorrect?

Of course, you can convert one or more known time stamps with gmtime.


Marcel
 

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

Forum statistics

Threads
473,768
Messages
2,569,574
Members
45,051
Latest member
CarleyMcCr

Latest Threads

Top