Question about ctime

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • Tivo Escobar

    Question about ctime

    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
  • Victor Bazarov

    #2
    Re: Question about ctime

    "Tivo Escobar" <tivo.escobar@b ol.com.br> wrote...[color=blue]
    > 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!).[/color]

    "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.
    [color=blue]
    > 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.[/color]

    Neither C Standard nor C++ Standard defines what's going to happen
    when you pass that value anywhere but gmtime, ctime, or localtime
    functions.
    [color=blue]
    > 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.[/color]

    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?
    [color=blue]
    > My conclusions are:
    > 1. time() returns the number of seconds since 1 Jan 1970 GMT minus one
    > hour;[/color]

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

    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.
    [color=blue]
    > Does anybody know if I am right?[/color]

    Partially.
    [color=blue]
    > If yes, why was it implemented that
    > way?[/color]

    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


    Comment

    Working...