473,398 Members | 2,404 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,398 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 12318
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
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...

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.