468,496 Members | 1,818 Online
Bytes | Developer Community
New Post

Home Posts Topics Members FAQ

Post your question to a community of 468,496 developers. It's quick & easy.

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(NULL); /* 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(clientSocket, buffer, sizeof(buffer) -1);

...

Any help is appreciated.

Thank in advance,

Jonathan
Mar 20 '08 #1
3 11497
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
Mar 20 '08 #2
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(NULL); /* 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(clientSocket, 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
ta*******@rocketmail.com
Mar 21 '08 #3
In article <X_*************@newsfe07.lga>, ta*******@rocketmail.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.
Mar 21 '08 #4

This discussion thread is closed

Replies have been disabled for this discussion.

Similar topics

4 posts views Thread by Dave Sinkula | last post: by
7 posts views Thread by Nimmy | last post: by
reply views Thread by Zwyatt | last post: by
reply views Thread by Zwyatt | last post: by
9 posts views Thread by Simple Simon | last post: by
7 posts views Thread by Angus Comber | last post: by
2 posts views Thread by ZR | last post: by
45 posts views Thread by loudking | last post: by
4 posts views Thread by PeteOlcott | last post: by
3 posts views Thread by gieforce | last post: by
By using this site, you agree to our Privacy Policy and Terms of Use.