Java's System.currentTimeMillis() to C++

Discussion in 'C++' started by tak, Sep 28, 2007.

  1. tak

    tak Guest

    Hi.

    I have a client / server application, which the client will send the
    server a timestamp everytime when there is a transaction. The client
    is using Java, and it sends the timestamp using java's
    System.currentTimeMillis(), which returns, "the difference, measured
    in milliseconds, between the current time and midnight, January 1,
    1970 UTC."

    On the server side - it is implemented in C++, so, with this number of
    millis since 1/1/1970 - what C++ function can I use to get the month,
    day, year, hour, minute, seconds, and milliseconds?

    TIA,
    Tak
    tak, Sep 28, 2007
    #1
    1. Advertising

  2. tak wrote:
    > I have a client / server application, which the client will send the
    > server a timestamp everytime when there is a transaction. The client
    > is using Java, and it sends the timestamp using java's
    > System.currentTimeMillis(), which returns, "the difference, measured
    > in milliseconds, between the current time and midnight, January 1,
    > 1970 UTC."
    >
    > On the server side - it is implemented in C++, so, with this number of
    > millis since 1/1/1970 - what C++ function can I use to get the month,
    > day, year, hour, minute, seconds, and milliseconds?


    There is no standard function that would do that. You need to look up
    time/date functions provided by your OS. It is possible that it does
    not have it either and you're going to have to implement it yourself
    using available functions.

    Some systems implement 'time()' function so that it provides the number
    of _seconds_ since the start of 1970. Not sure if it helps, though.

    V
    --
    Please remove capital 'A's when replying by e-mail
    I do not respond to top-posted replies, please don't ask
    Victor Bazarov, Sep 28, 2007
    #2
    1. Advertising

  3. tak

    Guest

    On Sep 28, 9:33 am, "Victor Bazarov" <> wrote:
    > tak wrote:
    > > I have a client / server application, which the client will send the
    > > server a timestamp everytime when there is a transaction. The client
    > > is using Java, and it sends the timestamp using java's
    > > System.currentTimeMillis(), which returns, "the difference, measured
    > > in milliseconds, between the current time and midnight, January 1,
    > > 1970 UTC."

    >
    > > On the server side - it is implemented in C++, so, with this number of
    > > millis since 1/1/1970 - what C++ function can I use to get the month,
    > > day, year, hour, minute, seconds, and milliseconds?

    >
    > There is no standard function that would do that. You need to look up
    > time/date functions provided by your OS. It is possible that it does
    > not have it either and you're going to have to implement it yourself
    > using available functions.


    What about gmtime(time(NULL))? That will return a pointer to a
    structure with all the information you need except milliseconds. From
    there, it'd be relatively easy to do some math to get one value like
    Java does, just faking the milliseconds part or using some platform-
    specific function to retrieve it.
    , Sep 28, 2007
    #3
  4. wrote:
    > On Sep 28, 9:33 am, "Victor Bazarov" <> wrote:
    >> tak wrote:
    >>> I have a client / server application, which the client will send the
    >>> server a timestamp everytime when there is a transaction. The client
    >>> is using Java, and it sends the timestamp using java's
    >>> System.currentTimeMillis(), which returns, "the difference, measured
    >>> in milliseconds, between the current time and midnight, January 1,
    >>> 1970 UTC."

    >>
    >>> On the server side - it is implemented in C++, so, with this number
    >>> of millis since 1/1/1970 - what C++ function can I use to get the
    >>> month, day, year, hour, minute, seconds, and milliseconds?

    >>
    >> There is no standard function that would do that. You need to look
    >> up time/date functions provided by your OS. It is possible that it
    >> does not have it either and you're going to have to implement it
    >> yourself using available functions.

    >
    > What about gmtime(time(NULL))? That will return a pointer to a
    > structure with all the information you need except milliseconds. From
    > there, it'd be relatively easy to do some math to get one value like
    > Java does, just faking the milliseconds part or using some platform-
    > specific function to retrieve it.


    I believe that's what I meant by "have to implement it yourself". The
    OP didn't specify whether milliseconds were important. The available
    functions are undoubtedly described in the manual, including 'gmtime'
    and 'time'. So, with additional information OP decided not to share,
    I am sure all this stuff is figure-out-able.

    Consider, however, that since 'gmtime' returns a pointer to some
    static struct somewhere, it's not thread-safe, and special precautions
    have to be made when using it on the server side, which is most likely
    be threaded.

    V
    --
    Please remove capital 'A's when replying by e-mail
    I do not respond to top-posted replies, please don't ask
    Victor Bazarov, Sep 28, 2007
    #4
  5. tak <> writes:

    [...]

    > what C++ function can I use to get the month, day, year, hour,
    > minute, seconds, and milliseconds?


    As others have said, it depends on the OS. In Unix, the library
    function is gettimeofday(). Not sure about other systems, or if
    there's a 3rd-party library available providing a portable interface.

    -----Scott.
    Scott Gifford, Sep 28, 2007
    #5
  6. tak

    Guest

    On Sep 28, 11:09 am, "Victor Bazarov" <> wrote:
    > wrote:
    > > On Sep 28, 9:33 am, "Victor Bazarov" <> wrote:
    > >> tak wrote:
    > >>> I have a client / server application, which the client will send the
    > >>> server a timestamp everytime when there is a transaction. The client
    > >>> is using Java, and it sends the timestamp using java's
    > >>> System.currentTimeMillis(), which returns, "the difference, measured
    > >>> in milliseconds, between the current time and midnight, January 1,
    > >>> 1970 UTC."

    >
    > >>> On the server side - it is implemented in C++, so, with this number
    > >>> of millis since 1/1/1970 - what C++ function can I use to get the
    > >>> month, day, year, hour, minute, seconds, and milliseconds?

    >
    > >> There is no standard function that would do that. You need to look
    > >> up time/date functions provided by your OS. It is possible that it
    > >> does not have it either and you're going to have to implement it
    > >> yourself using available functions.

    >
    > > What about gmtime(time(NULL))? That will return a pointer to a
    > > structure with all the information you need except milliseconds. From
    > > there, it'd be relatively easy to do some math to get one value like
    > > Java does, just faking the milliseconds part or using some platform-
    > > specific function to retrieve it.

    >
    > I believe that's what I meant by "have to implement it yourself". The
    > OP didn't specify whether milliseconds were important. The available
    > functions are undoubtedly described in the manual, including 'gmtime'
    > and 'time'. So, with additional information OP decided not to share,
    > I am sure all this stuff is figure-out-able.


    I apologize. I took "available functions" to mean those "provided by
    your OS," not what's available in the standard library.
    , Sep 28, 2007
    #6
  7. tak

    James Kanze Guest

    On Sep 28, 4:29 pm, tak <> wrote:
    > I have a client / server application, which the client will send the
    > server a timestamp everytime when there is a transaction. The client
    > is using Java, and it sends the timestamp using java's
    > System.currentTimeMillis(), which returns, "the difference, measured
    > in milliseconds, between the current time and midnight, January 1,
    > 1970 UTC."


    > On the server side - it is implemented in C++, so, with this number of
    > millis since 1/1/1970 - what C++ function can I use to get the month,
    > day, year, hour, minute, seconds, and milliseconds?


    You can't get it in a single function, but the value % 1000 will
    give milliseconds, and the value / 1000, converted to a time_t,
    can be used with localtime() or gmttime() for the rest.

    --
    James Kanze (GABI Software) email:
    Conseils en informatique orientée objet/
    Beratung in objektorientierter Datenverarbeitung
    9 place Sémard, 78210 St.-Cyr-l'École, France, +33 (0)1 30 23 00 34
    James Kanze, Sep 28, 2007
    #7
    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. Nelson
    Replies:
    17
    Views:
    9,106
    Darryl L. Pierce
    May 20, 2004
  2. Jerry

    System.currentTimeMillis()

    Jerry, Aug 3, 2005, in forum: Java
    Replies:
    18
    Views:
    46,255
    Thomas G. Marshall
    Aug 6, 2005
  3. Alex
    Replies:
    15
    Views:
    2,618
  4. Roedy Green
    Replies:
    8
    Views:
    1,092
    Daniel Dyer
    Mar 9, 2006
  5. neoedmund
    Replies:
    1
    Views:
    1,136
Loading...

Share This Page