Question about ctime

Discussion in 'C++' started by Tivo Escobar, Jul 23, 2003.

  1. Tivo Escobar

    Tivo Escobar Guest

    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
     
    Tivo Escobar, Jul 23, 2003
    #1
    1. Advertising

  2. "Tivo Escobar" <> 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
     
    Victor Bazarov, Jul 23, 2003
    #2
    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. Ernst Murnleitner

    replacement for <ctime>?

    Ernst Murnleitner, Jul 16, 2003, in forum: C++
    Replies:
    8
    Views:
    545
    Limech
    Jul 20, 2003
  2. Oplec
    Replies:
    2
    Views:
    530
    Oplec
    Oct 26, 2003
  3. Kevin

    ctime Help

    Kevin, May 16, 2004, in forum: C++
    Replies:
    3
    Views:
    802
    osmium
    May 16, 2004
  4. wukexin
    Replies:
    1
    Views:
    480
    Karl Heinz Buchegger
    May 25, 2005
  5. Hao Xu
    Replies:
    2
    Views:
    753
    Mark McIntyre
    Aug 17, 2004
Loading...

Share This Page