Convert time_t to char?

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

    Convert time_t to char?

    I have taken up C++ programming again and need to write a socket client,
    so far I am able to create the connection to the socket.

    As I need a unix timestamp in char to put on a socket in a HTTP 1.1 GET
    request I am looking for a way to convert the unix timestamp to a char?

    I am using the following code to generate my unix timestamp:

    ...

    time_t mytime; /* calendar time */
    mytime=time(NUL L); /* get current cal time */

    char chTime[10];

    // Here I would like to do some conversion

    strcpy (buffer, "GET /?t=" . chTime .
    "HTTP/1.1\r\nHost: localhost\r\n\r \n");

    write(clientSoc ket, buffer, sizeof(buffer) -1);

    ...

    Any help is appreciated.

    Thank in advance,

    Jonathan
  • Jonathan

    #2
    Re: Convert time_t to char?

    Victor Bazarov wrote:
    What do you expect, really? We have no idea what your server
    needs you to do.

    The syntax with dots is not an acceptable way,
    of course, but you probably know that already. Try 'sprintf'
    instead:
    I am aware of that. The dots where there to indicate there is code
    before and after as I just cut-and-pasted from my source.

    Jonathan

    Comment

    • Jim Langston

      #3
      Re: Convert time_t to char?

      Jonathan wrote:
      I have taken up C++ programming again and need to write a socket
      client, so far I am able to create the connection to the socket.
      >
      As I need a unix timestamp in char to put on a socket in a HTTP 1.1
      GET request I am looking for a way to convert the unix timestamp to a
      char?
      I am using the following code to generate my unix timestamp:
      >
      ...
      >
      time_t mytime; /* calendar time */
      mytime=time(NUL L); /* get current cal time */
      >
      char chTime[10];
      >
      // Here I would like to do some conversion
      >
      strcpy (buffer, "GET /?t=" . chTime .
      "HTTP/1.1\r\nHost: localhost\r\n\r \n");
      >
      write(clientSoc ket, buffer, sizeof(buffer) -1);
      Microsoft Visual C++ .net 2003 has asctime. I'm not sure if this is
      standard or not. See if your implemenation uses it.
      Apparently it's used like:

      #include <time.h>
      #include <stdio.h>

      struct tm *newtime;
      time_t aclock;

      int main( void )
      {
      time( &aclock ); // Get time in seconds
      newtime = localtime( &aclock ); // Convert time to struct tm form

      /* Print local time as a string */
      printf( "Current date and time: %s", asctime( newtime ) );
      }


      --
      Jim Langston
      tazmaster@rocke tmail.com


      Comment

      • Jerry Coffin

        #4
        Re: Convert time_t to char?

        In article <X_DEj.136$29.4 5@newsfe07.lga> , tazmaster@rocke tmail.com
        says...

        [ ... ]
        Microsoft Visual C++ .net 2003 has asctime. I'm not sure if this is
        standard or not. See if your implemenation uses it.
        Yes, asctime is standard. If you don't want the (one) format it can
        provide, take a look at strftime, which is a bit like printf, but
        specifically for time values.

        --
        Later,
        Jerry.

        The universe is a figment of its own imagination.

        Comment

        Working...