472,805 Members | 3,073 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 472,805 software developers and data experts.

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 12236
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 thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

4
by: Dave Sinkula | last post by:
I have been told conflicting advice. Please offer comments regarding the following. ===== I visited Ben Pfaff's "When are casts appropriate?" page....
7
by: Nimmy | last post by:
Hi, I have a file which has different dates, I want to scanf them as CHAR and convert them to DATE format, how can I do this? Thanks
0
by: Zwyatt | last post by:
I have the following code (simplified here): #include <time.h> class A { public: char *aString; int aNum; time_t aTime; }
0
by: Zwyatt | last post by:
having a really weird little bug w/ time_t...check it out: I have the following code (simplified here): #include <time.h> class A { public: char *aString; int aNum;
9
by: Simple Simon | last post by:
Java longs are 8 bytes. I have a Java long that is coming in from the network, and that represents milliseconds since Epoch (Jan 1 1970 00:00:00). I'm having trouble understanding how to get it...
7
by: Angus Comber | last post by:
Hello I need to do this conversion but can't seem to find the function. Angus
2
by: ZR | last post by:
Hello, I need to convert a GMT time string to local time. I can fill out a "tm" structure with the GMT time string. Are there any standard C (or OS) time functions that will allow me to do this? ...
45
by: loudking | last post by:
Hello, all I don't quite understand what does ((time_t)-1) mean when I execute "man 2 time" RETURN VALUE On success, the value of time in seconds since the Epoch is retu rned. On error,...
4
by: PeteOlcott | last post by:
I need to convert time_t to System::DateTime. All of the examples that I found are either in C# or in the obsolete managed C++ syntax. What is the current syntax for converting a (probably 64 bit)...
0
by: erikbower65 | last post by:
Using CodiumAI's pr-agent is simple and powerful. Follow these steps: 1. Install CodiumAI CLI: Ensure Node.js is installed, then run 'npm install -g codiumai' in the terminal. 2. Connect to...
0
linyimin
by: linyimin | last post by:
Spring Startup Analyzer generates an interactive Spring application startup report that lets you understand what contributes to the application startup time and helps to optimize it. Support for...
14
DJRhino1175
by: DJRhino1175 | last post by:
When I run this code I get an error, its Run-time error# 424 Object required...This is my first attempt at doing something like this. I test the entire code and it worked until I added this - If...
0
by: Rina0 | last post by:
I am looking for a Python code to find the longest common subsequence of two strings. I found this blog post that describes the length of longest common subsequence problem and provides a solution in...
5
by: DJRhino | last post by:
Private Sub CboDrawingID_BeforeUpdate(Cancel As Integer) If = 310029923 Or 310030138 Or 310030152 Or 310030346 Or 310030348 Or _ 310030356 Or 310030359 Or 310030362 Or...
0
by: lllomh | last post by:
Define the method first this.state = { buttonBackgroundColor: 'green', isBlinking: false, // A new status is added to identify whether the button is blinking or not } autoStart=()=>{
0
by: lllomh | last post by:
How does React native implement an English player?
0
by: Mushico | last post by:
How to calculate date of retirement from date of birth
2
by: DJRhino | last post by:
Was curious if anyone else was having this same issue or not.... I was just Up/Down graded to windows 11 and now my access combo boxes are not acting right. With win 10 I could start typing...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.