Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > C++ > Question about ctime

Reply
Thread Tools

Question about ctime

 
 
Tivo Escobar
Guest
Posts: n/a
 
      07-23-2003
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
 
Reply With Quote
 
 
 
 
Victor Bazarov
Guest
Posts: n/a
 
      07-23-2003
"Tivo Escobar" <(E-Mail Removed)> wrote...
> 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


 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
who do know std::ctime() how implement? Help me? wukexin C++ 1 05-25-2005 01:44 PM
ctime Help Kevin C++ 3 05-16-2004 04:09 PM
Undestanding proper ctime and clock() usage. Oplec C++ 2 10-25-2003 11:42 PM
replacement for <ctime>? Ernst Murnleitner C++ 8 07-20-2003 02:40 AM



Advertisments