Question about ctime

T

Tivo Escobar

Hi all,

anybody here can tell me what exactly is returned by the standard
function time() in <ctime> (or time.h). Please dont tell me it is the
number of seconds since 1 Jan of 1970 GMT, cos it is not (at least not
in my case!).

If I pass this number to a Java program (print it using a
java.util.Date() object), the result is the current date minus one
hour. If I generate the current number of seconds since 1970 in a Java
program and use it in a C++ program (print it using localtime and
asctime), the result is the current date plus one hour. I tested it
changing the time zone of my computer and the results were the same.

My conclusions are:
1. time() returns the number of seconds since 1 Jan 1970 GMT minus one
hour;
2. localtime() adjusts the date above to the correct localtime (sum
one hour) and store it in a structure.

Does anybody know if I am right? If yes, why was it implemented that
way?

Thanks in advance,
Tivo
 
V

Victor Bazarov

Tivo Escobar said:
anybody here can tell me what exactly is returned by the standard
function time() in <ctime> (or time.h). Please dont tell me it is the
number of seconds since 1 Jan of 1970 GMT, cos it is not (at least not
in my case!).

"The time function returns the implementation's best approximation
to the current calendar time. The value (time_t)(-1) is returned
if the calendar time is not available."

That's how C Standard defines it. Take it or leave it.
If I pass this number to a Java program (print it using a
java.util.Date() object), the result is the current date minus one
hour.

Neither C Standard nor C++ Standard defines what's going to happen
when you pass that value anywhere but gmtime, ctime, or localtime
functions.
If I generate the current number of seconds since 1970 in a Java
program and use it in a C++ program (print it using localtime and
asctime), the result is the current date plus one hour. I tested it
changing the time zone of my computer and the results were the same.

This has really no relevance in comp.lang.c++, but thank you for
sharing your findings.

Just for the fun of it, is the Daylight Savings Time active on
your system?
My conclusions are:
1. time() returns the number of seconds since 1 Jan 1970 GMT minus one
hour;

Nope. It returns "the implementation's best approximation of the
current calendar time" if one is available.
2. localtime() adjusts the date above to the correct localtime (sum
one hour) and store it in a structure.

localtime indeed takes a pointer to time_t value returned from (or
by) the time() function and converts it into 'tm' structure. Whether
it adjusts it or not is unspecified. Adjustment can be viewed as
part of the conversion.
Does anybody know if I am right?
Partially.

If yes, why was it implemented that
way?

Whatever way it was implemented, it's implementation detail. Why
it's done that way and not any other way, is a question to those
who implemented it.

Victor
 

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,744
Messages
2,569,482
Members
44,901
Latest member
Noble71S45

Latest Threads

Top